0

When the user inputs a word, he creates a number of Buttons equal to the length of the word. For example: if user inputs "aaaa" he will create 4 Buttons, side by side, in the first row. Then if the user enters "bb" he will create 2 Buttons, side by side, in the second row. And "ccc" he creates 3 Buttons...

Image to demonstrate:

enter image description here

I dynamically create a RelativeLayout, then dynamically add Buttons to that layout. And finally I add the RelativeLayout to my existing LinearLayout. But the problem is, only one Button is added per row. And my program currently looks like this:

enter image description here

Can someone please me fix this problem?

CODE:

    final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_bttn_words);

    final LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);

    button_test.setOnClickListener(
            new View.OnClickListener()
            {
                public void onClick(View view)
                {
                    RelativeLayout relativeLayout = new RelativeLayout(view.getContext());

                    RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                                    RelativeLayout.LayoutParams.WRAP_CONTENT);    


                    int size = enter_txt.getText().toString().length(); //the user input number of buttons

                    int id = 1;

                    for (int i=0; i<size; i++)
                    {
                        Button myButton = new Button(view.getContext());

                        myButton.setBackgroundResource(R.drawable.button);

                        myButton.setId(id);

                        rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId());

                        relativeLayout.addView(myButton, rlp);

                        id++;
                    }

                        linearLayout.addView(relativeLayout, llp);

3 Answers 3

2
rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId());

This line says that myButton should be added to right of myButton, which doesn't make any sense.

simple way to resolve this is to use the following line instead

rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId()-1);

But this isn't the best way to do this, you should use LinearLayout with horizontal orientation instead.

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

2 Comments

hm, there must be some more bugs. the program is still not wokring. I will try to do it perhaps in a LinearLayout like you suggested. I'm just so used to using vertical orientation I forgot there was a horizontal...
try incrementing the id before the line myButton.setId(id);
1

The structure should be simple

Just need to add your buttons in 3 different linear layout with orientation horizontal.

Like

<Relative layout>{
<LinearLayout global container with vertical orientation >{
<LinearLayout for 'a' type buttons container with horizontal orientation>
<LinearLayout for 'b' type buttons container with horizontal orientation>
<LinearLayout for 'c' type buttons container with horizontal orientation>
}
}

3 Comments

is that in the xml? I chose to program it dynamically since the user can keep entering words. It's not only three words. The user may enter 20 different words so that's 20 rows of buttons
you need to create layouts in xml just need to create words on user input
also for more words you also need to create horizontal scrollview
0

You guys are right. It is much easier using a LinearLayout. For those interested

final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_bttn_words);

final LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);

button_test.setOnClickListener(
        new View.OnClickListener()
            {
                public void onClick(View view)
                {
                    LinearLayout linearLayout2 = new LinearLayout(view.getContext());

                    linearLayout2.setOrientation(LinearLayout.HORIZONTAL);

                    LinearLayout.LayoutParams rlp = new LinearLayout.LayoutParams(
                                        LinearLayout.LayoutParams.WRAP_CONTENT,
                                        LinearLayout.LayoutParams.WRAP_CONTENT);      

                    int size = enter_txt.getText().toString().length();
                    for (int i=0; i<size; i++)
                    {
                        Button myButton = new Button(view.getContext());
                        myButton.setBackgroundResource(R.drawable.button);
                        linearLayout2.addView(myButton, rlp);
                     }
                     linearLayout.addView(linearLayout2, llp);

Comments

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.