LinearLayout weightsum not working with image - android

I have a LinearLayout with weightSum 10 which has 2 child LinearLayout with layout_weight 4 and 6. It works fine when background is color or nothing. But the problem occurs when I set a background image. What can be the problem and solution?
Working 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"
android:weightSum="10" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="4"
android:background="#color/Blue"
>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="6"
android:background="#color/new_back"
android:orientation="vertical"
android:weightSum="10" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="99" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="33"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/idbtnCustomers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/customer" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chemist"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="33"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/idbtnOrder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/order" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Order"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="33"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/idbtnItemStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/item_status" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item Status"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="99" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="33"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/idbtnNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/notification" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="33"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/idbtnAttendance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/attendance" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Attendance"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="33"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/idbtnExpenceClaim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/expence_clain" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exp. Claim"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Problematic 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"
android:weightSum="10" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="4"
android:background="#drawable/top_menu"
>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="6"
android:background="#color/new_back"
android:orientation="vertical"
android:weightSum="10" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="99" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="33"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/idbtnCustomers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/customer" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chemist"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="33"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/idbtnOrder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/order" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Order"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="33"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/idbtnItemStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/item_status" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item Status"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="99" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="33"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/idbtnNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/notification" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="33"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/idbtnAttendance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/attendance" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Attendance"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="33"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/idbtnExpenceClaim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/expence_clain" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exp. Claim"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>

You need to understand how weight works. Weight will assign remaining space after the measure has been passed. If your image is wrapping its contents and it uses all the space, the remaining unused space (none) will be distributed based on weights. You have to adjust your containers and read about Linear Layout weight.

<?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"
android:weightSum="10" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="4"
android:background="#drawable/ic_launcher" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="6"
android:background="#000000"
android:orientation="vertical"
android:weightSum="10" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="99" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="33"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/idbtnCustomers"
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="Chemist"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="33"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/idbtnOrder"
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="Order"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="33"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/idbtnItemStatus"
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="Item Status"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="99" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="33"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/idbtnNotification"
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="Notification"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="33"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/idbtnAttendance"
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="Attendance"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="33"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/idbtnExpenceClaim"
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="Exp. Claim"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>

Found the solution! The problem was with layout theme set in eclipse for that particular xml file. Theme was set to Theme.NoTitleBar in that xml file. I switched it to default AppTheme which is Theme.Light, now it works. :)

This is a common misunderstanding about layout_weight. Rather than divide the total space of parent view based on each item's weight, it is the Extra Space that is divided.
As the official document says about layout weight and I quote:
LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space is should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view. Child views can specify a weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. Default weight is zero.
In your case, set the layout_height to 0dp means wrap_content, since the image is large enough, it occupies almost all the space of the parent view, which leads to no Extra space to be divided based on layout_weight. Since the wrap_content for pure color is just 1dp, it works fine since there is enough extra space.
To sum up, if you want the layout_weight to work like you expected, each view should be less than its weight of parent view.
To fix it, just wrap the weight=4 view into a new LinearLayout with weight set to 4.

Related

Align android layout with equal margin

I am adding a footer to my app which will contain three buttons with image. I want similar to below view. I have tried android:layout_gravity="right/left/center" but seems not working.
Below is my XML
<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"
>
<!-- Root element should wrap to parent size. -->
<!-- Your view xml codes. -->
<!--Bottom bar layout should be in root element. Parent should be Relative layout so that we can always align to parent bottom-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:weightSum="4">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="left"
android:orientation="vertical"
>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/brands"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="brands"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/stores"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="brands"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:layout_gravity="right"
>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/coupons"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="brands"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/brands"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="brands"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/shops"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="shops"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/coupons"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="coupons"
/>
</LinearLayout>
</LinearLayout>
Try Something like this,
LinearLayout(Horizontal)
- LinearLayout(Vertical)
- ImageView (for the image)
- Textview (for title)
- LinearLayout(Vertical)
- ImageView (for the image)
- Textview (for title)
- LinearLayout(Vertical)
- ImageView (for the image)
- Textview (for title)
Your parent LinearLayouthave android:weightSum="4" and your child LinearLayout all sum is 3.
so change your parent LinearLayout weightSum android:weightSum="4" to android:weightSum="3".
Update :
add this way
<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"
>
<!-- Root element should wrap to parent size. -->
<!-- Your view xml codes. -->
<!--Bottom bar layout should be in root element. Parent should be Relative layout so that we can always align to parent bottom-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:weightSum="3">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/brands"
android:layout_gravity="center_horizontal"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="brands"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/stores"
android:layout_gravity="center_horizontal"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="brands"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/coupons"
android:layout_gravity="center_horizontal"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="brands"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Root element should wrap to parent size. -->
<!-- Your view xml codes. -->
<!--Bottom bar layout should be in root element. Parent should be Relative layout so that we can always align to parent bottom-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:weightSum="3">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/brands" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="brands" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/stores" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="brands" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/coupons" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="brands" />
</LinearLayout>
</LinearLayout>
Try this. I think it will solve your problem.
Please try this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:weightSum="3">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/brands" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="brands" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/stores" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stores" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/coupons" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Coupons" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Give equal weight to layouts
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
android:layout_height="wrap_content">
<ImageView
android:layout_width="24dp"
android:src="#android:drawable/ic_menu_rotate"
android:layout_height="24dp" />
<TextView
android:layout_width="wrap_content"
android:text="#string/description"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
android:layout_height="wrap_content">
<ImageView
android:layout_width="24dp"
android:src="#android:drawable/ic_menu_rotate"
android:layout_height="24dp" />
<TextView
android:layout_width="wrap_content"
android:text="#string/description"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
android:layout_height="wrap_content">
<ImageView
android:layout_width="24dp"
android:src="#android:drawable/ic_menu_rotate"
android:layout_height="24dp" />
<TextView
android:layout_width="wrap_content"
android:text="#string/description"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
change all inner layouts
android:layout_gravity="left"
android:layout_margin="10dp"//as required
and change
android:weightSum="3"
in parent layout

