Place Cheat button underneath True/False buttons


#1

Maybe I am trying to run before I can walk here, however I would like to place the Cheat button in landscape underneath the True/False buttons, just as I have in portrait mode:

This was easy enough, all I had to do was add a new LinearLayout section underneath the LinearLayout which contained the True/False buttons:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/cheat_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cheat_button"/>

</LinearLayout>

What I currently have in the landscape activity_quiz.xml file is:

[code]

<Button
    android:id="@+id/true_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/true_button"/>

<Button
    android:id="@+id/false_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/false_button"/>


<Button
    android:id="@+id/cheat_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/cheat_button"
    android:layout_gravity="bottom" />

[/code]

which of course places the Cheat button next to the False button:

I have tried placing the Cheat button inside a new LinearLayout section, similar to the portrait version, however it sits top left of the screen.

Any ideas?

(Boy this is a lot more complicated than iOS view layouts.)


#2

If you read pages 63/64 of the book again, you will see that the overall layout of the land/activity_quiz.xml was changed from a LinearLayout to a FrameLayout:

So you will need to change the overall layout to FrameLayout and give all components an android:layout_gravity attribute.
Should look somehow like this:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent">


    <TextView
        android:id="@+id/question_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp"
        android:layout_gravity="center_horizontal"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center_vertical|center_horizontal">

        <Button
            android:id="@+id/true_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/true_button"/>

        <Button
            android:id="@+id/false_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/false_button"/>

    </LinearLayout>

    <Button
        android:id="@+id/cheat_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center"
        android:text="@string/cheat_button"/>

    ... your code for the next and previous button here ...

</FrameLayout>

I copied this from my XML which looks a bit different since I tried a lot of stuff and challenges (see image below) but I tried to deleted all confusing attributes. This way the cheat button is at the bottom center for me.

My preview (just for the positioning):

Hope I could help you out.


#3

Thanks for your response feffefe. I do have the overall layout enclosed in a FrameLayout, I just omitted it from my question as I did not think that part was relevant.

I have however just tried something else by enclosed the cheat button in its own LinearLayout (which I had previously tried), however simply added 50dp marginTop to this, which nows moves the cheat button to underneath the True/False buttons:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|center_horizontal"
    android:orientation="horizontal"
    android:layout_marginTop="50dp">

    <Button
        android:id="@+id/cheat_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cheat_button"
        android:layout_gravity="bottom" />
</LinearLayout>

Of course, I don’t know if this is the correct way, however will most likely find out the further I progress through the book.

Thanks,
Nick


#4

According to what the author’s requirement. You don’t have to enclose the cheat_button with in another linear layout you can just change the layout gravity attribute… This is what the author wants us to do… here is my code

<Button android:id="@+id/cheat_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center" android:text="@string/cheat_button"/>

this code comes after the linearlayout which encloses the true button and false button…