patternMinor
Dividing the screen in two halves using LinearLayout
Viewed 0 times
halvesthetwousingdividinglinearlayoutscreen
Problem
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:
And this is how it looks like:
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?
This is my layout xml:
And this is how it looks like:
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?
Solution
Your layout looks very good.
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
-
I would write
-
Don't hardcode strings in your XML. Use the
-
You might know about this but either way you (and any future readers of this post) should also be aware that there exists
-
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
Summary:
Your layout looks excellent. At least in my eyes.
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.
Context
StackExchange Code Review Q#43806, answer score: 4
Revisions (0)
No revisions yet.