align buttons with texts to the left side xml android

In this code I need align the buttons a Textview to the left, I try everythnin , with relative layout, gravity etcc....
I dont now why this align still togehter to the center in horizontal line, I need to the left side!
<?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:background="#color/dark_background"
android:orientation="vertical"
android:gravity="left" >
<ScrollView android:id="#+id/ScrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:gravity="left"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="6dip"
>
<TextView
android:id="#+id/help_page_intro"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/help_page_intro"
android:padding="2dip"
android:layout_weight="1"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="4dip"
android:layout_gravity="left"
>
<Button android:id="#+id/help_button1"
android:layout_weight="1"
android:layout_width="180dip"
android:layout_height="wrap_content"
style="#style/HelpButton.Dark"
android:onClick="onClickHelp"
android:text="#string/help_title_section1"
android:drawableTop="#drawable/help_image1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/help_text_section1"
android:padding="8dip"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button android:id="#+id/help_button2"
android:layout_weight="1"
android:layout_width="180dip"
android:layout_height="wrap_content"
android:gravity="left"
style="#style/HelpButton.Dark"
android:onClick="onClickHelp"
android:text="#string/help_title_section2"
android:drawableTop="#drawable/help_image2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/help_text_section2"
android:padding="8dip"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_alignParentLeft="true">
<Button android:id="#+id/help_button3"
android:layout_weight="1"
android:layout_width="180dip"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
style="#style/HelpButton.Dark"
android:onClick="onClickHelp"
android:text="#string/help_title_section3"
android:drawableTop="#drawable/help_image3"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/help_text_section3"
android:padding="8dip"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left" >
<Button android:id="#+id/help_button4"
android:layout_weight="1"
android:layout_alignParentLeft="true"
android:layout_width="180dip"
android:layout_height="wrap_content"
android:gravity="left"
style="#style/HelpButton.Dark"
android:onClick="onClickHelp"
android:text="#string/help_title_section4"
android:drawableTop="#drawable/help_image4"/>
<TextView
android:layout_width="wrap_content"
android:gravity="left"
android:layout_height="wrap_content"
android:text="#string/help_text_section4"
android:padding="8dip"
android:layout_weight="1"
/>
</LinearLayout>
Sorry, not fully understanding. Did you want them lined up vertically? Then that would just be setting orientation to vertical in the linear layout. I looked at what this xml does right now. It appears to be button which is half the screen and textview with is the other half. Could you explain a little more what you want to see?
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="4dip"
>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<Button android:id="#+id/help_button1"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:onClick="onClickHelp"/>
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<TextView
android:id="#+id/help_page_intro"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:textColor="#color/black"
android:padding="2dip"
android:layout_weight="1"
/>
<View
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"/>
</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.

Creating a linearlayout with weights

