I'd like to make the same width for all buttons in vertical layout, which is equal to the button with maximum text. I try to get the width of button with max text. The following does not work. There are many questions about it in this site.
width=button1.getWidth();
button2.setWidth(width);
Okay, I can get button1.getWidth() as described here
But I can't find an answer how to set new width to other buttons. I can't set the width to button2.
"int width" should be final. If I make it final, I got error for width=button1.getWidth().
Any ideas please? Thanks!!!
MY CODE AFTER MODIFICATIONS. As I said it changes button2 width only once. When I return to page1 again, the width of button2 is not changed.
Button button1;
Button button2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
page1();
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
widthButton=button1.getWidth();
if (widthButtons==0) return;
LayoutParams lp = button2.getLayoutParams();
lp.width = widthButton;
button2.setLayoutParams(lp);
}
private void page1() {
button1 = (Button) this.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
page2();
}
});
button2 = (Button) this.findViewById(R.id.button2);
}
private void page2() {
Button button5 = (Button) this.findViewById(R.id.button5);
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
page1();
}
});
}