android can't put linearlayout under listview - android

i want to put a linearlayout under a listview, if i put it above the listview i got an exception and the activity stop working suddenly, and when i put it under the listview it doesn't appear, what am i doing wrong?
<?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="#+id/lvRestaurants"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
<LinearLayout
android:id="#+id/llButtons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/bDone"
android:layout_width="153dp"
android:layout_height="match_parent"
android:text="Done" />
<Button
android:id="#+id/bCancel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Cancel" />
</LinearLayout>
</LinearLayout>

Add android:layout_weight=1 to your ListView.
The reason why the other layout doesn't appear is because your listview takes all the place in the parent layout (android:layout_height="fill_parent")

I suggest using RelativeLayout instead. Using LinearLayout and specially android:layout_weight is expensive performance wise. This layout accomplishes what you need. The key is android:layout_alignParentBottom and andoid:layout_above properties. Try it out.
<?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" >
<LinearLayout
android:id="#+id/llButtons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<Button
android:id="#+id/bDone"
android:layout_width="153dp"
android:layout_height="match_parent"
android:text="Done" />
<Button
android:id="#+id/bCancel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Cancel" />
</LinearLayout>
<ListView
android:id="#+id/lvRestaurants"
android:layout_above="#id/llButtons"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</RelativeLayout>

you can try using weight Concept.
I did some changes 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"
android:weightSum="10" >
<ListView
android:id="#+id/lvRestaurants"
android:layout_weight="8"
android:layout_width="fill_parent"
android:layout_height="0dip" >
</ListView>
<LinearLayout
android:layout_weight="2"
android:id="#+id/llButtons"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:orientation="horizontal" >
<Button
android:id="#+id/bDone"
android:layout_width="153dp"
android:layout_height="fill_parent"
android:text="Done" />
<Button
android:id="#+id/bCancel"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="Cancel" />
</LinearLayout>
</LinearLayout>
Do changes according to your requirement

To save the overhead, provide listView with layout_weight="1"
<ListView
android:layout_weight="1" // The MagicLine
android:id="#+id/myListView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
and you LinearLayout with gravity="bottom"
<LinearLayout
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:orientation="horizontal">
// here will be views, you want to put with in your LinearLayout
</LinearLayout>
Also you can put a LinearLayout beneath a list view, would be the use of a Relativelayout.
Here is a sampleStructure.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/myListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="constantHeightofLinearLayout" /> // here the constant height of the linear Layout or buttons, should be inserted
<LinearLayout
android:layout_alignParentBottom="true" //this is the line, that would put this linearLayout beneath your Listview
android:layout_height="constantHeight" // this height should be the marginBottom of ListView
android:gravity="bottom"
android:orientation="horizontal">
// here will be views, you want to put with in your LinearLayout
</LinearLayout>
</RelativeLayout>

Related

ScrollView with LinearLayout child does not wrap_content correctly

So, I have the following .xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.scopesystems.cityalert.ui.Despre"
tools:showIn="#layout/app_bar_despre"
android:background="#drawable/toolbar_incercare">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/linear_parent">
<RelativeLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.1" />
<RelativeLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="2.8" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--General-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/sectionHeader"
android:text="#string/general"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/despre_general"/>
<!--Evaluti-ne-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/sectionHeader"
android:text="#string/evaluatine"
/>
<!--Confidentialitate si licente-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/sectionHeader"
android:text="#string/confidentialitate"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/despre_politici"/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.1" />
</LinearLayout>
</ScrollView>
The issue is that the last ListView is on only 1 row, and it has to be scrolled to display all the items in the list. I tried to add to the linear_parent a height of match_parent but it wont let me, it says that I should use instead wrapcontent. How can i change the layout to display the items on all the screen, not just half?
In this case, On the ScrollView use android:fillViewport="true" and for child of ScrollView android:height="wrap_content".

Placing Fragment below Listview

