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

6

13

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.

Querist answered 12/12, 2013 at 15:50 Comment(0)
Q
7

Well I have implemented by this way:

LinearLayout linearLayout = (LinearLayout)findViewById(R.id.layout);
ViewTreeObserver viewTreeObserver = linearLayout.getViewTreeObserver(); 
viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
        linearLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
        int width  = linearLayout.getMeasuredWidth();
        int height = linearLayout.getMeasuredHeight(); 

    } 
});

This example is for Linear layout, for Relative layout follow same process.

Hope this would help you.

Querist answered 7/7, 2016 at 7:2 Comment(2)
do you able to get the width?Solitta
@N.Droid, Yes. both.Querist
T
18

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);
}
Tillo answered 12/12, 2013 at 15:53 Comment(2)
Accept and close the question. ThanksTillo
i can not accept answer till 7 min from your answer time, just wait for 2 min :)Querist
Q
7

Well I have implemented by this way:

LinearLayout linearLayout = (LinearLayout)findViewById(R.id.layout);
ViewTreeObserver viewTreeObserver = linearLayout.getViewTreeObserver(); 
viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
        linearLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
        int width  = linearLayout.getMeasuredWidth();
        int height = linearLayout.getMeasuredHeight(); 

    } 
});

This example is for Linear layout, for Relative layout follow same process.

Hope this would help you.

Querist answered 7/7, 2016 at 7:2 Comment(2)
do you able to get the width?Solitta
@N.Droid, Yes. both.Querist
D
5

You can attach a OnGlobalLayoutListener to ViewTreeObserver of the relative layout which will get called when the view is attached to the window and it's actual height is assigned to it.

final RelativeLayout rl_cards_details_card_area = (RelativeLayout) findViewById(R.id.rl_cards_details_card_area);
        rl_cards_details_card_area.getViewTreeObserver()
                .addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

                    @Override
                    public void onGlobalLayout() {
                        // TODO Auto-generated method stub
                        int w = rl_cards_details_card_area.getWidth();
                        int h = rl_cards_details_card_area.getHeight();
                        Log.v("W-H", w + "-" + h);
                        rl_cards_details_card_area.getViewTreeObserver()
                                .removeOnGlobalLayoutListener(this);
                    }
                });
Diplomate answered 12/12, 2013 at 16:5 Comment(0)
L
0

try like this:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class AndroidResizeView extends Activity {

    TextView textMyTextView;
    Button myButton;

    RelativeLayout rlayout;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hightk);

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

        textMyTextView = (TextView) findViewById(R.id.textView1);

        myButton = (Button) findViewById(R.id.button1);

        myButton.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                updateSizeInfo();
            }
        });
    }

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

    private void updateSizeInfo() {

        textMyTextView.setText(String.valueOf("Width: "
                + rlayout.getWidth() + "height ::" + rlayout.getHeight()));

    }

}
Lopes answered 12/12, 2013 at 15:59 Comment(0)
A
0

we may not get correct height and wight of a view or layout as soon the activity is just created since the layout is not inflated completly

so

1.either you can let a runnable method triggered after some secs which in turn call a function that gets the width and height properties.

2.we can use view tree obsever

 l = (RelativeLayout)findviewbyid(R.id.rl_cards_details_card_area);
ViewTreeObserver observer = l.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            // TODO Auto-generated method stub
            ActivityClassName.this.getProperties();
        l.getViewTreeObserver().removeGlobalOnLayoutListener(
                this);
    }
});
void getProperties() {
    int a= l.getHeight();
        int b = l.getWidth();
    Toast.makeText(getActivity,""+a+" "+b,1000).show();
    }
Amy answered 12/12, 2013 at 16:8 Comment(2)
Why you have answered the same which @vipul had already posted. The place is not supposed to copy answers, instead you can upvote if your answer is similar.Immaterialism
sorry the answer was being typed by me at same time and didn't copy it - thanksAmy
P
0

For Kotlin: You can use AndroidX's doOnLayout extension function to get height or width of a layout

view.doOnLayout {
  Timber.d("View's Height: ${view.height}")
}

Performs the given action when this view is laid out. If the view has been laid out and it has not requested a layout, the action will be performed straight away, otherwise the action will be performed after the view is next laid out.

The action will only be invoked once on the next layout and then removed. For more info

Persis answered 29/10, 2019 at 8:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.