-1

Basically I'm trying to dynamically add buttons when I click another button

When I click the "Add a Class" button I would like the "Math button to appear above it like so: Link , the "Math" button is just an example of what it should look like, it is not programmed dynamically.

Currently, when I click the "Add a Class" button, this is what happens: link

I would like the dynamically added button in this case "New button" to look identical to "Math"

Here is my xml layout file:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#7BEDFC" >

<LinearLayout
    android:id="@+id/classesLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="10dp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Classes"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#FFFFFF"
        android:textStyle="bold" />

    <Button
        android:id="@+id/bExample"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:layout_height="40dp"
        android:layout_width="fill_parent"
        android:background="@drawable/roundedcorners"
        android:drawableRight="@drawable/rightarrow"
        android:gravity="left|center_vertical"
        android:text=" Math"
        android:textStyle="bold" />

    <Button
        android:id="@+id/bClass"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/roundedcorners"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:text="Add a Class"
        android:textStyle="bold" />

</LinearLayout>

Roundedcorners drawable file:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
 <shape android:shape="rectangle"  >
     <corners android:radius="10dip" />
     <stroke android:width="1dip" android:color="#FFFFFF" />
     <gradient android:angle="-90" android:startColor="#FFFFFF" 
    android:endColor="#FFFFFF"  />            
 </shape>
</item>
<item android:state_focused="true">
 <shape android:shape="rectangle"  >
     <corners android:radius="10dip" />
     <stroke android:width="1dip" android:color="#FFFFFF" />
     <solid android:color="#FFFFFF"/>       
 </shape>
</item>  
<item >
<shape android:shape="rectangle"  >
     <corners android:radius="10dip" />
     <stroke android:width="1dip" android:color="#FFFFFF" />
     <gradient android:angle="-90" android:startColor="#FFFFFF" 
 android:endColor="#FFFFFF" />            
 </shape>
</item>
</selector>

Relevant part of activity file:

public class MainActivity extends ActionBarActivity {

LinearLayout buttonsLayout; 
Button addClass;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    buttonsLayout = (LinearLayout) findViewById(R.id.classesLayout);

    addClass = (Button) findViewById(R.id.bClass);
    addClass.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Button newButton = new Button(MainActivity.this);

            newButton.setBackgroundResource(R.drawable.roundedcorners);
            newButton.setBackgroundResource(R.drawable.rightarrow);

            newButton.setGravity(Gravity.CENTER_VERTICAL| Gravity.LEFT);
            newButton.setText("New button");

            buttonsLayout.addView(newButton, buttonsLayout.getChildCount() - 1);

        }
    });
  }
}
Engr Waseem Arain
  • 1,163
  • 1
  • 17
  • 36
user3672888
  • 101
  • 3

1 Answers1

0

Use newButton.setCompoundDrawables(0, 0, R.drawable.rightarrow, 0); instead of the second call to setBackgroundResource. If it doesn't look quite right or doesn't show the icon, you might also try setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.rightarrow, 0).

EDIT

You can still use XML for these buttons. Make an XML file (let's call it "my_button.xml") with the following:

<Button
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="10dp"
    android:layout_height="40dp"
    android:layout_width="fill_parent"
    android:background="@drawable/roundedcorners"
    android:drawableRight="@drawable/rightarrow"
    android:gravity="left|center_vertical"
    android:textStyle="bold" />

Then you can write

addClass.setOnClickListener(new View.OnClickListener() {

    @Override 
    public void onClick(View v) {

        Button newButton = (Button) getLayoutInflater().inflate(R.layout.my_button, buttonsLayout, false);
        newButton.setText("New button");
        buttonsLayout.addView(newButton, buttonsLayout.getChildCount() - 1);

    } 
}); 
Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • Okay I got that part thanks! How about setting the size and margins/paddings? Also I need a way to make it dip instead of pixels – user3672888 May 27 '14 at 13:44