-2

I want to programatically add buttons in android, the xml file for the button would be

<Button
android:textStyle="bold"
android:background="@drawable/blue"
android:textColor="@drawable/blue_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/funny_excuses"
android:id="@+id/funny"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:textSize="25sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

What is the best way to do it? I will only change the text for each new button.. And maybe I will have another button type, like with other background and textcolor..

3
  • possible duplicate of Android: programmatically adding buttons to a layout Commented Sep 19, 2014 at 12:35
  • @2Dee no, is nothing like that question.. i want to add a custom button for more times.. with own settings Commented Sep 19, 2014 at 12:37
  • Basically it is the same thing, you just need to apply a custom style using this stackoverflow.com/questions/2016249/… or setting the style programatically as well... Commented Sep 19, 2014 at 12:40

4 Answers 4

2

Also its possible to create this Button xml and inflate layout recource from code: button.xml:

<Button
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:textStyle="bold"
 android:background="@drawable/blue"
 android:textColor="@drawable/blue_text"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/funny_excuses"
 android:id="@+id/funny"
 android:paddingBottom="10dp"
 android:paddingTop="10dp"
 android:paddingLeft="6dp"
 android:paddingRight="6dp"
 android:textSize="25sp"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true" />

code:

    Button button = (Button) getLayoutInflater().inflate(R.layout.button, null);
    button.setText("Hello world");
    RelativeLayout ll = (RelativeLayout) findViewById(R.id.ll); //layout to add
    ll.addView(button);
Sign up to request clarification or add additional context in comments.

2 Comments

This was exactly what I wanted to do, thanks. How can I set the android:layout_below= tag programatically so the button is added the as the last one?
you have to create LayoutParams and add rule, this link may help you: link also think about replacing to LinearLayout.
1

proje-->res-->values-->style.xml

<style name="othername" >
        <item name="android:layout_width">match_parent</item>
        <item name="android:textColor">#000000</item>
        <item name="android:textSize">20sp</item>
        <item name="android:gravity">left</item>
        <item name="android:layout_marginLeft">30sp</item>
        <item name="android:layout_marginRight">30sp</item>
        <item name="android:layout_marginTop">10sp</item>
   </style>

<Button
style="@style/othername"
/>

3 Comments

How to set the style programatically?
May I know the reason of doing everything programmatically
I Have a list of categories..and need to put them programatically one below other..in buttons
1

ok do one thing.only you want to change the button text.so, programmatically for button object keep setText() and setBackground()...

2 Comments

ok.tell me one thing you have created the button using xml file.so dynamically you want to change the button text for that i mentioned the above solution (or) you want to create the button dynamically without defining in the xml file?
i want to programatically add buttons that would look like the one in the xml i provided
1

Create your button in your layout, then use yourButton.setVisibility(View.GONE); for hide it and use yourButton.setVisibility(View.VISIBLE); for make it visible.

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.