Reputation: 113
I am new to Android Studio and am using a Blank Activity template to try to figure some things out, specifically how to programmatically add Buttons to a view from a List.
I have a List that contains the text for each button as well as some other data that will be used in the click events for them.
First, this is the activity_main.xml layout file I am using:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<Button
android:id="@+id/play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play Sound"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="48dp" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:id="@+id/menuLayout">
</LinearLayout>
</RelativeLayout>
<android.support.design.widget.FloatingActionButton android:id="@+id/fab"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
In the onClick event for the play_sound button I am iterating over the list and then calling this method for each item that needs a button created inside the LinearLayout with id menuLayout:
private void createButton(String name){
Button myButton = new Button(this);
myButton.setText(name);
LinearLayout ll = (LinearLayout)findViewById(R.id.menuLayout);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
ll.addView(myButton);
}
The buttons are created behind the Play Sound button... I have searched through many similar posts on Stack Exchange and tried various things but I have been unable to figure out how to fix this.
Upvotes: 0
Views: 232
Reputation: 3654
You have the height of menuLayout
set to match_parent
, which in a RelativeLayout
means it will fill its parent, regardless of other views. If you want it to only display below play_button
, you should add a layout_below
parameter, like android:layout_below="@id/play_button
.
Upvotes: 1