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