How to do complex layout with scrollview - android

I need to perform this layout:
See image from:
https://lh3.googleusercontent.com/-_uW9jKzUY_M/UVWlL2SnNBI/AAAAAAAAAOw/geY-Q-vTsqY/s540/myImage+%283%29.png
Area 1 is a complex imbrication of linear / relative layouts
Area 2 is also complex, but would be contained in a scrollview
Area 3 contains 2 buttons in a vertical linear layout
How this can be achieved?

What about this?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:orientation="vertical" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.33" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TextView" />
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</LinearLayout>

I found the answer myself.
I have to use the layout_above attribute and a main RelativeLayout.
See this example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/footer"
android:layout_gravity="top"
android:orientation="vertical" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView1" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView2" />
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" />
</LinearLayout>
</RelativeLayout>

Related

Horizontal ScrollView in Eclipse

I am making an advertising application in Eclipse and wish to display scroller ad's in horizontal ScrollView. Also I need to rotate this scroller view after every 5 seconds.
The following is the configuration
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:gravity="bottom"
android:orientation="vertical">
<ScrollView
android:id="#+id/hsHorizontalAds"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/llHorizontalAds"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</ScrollView>
</LinearLayout>
Please suggest.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:gravity="bottom"
android:orientation="vertical">
<HorizontalScrollView
android:id="#+id/hsHorizontalAds"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/llHorizontalAds"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</ScrollView>
</LinearLayout>
Try like this way,this is what you looking for
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button5" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>

can not change layout_gravity from left

I have a simple LinerLayout with 3 child linear layouts , each have a text view
when Im trying to align the text view to the center or to the right , nothing happens. its always aligned to the left, why ?
<?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:layout_weight="1"
android:id="#+id/alert_type"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="TYPE"/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_below="#id/alert_type"
android:id="#+id/alert_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/alert_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="ALERT"/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_below="#id/alert_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/alert_details"
android:text="DETAILS" />
</LinearLayout>
</LinearLayout>
you can use RelativeLayout instead
<?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">
<RelativeLayout
android:layout_weight="1"
android:id="#+id/alert_type"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="TYPE"/>
</RelativeLayout>
<RelativeLayout
android:layout_weight="1"
android:layout_below="#id/alert_type"
android:id="#+id/alert_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/alert_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="ALERT"/>
</RelativeLayout>
<RelativeLayout
android:layout_weight="1"
android:layout_below="#id/alert_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/alert_details"
android:text="DETAILS" />
</RelativeLayout>
</LinearLayout>
or you can use gravity to the linearlayout not textview..like
<LinearLayout
android:layout_weight="1"
android:id="#+id/alert_type"
android:gravity="right"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TYPE"/>
</LinearLayout>
Change android:layout_width="wrap_content" of TextView to match_parent
Set gravity like
android:gravity="center"
You need to set
android:gravity="center"
for each TextView's and LinearLayout.
change layout_width="wrap_content" and layout_gravity="centre"
<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:id="#+id/alert_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" >
<TextView
android:id="#+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="TYPE" />
</LinearLayout>
<LinearLayout
android:id="#+id/alert_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/alert_type"
android:layout_gravity="center"
android:layout_weight="1" >
<TextView
android:id="#+id/alert_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="ALERT" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/alert_layout"
android:layout_gravity="center"
android:layout_weight="1" >
<TextView
android:id="#+id/alert_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DETAILS" />
</LinearLayout>
Use below XML code for your layout, in which I set all textview at center position
<?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:id="#+id/alert_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" >
<TextView
android:id="#+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TYPE" />
</LinearLayout>
<LinearLayout
android:id="#+id/alert_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" >
<TextView
android:id="#+id/alert_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="ALERT" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" >
<TextView
android:id="#+id/alert_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DETAILS" />
</LinearLayout>
</LinearLayout>
You just need to add android:gravity="center" in your linear layout. You can also provide any alignment like right, center_vertical etc.

Android linear layout, how to match remaining parent?

