how to align button at the bottom of the screen in LinearLayout - android

I am using linearlayout as parent and inside it I have two child linearlayouts. I want to align the buttons in the last linearlayout to the bottom of the screen.The bottom margin of the buttons should match the main linearlayouts bottom margin
<?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="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/parentlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="horizontal" >
<Button
android:id="#+id/save"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Save" />
<Button
android:id="#+id/submit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Submit" />
<Button
android:id="#+id/cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
<Button
android:id="#+id/delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Delete" />
<Button
android:id="#+id/reset"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Reset" />
</LinearLayout>
</LinearLayout>

Just reduce the textsize of the button Text. Since it is not able to display text in one line the margin is getting distorted.
Here I have made textSize to 11dp for each buttontext and the view is perfect for NexusOne size display
<?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="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/parentlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="horizontal" >
<Button
android:id="#+id/save"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Save"
android:textSize="11dp" />
<Button
android:id="#+id/submit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Submit"
android:textSize="11dp" />
<Button
android:id="#+id/cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel"
android:textSize="11dp" />
<Button
android:id="#+id/delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Delete"
android:textSize="11dp" />
<Button
android:id="#+id/reset"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Reset"
android:textSize="11dp" />
</LinearLayout>
</LinearLayout>

try this using your parent as RelativeLayout
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/parentlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:orientation="horizontal" >
<Button
android:id="#+id/save"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Save" />
<Button
android:id="#+id/submit"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Submit" />
<Button
android:id="#+id/cancel"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Cancel" />
<Button
android:id="#+id/delete"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Delete" />
<Button
android:id="#+id/reset"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Reset" />
</LinearLayout>

Its better if you use RelativeLayout for this kind of requirements. Would work without hassle.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/parentlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<Button
android:id="#+id/save"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Save" />
<Button
android:id="#+id/submit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Submit" />
<Button
android:id="#+id/cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
<Button
android:id="#+id/delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Delete" />
<Button
android:id="#+id/reset"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Reset" />
</LinearLayout>
</RelativeLayout>

<?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="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/parentlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:orientation="horizontal" >
<Button
android:id="#+id/save"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Save"
android:textSize="12sp" />
<Button
android:id="#+id/submit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Submit"
android:textSize="12sp" />
<Button
android:id="#+id/cancel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Cancel"
android:textSize="12sp" />
<Button
android:id="#+id/delete"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Delete"
android:textSize="12sp" />
<Button
android:id="#+id/reset"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Reset"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
</FrameLayout>

Related

Linear layout below the ListView, Some portions not visible?

I have a problem with LinearLayout which is below the listview. I tried to give the listview weight and layout above, set the layout_height to 0dp but nothing wotked.
Here is my XML code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="100"
android:background="#00c4ff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Application № - "
android:layout_weight="50"/>
<TextView
android:id="#+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="__"
android:layout_weight="20" />
<EditText
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="date"
android:hint="dd/mm/yyyy"
android:layout_weight="30"
/>
</LinearLayout>
<ListView
android:id="#+id/lvMain"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="bottom"
android:id="#+id/ll">
<Button
android:id="#+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/comment"
android:inputType="text"
android:hint="Comment" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="bottom">
<Button
android:id="#+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send" />
<Button
android:id="#+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save and close"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
when ListView fills the screen, I cannot see the buttons below to it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100">
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="100"
android:background="#00c4ff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Application № - "
android:layout_weight="50"/>
<TextView
android:id="#+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="__"
android:layout_weight="20" />
<EditText
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="date"
android:hint="dd/mm/yyyy"
android:layout_weight="30" />
</LinearLayout>
<ListView
android:id="#+id/lvMain"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="90">
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:orientation="vertical"
android:gravity="bottom"
android:id="#+id/ll">
<Button
android:id="#+id/plus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/comment"
android:inputType="text"
android:hint="Comment" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="bottom">
<Button
android:id="#+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send" />
<Button
android:id="#+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save and close"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Your layout edited by adding layout weights. Please check.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:weightSum="100"
android:background="#00c4ff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Application № - "
android:layout_weight="50"/>
<TextView
android:id="#+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="__"
android:layout_weight="20" />
<EditText
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="date"
android:hint="dd/mm/yyyy"
android:layout_weight="30"
/>
</LinearLayout>
<ListView
android:id="#+id/lvMain"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.6">
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="0.3"
android:gravity="bottom"
android:id="#+id/ll">
<Button
android:id="#+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/comment"
android:inputType="text"
android:hint="Comment" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="bottom">
<Button
android:id="#+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send" />
<Button
android:id="#+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save and close"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
You should try List View with Layout having weight.
Sample Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
/>
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.4"
>
<ListView
android:id="#+id/lvMain"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
/>
</LinearLayout>
In this your height is fixed for listview. Assuming that you can use list view as srollable in particular height.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/topLayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alignParentTop="true"
android:weightSum="100"
android:background="#00c4ff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Application № - "
android:layout_weight="50"/>
<TextView
android:id="#+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="__"
android:layout_weight="20" />
<EditText
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="date"
android:hint="dd/mm/yyyy"
android:layout_weight="30"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/bottomLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="bottom"
android:alignParentBottom="true"
android:id="#+id/ll">
<Button
android:id="#+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/comment"
android:inputType="text"
android:hint="Comment" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="bottom">
<Button
android:id="#+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send" />
<Button
android:id="#+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save and close"/>
</LinearLayout>
</LinearLayout>
<ListView
android:id="#+id/lvMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/topLayout"
android:layout_above="#+id/bottomLayout">
</ListView>
</RelativeLayout>
Have edited your code. Try this.
Here in this I have used RelativeLayout, so that we use android:alignParentBottom="true" for bottom layout which makes the layout in fix in bottom. Simillarly we can do for top layout using android:alignParentBottom="true". Now coming to ListView, which we need to place between top and bottom layout I have used the following code to achieve it
android:layout_below="#+id/topLayout"
android:layout_above="#+id/bottomLayout"
This makes the ListView to stick in between the top and bottom layout.
Hope this is helpful:)
Change as below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00c4ff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Application № - "
android:layout_weight="50"/>
<TextView
android:id="#+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="__"
android:layout_weight="20" />
<EditText
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="date"
android:hint="dd/mm/yyyy"
android:layout_weight="30"
/>
</LinearLayout>
<ListView
android:id="#+id/lvMain"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="bottom"
android:id="#+id/ll">
<Button
android:id="#+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"/>
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/comment"
android:inputType="text"
android:hint="Comment" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="bottom">
<Button
android:id="#+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send" />
<Button
android:id="#+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save and close"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>

