-1

I am having a strange issue in android. I have a LinearLayout below a ListView. The list view has a 0dp width and a layout_weight of 90. linear layout layout_weight is 10. and height 0dp. At device the linearlayout disappear.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView android:id="@+id/chatsListView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="90"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:listSelector="@android:color/transparent"
        android:transcriptMode="alwaysScroll"
        />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="10"
        android:padding="10dp">
        <EditText
            android:id="@+id/messageEditText"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:inputType="textMultiLine"
            android:hint="type message here"
            android:ems="10"/>

        <ImageButton
            android:id="@+id/sendMessageButton"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/send_button"/>

    </LinearLayout>

</LinearLayout>

I need to know what it is the problem above, I do not want to do it in another way. I am doing this project to learn logically, and not copy paste.

Here preview at designer:

Here preview at designer

Here is preview in device:

Here is preview in device

When I open and close keyboard, it resize like in designer. I need to know if there is any error on the code.

Edit: Zharf from android-dev IRC found this solution:

Viewpager is being pushed out of the screen [CoordinatorLayout] [Design Library]

Community
  • 1
  • 1
  • If you only would have used a **PercentRelativeLayout**, you could get rid of the extra, unnecessary LinearLayout. – Phantômaxx Feb 09 '17 at 08:28
  • Please try adding `android:weightSum="100"` to the outer `LinearLayout`. – Slav Feb 09 '17 at 08:32
  • can you provide details from the device in the "designer" and "actual" device? like what are the sizes of the devices and what are the devices themselves (nexus, samsung, etc). – hehe Feb 09 '17 at 08:32
  • the device is nexus, and i have tested in many devices in designer and it looks good. but in simulator it just goes down of screen i think. I have set before weightsum in parent to one and the other layout weight were 0.9 and 0.1. the same problem happened. – Programmer01 Feb 09 '17 at 08:53
  • @Slav that's totally unnecessary. Android calculates the weightSum automatically. – Phantômaxx Feb 09 '17 at 09:05

2 Answers2

0

Use android:layout_weight="1" instade of android:layout_weight="90" in ListView. Also android:layout_height="wrap_content" instade of android:layout_height="0dp" and don't use layout_weight in LinearLayout. You can use layout_weight="1" in EditText . Hope this will fix your issue :)

Farya
  • 277
  • 3
  • 14
  • 1
    Where is logic behinbd that? As I said i need to know where is the problem logically. – Programmer01 Feb 09 '17 at 08:47
  • you use layout_weight="90" & layout_weight="10" in ListView and LinearLayout, so it divides in 90 part of your device height to take for listview and 10 part of your device height to take for LinearLayout. For that, it will behave differently in different devices. – Farya Feb 09 '17 at 09:17
  • I think that you are wrong wrong. In all devices this rule should be respected. – Programmer01 Feb 09 '17 at 09:33
-1

try this... you just missed one line weightsum at parrent linear layout. add this.

android:weightSum="100"

so your final code looks like

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="100">

       <ListView android:id="@+id/chatsListView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="90"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:listSelector="@android:color/transparent"
        android:transcriptMode="alwaysScroll"
        />

       <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="10"
        android:padding="10dp">

        <EditText
            android:id="@+id/messageEditText"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:inputType="textMultiLine"
            android:hint="type message here"
            android:ems="10"/>

        <ImageButton
            android:id="@+id/sendMessageButton"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/send_button"/>

    </LinearLayout>

</LinearLayout>
Samarth Sevak
  • 521
  • 4
  • 16