Determining the size of an Android view at runtime

Use the ViewTreeObserver on the View to wait for the first layout. Only after the first layout will getWidth()/getHeight()/getMeasuredWidth()/getMeasuredHeight() work. ViewTreeObserver viewTreeObserver = view.getViewTreeObserver(); if (viewTreeObserver.isAlive()) { viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { view.getViewTreeObserver().removeOnGlobalLayoutListener(this); viewWidth = view.getWidth(); viewHeight = view.getHeight(); } }); }

ConstraintLayout: change constraints programmatically

To set constraints of image view to: app:layout_constraintRight_toRightOf=”@+id/check_answer1″ app:layout_constraintTop_toTopOf=”@+id/check_answer1″ use: ConstraintLayout constraintLayout = findViewById(R.id.parent_layout); ConstraintSet constraintSet = new ConstraintSet(); constraintSet.clone(constraintLayout); constraintSet.connect(R.id.imageView,ConstraintSet.RIGHT,R.id.check_answer1,ConstraintSet.RIGHT,0); constraintSet.connect(R.id.imageView,ConstraintSet.TOP,R.id.check_answer1,ConstraintSet.TOP,0); constraintSet.applyTo(constraintLayout); To set constraints of image view to: app:layout_constraintRight_toRightOf=”@+id/check_answer2″ app:layout_constraintTop_toTopOf=”@+id/check_answer2″ use: ConstraintLayout constraintLayout = findViewById(R.id.parent_layout); ConstraintSet constraintSet = new ConstraintSet(); constraintSet.clone(constraintLayout); constraintSet.connect(R.id.imageView,ConstraintSet.RIGHT,R.id.check_answer2,ConstraintSet.RIGHT,0); constraintSet.connect(R.id.imageView,ConstraintSet.TOP,R.id.check_answer2,ConstraintSet.TOP,0); constraintSet.applyTo(constraintLayout);

How can I assign an ID to a view programmatically?

Android id overview An Android id is an integer commonly used to identify views; this id can be assigned via XML (when possible) and via code (programmatically.) The id is most useful for getting references for XML-defined Views generated by an Inflater (such as by using setContentView.) Assign id via XML Add an attribute of … Read more

android – listview get item view by position

Use this : public View getViewByPosition(int pos, ListView listView) { final int firstListItemPosition = listView.getFirstVisiblePosition(); final int lastListItemPosition = firstListItemPosition + listView.getChildCount() – 1; if (pos < firstListItemPosition || pos > lastListItemPosition ) { return listView.getAdapter().getView(pos, null, listView); } else { final int childIndex = pos – firstListItemPosition; return listView.getChildAt(childIndex); } }

How to lay out Views in RelativeLayout programmatically?

From what I’ve been able to piece together, you have to add the view using LayoutParams. LinearLayout linearLayout = new LinearLayout(this); RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); relativeParams.addRule(RelativeLayout.ALIGN_PARENT_TOP); parentView.addView(linearLayout, relativeParams); All credit to sechastain, to relatively position your items programmatically you have to assign ids to them. TextView tv1 = new TextView(this); tv1.setId(1); TextView … Read more

Android View.getDrawingCache returns null, only null

I was having this problem also and found this answer: v.setDrawingCacheEnabled(true); // this is the important code 🙂 // Without it the view will have a dimension of 0,0 and the bitmap will be null v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); v.buildDrawingCache(true); Bitmap b = Bitmap.createBitmap(v.getDrawingCache()); v.setDrawingCacheEnabled(false); // clear drawing cache

Is there an easy way to add a border to the top and bottom of an Android View?

In android 2.2 you could do the following. Create an xml drawable such as /res/drawable/textlines.xml and assign this as a TextView’s background property. <TextView android:text=”My text with lines above and below” android:background=”@drawable/textlines” /> /res/drawable/textlines.xml <?xml version=”1.0″ encoding=”utf-8″?> <layer-list xmlns:android=”http://schemas.android.com/apk/res/android” > <item> <shape android:shape=”rectangle”> <stroke android:width=”1dp” android:color=”#FF000000″ /> <solid android:color=”#FFDDDDDD” /> </shape> </item> <item android:top=”1dp” android:bottom=”1dp”> … Read more