4

I am trying to add buttons to an Android Layout using a for each loop. I have tried to create a layout file for the button, and then I want to add buttons with that layout, to a layout. If I create new buttons in the loop using the commented code it works, but not if I create a new button from the layout file.

public class MainActivity extends ActionBarActivity {
ArrayList<String> panel1 = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    panel1.add("1");
    panel1.add("2");


    for(String s : panel1){
        Button knap = (Button) findViewById(R.id.knap);
        //Button knap = new Button(this);
        knap.setText(panel1.get(i));
        LinearLayout l = (LinearLayout) findViewById(R.id.linearLayout1);
        l.addView(knap);
   }

The XML file containing the layout of the button:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="40dp"
android:clickable="true"
android:orientation="vertical"
android:paddingLeft="40dp"
tools:context=".MainActivity" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="39dp"
    android:gravity="center_vertical" >


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="knap"
        android:id="@+id/knap"
        android:background="#99D6D6"/>


</LinearLayout>

2
  • 1
    What is the your problem(error)?? Commented Feb 10, 2015 at 7:55
  • The error log is quite large, so i added it here: pastebin.com/ePjLmKN4 Commented Feb 10, 2015 at 8:28

4 Answers 4

3

You should add your Button's to a defined layout on xml. Firstly, your xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:orientation="vertical"
    android:paddingLeft="40dp"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>

Then in your java code, you should take this LinearLayout with id linearLayout, add the Button's to that layout like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    panel1.add("1");
    panel1.add("2");
    LinearLayout l = (LinearLayout) findViewById(R.id.linearLayout);

    for(String s : panel1){
        Button newButton = new Button(this);
        newButton.setText(panel1.get(i));
        newButton.setBackgroundColor(0xFF99D6D6);
        l.addView(newButton);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I have already defined a linearlayout1, I just forgot to add the code. But it doesn't seem like the button you are adding uses the settings from the button xml file?
But you can not take the settings of the button from xml and define a new button with this settings. You should make this with the methods of Button from java codes.
I added setting background of the new added Buttons.
0

Probably you are getting following Exception:

IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

Because knap Button is already in linearLayout1 layout and you are trying to add it again in same layout using l.addView(knap); line. so it's not possible to add same view again in Layout

Comments

0

The reason is simple

Button knap = (Button) findViewById(R.id.knap);

This way you are binding "knap" Button to a certain ID defined in your XML Layout. Basically, it's always the same Button, no matter how many loop cycles you pass through, because the ID never changes (by definition, the ID cannot change).

Button knap = new Button(this);

This way, you create a brand new instance of a Button variable. It is not bound by anything, no Parameters of any sort(ID included).

This is a kind of View that can be dynamically added to a Layout ViewGroup.

To "import" values from a defined XML item, you can always copy LayoutParams.

Button dummyButton = (Button) findViewById(R.id.knap);
LayoutParams params = dummyButton.getLayoutParams();

Button myNewButton;

LinearLayout l = (LinearLayout) findViewById(R.id.linearLayout1);

for (String s : panel1) {

  myNewButton = new Button(this);
  myNewButton.setLayoutParams(params);

  l.addView(myNewButton);

}

something like that.

Beware, you also need to declare Button and LinearLayout variables out of the cycle(as I did in the snippet above).

Also, your "example Button" declared in XML would be visible and added to the Layout.

Either take notice of that(for example, in the whole cycle skip that first view, or do the full cycle and set the view's visibility to GONE

dummyButton.setVisibility(View.GONE);

or via XML

<Button android:id="+@id/knap"
android:visibility="gone" />

Not actually sure right now if the XML param 'visibility="gone" ' carries with the LayoutParams described above. In such a case your loop would add the buttons, but they would be not visibile. To fix it,

myNewButton.setVisibility(View.VISIBLE);

1 Comment

If I make a Button as you suggest, can I then copy the settings from the xml file, and apply them to a given button?
0

If you want to add Buttons in a loop from layout you need to defined layout resource file containing only that button.

Then in loop Button button = (Button) getLayoutInflater().inflate(R.layout.your_button_layout_file, l, false);

And Finally add button to view group l.addView(button);

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.