0

I want to generate a button Dynamically by clicking on an Image Button, in XML i mentioned the Image Button and i also called it in .java class. My xml is as follows :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/theme"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<ImageButton android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/plus1" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/welcomemilan" />

</LinearLayout>

My java class for this is as follows public class MainActivity extends Activity {

private static final String TAG = "MyActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //to access linear layout
   final LinearLayout l = (LinearLayout)findViewById(R.id.theme);
    // my add button image 
    ImageButton b = (ImageButton)findViewById(R.id.button);
    //to generate a new button
    final Button b1 = new Button(this);
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // the button is clicked now i need to generate a button

            for(int j =0; j<10;j++){

             b1.setText("Add");
             l.addView(b1);

             Log.i(TAG, "the button is pressed"+j);

            }
          }
    });

  }
}

Till now all were fine. I am getting the button and seeing the image but when press the button i am getting fatal Exception like this. kindly help in this regard, i am new to android.

07-03 10:31:46.853: E/AndroidRuntime(639): FATAL EXCEPTION: main
07-03 10:31:46.853: E/AndroidRuntime(639): java.lang.IllegalStateException:The specified child already has a parent. You must call removeView() on the child's parent first.
07-03 10:31:46.853: E/AndroidRuntime(639):  at android.view.ViewGroup.addViewInner(ViewGroup.java:3344)
07-03 10:31:46.853: E/AndroidRuntime(639):  at android.view.ViewGroup.addView(ViewGroup.java:3215)
07-03 10:31:46.853: E/AndroidRuntime(639):  at android.view.ViewGroup.addView(ViewGroup.java:3172)
07-03 10:31:46.853: E/AndroidRuntime(639):  at android.view.ViewGroup.addView(ViewGroup.java:3152)
07-03 10:31:46.853: E/AndroidRuntime(639):  at com.pro.raisebutton.MainActivity$1.onClick(MainActivity.java:38)
07-03 10:31:46.853: E/AndroidRuntime(639):  at android.view.View.performClick(View.java:3480)
07-03 10:31:46.853: E/AndroidRuntime(639):  at android.view.View$PerformClick.run(View.java:13983)
07-03 10:31:46.853: E/AndroidRuntime(639):  at android.os.Handler.handleCallback(Handler.java:605)
07-03 10:31:46.853: E/AndroidRuntime(639):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-03 10:31:46.853: E/AndroidRuntime(639):  at android.os.Looper.loop(Looper.java:137)
07-03 10:31:46.853: E/AndroidRuntime(639):  at android.app.ActivityThread.main(ActivityThread.java:4340) 
07-03 10:31:46.853: E/AndroidRuntime(639):  at java.lang.reflect.Method.invokeNative(Native Method)
07-03 10:31:46.853: E/AndroidRuntime(639):  at java.lang.reflect.Method.invoke(Method.java:511)
07-03 10:31:46.853: E/AndroidRuntime(639):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-03 10:31:46.853: E/AndroidRuntime(639):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-03 10:31:46.853: E/AndroidRuntime(639):  at dalvik.system.NativeStart.main(Native Method)

Kindly help in this regard

Maroun Melhem
  • 3,100
  • 1
  • 20
  • 22
Tortoise Walker
  • 61
  • 2
  • 10

3 Answers3

2

your layout contains already that button. That's why you are getting that exception.

If you want add dynamically a button to your layout, you have to create a new instance of it at each iteration:

for(int j =0; j<10;j++){
  Button newButton = new Button(MainActivity.this);
  l.addView(newButton);
}

or you can create a layout that contains the button with the layout params you prefer and use an inflater to get a new instance of the button at each iteration.

I noticed that your layout is horizontal, is that what you want?

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
1

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

This line from your log cat means that a View can have only one parent. So when you are trying to add b1 **multiple times**in your loop, then this generates this problem. To get over this issue, you need to have a separate instance of a button for each of your loop iterations.

Some thing like this should work out:

 for(int j =0; j<10;j++)
    {
    Button b1 = new Button(getApplicationContext());
    b1.setText("Add");
    l.addView(b1);
    Log.i(TAG, "the button is pressed"+j);
    }
Parth Kapoor
  • 1,494
  • 12
  • 23
0

You can use this code to add a button dynamicaly.

Button myButton = new Button(this);
myButton.setText("Add Me");
LinearLayout ll = (LinearLayout)findViewById(R.id.theme);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);

Put this code inside onclick function and adjust it

João Marcos
  • 3,872
  • 1
  • 19
  • 14