I am struggling with my Android Layout.
I want to create a Linear Layout which contains a Button at the Top, next a List View and when all elements of the list view are displayed I want to show a Fragment which displays Google Maps.
This is my Layout xml file. In that case you can only see the Fragment and the Button but not the listview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:orientation="vertical"
tools:context="de.mypackage.MyActivity">
<Button
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="0"
android:id="#+id/button_back"
android:text="#string/back"
/>
<ListView
android:id="#+id/list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="1"/>
/>
<fragment
android:id="#+id/fragement"
android:name="de.mpackage.MapsFragment"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="0"
tools:layout="#layout/fragment_maps"/>
</LinearLayout>
Any Idea how I can realize my layout?
Try this code
<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="wrap_content"
android:orientation="vertical"
android:paddingLeft="10dp"
android:weightSum="1"
android:paddingRight="10dp">
<Button
android:id="#+id/button_back"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/back" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5" />
<fragment
android:id="#+id/fragement"
android:name="com.yougotag.supplierfieldagent.fragments.AboutFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
tools:layout="#layout/fragment_about_app" />
</LinearLayout>
this will set both in half screen you can change ration of weights in layout.
Advice :
Declare your root layout as RelativeLayout
RelativeLayout is a view group that displays child views in relative
positions. The position of each view can be specified as relative to
sibling elements .
Then use android:layout_below
Positions the top edge of this view below the given anchor view ID.
Accommodates top margin of this view and bottom margin of anchor view.
<fragment
android:id="#+id/fragement"
android:name="de.mpackage.MapsFragment"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="#+id/birth"
tools:layout="#layout/list"/>
Try this code
<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="wrap_content"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<Button
android:id="#+id/button_back"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/back" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<fragment
android:id="#+id/fragement"
android:name="com.yougotag.supplierfieldagent.fragments.AboutFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout="#layout/fragment_about_app" />
</LinearLayout>
Try this :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp"
tools:context="de.mypackage.MyActivity">
<Button
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="0"
android:id="#+id/button_back"
android:text="#string/back"
/>
<ListView
android:id="#+id/list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_below="#+id/button_back"/>
/>
<fragment
android:id="#+id/fragement"
android:name="de.mpackage.MapsFragment"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="0"
android:layout_below="#+id/list"
tools:layout="#layout/fragment_maps"/>
</RelativeLayout>
I found a solution hope it helps.
In my ListView for the height instead of using 0dp I used wrap_content and adds layout_weight=1 for my Fragment
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EFEFEF"
android:orientation="vertical">
<Button
android:id="#+id/button_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:divider="#null"/>
<fragment
android:name="de.mpackage.MapsFragment"
android:id="#+id/fragement"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="5dp"
android:layout_weight="1"
/>
</LinearLayout>

Design a round button over two colored layouts

I'm trying to design something like that :
Currently i'm having issue putting the button at "the middle" of the two layouts reunion point. Is there a way i do this ?
Thanks.
Here is a way to do it
<?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:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="#FF0000">
<!-- TO ADD CONTENT HERE -->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="#FFFF00"
android:weightSum="0.5">
<!-- TO ADD CONTENT HERE -->
</LinearLayout>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#000000"
android:text="TEST"
android:textColor="#FFFFFF"/>
</RelativeLayout>
OUTPUT:
You can change the layout height from android:layout_weight="0.5"
you can use RelativeLayout for your parent and try adding twoLinearLayouts , one below another and set their heights, now place a Button in your RelativeLayout and set android:layout_centerInParent="true" for your Button .
and if you want to use weight for layouts, instead of adding two LinearLayout to your RelativeLayout, add one LinearLayout with weightSum=100 and then add two LinearLayouts to it with custom weights.
for your case , you have to do sth like 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="match_parent"
android:orientation="vertical"
android:weightSum="100" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:background="#f00" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="80"
android:background="#0f0" >
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="test" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>

Relative layout placing to other relative layout

