I've tried various solutions asked here on stackoverflow for the same problem but nothing is working for me. I want to display two text views under two list views. My code is this
<LinearLayout
....>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="2" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<ListView
android:id="#+id/lv1InBS"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
<ListView
android:id="#+id/lv2InBS"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="TextView"
android:textColor="#800000"
android:typeface="serif" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="TextView"
android:textColor="#800000"
android:typeface="serif" />
</LinearLayout>
</LinearLayout>
....
Please help. I've been sick trying to correct it.
Thanks.
ok you should try this methodology,
first take a simple linear layout with vertical orientation,
than add two list view and two text views in the way you want it on the screen,
than go to grafical editor and select first component, either listview or text view than click on the distribute waight evenly button given on the upper panel of the graphical editor.
This will work for sure.
<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" >
<TextView
android:id="#+id/welcome"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="#string/hello_world" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
<ListView
android:id="#+id/listView2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
<TextView
android:id="#+id/welcome"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="#string/hello_world" />
<TextView
android:id="#+id/welcome"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="#string/hello_world" />
add this in your xml and have a look,because it is working for me.
Ok , here is a way to do it, by combining the Weight attribute of android views and Horizontal/Vertical LinearLayouts
<?xml version="1.0" encoding="utf-8"?>
<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" >
<ListView
android:id="#+id/ListView01"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</LinearLayout>
The first Horizontal Linearlayout acts as a container for the two parallel rows.
inside each row there are two vertical layouts ( by using equal weights and *layout_width="0"*) the layouts would split the screen in half.
Inside each LinearLayout a listView item (with height set to zero and weight to 1) and a textview item with any values you want.
Related
Trying to align button at Bottom using LinearLayout, but getting just below TextView.
To set button at bottom, I am using android:layout_gravity="bottom" but still not done
LinearLayout xml
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_gravity="bottom"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Bottom" />
</LinearLayout>
</LinearLayout>
Change second linear layout to
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
This will put the button at the bottom and this layout will take the rest of the space
Yoy have to use like this....
<RelativeLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center">
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Bottom" />
</RelativeLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_gravity="bottom"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Bottom" />
</LinearLayout>
</LinearLayout>
I could not use the suggested answer as I had multiples of 3 vertical ones (each with specific weights) inside a horizontal one. So, ended up using margin top for specific button/widget instead. Working fine so far.
Did not want to change to RelativeLayout as that meant many changes in this scenario.
I want to make a layout in xml for a Fragment class. This layout have to be match_parent width and height.
Picture 1
In picture 1, the layout has four items added programatically...
Picture 2
In picture 2 is the same layout but with 6 items.
none of these layouts examples are scrollable, so if i add 8 items, all items has to fit to
the screen size.
how can i do this?
You can uses ScrollView.
if you use GridView now, ScrollView is contain GridView.
Or
You use over scroll mode if ifContentScrolls mode.
You can do it simply: refer to the LinearLayout by id, and add children programatically. If you want its children to have the same height, set their height to 0px and their weight to the same, 1 for instance. Since its children will be Linearlayouts as well, you can add the items manu1, menu2, etc to them without problem. I don't know you exact usage, but it's a start.
You can do this using layout_weight attribute in LinearLayout as below...
<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="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000000"
android:text="Menu 1" />
<TextView
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#FFFFFF"
android:text="Menu 2" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#FFFFFF"
android:text="Menu 1" />
<TextView
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000000"
android:text="Menu 2" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000000"
android:text="Menu 3" />
<TextView
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#FFFFFF"
android:text="Menu 4" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#FFFFFF"
android:text="Menu 5" />
<TextView
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000000"
android:text="Menu 6" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000000"
android:text="Menu 7" />
<TextView
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#FFFFFF"
android:text="Menu 8" />
</LinearLayout>
</LinearLayout>
I'm new android. I have a little bit stack in designing layout for Android. In my scenario, I have two button with Linear Layout. Here my xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="#color/isp_home_color"
>
<Button
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:text="#string/sync_text"
android:background="#color/sync_text_color"
android:id="#+id/btn_syncpin"
android:layout_gravity="left"
android:textStyle="bold"
/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="70dp"
android:text="#string/topup_text"
android:background="#color/topup_text_color"
android:id="#+id/btn_topup"
android:layout_gravity="right"
android:textStyle="bold"/>
<!--Table Layout will come here -->
</LinearLayout>
Here image for this.
When I add new TableLayout inside Linear Layout. I got this.
Here my xml for Table Layout.
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</TableLayout>
Actually, I don't want like this. I want two button as shown in first image. Then, next table layout will occur under this two button. Please help me how can I do this layout. Please pointing to me how to make this. Thanks with advanced.
Give orientation as vertical to the LinearLayout not the TableLayout.Put Two buttons in a LinearLayout with orizontal orientation and give orientation "vertical" for the parent LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#456789"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="horizontal">
<Button
android:id="#+id/btn_syncpin"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_gravity="left"
android:layout_weight="1"
android:background="#123456"
android:text="tring/"
android:textStyle="bold" />
<Button
android:id="#+id/btn_topup"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_gravity="right"
android:layout_weight="1"
android:background="#978969"
android:text="topup_text"
android:textStyle="bold" />
</LinearLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF" >
</TableLayout>
</LinearLayout>
Check this out, I just checked it on my Machine and it works as desired, although the background preference and the display strings have been removed.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_syncpin"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_gravity="left"
android:layout_weight="1"
android:textStyle="bold" />
<Button
android:id="#+id/btn_topup"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_gravity="right"
android:layout_weight="1"
android:textStyle="bold" />
</LinearLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="#+id/linearLayout2">
</TableLayout>
</RelativeLayout>
take Relative Layout as a parent layout.
Following is an example. it will display your buttons and table layout too
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/isp_home_color"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_syncpin"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_gravity="left"
android:layout_weight="1"
android:background="#color/sync_text_color"
android:text="#string/sync_text"
android:textStyle="bold" />
<Button
android:id="#+id/btn_topup"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_gravity="right"
android:layout_weight="1"
android:background="#color/topup_text_color"
android:text="#string/topup_text"
android:textStyle="bold" />
</LinearLayout>
<TableLayout
android:id="#+id/table"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/linearLayout2" >
</TableLayout>
</RelativeLayout>
thanks
This answer tells only implementation of button_bar_layout for all android versions with buttons at the bottom/top of an activity which look like AlertDialog with OK/Cancel buttons.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="?android:attr/listDivider"
android:orientation="vertical"
android:showDividers="middle" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:divider="?android:attr/listDivider"
android:dividerPadding="12dp"
android:orientation="horizontal"
android:showDividers="middle" >
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:drawable/list_selector_background"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:text="One" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:drawable/list_selector_background"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:text="Two" />
</LinearLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF" >
</TableLayout>
</LinearLayout>
Credits: Who else ? Johnkil
// try this way here i gave you alternative to achieve your requirement from give all of above
1. header.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="wrap_content"
android:orientation="horizontal"
android:background="#color/isp_home_color">
<Button
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:text="#string/sync_text"
android:background="#color/sync_text_color"
android:id="#+id/btn_syncpin"
android:textStyle="bold"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="70dp"
android:text="#string/topup_text"
android:background="#color/topup_text_color"
android:id="#+id/btn_topup"
android:textStyle="bold"/>
</LinearLayout>
2.main.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:padding="5dp"
android:orientation="vertical">
<include
layout="#layout/header"/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_weight="1">
</TableLayout>
</LinearLayout>
i have two layouts vertically , first one has edittext and button, second one has two listvies ,
the first one doesn't appear, and i got an exception when running the application
<?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" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/etRestaurantSearchName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Enter name"
android:inputType="text"
android:textSize="15dip" />
<Button
android:id="#+id/bRestaurantSearchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Search" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ListView
android:id="#+id/lvRestaurants"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ffffff"
android:fillViewport="true" >
</ListView>
<ListView
android:id="#+id/lvAlphabets"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="#drawable/foods_alphabets_bg"
android:orientation="vertical" >
</ListView>
</LinearLayout>
</RelativeLayout>
i ma sure it is something about hiegh , width or weight,
You have used RelativeLayout as the parent layout for the inner two layouts. Therefore the second inner Linear Layout overlaps the first one , thus first one cant be seen. Make use of linear layout as parent layout
Try this:
Correction :
android:layout_below="#+id/Layout1" for layout two.
and some weight related changes.
<LinearLayout
android:id="#+id/Layout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/etRestaurantSearchName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Enter name"
android:inputType="text"
android:textSize="15dip" />
<Button
android:id="#+id/bRestaurantSearchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Search" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/Layout1"
android:orientation="horizontal" >
<ListView
android:id="#+id/lvRestaurants"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/edit_text"
android:fillViewport="true" >
</ListView>
<ListView
android:id="#+id/lvAlphabets"
android:layout_width="40dp"
android:layout_height="match_parent"
android:background="#drawable/btn_bg_pressed"
android:orientation="vertical" >
</ListView>
</LinearLayout>
How to display the layout with a ListView at top and mutliple Buttons at the bottom (as in pic)
Below is the code that I tried (Problem is the buttons are overlapping the list which is somewhat visible behind the buttons & the 3 buttons are not equal is size though I used the weight sum):
<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:weightSum="3" >
<Button
android:id="#+id/bDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Done" />
<Button
android:id="#+id/bCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
<Button
android:id="#+id/bSelAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Select All" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
What I did is basically do a 2 linear layouts, 1 for the list and 1 for the button. Also setting the weight = 1 and the width = 0 will make them equal size
This code worked for me:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="5dip"
android:layout_marginBottom="5dip"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_above="#+id/footerlayout"
android:id="#+id/listviewlayout">
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView>
</LinearLayout>
<LinearLayout android:id="#+id/footerlayout"
android:layout_marginTop="3dip"
android:layout_height="45dip"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/bDone"
android:text="Done"
android:layout_width="0dip"
android:layout_height="40dip"
android:layout_weight="1">
</Button>
<Button
android:id="#+id/bCancel"
android:text="Cancel"
android:layout_width="0dip"
android:layout_height="40dip"
android:layout_weight="1"
>
</Button>
<Button
android:id="#+id/bSelAll"
android:text="Select All"
android:layout_width="0dip"
android:layout_height="40dip"
android:layout_weight="1"
>
</Button>
</LinearLayout>
</RelativeLayout>
Add to LinearLayout id property and add this to ListView
android:layout_above="#id/linear_layout_id"
To make buttons equal size change in their properties this
android:layout_width="wrap_content"
to this
android:layout_width="match_parent"