2

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" />
2
  • Button b = new Button(this); then add it to table row Commented Mar 20, 2014 at 5:05
  • The below solution may be useful for u stackoverflow.com/questions/21333867/… Commented Mar 20, 2014 at 5:52

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

0

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)

Comments

0

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.

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.