0

With the following Java and XML code, I was wondering how to retrieve the dimensions in pixels of an inner/child layout (LinearLayout, whereas the parent is RelativeLayout with paddings of 20dp) properly:

Java code:

public class TestActivity extends Activity {
    private LinearLayout linLay;

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

        linLay = (LinearLayout)findViewById(R.id.linLay);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);
        linLay.setLayoutParams(lp);

        getDimensions();
    }

    private void getDimensions() {
        System.out.println(linLay.getHeight());
        System.out.println(linLay.getWidth());
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp" >

    <LinearLayout
        android:id="@+id/linLay"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true">

    </LinearLayout>
</RelativeLayout>

... Since I get outputs of 0 for the dimensions, I am aware that the LinearLayout needs to be set first on the screen before I could retrieve the dimensions... But what am I doing wrong here?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
DaveNOTDavid
  • 1,753
  • 5
  • 19
  • 37
  • 1
    http://stackoverflow.com/questions/7474572/which-activity-method-is-called-after-all-the-layout-methods-was-made – Entreco Mar 28 '16 at 14:57

1 Answers1

1

It didn't get its dimensions at the time you try to output them.You can just add a delay or just use method below.

private void getDimensions() {

    ViewTreeObserver vto = linLay.getViewTreeObserver();  
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
        @Override 
        public voidonGlobalLayout() { 
            linLay.getViewTreeObserver().removeGlobalOnLayoutListener(this);      
            int height =linLay.getMeasuredHeight(); 
            int width =linLay.getMeasuredWidth();  
            System.out.println(height);
            System.out.println(width);
        }  
    });

}
tiny sunlight
  • 6,231
  • 3
  • 21
  • 42
  • So I came across another problem... How would I catch the View during a configuration change if another background thread (such as a timer) is running? Here's my post in more detail: http://stackoverflow.com/questions/36388337/runnable-and-viewtreeobserver-together-in-one-activity – DaveNOTDavid Apr 03 '16 at 18:28
  • @dpark14 it said Page Not Found – tiny sunlight Apr 04 '16 at 04:09
  • My apologies... Here it is: http://stackoverflow.com/questions/36408277/handling-a-runnable-thread-during-configuration-changes – DaveNOTDavid Apr 04 '16 at 16:43