I don't understand how to do this. Can someone explain to me how to do that with an example using an ImageView or a simple button?
Asked
Active
Viewed 344 times
0
-
yes you can do it with percentage check this http://stackoverflow.com/a/16518557/1939564 – Muhammad Babar Sep 27 '13 at 04:58
2 Answers
1
I do something like this in onCreate to get the correct width and height:
final ViewTreeObserver vto = view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < 16) {
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
width = view.getWidth();
height = view.getHeight();
// using the method Alécio mention
setupOtherViewsThatDependOnWidthAndHeight();
}
});
The case here is that size and height does not return correct values before screen is done rendering.

cYrixmorten
- 7,110
- 3
- 25
- 33
0
You can use the Layout parameters for this. You can use it like this, passing the width and height programmatically (100,100 respectively)
LayoutParams params = new LayoutParams(100,100);
view.setLayoutParams(params);

Alécio Carvalho
- 13,481
- 5
- 68
- 74