I am developing a chat app.I want to place edittext to bottom then messages above it.But I have a problem:
I don't want to place messages to top of edittext and button.How can I prevent this ? This is my xml:
<?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:background="#ECECFB"
android:orientation="vertical" >
<ListView
android:id="#+id/message_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<EditText
android:id="#+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/get_from_user"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="Gönder" />
</RelativeLayout>
</RelativeLayout>
And I have one more question.How can I do message list system like other chat apps ? I mean when I send a message this message place to bottom of the list not to top.Now in my xml it's placing to top.Like this:
Set your ListView above the EditText layout.
Your layout will be as below :
<?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:background="#ECECFB">
<RelativeLayout
android:id="#+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<EditText
android:id="#+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/get_from_user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="Gönder" />
</RelativeLayout>
<ListView
android:id="#+id/message_list"
android:layout_above="#id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
And for your second problem, set
android:stackFromBottom="true"
android:transcriptMode="normal"
to the ListView
For example, add
android:layout_below="#id/message_list"
to your inner RelativeLayoutto constrain the ListView and bottom RelativeLayout not to lay out on top of each other.
Try this:
Add this to your Relative layout : android:layout_below="#+id/message_list"
and change your listview to wrap_content
Basically do this:
<?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:background="#ECECFB"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/comment_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
<EditText
android:id="#+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<Button
android:id="#+id/get_from_user"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="Gönder" />
</RelativeLayout>
<ListView
android:id="#+id/message_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/comment_layout"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
Try changing this:
<ListView
android:id="#+id/message_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
To this:
<ListView
android:id="#+id/message_list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
This should do the trick
First Problem:
You need to set on RelativeLayout
android:bellow="#+id/message_list"
2º Problem:
You need to set on ListView:
android:stackFromBottom="true"
android:transcriptMode="normal"
Final Xml:
<?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:background="#ECECFB"
android:orientation="vertical" >
<ListView
android:id="#+id/message_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stackFromBottom="true"
android:transcriptMode="normal"/>
<RelativeLayout
android:bellow="#+id/message_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<EditText
android:id="#+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/get_from_user"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="Gönder" />
</RelativeLayout>
</RelativeLayout>
Add android:layout_above="#+id/id_of_relative_layout".
Add an ID to the RelativeLayout which contains the EditText.

How to add two listviews in a fragment in android layout?

i'm trying display two listviews in a fragment. but i'm getting problems in xml file itself. I am using sherlock fragment.
Also one list should be displayed on 2/3 space of the screen and another list on 1/3 of the screen space. please can u help? Any help would be appreciated. Thanks in advance.
<?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:background="#DEF7C6"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="60dp"
android:layout_height="35dp"
android:paddingLeft="0dp"
android:src="#drawable/search" />
<EditText
android:id="#+id/EditText01"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:hint="Search"
android:paddingLeft="50dp"
android:textColor="#000000" >
</EditText>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/EditText01"
android:drawSelectorOnTop="false"
android:listSelector="#android:color/darker_gray" >
</ListView>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="#CFCACC" >
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/layout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/layout1"
android:layout_weight="2" >
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#000000" />
</RelativeLayout>
<Button
android:id="#+id/addbutton"
android:layout_width="42dp"
android:layout_height="41dp"
android:layout_above="#+id/list"
android:layout_alignParentRight="true"
android:background="#drawable/buttonadd" />
</RelativeLayout>
Here's the code for the ListViews only:
<?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:background="#DEF7C6"
android:orientation="vertical" >
<ListView
android:id="#+id/list1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:drawSelectorOnTop="false"
android:entries="#array/l1"
android:listSelector="#android:color/darker_gray" >
</ListView>
<ListView
android:id="#+id/list2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:drawSelectorOnTop="false"
android:entries="#array/l2"
android:listSelector="#android:color/darker_gray" >
</ListView>
</LinearLayout>
Note the "weight" parameter to achieve the 2/3 - 1/3 proportion. You should look here if you want to add list view elements dynamically.
If you want other elements instead of the second ListView just change it for a LinearLayout or a RelativeLayout.

Categories

Resources