Im trying to make something similar to this - Android Layout(User Interface) only I want to make the first column with a weight of 4 the second one with a weight of 2 (and also with two rows) and the last one with a weight of 1 (and also with three rows)...problem is It messes up itself (I copy and pasted the answer and changed everything according to my needs but it keeps on get messed up.. =) any help?
[code]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background_color"
android:orientation="vertical"
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:gravity="center_horizontal|center_vertical"
android:orientation="horizontal" >
<TextView
android:id="#+id/textDishes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="#string/dishes"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageDishes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="#dimen/seperator_size"
android:background="#color/seperator" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:measureWithLargestChild="true"
android:orientation="horizontal"
android:weightSum="2" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/textfood"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/food"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imagefood"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="#dimen/seperator_size"
android:background="#color/seperator" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/textMalls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/malls"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageMalls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="#dimen/seperator_size"
android:background="#color/seperator" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:weightSum="3"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="#dimen/seperator_size"
android:background="#color/seperator" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/coupons"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="#dimen/seperator_size"
android:background="#color/seperator" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/markets"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
[/code]
That is the result -
The result should look like -
Any help??
EDIT working example now
This is not a complete answer because the vertical dividers are still not working, but hopefully it gets you going in the right direction:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/textDishes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dishes"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageDishes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="#color/red" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/textfood"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Food"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imagefood"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<View
android:layout_width="5dp"
android:layout_height="fill_parent"
android:background="#color/red"
android:gravity="center" >
</View>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/textMalls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Malls"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageMalls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="#color/red" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="App name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<View
android:layout_width="5dp"
android:layout_height="fill_parent"
android:background="#color/red"
android:gravity="center" >
</View>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Coupons"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<View
android:layout_width="5dp"
android:layout_height="fill_parent"
android:background="#color/red"
android:gravity="center" >
</View>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Markets"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
I'm not sure if this was just a mistake when you included your XML here, but you're missing a ">" after the parent LinearLayout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background_color"
android:orientation="vertical"
Should be
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background_color"
android:orientation="vertical" >
Additionally, you are using weights in some of this parent layout's children, but you have not specified a weightSum total in the parent LinearLayout. Remember that a weightSum is the total weight to be distributed amongst the children, so their weights should add up to this weightSum. Also, just a head's up, but using nested weights (which you would be doing if you added that in) is bad for performance.
Maybe the best solution to your problem would be to explore a different layout option instead of using a LinearLayout? You could get it to work this way, though. Make the parent weightSum 7 and make sure you distribute this as desired to the children.
As per your picture (and using linear layouts), it should look something like this, I think:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="7" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weight="4" >
// This one's child here - it has weight 1.
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="2"
android:weight="2" >
// This one's children here - they both have weight 1.
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="3"
android:weight="1" >
// This one's children here - they all have weight 1.
Don't forget to close the LinearLayouts as well. I haven't tested that code, either, so you will want to review it for possible errors.
I'm not sure if this is your only problem but when using weight your layout_width should be "0dp" for a horizontal orientation and layout_height should be "0dp" for a vertical orientation. So for example your first child should be
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:gravity="center_horizontal|center_vertical"
android:orientation="horizontal" >
because it belongs to a parent LinearLayout who's orientation is vertical
Update
Something like this should get you really close. Note I had to take out your resources to make it work in my editor so you will just have to put those back in
<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="fill_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:gravity="center_horizontal|center_vertical"
android:orientation="horizontal" >
<TextView
android:id="#+id/textDishes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Dishes"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageDishes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:measureWithLargestChild="true"
android:orientation="horizontal"
android:weightSum="2" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/textfood"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Food"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imagefood"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="fill_parent" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/textMalls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Malls"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageMalls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:weightSum="3"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="App name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Coupons"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Markets"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>

Issue with layout alignment

I have a relative layout with two linear layout inside it with two textviews in each of them.
I wanted the textviews to appear horizontally as a row.
The code is given below:
<RelativeLayout
android:id="#+id/linear2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/txtlinear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/esms_parentfees_classtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/esms_parentfees_classdetais"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/esms_parentfees_classdetais"
android:orientation="horizontal" >
<TextView
android:id="#+id/esms_parentfees_datetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/esms_parentfees_datedetais"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
Notes on your XML:
You can't use toRightOf, etc, within a LinearLayout
RelativeLayout's don't have an orientation
This is needed for some reason to show all the XML? Ignore this line
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/esms_parentfees_classtext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5" />
<TextView
android:id="#+id/esms_parentfees_classdetais"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/esms_parentfees_datetext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5" />
<TextView
android:id="#+id/esms_parentfees_datedetais"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5" />
</LinearLayout>
</RelativeLayout>
The easiest way would be to make the outer RelativeLayout a LinearLayout
<LinearLayout
android:id="#+id/linear2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
Then remove any tag like
android:layout_toRightOf="#+id/esms_parentfees_classdetais"
Or, you could just change the tag mentioned above to
android:layout_below="#+id/esms_parentfees_classdetais"
if you need the four textviews in a row its easy to just use a linear layout with the four text views in it (or a table layout) instead of using two linear layouts in a relative layout.
Have a look at this :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/txtlinear"
>
<TextView
android:id="#+id/esms_parentfees_classtext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="hi" />
<TextView
android:id="#+id/esms_parentfees_classdetais"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="hi" />
<TextView
android:id="#+id/esms_parentfees_datetext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="hi" />
<TextView
android:id="#+id/esms_parentfees_datedetais"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="hi" />
</LinearLayout>
or
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/txtlinear"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/esms_parentfees_classtext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="hi" />
<TextView
android:id="#+id/esms_parentfees_classdetais"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="hi" />
<TextView
android:id="#+id/esms_parentfees_datetext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="hi" />
<TextView
android:id="#+id/esms_parentfees_datedetais"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="hi" />
</TableRow>
</TableLayout>
or if its a compulsion that you need a relative layout with two linear layouts in it you can follow any of the solutions above
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/esms_parentfees_classtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/esms_parentfees_classdetais"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/esms_parentfees_classtext"
/>
</RelativeLayout>

Categories

Resources