I have an Android Activity layout with a list view that I want to be centered in the activity but do not want it's width to exceed a specified width. I've tried several variations but I cannot get the ListView to not expand to 100% width and height.
In the example below, I have a LinearLayout with a TextView and a ListView. With the ListView removed, it works as expected. Once you uncomment the ListView the entire free screen will be consumed by the ListView.
Also, I put the android:maxWidth="250dp" on the TextView because it allows for it and works. The LinearLayout parent seems to ignore this property.
- How do you get ListView to not stretch to takeup all available screen space?
- How do I set a maximum width (and height) on the LinearLayout so it's children cannot force it beyond that size?
t
<?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:background="#000000"
tools:context=".MenuActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#ffffff"
android:orientation="vertical">
<TextView android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu Menu "
android:padding="15dp"
android:maxWidth="250dp" />
<!--<ListView-->
<!--android:id="@+id/lvMenuItems"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content" />-->
</LinearLayout>
</RelativeLayout>