3

I know that this has been asked several times before but I can't get a solution to mine issue. I am trying to add multiple buttons programmatically. I want the buttons to be aligned horizontally. However, only one button is showing up. What I have tried so far is,

    private void locationSort() {

    RelativeLayout townLayout = (RelativeLayout) locationLayout.findViewById(R.id.town_sort);
    for (int i = 0; i <= 3; i++) {

        LayoutInflater inflater = getLayoutInflater();

        Button btnTag = (Button) inflater.inflate(R.layout.buttons, null,
                false);
        for (int j = 0; j < 4; j++) {

            btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            btnTag.setText("Button");
            btnTag.setBackgroundResource(R.drawable.alpha_button_selector);
            btnTag.setClickable(true);
            btnTag.setTextColor(Color.WHITE);
            btnTag.setGravity(Gravity.CENTER);
            btnTag.setId(j);
        }
        townLayout.addView(btnTag);

        btnTag.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "this is test", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

I have searched on the same and gone through the links like,

and many other, but I can't make multiple buttons here. Only a single button is being displayed everytime. Can anyone please help?

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
manini
  • 358
  • 1
  • 4
  • 14

2 Answers2

3

First of all, you should use LinearLayout with HORIZONTAL orientation, istead of RelativeLayout , because in Relative all your views will be in the same place (that's why you can see only one Button)

Olena Y
  • 233
  • 2
  • 8
1

First, you create single Button btnTag, then you loop and change this single button multiple times (so it makes no sense as all changes but last ones are overwritten). Finally, you add that single button to the view group. Once. So all here works correctly (except this is not what you expected).

You should make button creation and addView() part of your loop.

Button btnTag;

for (int j = 0; j < 4; j++) {
        btnTag = (Button) inflater.inflate(R.layout.buttons, null,
            false);

        ...

        btnTag.setId(j);

        townLayout.addView(btnTag);
    }

Also, as you use own XML file for button inflation, you should move certain attributes to that XML and then remove all setClickable(), setTextColor() etc.

You should consider replacing RelativeLayout container with i.e. vertical LinearLayout, otherwise you will end up with buttons overlapping each other (as your code does not position them).

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • This is working but the issue is that each time the locationSort() is being called, the number of buttons is getting incremented. – manini Aug 03 '17 at 09:15
  • 1
    if the method is names "sort" then why it creates anything in first place? – Marcin Orlowski Aug 03 '17 at 09:20
  • Does the method name matters? It's my bad then. But i don't understand why is the number of buttons getting incremented each time. – manini Aug 03 '17 at 09:25
  • 1
    name matters in terms of bad habits and code readability. And the number of buttons increase because each time you create new buttons. if you need i.e. change existing then you need to rework the logic completely then you need to create them elsewhere and then look for them using `findViewById()`. or remove current buttons and add new ones. – Marcin Orlowski Aug 03 '17 at 09:47
  • What I want is to create 'n' number of buttons at a time, where the value of n can change each time. – manini Aug 03 '17 at 09:53