Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

layout within layout android

Tags:

android

layout

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>
like image 326
user2869238 Avatar asked Sep 01 '25 20:09

user2869238


2 Answers

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>
like image 53
Matt Wolfe Avatar answered Sep 05 '25 02:09

Matt Wolfe


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">
like image 23
Sino Avatar answered Sep 05 '25 01:09

Sino