4

Sorry for the really bad free hand paint skills. Thats a sideways phone, split down the middle with 4 buttons and a text box on each side. My posted code only has 4 buttons and a text box because I cant get the layout right for the other set of buttons.I want to have several layouts inside a layout so that I can better organize the UI. What I want is the layout to be horizontal. I need the layout to be split in half horizontally down the middle and the 4 buttons and text box to be on one side and copied on the other side as well so it can keep track of two totals. I only have one set of buttons right now because I cant get the layouts right.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="250.0dip"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical" >
<LinearLayout
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal"
 android:orientation="vertical" >
 <LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal" >
 <Button
 android:id="@id/button_add"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal"
 android:layout_weight="1.0"
 android:text="+1" />
 <Button
 android:id="@id/button_add_5"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal"
 android:layout_weight="1.0"
 android:text="+5" />
 </LinearLayout>
 <EditText
 android:id="@id/currentlife"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal"
 android:gravity="center_horizontal"
 android:inputType="number"
 android:minWidth="120.0dip"
 android:text="20"
 android:textSize="40.0dip" />
 <LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal" >
 <Button
 android:id="@id/button_minus"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal"
 android:layout_weight="1.0"
 android:text="-1" />
 <Button
 android:id="@id/button_minus_5"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal"
 android:layout_weight="1.0"
 android:text="-5" />
 </LinearLayout>
 </LinearLayout>
asked Oct 11, 2013 at 3:37
7
  • 1
    You can use <include /> tag Commented Oct 11, 2013 at 3:37
  • 2
    Is it possible that you could draw out what you want to look like in paint or some simple program first (just a very rough idea) so we have a better idea what you are looking for? Commented Oct 11, 2013 at 3:42
  • When you say horizontal you mean you want a line cut that devices the top and the bottom? Commented Oct 11, 2013 at 3:47
  • 1
    What is your question??? Commented Oct 11, 2013 at 4:05
  • 1
    As said Matt Wolfe, without a graphic it will be difficult for anybody to answer you correctly. We need to see what result you expect. Commented Oct 11, 2013 at 4:09

4 Answers 4

6

How about this (fix id's so they aren't duplicate):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="horizontal" >
 <LinearLayout
 android:layout_width="0dp"
 android:layout_weight="1.0"
 android:layout_height="match_parent"
 android:orientation="vertical" >
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="0dp"
 android:layout_weight="2"
 android:gravity="center"
 >
 <EditText
 android:id="@+id/currentlife"
 android:layout_width="wrap_content"
 android:gravity="center"
 android:minWidth="120dp"
 android:layout_height="wrap_content"
 android:inputType="number"
 android:text="20"
 android:textSize="40dp" />
 </LinearLayout>
 <LinearLayout
 android:layout_height="0dp"
 android:layout_width="wrap_content"
 android:layout_weight="1"
 android:orientation="vertical"
 >
 <LinearLayout
 android:layout_width="match_parent"
 android:orientation="horizontal"
 android:layout_height="wrap_content"
 android:gravity="center"
 >
 <Button
 android:id="@+id/button_add"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_weight="1.0"
 android:text="+1" />
 <Button
 android:id="@+id/button_add_5"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_weight="1.0"
 android:text="+5" />
 </LinearLayout>
 <LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_weight="1.0"
 android:orientation="horizontal"
 android:gravity="center"
 >
 <Button
 android:id="@+id/button_minus"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="-1" />
 <Button
 android:id="+@id/button_minus_5"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="-5" />
 </LinearLayout>
 </LinearLayout>
 </LinearLayout>
 <LinearLayout
 android:layout_width="1dp"
 android:layout_height="match_parent"
 android:background="@android:color/black"/>
 <LinearLayout
 android:layout_width="0dp"
 android:layout_weight="1.0"
 android:layout_height="match_parent"
 android:orientation="vertical" >
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="0dp"
 android:layout_weight="2"
 android:gravity="center"
 >
 <EditText
 android:id="@+id/currentlife"
 android:layout_width="wrap_content"
 android:gravity="center"
 android:layout_height="wrap_content"
 android:inputType="number"
 android:minWidth="120dp"
 android:text="20"
 android:textSize="40dp" />
 </LinearLayout>
 <LinearLayout
 android:layout_height="0dp"
 android:layout_width="wrap_content"
 android:layout_weight="1"
 android:orientation="vertical"
 android:layout_gravity="right"
 >
 <LinearLayout
 android:layout_width="match_parent"
 android:orientation="horizontal"
 android:layout_height="wrap_content"
 android:gravity="center"
 >
 <Button
 android:id="@+id/button_add"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_weight="1.0"
 android:text="+1" />
 <Button
 android:id="@+id/button_add_5"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_weight="1.0"
 android:text="+5" />
 </LinearLayout>
 <LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_weight="1.0"
 android:orientation="horizontal"
 android:gravity="center"
 >
 <Button
 android:id="@+id/button_minus"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="-1" />
 <Button
 android:id="+@id/button_minus_5"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="-5" />
 </LinearLayout>
 </LinearLayout>
 </LinearLayout>
</LinearLayout>
answered Oct 11, 2013 at 4:10
Sign up to request clarification or add additional context in comments.

7 Comments

your top code worked really well. A small change I would like to make would be for the layouts to go extend all the way down the screen. I would like the 4 buttons to be stacked on top of eachother and not so long, with the text box above them a little more then half way up the screen. I am super new to android, first week of teaching myself so I appologize for the noob questions
If you make the layout extend all the way down you will have a lot of white space or huge buttons.
the white space is fine, I am going to end up putging a picture as the backgound eventually.
looks great thank you. one last question, how can i make the buttons uniform in size?
They were the same size on my screen.. I didn't test on an actual device though. Do you mean you want them to fill the space better?
|
1

Give weight for two horizontal layouts.

 <LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal" 
 android:layout_weight="1.0">
answered Oct 11, 2013 at 3:48

2 Comments

the weight for both should just be 1?
maybe I did something wrong but by doing that it just has two layouts that are full screen on top of eachother
1
// try this and let me know is it ok for ur requirement ?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:padding="5dp"
 android:gravity="center" >
 <Button
 android:id="@+id/button_add"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="+1" />
 <Button
 android:id="@+id/button_add_5"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="+5" />
 <EditText
 android:id="@+id/currentlife"
 android:layout_width="0dp"
 android:layout_weight="1"
 android:layout_height="wrap_content"
 android:inputType="number"
 android:gravity="center"
 android:text="20"
 android:textSize="40dp" />
 <Button
 android:id="@+id/button_minus"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="-1" />
 <Button
 android:id="@+id/button_minus_5"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="-5" />
</LinearLayout>
answered Oct 11, 2013 at 4:07

Comments

1

You can create left and right layouts as separate layouts and inflate it in the main layout. This will simplify the layout design.

And try to start the design with android:layout_width="fill_parent" instead of android:layout_width="250.0dip"

Once you get the layout you want, then try to adjust the width as you need.

answered Oct 11, 2013 at 6:30

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.