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

亡梦爱人 提交于 2019-11-29 13:17:07
user2511882

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);
}

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);
                    }
                });

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.

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()));

    }

}

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();
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!