Pixelknight1398
Pixelknight1398

Reputation: 555

Trying to add buttons at the end of a RelativeLayout in Android

Hi =) I am trying to develop an app in android, and its a basic text adventure game, which displays some text, and then has a couple of buttons for the options you can choose. Very very basic. However the text goes into a TextView which is dynamically changed every time a button is pressed to generate the next part of the story. And my buttons, are also dynamically generated on the fly. I think that due to this, they don't generate underneath the TextView as it shows in the layout tree. Regardless, here is my code. My question mainly is how do i make the buttons show up at the bottom of the scrollable TextView, compared to the top? And secondly, if anyone could allow me to have this info, how do i clear the button layout so that i can create some more buttons? If either of these get asked it would be very helpful. Thanks! ^-^

Java:

package toymakersdev.com.theinsane;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

public class GameActivity extends AppCompatActivity {

private TextView storyText;
private LinearLayout buttonLayout;

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

    buttonLayout = (LinearLayout)findViewById(R.id.buttonLayout);

    storyText = (TextView)findViewById(R.id.storyText);
    storyText.setMovementMethod(new ScrollingMovementMethod());

    storyText.setText("Blah Blah Blah");

    Button button1 = new Button(this);
    Button button2 = new Button(this);
    button1.setText("Button1");
    button2.setText("Button2");

    button1.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    button2.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));

    buttonLayout.addView(button1);
    buttonLayout.addView(button2);
}
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="toymakersdev.com.theinsane.GameActivity"
android:background="#000000"
android:padding="10dp">

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/scrollView"
    android:padding="10dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/storyText"
            android:textColor="#ffffff"
            android:textAlignment="center"
            android:textSize="32sp"/>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/buttonLayout">
        </LinearLayout>

    </RelativeLayout>
</ScrollView>

</RelativeLayout>

Upvotes: 2

Views: 1915

Answers (1)

Inducesmile
Inducesmile

Reputation: 2493

how do i make the buttons show up at the bottom of the scrollable TextView, compared to the top?

Use

android:layout_alignParentBottom="true"

and in your ScrollView, add this line

android:layout_above="@+id/button"

Modify layout is as shown

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="toymakersdev.com.theinsane.GameActivity"
android:background="#000000"
android:padding="10dp">

<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="@+id/button"
android:id="@+id/scrollView"
android:padding="10dp">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/storyText"
        android:textColor="#ffffff"
        android:textAlignment="center"
        android:textSize="32sp"/>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/buttonLayout">
    </LinearLayout>

</RelativeLayout>
</ScrollView>

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"/>

</RelativeLayout>

how do i clear the button layout so that i can create some more buttons?

Your can remove a button view from its parent using removeView() method. Like example

((ViewGroup)button.getParent()).removeView(button);

Upvotes: 3

Related Questions