I am going to create a listview with two button at the bottom
<?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:layout_width="match_parent"
android:layout_height="wrap_content" >
<ListView
android:layout_width="match_parent"
android:layout_height="400dp"
android:id="#android:id/list"></ListView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1"
android:text="Add Task"
android:id="#+id/addTaskButton"></Button>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:text="Add Task2"
android:id="#+id/addTaskButton2"></Button>
</LinearLayout>
</LinearLayout>
How to make the listview match the height to the remaining parent?
If I set a height to listview
android:layout_height="400dp"
this is not suitable to all device
If I set match_parent
android:layout_height="match_parent"
The listview will over the buttons at the bottom
If you set its height to 0dp and it's layout weight to 1, it will fill out the remaining space. Also, you don't need the LinearLayout surrounding the ListView.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#android:id/list" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1"
android:text="Add Task"
android:id="#+id/addTaskButton" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:text="Add Task2"
android:id="#+id/addTaskButton2" />
</LinearLayout>
</LinearLayout>
This will work and I have also reduced number of lines in your code:
<?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" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/addTaskButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1"
android:text="Add Task" >
</Button>
<Button
android:id="#+id/addTaskButton2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:text="Add Task2" >
</Button>
</LinearLayout>
</LinearLayout>
Use this layout
<?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:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.9" >
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/list"></ListView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1"
android:text="Add Task"
android:id="#+id/addTaskButton"></Button>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:text="Add Task2"
android:id="#+id/addTaskButton2"></Button>
</LinearLayout>
</LinearLayout>
You may use this layout :
<RelativeLayout 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"
>
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/lv_button"
android:layout_alignParentTop="true"
android:layout_marginBottom="#dimen/margin_bottom"
>
</ListView>
<LinearLayout
android:id="#+id/lv_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="#dimen/margin_bottom"
android:layout_marginLeft="#dimen/margin_left"
android:layout_marginRight="#dimen/margin_right"
android:layout_marginTop="#dimen/margin_top"
android:orientation="horizontal" >
<Button
android:id="#+id/btn1"
android:layout_width="0.0dip"
android:layout_height="#dimen/button_height"
android:layout_marginRight="#dimen/margin_right"
android:layout_weight="1.0"
android:text="Button1"
/>
<Button
android:id="#+id/btn2"
android:layout_width="0.0dip"
android:layout_height="#dimen/button_height"
android:layout_marginLeft="#dimen/margin_left"
android:layout_weight="1.0"
android:text="Button2"
/>
</LinearLayout>
</RelativeLayout>
Use weight sum. Refer this link to know about weightsum
<?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="10">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/list"></ListView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0"
android:layout_weight="2"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1"
android:text="Add Task"
android:id="#+id/addTaskButton"></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:text="Add Task2"
android:id="#+id/addTaskButton2"></Button>
</LinearLayout>
</LinearLayout>
Try this way,hope this will help you to solve your problem.
<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:layout_width="match_parent"
android:layout_height="wrap_content" >
<ListView
android:layout_width="match_parent"
android:layout_height="400dp"
android:id="#android:id/list"></ListView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1"
android:text="Add Task"
android:id="#+id/addTaskButton"></Button>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:text="Add Task2"
android:id="#+id/addTaskButton2"></Button>
</LinearLayout>
</LinearLayout>

layout within layout android

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>
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>
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">
// 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>
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.

Android- Proper positioning of layouts

I cant seem to properly position my layouts the way I want them to be.
What my layout looks like is like this:
LinearLayout
LinearLayout
ListView
LinearLayout
TextView
TextView
TextView
Button
My aim is to have it like this:
Any thoughts on this please as to what Layouts i will use?
Help will be appreciated
try this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/homeTableLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<LinearLayout
android:layout_centerInParent="true"
android:layout_width="fill_parent"
android:layout_height="100dip"
android:background="#android:color/holo_blue_dark"
>
<!-- arrange your lables and button here -->
</LinearLayout>
<LinearLayout
android:layout_width="100dip"
android:layout_height="fill_parent"
android:background="#android:color/holo_purple"
android:layout_marginLeft="20dip"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
></ListView>
</LinearLayout>
</RelativeLayout>
look the wireframe below . you can set corner-radius and add controls in it.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/layout2"
android:layout_width="fill_parent"
android:layout_height="200dip"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="100dip"
android:layout_weight="1" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="#+id/layout1"
android:layout_width="100dip"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
</RelativeLayout>
You can use the Relative layout for this as follows
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/first_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dip" >
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btn1" />
<Button
android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btn2" />
<Button
android:id="#+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btn3" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/second_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toRightOf="#+id/first_layout" >
<GridView
android:id="#+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</GridView>
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
</RelativeLayout>

Categories

Resources