I have a xml layout file in which I have a Button
. The Height of the Button
is set as match_parent
. Now I want the Width to be the same as the Height of the Button to make a square button regardless of changing Height and Width of parent layout..
Asked
Active
Viewed 1.3k times
7

TNR
- 5,839
- 3
- 33
- 62

Salman Zaidi
- 9,342
- 12
- 44
- 61
2 Answers
16
How about this:
Button yourBtn = (Button) findViewById.(R.id.yourBtn);
int btnSize=yourBtn.getLayoutParams().width;
yourBtn.setLayoutParams(new LayoutParams(btnSize, btnSize));

Booger
- 18,579
- 7
- 55
- 72
-
1When should this code be called? I call it in `onCreate` but the `btnSize` is -2. – Felix Aug 22 '16 at 13:37
3
You have to do it programmatically. XML layout is only for static stuff.
After the button have been draw at least once on the screen you will resize it by setting a new LayoutParams
to it with the desired values.

Budius
- 39,391
- 16
- 102
- 144