6
\$\begingroup\$

I want to divide the screen in two halves. The layout is locked on portrait mode. The top half will have one button and the bottom half will have many buttons. That's why I choose this division, since the top half can be potentially scrollable.

This is my layout xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout 
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:orientation="horizontal" > 

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

    </LinearLayout>

    <LinearLayout 
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:orientation="horizontal" > 

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

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

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

    </LinearLayout>

</LinearLayout>

And this is how it looks like:

Layout

It looks as expected, but I was wondering if I'm doing it correctly. Is there a better, easier, more common, different, (etc) way to achieve this? Are there any mistakes or anti-patterns or bad things in there?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

Your layout looks very good. android:layout_weight is the correct way to solve this.

A couple of other notes regarding your XML layout:

  • As your outermost LinearLayout has match_parent for both width and height, there is no need for android:gravity="center".

  • I would write 0dp instead of 0dip (even though it is the exact same thing). Using dp just seems more common to me. This is a very minor nitpick though :)

  • Don't hardcode strings in your XML. Use the strings.xml resource file and use android:text="@string/button3text"

  • You might know about this but either way you (and any future readers of this post) should also be aware that there exists android:layout_gravity. Although they sound similar, they are not the same thing. Read more about the differences between them in Android - gravity and layout_gravity

  • If, in the future you want to play around a bit more with layout_weights, you should take a look at What is android:weightSum in android, and how does it work?, especially this answer

  • If you would like to support landscape mode as well, I suggest learning how to use Android Fragments. If you would like to support older devices, there's a support package that can help with that. A Fragment has one onCreate method and one onCreateView method, which makes it easy to persist data between screen orientation changes (or other changes) while creating a new view for the data.

Summary:

Your layout looks excellent. At least in my eyes.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.