0

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();
        }
    });
}
Community
  • 1
  • 1
Niaz
  • 545
  • 2
  • 12
  • 21
  • No, this is another question. – Niaz Aug 25 '14 at 17:26
  • Try this method: http://stackoverflow.com/questions/25295293/give-all-child-elements-fixed-width-in-android/25296962#25296962 – x-code Aug 26 '14 at 16:37
  • Thank you, but there no answer. Tablelayout does not guarantee that text in button will not be wrapped. As for getChildAt, there are not code example. And it looks it is not my case. – Niaz Aug 28 '14 at 03:59

3 Answers3

0

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.

MHP
  • 2,613
  • 1
  • 16
  • 26
  • Wow! It works. Thank you very much! But when I run this page again, I see the old width for button2. Each page runs another from OnCreate(). – Niaz Aug 25 '14 at 17:24
  • Adding/deleting the line "super.onWindowFocusChanged(hasFocus);" does not change the performance. – Niaz Aug 25 '14 at 17:30
  • onwindowfocuschange should call when you run activity,so if you settwidth in that method it should work – MHP Aug 25 '14 at 17:37
  • To show buttons I call page1() from onCreate. Now buttons view are as expected. Then in page1() I call page2() after clicking some button. In page2() I call page1() again. And now the width of button1 is not populated to button2 – Niaz Aug 25 '14 at 18:01
  • ok,first I should say don't need to start activity from page2,just call finish().second normally onWindowFocusChange() call after each time you visit activity but i saw in some device it's different,so you can store button1 width first time and in onresume() set width for button2 or do some thing like @user3249477 answer if it not work ,call me – MHP Aug 25 '14 at 19:00
  • Sorry, not understood. I call all page1, page2,... in the same activity. Button1.post does not work. Maybe I have to place his code in another place than in page1(). Thanks. – Niaz Aug 26 '14 at 03:59
  • I am chaking your project but at first you should change button1 and button2 as global variable not in method page1 and page2 – MHP Aug 26 '14 at 13:49
  • Oh, pardon. They are global. I am mistaken writing the program schema here – Niaz Aug 26 '14 at 14:21
  • code work for me,I don't know where is your problem.I can help you if you want by sending me your project(by email) – MHP Aug 26 '14 at 17:48
  • I expected that it is easy to populate width of one button to another. But it seems it is not so easy. Do you think such approach makes sense? Margins or weight for vertical layouts looks much more easy. Okay, I will send the project. It is interesting anyway. Thanks! – Niaz Aug 27 '14 at 08:13
  • My page1 and page2 in the same activity. But they are in different activities in your code. It seems it the reason that my code does not work properly. But having special activity for each page looks me cumbersome. Or my approach is incorrect? – Niaz Aug 27 '14 at 08:46
  • TableLayout solution below looks me more simple. Sorry, MHP, I have not enough reputation to vote for you, since your approach also works. Thanks again for participation! – Niaz Aug 30 '14 at 07:48
  • @Niaz ,there is multiple way that I told you,like fragment that is best way instead change setcontentview or viewSwitcher or viewpager or... I learn new one from x-code too.but I try to help you as you like it to do. also I help you because we are from same country,and I want to improve you. no problem at all :) – MHP Aug 30 '14 at 16:43
  • MHP, thank you for help. But it seems the country of user is not visible here. Maybe I don't understand something in this site... – Niaz Aug 30 '14 at 17:51
  • from your name I understand – MHP Aug 30 '14 at 19:02
  • My name is popular in many countries. There are countries where are few or many native nations. – Niaz Aug 31 '14 at 06:00
0

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 :)

Simas
  • 43,548
  • 10
  • 88
  • 116
  • Unfortunately it does not work. (onWindowFocusChanged block is removed) – Niaz Aug 25 '14 at 18:08
  • @Niaz are you not getting the correct width or setting the width doesn't work? – Simas Aug 25 '14 at 18:11
  • I placed your code in page1(), which is called from onCreate(). The width of button2 was not changed. Thanks! – Niaz Aug 26 '14 at 04:01
0

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.

Community
  • 1
  • 1
x-code
  • 2,940
  • 1
  • 18
  • 19
  • Thank you, but TableLayout does not guarantee text from wrapping, if android:maxLines="1", then as I think, text can be shorten. – Niaz Aug 29 '14 at 04:47
  • Yes!!! It works! Thank you. This is the most simple solution for this question. Thank you! I 've switched the best answer to your answer. But MHP's approach also works. Sorry, I have not enough reputation to vote for MHP. – Niaz Aug 30 '14 at 07:45