I need to create 4 buttons with equal width in a row. I know how to do that using linear layout and weights, but that's not quite acceptable for me. I have some other views that are related to my 4 buttons, so I'd like to do everything inside relative layout. Is it possible?
Asked
Active
Viewed 3,197 times
0
-
2Put your buttons in a `LinearLayout` and put the `LinearLayout` in the `RelativeLayout`. Or do you mean you have views which must be relative to a specific `Button`? – Squonk Jun 01 '12 at 21:37
-
@MisterSquonk yes, views are relative to a specific Button. Actually, I can put a LinearLayout inside the RelativeLayout, and then just wrap each Button with it's own RelativeLayout, but it's rather weird solution and sounds like "layoutCeption" :) – Yury Pogrebnyak Jun 01 '12 at 21:43
-
2Maybe you should add more detail about the entire layout. Then we can offer more complete suggestions. Trying to make buttons be the same length without a LinearLayout is asking for trouble. – you786 Jun 01 '12 at 21:50
-
@YuriyPogrebnyak : As you786 says, if you can expand your question to explain what the other views are and how they need to relate to each button then it would possibly help get an answer. – Squonk Jun 01 '12 at 21:53
3 Answers
1
How about setting the buttons' width to a standard size?
<Button
android:width="75dp"
... />
Addition
To find the width of the display at runtime:
int width = getWindowManager().getDefaultDisplay().getWidth() / 4;
// Set this width to your buttons

Sam
- 86,580
- 20
- 181
- 179
-
2The problem is that total screen width is specific to each android device – Yury Pogrebnyak Jun 01 '12 at 21:49
0
choose one of the views that will tell the others to have the same width and height . in order to fetch the size of a view , use something like this:
private static void runJustBeforeBeingDrawn(final View view, final Runnable runnable)
{
final ViewTreeObserver vto = view.getViewTreeObserver();
final OnPreDrawListener preDrawListener = new OnPreDrawListener()
{
@Override
public boolean onPreDraw()
{
Log.d(App.APPLICATION_TAG, CLASS_TAG + "onpredraw");
runnable.run();
final ViewTreeObserver vto = view.getViewTreeObserver();
vto.removeOnPreDrawListener(this);
return true;
}
};
vto.addOnPreDrawListener(preDrawListener);
}
Alternative to runJustBeforeBeingDrawn: https://stackoverflow.com/a/28136027/878126

android developer
- 114,585
- 152
- 739
- 1,270
0
I don't know for sure, but I think that the only way you can do this, without weight tags, is to runtime/programatically setup it. I've done this by setting a constant in a integer.xml, like the following:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="numberOfButtons">4</integer>
</resources>
And, in your Adapter/Activity class, something like the following:
DisplayMetrics dm = new DisplayMetrics();
// dm holds your structure of resolution
context.getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.getWidth();
btn1.setWidth(width/getInteger(R.integer.numberOfButtons));
btn2.seTwidth(width/getInteger(R.integer.numberOfButtons));
btn3.seTwidth(width/getInteger(R.integer.numberOfButtons));
btn4.seTwidth(width/getInteger(R.integer.numberOfButtons));
Hope this helps you in some way.

Marcelo
- 2,075
- 5
- 21
- 38
-
As stated before by Yuriy, each androidian device has its own resolution/screensize. This value can be obtained with "DisplayMetrics" class. – Marcelo Jun 01 '12 at 22:05