Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get programmatically width and height of Relative - Linear layout in android?

I required runtime dimensions of Relative/Linear Layout.

activity_home.xml:

<RelativeLayout
    android:id="@+id/rlParent"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</RelativeLayout>

HomeActivity.java:

private int width, height;

RelativeLayout rlParent = (RelativeLayout) findViewById(R.id.rlParent);

w = rlParent.getWidth();
h = rlParent.getHeight();

Log.i("Width-Height", width+"-"+height);

Please share your thoughts.

like image 288
Hiren Patel Avatar asked Dec 12 '13 15:12

Hiren Patel


People also ask

How do I find the height and width of RelativeLayout in android programmatically?

private int width, height; RelativeLayout rlParent = (RelativeLayout) findViewById(R. id. rlParent); w = rlParent. getWidth(); h = rlParent.

How do you find the width of a view?

The width and height can be obtained by calling getWidth() and getHeight().

What is the difference between relative layout and FrameLayout?

RelativeLayout : is a ViewGroup that displays child views in relative positions. AbsoluteLayout : allows us to specify the exact location of the child views and widgets. TableLayout : is a view that groups its child views into rows and columns. FrameLayout : is a placeholder on screen that is used to display a single ...

What are two different ways to set the height and width of component in Android?

First get layout params using view. getLayoutParams(). Second then set the height and width value then last set the layout params of view. Save this answer.


1 Answers

Try this

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub
    super.onWindowFocusChanged(hasFocus);
    updateSizeInfo();
}

private void updateSizeInfo() {
    RelativeLayout rlayout = (RelativeLayout) findViewById(R.id.rlayout);
    w = rlayout.getWidth();
    h = rlayout.getHeight();
    Log.v("W-H", w+"-"+h);
}
like image 186
user2511882 Avatar answered Oct 19 '22 05:10

user2511882