0

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.

  1. How do you get ListView to not stretch to takeup all available screen space?
  2. 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>
johnw182
  • 1,349
  • 1
  • 16
  • 22
  • We don't use `wrap_content` for `android:layout_height` on a `ListView`, as the "content" is variable. In terms of width, the `ListView` width will be determined by the contents of the rows, since you have it set to `wrap_content` as well. – CommonsWare Sep 11 '16 at 23:02
  • You are right on the layout_height, the wrap_content got thrown in the example. But for the width, that is my problem. No matter what I have done, the ListView expands to 100% of the activity, pushing it's parent 100% of the activity. – johnw182 Sep 12 '16 at 02:12
  • apply listview.getLayoutparams.width= screenwidth * 0.6; it will apply 60% of screenwidth to listview – Jay Shah Sep 12 '16 at 05:21
  • Possible duplicate of [Setting a maximum width on a ViewGroup](http://stackoverflow.com/questions/5875877/setting-a-maximum-width-on-a-viewgroup) – tynn Sep 12 '16 at 05:30
  • "But for the width, that is my problem" -- then perhaps you should edit your question and show what is going *in* your `ListView`, in terms of row layouts. – CommonsWare Sep 12 '16 at 11:07
  • @CommonsWare I think the title handles it pretty well. "maxWidth is not working" / "width, that is my problem". What is going into the ListView does not matter, even if the ListView is empty, it always takes over the entire available space. – johnw182 Sep 12 '16 at 14:49
  • @tynn I looked at that solution and it is very hacky because I feel like this is functionality that should work with just xml. It works with many other Views just not ListView, View and some others. This is the cleanest solution I have found so far so unless someone can point out what I am doing wrong with the layout xml, I will probably end up going with this. – johnw182 Sep 12 '16 at 14:55

0 Answers0