-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..

Maveňツ
  • 1
  • 12
  • 50
  • 89
Boldijar Paul
  • 5,405
  • 9
  • 46
  • 94
  • possible duplicate of [Android: programmatically adding buttons to a layout](http://stackoverflow.com/questions/11710200/android-programmatically-adding-buttons-to-a-layout) – 2Dee Sep 19 '14 at 12:35
  • @2Dee no, is nothing like that question.. i want to add a custom button for more times.. with own settings – Boldijar Paul Sep 19 '14 at 12:37
  • Basically it *is* the same thing, you just need to apply a custom style using this http://stackoverflow.com/questions/2016249/how-to-programmatically-setting-style-attribute-in-a-view or setting the style programatically as well... – 2Dee Sep 19 '14 at 12:40

4 Answers4

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);
  • 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? – Boldijar Paul Sep 19 '14 at 12:53
  • you have to create LayoutParams and add rule, this link may help you: [link](http://stackoverflow.com/questions/10700273/relativelayout-add-rule-relativelayout-left-of-not-working) also think about replacing to LinearLayout. – Greta Radisauskaite Sep 19 '14 at 13:04
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"
/>
buket
  • 9
  • 3
1

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

Sandeep Kumar
  • 350
  • 4
  • 18
  • 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? – Sandeep Kumar Sep 19 '14 at 12:37
  • i want to programatically add buttons that would look like the one in the xml i provided – Boldijar Paul Sep 19 '14 at 12:38
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.

Mohammad Farahi
  • 1,006
  • 4
  • 14
  • 38