Multiple layouts, one activity

I want to create an activity that contains 5 "FrameLayout" like the picture below.
3 equal FrameLayout in the first row and 2 FrameLayout in the second row.
I did it using Linear layouts but I got a warning that nested weights are bad. So is there an other way to make it.
Fragments should be streched on the screen without using values like (100dp).
you cal also use the two table layout..
like
<TableLayout android:weight='1'>
<TableRow>
<Linear>
<FrameLayout /> <FrameLayout /> <FrameLayout />
</Linear>
</TableRow>
</TableLayout>
<TableLayout android:weight='1'>
<TableRow>
<Linear>
<FrameLayout /> <FrameLayout/>
</Linear>
</TableRow>
</TableLayout>
its rough layout . i hope you got the idea.
Try this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<FrameLayout
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"
android:background="#00bbff" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="1" />
</FrameLayout>
<FrameLayout
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"
android:background="#bb00ff" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="2" />
</FrameLayout>
<FrameLayout
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"
android:background="#00ff00" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="3" />
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<FrameLayout
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"
android:background="#bbff00">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="4" />
</FrameLayout>
<FrameLayout
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"
android:background="#ffbb00" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="5" />
</FrameLayout>
</LinearLayout>
</LinearLayout>
Try this::Hope its useful for you.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" >
<FrameLayout
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"
android:background="#00bbff"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="1" />
</FrameLayout>
<View android:layout_width="10dp"
android:layout_height="10dp"
/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"
android:background="#bb00ff"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="2"
/>
</FrameLayout>
<View android:layout_width="10dp"
android:layout_height="10dp"
/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"
android:background="#00ff00"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="3" />
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp">
<FrameLayout
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"
android:background="#bbff00" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="4" />
</FrameLayout>
<View android:layout_width="10dp"
android:layout_height="10dp"
/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"
android:background="#ffbb00" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="5" />
</FrameLayout>
</LinearLayout>
</LinearLayout>
see image below ![image][1]
See This code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#FFFFFF"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="2dp"
android:background="#FFFFFF"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="2dp"
android:background="#FFFFFF"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="1"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_marginTop="2dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="150dp"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"/>
</LinearLayout>
<LinearLayout
android:layout_width="150dp"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:gravity="center"
android:layout_marginLeft="1dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4" />
</LinearLayout>
</LinearLayout>

Divide Screen into Four Equal Parts using Relative Layout

