-1

First, I know this is a duplicate of other question but I couldn't manage to fix it so I created my own question for help. I am trying to get the RelativeLayout width, it was successful at first but I don't know what I changed and the code returns only zero.

Here is my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView[] imageViewArray = new ImageView[20];
    ArrayList<Float> xarray = new ArrayList<>();
    ArrayList<Float> yarray = new ArrayList<>();
    rlt = (RelativeLayout) findViewById(R.id.layout);
    imageView = (ImageView) findViewById(R.id.imageView);
    rlt.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            rlt.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            Toast.makeText(MainActivity.this, "right" + rlt.getWidth(), Toast.LENGTH_SHORT).
                    show();
            layoutwidth = rlt.getWidth();
            layoutheight = rlt.getHeight();
        }
    });
}

Here is my activity_main xml file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/layout"
    android:background="@android:color/black"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@mipmap/you"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

Simple one, but yet not working, any help please?

Guy Balas
  • 98
  • 3
  • 10
  • 1
    Look at this post from a duplicate question which is very comprehensive in the answer given: http://stackoverflow.com/a/24035591/2240706 – rcbevans Sep 11 '15 at 14:25
  • Do you use Android Studio? Maybe you can find it in your local history? Right click in your Activity file -> Local History -> Show History. – Robby Smet Sep 11 '15 at 14:25

4 Answers4

2

It's because your layout's width isn't set yet You should override onLayout() method

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    height = bottom - top;
    width = right - left;
}
uneven
  • 341
  • 2
  • 13
  • After calling `super.onLayout(...)` the width and height from the layout are calculated allowing you to call `rlt.getWidth();` as you would expect before – rcbevans Sep 11 '15 at 14:27
  • Actually I did some debugging and noticed the value returns zero only when I use layoutwidth outside the onGlobalLayout listener, Current code is working, it returns the width actually, but I need to use it outside, If I will move the Toast to after the OnGlobalLayout it will return zero. I don't know why. anyway, thx. – Guy Balas Sep 11 '15 at 17:47
  • @GuyBalas, so you can store these values in local variables and access via getters – uneven Sep 14 '15 at 03:22
0

Usually you would extend whatever View you're interested in (here RelativeLayout) and override onSizeChanged():

https://developer.android.com/reference/android/view/View.html#onSizeChanged(int,%20int,%20int,%20int)

Whether that is useful in your scenario or not is hard to tell, since your question is fairly generic.

rcbevans
  • 7,101
  • 4
  • 30
  • 46
ci_
  • 8,594
  • 10
  • 39
  • 63
0

Try

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run()
    {
        //get width here
    }
}, 10);
Pang
  • 9,564
  • 146
  • 81
  • 122
Mayur R. Amipara
  • 1,223
  • 1
  • 11
  • 32
  • This will not always work and is simply delaying the execution of whatever goes inside the "run" method by 10 milliseconds. – dell116 Jul 25 '16 at 22:50
0

Maybe try to put the code for the views into the "onPostCreate" Method

Kai_Jan_57
  • 62
  • 6