Get RelativeLayout Values

How to get dimensions of a specific layout, for example linearlayout or relativelayout, with its layout_width and layout_height set to wrap_content? using this method

 layout.measure(MeasureSpec.makeMeasureSpec(0,
            MeasureSpec.UNSPECIFIED),
            MeasureSpec.makeMeasureSpec(0,
                    MeasureSpec.UNSPECIFIED));

 layout.layout(0, 0,
            layout.getMeasuredWidth(),
            layout.getMeasuredHeight());

returns null. I am trying to create a bitmap image from relativelayout, kind of like a screenshot.

+3
source share
3 answers

Do not do this using the onCreate () method. Try the following:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus == true) {
        LinearLayout myLinearLayout = (LinearLayout) findViewById(R.id.myLinearLayout);
        int layoutWidth = myLinearLayout.getWidth();
        int layoutHeight = myLinearLayout.getHeight();
    }
}

This method is called after onCreate () when sizes are already known.

+5
source

You can use getMeasuredWidth () and getMeasuredHeight () in your view.

, . onCreate, . , .

+1

AndroidResizeView {

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

}

}

0

All Articles