s k
s k

Reputation: 5192

Android dynamically creating a Button

I am quite new to Android, and is having problem in creating a Button dynamically. I have it working by creating in the Xml layout and working fine. Hope someone can help, to write code to create the button below during runtime, and after that Add it into a TableRow.

Note: I wanted to create a Button which is equivalent to the Xml below, but NOT using findViewById(), because this button doesn't exist. I know how to new a Button, but I am having difficulties on setting all the attributes below. Especially the layout_weight, background and drawableTop.

<Button
    android:id="@+id/BtnRating1"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:text="@string/Rating_1"
    android:drawableTop="@drawable/face_1"
    style="?android:attr/borderlessButtonStyle"
    android:background="?android:selectableItemBackground"
    android:gravity="center"
    android:onClick="OnRating_1" />

Upvotes: 2

Views: 958

Answers (4)

maddy d
maddy d

Reputation: 1536

You can do it in two way

  1. create a table row layout with button and inflate it.
  2. you can create button dynamically and add it in your table row see below code for creating buttons dynamically.
 TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);
    Button button = new Button(this);
    params.weight = 1;
    params.gravity = Gravity.CENTER;
    tablerow.addView(button,params);   

Here this is context of activity.

Upvotes: 0

Biplab
Biplab

Reputation: 564

This below line will create the button programetically

Button btn = new Button(ActivityContext);

Then you can also add button properties as below

RelativeLayout.LayoutParams btnparamLayoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT, 90);
        btn.setGravity(Gravity.CENTER_VERTICAL);
        btnparamLayoutParams.height = adjustedDp;
        btn1paramLayoutParams.setMargins(0, 0, 0, 100);
        btn.setLayoutParams(btnparamLayoutParams);
        btn.setBackgroundColor(Color.WHITE);

at the end add your button to the parentlayout.

LinearLayout ll = (LinearLayout)findViewById(R.id.linID);
ll.addView(btn)

Upvotes: 0

Bhanu Sharma
Bhanu Sharma

Reputation: 5145

I gave you code how to create a button on dynamic time, you can set all property which you want for this button.

Button myButton = new Button(this);
    myButton.setText("Push Me");

LinearLayout ll = (LinearLayout)findViewById(R.id.BtnRating1);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);

Upvotes: 0

Pulkit Sethi
Pulkit Sethi

Reputation: 1325

Assuming the above xml is named button_view.xml

View view = LayoutInflater.from(context).inflate(R.layout.button_view);
Button button = (Button) view.findViewById(R.id.BtnRating1);

This way you can inflate directly from xml

Upvotes: 1

Related Questions