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?

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

Comments

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

5 Comments

This is working but the issue is that each time the locationSort() is being called, the number of buttons is getting incremented.
if the method is names "sort" then why it creates anything in first place?
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.
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.
What I want is to create 'n' number of buttons at a time, where the value of n can change each time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.