I currently have code that allows me to divide the screen into four equal parts, with an image button in each part. However, the code uses linear layouts which brings up the suggestion "nested weights are bad for performance". How can I make my layout using relative layouts?
Below is the code, and here is a picture of the intended format
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="horizontal"
android:background="#ff191919">
<ToggleButton
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff414141"
android:layout_weight="1.0"
android:text="Plane"
android:layout_margin="5dp"
android:textSize="20sp"
android:onClick="airplaneClicked"/>
<ImageButton
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff414141"
android:layout_weight="1.0"
android:layout_margin="5dp"
android:scaleType="fitCenter"
android:src="#drawable/ic_brightness"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="horizontal"
android:background="#ff191919">01
<ImageButton
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff414141"
android:layout_weight="1.0"
android:layout_margin="5dp"
android:scaleType="fitCenter"
android:src="#drawable/ic_sound" />
<ImageButton
android:id="#+id/settingsbutton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff414141"
android:layout_weight="1.0"
android:layout_margin="5dp"
android:scaleType="fitCenter"
android:src="#drawable/settings2"/>
</LinearLayout>
</LinearLayout>
Using below xml:
<?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="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:background="#242425"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#d5d5d5" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:background="#d5d5d5"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:background="#d5d5d5"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:background="#242425"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#d5d5d5" />
</LinearLayout>
</LinearLayout>
Try this code for your RelativeLayout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/r1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/r3"
android:layout_width="match_parent"
android:layout_height="225dp"
android:layout_marginBottom="10dp"
android:layout_alignParentTop="true" >
<ImageButton
android:id="#+id/i1"
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/ic_launcher" />
<ImageButton
android:id="#+id/i2"
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/ic_launcher" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/r2"
android:layout_width="match_parent"
android:layout_height="225dp"
android:layout_alignParentBottom="true"
android:layout_below="#+id/r3" >
<ImageButton
android:id="#+id/i3"
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/ic_launcher" />
<ImageButton
android:id="#+id/i4"
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/ic_launcher" />
</RelativeLayout>
</RelativeLayout>
Try this
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0">
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_green_light"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="horizontal" >
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_light"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_red_light"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>

Relative Layout: Buttons (Android)

by setting four Buttons on a XML. I try this in the RelativeLayout, and for Galaxy Nexus 4 in the Graphicsl View it looks good, but when i change to another VIew like the Nexus 10 it looks horrible.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.example.b2.MainActivity"
tools:ignore="MergeRootFrame" >
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:text="Button" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_above="#+id/button2"
android:layout_centerHorizontal="true"
android:text="Button" />
<Button
android:id="#+id/button3"
android:layout_width="180dp"
android:layout_height="80dp"
android:layout_above="#+id/button4"
android:layout_alignParentRight="true"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_above="#+id/button4"
android:layout_alignParentLeft="true"
android:text="Button"
android:layout_toLeftOf="#+id/button3"
/>
</RelativeLayout>
<?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:orientation="horizontal" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/white"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="#+id/okbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="SET"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/white"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="#+id/okbuttodn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="SET"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/white"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="#+id/okbuttdtgyodn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="SET"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/white"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="#+id/okbuttrtodn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="SET"
/>
</LinearLayout>
</LinearLayout>
<?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:orientation="horizontal" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/white"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="#+id/okbuttodn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="SET"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/white"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="#+id/okbuttdtgyodn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="SET"
/>
</LinearLayout>
</LinearLayout>
use LinearLayout and android:layout_weight="size".

Alignment problems in XML

I'm Trying to get the buttons to go under the white box. In the java file I am adding a special view to the id:ViewLayout. I have tried putting the buttons after the Framelayout, but they dont appear on screen....
<TextView
android:id="#+id/tvClock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="12:00"
android:textColor="#android:color/white"
android:textSize="100dp" />
<FrameLayout
android:id="#+id/Container"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/ViewLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</RelativeLayout>
<Button
android:id="#+id/bUnlock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Unlock" />
<Button
android:id="#+id/bPin"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PIN" />
</FrameLayout>
Thanks!
Try this one.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/black"
android:orientation="vertical" >
<TextView
android:id="#+id/tvClock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="12:00"
android:textColor="#android:color/white"
android:textSize="100dp" />
<LinearLayout
android:id="#+id/Container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.58" >
<LinearLayout
android:id="#+id/ViewLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/tvClock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="12:00"
android:textColor="#android:color/white"
android:textSize="100dp" />
<RelativeLayout
android:id="#+id/Container"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/ViewLayout"
android:layout_width="match_parent"
android:layout_height="100dp" >
</RelativeLayout>
<Button
android:id="#+id/bUnlock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Unlock"
android:layout_below="#id/ViewLayout"
/>
<Button
android:id="#+id/bPin"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PIN"
android:layout_below="#id/bUnlock"/>
</RelativeLayout>
Try RelativeLayout instead of framelayout.
This may help you:
<TextView
android:id="#+id/tvClock"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:gravity="center"
android:text="12:00"
android:textColor="#android:color/white"
android:textSize="100dp"
android:layout_weight="2" />
<LinearLayout
android:id="#+id/Container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<RelativeLayout
android:id="#+id/ViewLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="#+id/bUnlock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Unlock" />
<Button
android:id="#+id/bPin"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PIN" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

Categories

Resources