6

When a view inside a ConstraintLayout is set up with android:layout_width="0dp" and android:layout_height="0dp" so that the constraints set in app:layout_constraintLeft_toLeftOfand the likes are applied, methods getWidth(), getMeasuredWidth(), getHeight(), etc return 0. How can I get the actual width, height, etc for such a view?

Here is a sample code:

<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/scMainFrame" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="?attr/actionBarSize" app:layout_constraintHorizontal_chainStyle="spread_inside" app:layout_constraintVertical_chainStyle="spread_inside" app:layout_optimizationLevel="chains" tools:context=".MainActivity" tools:layout_behavior="@string/appbar_scrolling_view_behavior"> <TextView android:id="@+id/textView10" android:layout_width="14dp" android:layout_height="0dp" android:gravity="center_vertical" android:text="A" android:background="#FF0000" app:layout_constraintTop_toTopOf="@+id/SCFrame" app:layout_constraintRight_toLeftOf="@+id/SCFrame" app:layout_constraintBottom_toTopOf="@+id/textView11" /> <TextView android:id="@+id/textView11" android:layout_width="14dp" android:layout_height="0dp" android:gravity="center_vertical" android:text="B" android:background="#FF0000" app:layout_constraintTop_toBottomOf="@+id/textView10" app:layout_constraintRight_toLeftOf="@+id/SCFrame" app:layout_constraintBottom_toBottomOf="@+id/SCFrame" /> <be.ema.sclibrary.SCConstraintLayout android:id="@+id/SCFrame" android:layout_width="150dp" android:layout_height="wrap_content" android:layout_marginLeft="14dp" android:background="#0000FF" app:layout_constraintLeft_toRightOf="@+id/textView10" app:layout_constraintTop_toTopOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintBottom_toBottomOf="parent" > <TextView android:id="@+id/A1" android:layout_width="0dp" android:layout_height="0dp" android:text="this is A1" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintRight_toLeftOf="@+id/A2" app:layout_constraintBottom_toTopOf="@+id/B1" /> <TextView android:id="@+id/A2" android:layout_width="0dp" android:layout_height="0dp" android:text="this is A2" app:layout_constraintLeft_toRightOf="@+id/A1" app:layout_constraintTop_toTopOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintBottom_toTopOf="@+id/B2" /> <TextView android:id="@+id/B1" android:layout_width="0dp" android:layout_height="0dp" android:text="this is B1" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toBottomOf="@+id/A1" app:layout_constraintRight_toLeftOf="@+id/B2" app:layout_constraintBottom_toBottomOf="parent" /> <TextView android:id="@+id/B2" android:layout_width="0dp" android:layout_height="0dp" android:text="this is B2" app:layout_constraintLeft_toRightOf="@+id/B1" app:layout_constraintTop_toBottomOf="@+id/A1" app:layout_constraintRight_toRightOf="parent" app:layout_constraintBottom_toBottomOf="parent" /> </be.ema.sclibrary.SCConstraintLayout> </android.support.constraint.ConstraintLayout> 

and

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); View v = findViewById(R.id.A1); System.out.println("A1 width=" + v.getWidth()); System.out.println("A1 measured width=" + v.getMeasuredWidth()); } 
1
  • 1
    See this article for a possible solution Commented May 25, 2017 at 7:25

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.