Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ConstraintLayout getWidth return 0 [closed]

Tags:

android

I use ConstraintLayout in my project. While I find a problem when I try to call getWidth in onCreate. The result is 0.

The code is here.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_paint);
    iv = (ImageView) findViewById(R.id.iv);
    iv.setOnTouchListener(new ImageViewTouchListener());

    System.out.println("imageview width:" + iv.getWidth() + " height:" + iv.getHeight());
}

The layout design view

The xml content.

<?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:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_red"
        android:background="#ff0000"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_green"
        android:background="#00ff00"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/btn_red" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_blue"
        app:layout_constraintLeft_toRightOf="@+id/btn_green"
        android:background="#0000ff"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.constraint.Guideline
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/guideline"
        android:orientation="horizontal"
        tools:layout_editor_absoluteY="55dp"
        tools:layout_editor_absoluteX="0dp"
        app:layout_constraintGuide_percent="0.09784412" />

    <SeekBar
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/sb_stroke"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        android:layout_marginTop="16dp"
        app:layout_constraintTop_toBottomOf="@+id/btn_green" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline4"
        android:orientation="horizontal"
        tools:layout_editor_absoluteY="103dp"
        tools:layout_editor_absoluteX="0dp"
        app:layout_constraintGuide_percent="0.1840796" />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@+id/iv"
        app:layout_constraintTop_toTopOf="@+id/guideline4"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        tools:ignore="ContentDescription"
        android:background="#22000000"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintVertical_bias="0.0" />

</android.support.constraint.ConstraintLayout>

I want to know how to get the right width and height of ImageView.

I try to get width and height in onStart and onResume but get the same result 0.

like image 839
ittianyu Avatar asked Dec 15 '22 03:12

ittianyu


1 Answers

In OnCreate() your view has not yet been created as per your xml parameters. So you have to add addOnGlobalLayoutListener() on your view and you will get your dimensions in that listener once view has been created.

iv.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
    iv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
    System.out.println("imageview width:" + iv.getWidth() + " height:" + iv.getHeight());
   }
});
like image 62
Nikhil Avatar answered Jan 24 '23 02:01

Nikhil