2

I want to create a two-column layout. The first column should be a List, and it should take as much width as they want. The second column should be FrameLayout and should occupy all the remaining space.
I can achieve the desired, if I use the following layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
    tools:context="ru.automatikaplus.caliberadmin.TestActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:baselineAligned="false"
        android:divider="?android:attr/dividerHorizontal"
        android:orientation="horizontal"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/list"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp" />

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/colorGreen"/>

    </LinearLayout>


</android.support.design.widget.CoordinatorLayout>

And the result:

Screenshot_1

FrameLayout is green for clarity.

BUT! If I change RecyclerView to ListView or anything else - this layout doesn't work:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
    tools:context="ru.automatikaplus.caliberadmin.TestActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:baselineAligned="false"
        android:divider="?android:attr/dividerHorizontal"
        android:orientation="horizontal"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <ListView
            android:id="@+id/list"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp" />

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/colorGreen"/>

    </LinearLayout>


</android.support.design.widget.CoordinatorLayout>

enter image description here

FrameLayout is gone, ListView take all the space. Why?

How can I make layout from first screenshot with ListView instead of RecycleView?

Edit1: This scheme works perfectly in simple cases. For example there are three TextViews, two with wrap_content and one with weight=1:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="Test1"/>

    <TextView
        android:id="@+id/text2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:layout_weight="1"
        android:background="@color/colorGreen"
        android:text="Test2"/>

    <TextView
        android:id="@+id/text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="Test3"/>

</LinearLayout>

It works like a charm, middle TextView takes all free space:

enter image description here

But this doesn't work with ListView. I need ListView to take only space he needs, not 0.3 of screen, not hard-coded value of dp, just a wrap_content.

Edit2: List item layout is follow:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:textSize="18sp"/>

Edit3: I can say you more: if you remove all the stuff from second example and leave only CoordinatorLayout with ListView inside it, the ListView still takes fullscreen regardless of wrap_content width. Why?

Aleksandar G
  • 1,163
  • 2
  • 20
  • 25
Exerion
  • 449
  • 7
  • 20
  • please see my answer and let me know about result – Dhiraj May 31 '16 at 11:13
  • @Dhiraj commented. Unfortunately this is not what I need. – Exerion May 31 '16 at 11:45
  • can you post the list item xml? might be there would be some view which has width = match_parent – SAIR May 31 '16 at 11:47
  • @Exerion can i see your list item xml??? – Dhiraj May 31 '16 at 11:52
  • 1
    @SAIR edited question, added list item xml – Exerion May 31 '16 at 11:59
  • i have just copied you layout with recycler view and list view. and even in layout editor it show that recycler view is warped content. but list view takes the full screen. – SAIR May 31 '16 at 12:01
  • @SAIR that is exactly what question is about. What can I do to make ListView behave like RecycleView is this case? – Exerion May 31 '16 at 12:03
  • i am going to run a sample adapter. just let me complete that. then i will share my findings – SAIR May 31 '16 at 12:06
  • @SAIR, also take a look on **Edit3** section. This is weird. – Exerion May 31 '16 at 12:10
  • @Exerion can you take look of this link http://stackoverflow.com/questions/11295080/android-wrap-content-is-not-working-with-listview – Dhiraj May 31 '16 at 12:13
  • @Exerion see the accepted answer on that link it will might help you – Dhiraj May 31 '16 at 12:13
  • @Exerion i tried with sample using listview it is but not working – Dhiraj May 31 '16 at 12:20
  • @Dhiraj `to be as wide as the widest of its children` - he said. Items of my list are the same width and they are small, because I fill them just with test strings like "Test 1". But still - fullscreen. – Exerion May 31 '16 at 12:21
  • seems like ListView just can't have wrap_content width. But RecycleView can. I suppose I have to use it instead of simple ListView. – Exerion May 31 '16 at 12:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113413/discussion-between-dhiraj-and-exerion). – Dhiraj May 31 '16 at 12:25
  • @Exerion: yes you are quite right. listview does not wrap to its child views. i have faced the same issue with grid view. i suggest you to use recycler view – SAIR May 31 '16 at 12:34

3 Answers3

0

change your code for listview in xml as follows

    <ListView
                android:id="@+id/list"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.3" />

As you are using wrap_content for first child in linear layout and layout_weight for second child in linear layout so it might be possible that it will lead to weird behaviour.WRAP_CONTENT takes the space exactely to item size so in your case it might be possible that list requires all the space of the screen

Dhiraj
  • 870
  • 2
  • 12
  • 25
  • No, list content is very narrow, but it still takes all space. Your solution as long as a @Mehta's solution make a list always be the same width = 0.3 of the width of the screen no matter what, this is not what I need. – Exerion May 31 '16 at 11:27
0

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.

Community
  • 1
  • 1
Exerion
  • 449
  • 7
  • 20
-1

try this by applying weight to your ListView and Frame Layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout >

    <android.support.design.widget.AppBarLayout >
        <android.support.v7.widget.Toolbar/>
    </android.support.design.widget.AppBarLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:baselineAligned="false"
        android:divider="?android:attr/dividerHorizontal"
        android:orientation="horizontal"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <ListView
            android:id="@+id/list"
            android:layout_width="0dp"
            android:layout_weight="0.3"
            android:layout_height="match_parent"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp" />

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.7"
            android:background="@color/colorGreen"/>

    </LinearLayout>


</android.support.design.widget.CoordinatorLayout>
Mehta
  • 1,228
  • 1
  • 9
  • 27