SadPossum
SadPossum

Reputation: 43

How do I make my buttons fit on screen in Android?

I have a method that takes a string of text, chops it up and makes buttons out of all the words in the string. I add these to a horizontal Linear Layout like this:

for (int x = 0; i < string.length; x++) {
            Button word = new Button(context);
            word.setText(string[x]+"");
            myLinearLayout.addView(word);
        }

The problem is that if the string is too long, the buttons will go off screen. How do I make it so that the buttons appear on a line below instead of jumping off screen? I've been looking for a solution all day but couldn't find an answer.

Thanks!

Upvotes: 2

Views: 116

Answers (1)

Hariharan
Hariharan

Reputation: 24853

Try this..

   for (int x = 0; i < string.length; x++) {
        Button word = new Button(context);
        word.setText(string[x]+"");
        LinearLayout.LayoutParams left_on = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,1);
        word.setLayoutParams(left_on);
        myLinearLayout.addView(word);
    }

Upvotes: 1

Related Questions