2

In my app, there is a tabbed activity with three fragments. The first fragment has a form to create a new task, the second fragment has the list of all the saved tasks, and the third fragment will show the comments on a task when selected from the list in the second fragment. The third fragment is also supposed to act like a chat activity which posts comments when you type them in and tap the send button. The XML layout of this third fragment is as follows:

<?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:background="@android:color/darker_gray"
android:orientation="vertical"
tools:context="com.example.ishita.assigntasks.CommentsFragment">

<TextView
    android:id="@+id/frag_task_details"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#dd55ff"
    android:padding="10dp" />

<ListView
    android:id="@+id/frag_comment_list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:transcriptMode="normal" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@android:color/white">

    <EditText
        android:id="@+id/frag_msg_edit"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:layout_weight="1" />

    <ImageButton
        android:id="@+id/frag_send_btn"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:background="@android:color/background_light"
        android:contentDescription="@string/send_btn"
        android:src="@android:drawable/ic_menu_send" />

</LinearLayout>

</LinearLayout>

As you can see, there is a TextView and a ListView and below that is another LinearLayout. Here is how it should look (the purple bar is the TextView):

expected_behavior

And here is how it actually looks:

malfunctioning_behavior

The TextView shows up above the ListView, but the LinearLayout does not. It is there, though. If I do android:layout_marginBottom="20dp" on the outermost LinearLayout, it does show up, but the ActionBar scrolls up and overlaps with the notification bar so that the title on the ActionBar and the notifications on the notification bar are both visible simultaneously and neither is legible.

I searched a lot and this seemed a common issue, but none of the solutions helped me. Believe me, I tried everything I could find. I tried wrapping the whole thing in a FrameLayout, using a RelativeLayout in place of the outermost LinearLayout, using two LinearLayouts--one to wrap the TextView and the ListView and the other to wrap the EditText and the ImageButton, etc., but nothing was able to show the bottom LinearLayout below the ListView. I even tried to set focus on the EditText when the fragment launches so that the keyboard would show and I can type, but even that doesn't help.

Note: On the other hand, if I use the exact same layout on an activity, the bottom LinearLayout displays exactly as it should.

I am unable to find the bug. Please help!

Ishita Sinha
  • 2,168
  • 4
  • 25
  • 38

4 Answers4

1

It looks like your toolbar pushes fragment layout down without decreasing its height. I have no idea why this happens (there are no root layout code here).

As a workaround you can set fragments layout bottom margin to ?attr/actionBarSize

Alexandr Shutko
  • 1,857
  • 2
  • 20
  • 27
0

As you have given layout_weight 1 in listview layout, so it occupies the whole space available. In order to get rid of you have to give some static height or use layout_weight in right manner

<ListView
android:id="@+id/frag_comment_list"
android:layout_width="match_parent"
android:layout_height="500dp"
android:transcriptMode="normal" />

or try like this

<?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:background="@android:color/darker_gray"
android:orientation="vertical"
tools:context="com.example.ishita.assigntasks.CommentsFragment">

   <TextView
   android:id="@+id/frag_task_details"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="#dd55ff"
   android:padding="10dp" />
  <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="0dp"
  android:layout_weight="1"
    android:orientation="vertical"
  android:background="@android:color/white">
  <ListView
android:id="@+id/frag_comment_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:transcriptMode="normal" />
</LinearLayout>
 <LinearLayout
android:layout_width="match_parent"
 android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:background="@android:color/white">

<EditText
    android:id="@+id/frag_msg_edit"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:inputType="textMultiLine"
    android:layout_weight="1" />

<ImageButton
    android:id="@+id/frag_send_btn"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0"
    android:background="@android:color/background_light"
    android:contentDescription="@string/send_btn"
    android:src="@android:drawable/ic_menu_send" />

   </LinearLayout>

   </LinearLayout>
Awadesh
  • 3,530
  • 2
  • 20
  • 32
  • I cannot use fixed heights on any of the `Views` due to project specifications. Your second solution only has the effect of splitting the screen 50-50 between the list and the comments bar. This is definitely not what I'm trying to achieve. I want the bar at the _bottom_ of the screen. – Ishita Sinha Feb 26 '16 at 04:59
  • this is not the right way as you think if the screen height is below `500dp`, how it'll get displayed?? – ELITE Feb 26 '16 at 05:48
0

All,

Thank you for all your responses. I found a workaround by trial and error now and thought I should post the answer for others who face the same issue. I set android:layout_marginBottom="50dp" on the inner LinearLayout (the one wrapping the comments bar--EditText and ImageButton). Somehow, this sets the layout correctly and the fragment functions properly in both Lollipop and Jellybean OS's. I haven't tested on other OS versions.

Ishita Sinha
  • 2,168
  • 4
  • 25
  • 38
  • If this is a toolbar issue you should use ?attr/actionBarSize instead of 50dp. – Alexandr Shutko Feb 26 '16 at 05:11
  • @AlexandrShutko Ooh! Yes, thanks! This is a _solution_, rather than my workaround. If you post it as an answer, I will accept it. :) – Ishita Sinha Feb 26 '16 at 05:28
  • instead of setting `margin` try `android:layout_weight="0.7"` to `ListView` and is the right solution i guess. – ELITE Feb 26 '16 at 05:44
  • @ELITE. Why 0.7 ? Layout with weight=1 and height=0 eats all FREE space. His problem is in parent layout. Its toolbar pushes all layout down without decreasing height. P.S. I changed an answer. – Alexandr Shutko Feb 26 '16 at 05:50
  • @AlexandrShutko Thanks. Oh, and correction: * _her_ problem ;) – Ishita Sinha Feb 26 '16 at 05:58
0

First of all you should read this, what is layout_weight and how it works.

You assigned layout_weight to ListView as 1, it means it covers whole height. just change it to 0.7 or any proportion you want (less than 1) will solve the problem.

<ListView
    android:id="@+id/frag_comment_list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.7"
    android:transcriptMode="normal" />
Community
  • 1
  • 1
ELITE
  • 5,815
  • 3
  • 19
  • 29