Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set same width and height of a button

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..

like image 396
Salman Zaidi Avatar asked Jan 11 '13 14:01

Salman Zaidi


People also ask

How do I fix a button size in CSS?

Tip: Use pixels if you want to set a fixed width and use percent for responsive buttons (e.g. 50% of its parent element).


2 Answers

How about this:

Button yourBtn = (Button) findViewById.(R.id.yourBtn);

int btnSize=yourBtn.getLayoutParams().width;
yourBtn.setLayoutParams(new LayoutParams(btnSize, btnSize));
like image 105
Booger Avatar answered Sep 17 '22 11:09

Booger


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.

like image 36
Budius Avatar answered Sep 19 '22 11:09

Budius