0

I am trying to program an alternative landscape view file (an xml file) for my app, and I must use FrameLayout instead of LinearLayout (that's what the book said). But Framelayout does not stack well, so we are supposed to use android:layout_gravity and then assign an x/y dimension, for example: android:layout_gravity="center". This example centers something exactly in the middle (both vertically and horizontally).

But my problem is, I have 4 levels on the vertical plane, where I want to place things. A text line, a line with 2 buttons (true, false), another button (cheat), then finally a line with 2 arrow buttons (previous and next). But with the layout_gravity, they only have very crude placements: top, center, bottom. I noticed that if you do not assign anything, they all end up in the upper left corner.

So how do I stack these vertically so they fall nicely spaced from top to bottom? Assigning 2 things the same parameters does not stack them, but rather overlaps them terribly.

Thank you for your help, below is my code. I have not put any gravity layouts in there yet.

<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" />        

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="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:text="@string/cheat_button" />

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

        <ImageButton
        android:id="@+id/prev_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow_left"
        android:contentDescription="@string/move_back"
         />

        <ImageButton
        android:id="@+id/next_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow_right"
        android:contentDescription="@string/move_forward"
        />

    </LinearLayout>

</FrameLayout>  
Azurespot
  • 3,066
  • 3
  • 45
  • 73
  • FrameLayout makes layer of contents means its overlaps the contents to each other... – Piyush Jan 01 '14 at 08:06
  • 1
    You can have multiple elements within a FrameLayout but each element will be positioned based on the top left of the screen. Elements that overlap will be displayed overlapping. You should go to this [link](http://learnandroideasily.blogspot.in/2013/05/frame-layout-in-androis.html) for better understanding. – M D Jan 01 '14 at 08:56
  • Thanks MD, I did not know that about FrameLayout. Which makes it even more odd why this book recommended us to use it, when it clearly won't stack all of our elements correctly. Do you know if there is anything particular about a landscape view file (like if you rotate your device, you get a landscape view) that requires a special layout? I think I will fiddle with LinearLayout again and Relative Layout to come up with a better solution. – Azurespot Jan 02 '14 at 01:10

1 Answers1

2

You should use a Relative Layout or Linear Layout to achieve this because the Frame Layout is simply not designed for this. Here is the api documentation for frame layout-

FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.

So controlling the position of child in Frame layout is very much limited,ie only using gravity.

nikvs
  • 1,090
  • 5
  • 9
  • Thanks nikvs, I wonder why the book wanted us to use FrameLayout specifically for the landscape view file? Because the regular layout does not use FrameLayout, but rather LinearLayout and everything stacks really well. Do you have any idea why it would have been recommended to use FrameLayout? I will also look up Relative Layout, thanks for the suggestion. – Azurespot Jan 02 '14 at 01:03