1

I am not able to get width and height of RelativeLayout, how can I get this? My layout.xml file is

<?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:orientation="vertical" >

<RelativeLayout
    android:id="@+id/layoutMain"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="50dp" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/ic_launcher" />
</RelativeLayout>

and I just get programtically height and width of RelativeLayout using its built-in provided method like,

public class CustomShowcaseActivity extends Activity {    static float width;
  static float height;  RelativeLayout relMain;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_custom_showcase);

    relMain= (RelativeLayout) findViewById(R.id.layoutMain);


    width = relMain.getMeasuredWidth();
      height = relMain.getMeasuredHeight();

    width = relMain.getWidth();
    height = relMain.getHeight();

    System.out.println("=== Width : " + width);
    System.out.println("=== height : " + height);

} } 

Can anyone help me with this issue?

abarisone
  • 3,707
  • 11
  • 35
  • 54
Gaurav Mandlik
  • 525
  • 1
  • 9
  • 42

1 Answers1

4

override this method first.

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

Try by using only getHeight() and getWidth()

 private void getRelativeLayoutInfo(){
    relMain = (RelativeLayout) findViewById(R.id.layoutMain);
    w = relMain.getWidth();
    h = relMain.getHeight();
    Log.v("W-H", w+"-"+h);
 }
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58