5

I am working on layout in android XML in which I would like to set a buttons height to match it's width when setup to fill parent. Obviously this number will change based on screen size, so I cannot use a set pixel size. Can someone help me with getting the button width based on screen size and then passing that to the height setting?

Thank You, Josh

Paresh Mayani
129k71 gold badges244 silver badges297 bronze badges
asked Oct 15, 2010 at 10:17

2 Answers 2

3

I once had a similar problem and found no solution which works just from XML. You have to write your own Button-Class and overwrite the [onMeassure][1] method.

Example:

/**
 * @see android.view.View#measure(int, int)
 */
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
}
private int width; // saves the meassured width
/**
 * Determines the width of this view
 * 
 * @param measureSpec
 * A measureSpec packed into an int
 * @return The width of the view, honoring constraints from measureSpec
 */
private int measureWidth(int measureSpec) {
 int result = 30;
 int specMode = MeasureSpec.getMode(measureSpec);
 int specSize = MeasureSpec.getSize(measureSpec);
 if (specMode == MeasureSpec.EXACTLY) {
 // We were told how big to be
 result = specSize;
 } else {
 result =1123; // meassure your with here somehow
 if (specMode == MeasureSpec.AT_MOST) {
 // Respect AT_MOST value if that was what is called for by measureSpec
 result = Math.min(result, specSize);
 }
 }
 width = result;
 return result;
}
/**
 * Determines the height of this view
 * 
 * @param measureSpec
 * A measureSpec packed into an int
 * @return The height of the view, honoring constraints from measureSpec
 */
private int measureHeight(int measureSpec) {
 int result = 0;
 int specMode = MeasureSpec.getMode(measureSpec);
 int specSize = MeasureSpec.getSize(measureSpec);
 if (specMode == MeasureSpec.EXACTLY) {
 // We were told how big to be
 result = specSize;
 } else {
 result = width;
 if (specMode == MeasureSpec.AT_MOST) {
 // Respect AT_MOST value if that was what is called for by measureSpec
 result = Math.min(result, specSize);
 }
 }
 return result;
}

[1]: http://developer.android.com/reference/android/view/View.html#onMeasure(int, int)

answered Oct 15, 2010 at 11:43
Sign up to request clarification or add additional context in comments.

Comments

-1

Instead of using Pixel size by PX, mention dip(i.e. device independent pixel), dip will take the size in pixel according to the device screen size independently.

for example: android:textSize="12dip"

You can either use dip or dp.

Enjoy!!

answered Oct 15, 2010 at 10:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.