Niaz
Niaz

Reputation: 564

How to populate width of a button to another button (no using weight)

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();
        }
    });
}

Upvotes: 0

Views: 101

Answers (3)

x-code
x-code

Reputation: 3000

Use a TableLayout as in this answer and then prevent the text in the buttons from wrapping by adding to the XML for each button android:maxLines="1"

Using the right layout manager is more reliable than trying to change view sizes by hand.

Upvotes: 0

MHP
MHP

Reputation: 2731

you should do this to set width for you button.

  int width = button1.getWidth();
  LayoutParams lp = button2.getLayOutParams();
  lp.width = width;
  button2.setLayoutParams(lp);  

NOTE: do this in onWindowFocusChange() method,because before that your views was not created

UPDATE: this work for me

private int widthButton = 0;
private Button button1;
private Button button2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button1 = (Button) this.findViewById(R.id.button1);
    button2 = (Button) this.findViewById(R.id.button2);

    button1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(Main.this,SecondActivity.class);
            startActivity(intent);
        }
    });
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    widthButton=button1.getWidth();
    if (widthButton==0) return;
    LayoutParams lp = button2.getLayoutParams();
    lp.width = widthButton;
    button2.setLayoutParams(lp);
}  

UPDATE:
change page1() and page2() to this:

private void page1() {
    setContentView(R.layout.page_1);
    button1a = (Button) findViewById(R.id.button1a);
    button1a.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            page2();
        }
    });

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

    LayoutParams lp = button1b.getLayoutParams();
    lp.width = width;
    button1b.setLayoutParams(lp);
}

private void page2() {
    setContentView(R.layout.page_2);
    button2a = (Button) findViewById(R.id.button2a);
    button2a.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            page1();
        }
    });

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

    LayoutParams lp = button1b.getLayoutParams();
    lp.width = width;
    button2b.setLayoutParams(lp);
}  

NOTE: your code works perfectly but it isn't true code. if you want to change view of your activity you should use fragment.

Upvotes: 0

Simas
Simas

Reputation: 44158

That's because you are trying to getWidth() of button1 when it wasn't yet drawn. Instead add it to the queue with post():

final Button button1 = (Button) findViewById(R.id.button1);
final Button button2 = (Button) findViewById(R.id.button2);

button1.post(new Runnable() {
    @Override
    public void run() {
        int width = button1.getWidth();
        button2.setWidth(width);
    }
});

The reason why setWidth(0) made it look like nothing has changed, is because the Button has a min width of 64dip :)

Upvotes: 0

Related Questions