Layout_width of a ListView
By setting the width to "wrap_content" you are telling ListView to be as wide as the widest of its children. ListView must therefore measure its items and to get the items it has to call getView() on the Adapter. This may happen several times depending on the number of layout passes, the behavior of the parent layout, etc.
Remember there are no guarantees on the number of times getView() is invoked on the Adapter, nor in what orders the calls will happen. You must therefore implement getView() to be as efficient as possible. The bottom line is, this is not an issue, it's the expected behavior in this situation.
Android LinearLayout weight and wrap_content of ListView
As it is said in here and in here, standart android ListView can not have wrap_content
as a width since the width of child elements may be different.
As a workaround, one should use RecycleView instead of ListView.
Android Layout - layouts of 3 textviews inside a listview
Try Using Relative Layout
with your code.
Replace this with your XML Code.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dp"
android:text="Test" />
<TextView
android:id="@+id/text2"
android:layout_width="80sp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="25dp"
android:layout_toLeftOf="@+id/text3"
android:layout_toRightOf="@+id/text1"
android:maxEms="15"
android:singleLine="false"
android:text="TEsting TEsting TEsting TEstingTEstingTEstingTEstingTEsting " />
<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="25dp"
android:layout_marginRight="5dp"
android:text="Test"
android:textSize="30dp" />
</RelativeLayout>
Here is Screen Shot
How it Looks with Above XML.
android layout dynamic width of listviews
Don't EVER put ListView
inside ScrollView
, anyways here is how you do it:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="0dp"
android:layout_height="fill_parent" android:layout_weight="20"
android:id="@+id/listView2" />
<ListView
android:layout_width="0dp"
android:layout_height="fill_parent" android:layout_weight="20"
android:id="@+id/listView3" />
<ListView
android:layout_width="0dp"
android:layout_height="fill_parent" android:layout_weight="20"
android:id="@+id/listView4" />
<ListView
android:layout_width="0dp"
android:layout_height="fill_parent" android:layout_weight="20"
android:id="@+id/listView5" />
<ListView
android:layout_width="0dp"
android:layout_height="fill_parent" android:layout_weight="20"
android:id="@+id/listView6" />
</LinearLayout>
ListView takes up all space in LinearLayout
That is because you are using fill_parent
, and it will do exactly that.
You could try something along the lines of this... It will cause the ListView to expand to fill the space.
<?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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="Hej"/>
</LinearLayout>
</LinearLayout>
Another alternative is to use a RelativeLayout. So long as the height of the ListView is not wrap_content, it should be OK.
<?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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_above="@+id/footer"
android:layout_height="match_parent"/>
<LinearLayout
android:id="@+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:padding="10dp">
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="Hej"/>
</LinearLayout>
</RelativeLayout>
How to use android:layout_weight with listview
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="25"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="25"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
</LinearLayout>
<ListView
android:id="@+id/relatedContent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginBottom="60dp"
android:layout_weight="50"
android:cacheColorHint="@android:color/transparent" >
</ListView>
Related Topics
CSS Media Queries on Nexus 7, Display Resolution Not Working in Code
Error Inflating Class Android.Support.V4.View.Viewpager
Data Sharing Between Fragments and Activity in Android
Bypass Android's Hidden API Restrictions
How to Apply Custom CSS to Android Webview When Url Is from Internet Server
How to Implement Zoom Effect for Image View in Android
Onitemclicklistener and Onclicklistener Not Working for Listview
How Many Activities Vs Fragments
Fragment Must Be a Public Static Class to Be Properly Recreated from Instance State
CSS - Background-Size: Cover; Not Working in Firefox
How to Close Activity and Go Back to Previous Activity in Android
Bordercolor Not Picking The Lineargradient Color on Android
Why to Use Handlers While Runonuithread Does The Same
Mat (Eclipse Memory Analyzer) - How to View Bitmaps from Memory Dump