0

I am trying to pass in view height as a max value for an animation inside onCreate to a custom gesture listener:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mParent = findViewById(R.id.root_ll);
        mImage1 = findViewById(R.id.image1);

        mImage1.setOnTouchListener(this);

        float max = mParent.getHeight() - mImage1.getHeight();
        mGestureDetector = new GestureDetectorCompat(this, new CustomGestureListener(mImage1, 0, max));
    }

The problem is that 0 is passed as max, showing that mRoot as no height inside the onCreate method. However, when I get its height inside another method like onTouch, then it has height. The layout for mRoot is as follows:


    <LinearLayout
        android:id="@+id/root_ll"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#4CAF50"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:layout_margin="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/image1"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_margin="8dp"
            android:layout_weight="1"
            android:src="@drawable/myimage" />


    </LinearLayout>
Mehdi Haghgoo
  • 3,144
  • 7
  • 46
  • 91

1 Answers1

0

It is because the 'onCreate' method is called before the view is rendered. In your case, the layout is not rendered and so the container has no heigth yet.

I think the 'onResume' is where the rendering should be finished.

kaputnix
  • 123
  • 10