• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Add button dynamically

wseng92

Android Enthusiast
Oct 26, 2015
279
55
How do I add a button dynamically? I have a button in Claims.java named button1 and it used to open the radio button dialog.

It looked like this

Iwag1.png


What I trying to achieve is:
I want a button added dynamically and clickable once `button1` gets text from another activity. The following image is the result of what I've tried so far but I only able to make the button looked like image below(grey color).
a1Lmk.png


Claims.java**
Code:
      public class Claims extends Fragment {
        private TextView c;
        private Button b;
        private TextView d;
        private LinearLayout L1;
        private LinearLayout.LayoutParams params;
 
        private static final int PROJECT_REQUEST_CODE=1;
        public static final int CAMERA_REQUEST_CODE = 2;
 
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            View claims = inflater.inflate(R.layout.claims, container, false);
            View.OnClickListener listener = new View.OnClickListener() {
                public void onClick(View v) {
                    AlertDialogRadio();
 
                }
            };
            Button button1 = (Button) claims.findViewById(R.id.button10);
            Button button = (Button) claims.findViewById(R.id.button8);
            button1.setOnClickListener(listener);
            L1=(LinearLayout)claims.findViewById(R.id.LinearLayout01);
            params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
             c=(TextView)claims.findViewById(R.id.textView49);
 
 
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {
                    Intent intent = new Intent(getActivity().getApplicationContext(), CameraMain.class);
                    startActivity(intent);
                }
            });
            return claims;
        }
 
        public void AlertDialogRadio() {
            final CharSequence[] ClaimsModel = {"Project", "Petrol", "Car Maintenance"
                    , "Medical", "Other"};
 
            AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity());
            alt_bld.setTitle("Select a Claims");
            alt_bld.setSingleChoiceItems(ClaimsModel, -1, new DialogInterface
                    .OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    if (item == 0) {
                        Intent intent = new Intent(getActivity().getApplicationContext(), Project1.class);
                        startActivityForResult(intent, PROJECT_REQUEST_CODE);
                    } else if (item == 1) {
                        Intent intent = new Intent(getActivity().getApplicationContext(), Petrol.class);
                        startActivity(intent);
                    } else if (item == 2) {
                        Intent intent = new Intent(getActivity().getApplicationContext(), CarMainten.class);
                        startActivity(intent);
                    } else if (item == 3) {
                        Intent intent = new Intent(getActivity().getApplicationContext(), Medical.class);
                        startActivity(intent);
                    } else if (item == 4) {
                        Intent intent = new Intent(getActivity().getApplicationContext(), Other.class);
                        startActivity(intent);
                    }
                    dialog.dismiss();
 
                }
            });
            AlertDialog alert = alt_bld.create();
            alert.show();
 
 
        }
    [USER=1021285]@override[/USER]
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
 
            if (requestCode == PROJECT_REQUEST_CODE ) {
                if(resultCode == Activity.RESULT_OK){
                    String result=data.getStringExtra("text");
                    c.setText(result);
                   addButton();
 
                }
                if (resultCode == CAMERA_REQUEST_CODE) {
                    //Write your code if there's no result
                }
            }
        }// /onActivityResult
 
        public void addButton() {
            Button button=new Button(getActivity());
            button.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            button.setOnClickListener(new View.OnClickListener() {
                [USER=1021285]@override[/USER]
                public void onClick(View v) {
 
                    AlertDialogRadio();
                }
            });
 
                    L1.addView(button);
 
        }
    }
**claims.xml**


Code:
<?xml version="1.0" encoding="utf-8"?>
 
 
    <AbsoluteLayout
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:eek:rientation="vertical"
        android:padding="10dp"
        xmlns:android="http://schemas.android.com/apk/res/android">
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:id="@+id/LinearLayout01"
            android:eek:rientation="vertical"
            android:padding="5dp" >
 
        </LinearLayout>
 
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="49dp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Click the + icon to select your claims"
            android:textSize="20dp"
            android:id="@+id/textView45"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_x="8dp"
            android:layout_y="33dp" />
 
        <RelativeLayout
            android:id="@+id/relativeLayout"
            android:layout_width="332dp"
            android:layout_height="40dp"
            android:background="#ffdab9"
            android:eek:rientation="vertical"
            android:layout_x="10dp"
            android:layout_y="85dp" >
 
            <Button
                android:id="@+id/button10"
                android:layout_width="41dp"
                android:layout_height="40dp"
                android:background="#454545"
                android:eek:nClick="showAddForm"
                android:text="+"
                android:textColor="#FFFFFF"
                android:textSize="30sp"
                android:layout_alignParentTop="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true" />
 
            <ListView android:id="@+id/list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_x="-11dp"
                android:layout_y="217dp"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_marginLeft="110dp"
                android:layout_marginStart="110dp">
                </ListView>
 
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Text"
                android:id="@+id/textView49"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_toLeftOf="@+id/button10"
                android:layout_toStartOf="@+id/button10"
                android:layout_alignParentBottom="true" />
 
        </RelativeLayout>
 
        <Button
            android:layout_width="143dp"
            android:layout_height="wrap_content"
            android:text="Next"
            android:id="@+id/button8"
            android:layout_x="204dp"
            android:layout_y="405dp" />
 
 
    </AbsoluteLayout>

Any idea or suggestions would be greatly appreciated.

Problem:

1. How to create a button exactly same like `button1` and place below `button1` ?
2. Clickable and having the same function like `button1`
3. The number of `button` does not fixed.
 
Last edited:

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones