Reputation: 955
I want to make a button square with the width the same as the heigth.
I try bt.setWidth(bt.getHeight())
but it doesn't work.
If I hardcode the width (bt.setWidth(90)
) it works but I don't know the height so I can hardcoded it.
Here is some code. When I click on a button, it opens a dialog and this dialog must contain the square button.
public class MyClass extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myLayout);
}
public void doClick(View view) {
final Dialog dialog = new Dialog(view.getContext());
dialog.setContentView(R.layout.dialogLayout);
dialog.setTitle("Title");
Button bt = (Button) dialog.findViewById(R.id.myButton);
bt.setWidth(bt.getHeight());
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
...
}
});
dialog.show();
}
}
How could I do it?
Upvotes: 1
Views: 764
Reputation: 14472
The direct answer is that until the dialog is displayed, it has not measured it's layout and the button height will be zero. You could extend the Dialog class and override the onMeasure() method or attach a global layout listener to the layout and set the button size in onLayoutComplete().
However, your approach might be wrong. Why can't you do this in the dialog's layout XML?
Upvotes: 2