Hi in my application I am displaying image and text by using grid view for that i used frame layout inside linear layout.
Now i want above the list view i want to display some image.
can anyone please help me.
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<com.agilerise.hotel.SquareImageView
android:id="#+id/picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:paddingTop="50dp"
android:paddingBottom="50dp"
/>
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_gravity="bottom"
android:textColor="#android:color/black"
/>
</FrameLayout>
<GridView
android:id="#+id/buttom"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:numColumns="3"
android:layout_weight="1">
</GridView>
</LinearLayout>
add list view in your xml
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1.68" >
</ListView>
and create item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="#drawable/ic_avatar"
android:scaleType="centerCrop"/>
</LinearLayout>
Then you should use ArrayAdapter in your activity class to create your listview...
Related
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>
I am using a gridview to display a collection of items in the form of an image with a textview beneath them for their title.
The final item of collection of items is unique in the fact that it only has an image, no textview (this might be relevant).
At the moment I am finding that as soon as a row is created that has a normal item and an image only item in it, the textview is cutoff.
I have tried setting the min height and height of the items but it doesn't seem to affect the grid at all.
Grid:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#drawable/Background"
android:id="#+id/root_layout"
android:padding="10dip"
android:layout_height="match_parent">
<GridView
android:id="#+id/connection_grid"
android:listSelector="#android:color/transparent"
android:horizontalSpacing="25dip"
android:stretchMode="columnWidth"
android:verticalSpacing="25dip"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Grid Item:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="300dp">
<FrameLayout
android:id="#+id/screencap_layout"
android:layout_height="wrap_content"
android:background="#drawable/RowBorderBackground"
android:layout_width="wrap_content"
android:padding="1dip"
android:layout_centerHorizontal="true">
<ImageView
android:id="#+id/connection_icon"
android:layout_width="200dip"
android:layout_height="147dip"
android:background="#drawable/grid_view_selector"
android:scaleType="centerCrop" />
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/devicetype_layout"
android:background="#drawable/RowBorderBackground"
android:layout_gravity="right|bottom"
android:layout_marginBottom="-2dip"
android:layout_marginRight="-2dip"
android:padding="2dip">
<TextView
android:text="NA"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/device_type" />
</RelativeLayout>
</FrameLayout>
<TextView
android:id="#+id/connection_name"
android:text="Machine 1"
android:gravity="center"
android:textSize="18dip"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
Set some paddingBottom to gridview gridview class.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#drawable/Background"
android:id="#+id/root_layout"
android:layout_height="match_parent">
<GridView
android:id="#+id/connection_grid"
android:listSelector="#android:color/transparent"
android:horizontalSpacing="25dip"
android:stretchMode="columnWidth"
android:verticalSpacing="25dip"
android:paddingBottom="20dip"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
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.
I am really struggling with my xml layout, even though I have over a year of experience working on android :). Anyway I need to create an xml layout as following:
I tried with this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="15">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5" >
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20" >
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5" >
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50" >
</RelativeLayout>
</LinearLayout>
And everything looks alright till moment i want to add child to some inner layout.
So when i add listView to RelativeLayout, marked with number 3, it doesn't match it's
parent but takes whole screen. How can i add listview to inner layouts 2 and 3 so they
match their boundaries?
Look at this demo code,may be you can do some changes in this and apply it for your use.
directly put this code in your xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/com_facebook_button_grey_focused" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:layout_margin="10dp"
>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_margin="10dp"
>
<ListView
android:id="#+id/listView2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
</LinearLayout>
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>