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.
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();
}
来源:https://stackoverflow.com/questions/20547974/how-to-get-programmatically-width-and-height-of-relative-linear-layout-in-andr