0

I am sitting a long time to get my layout working. There should be an EditText on top. A RecyclerView after that. An ImageButton after that. At the bottom of the activity should be a Button. This looks fine. There is a Space between the ImageButton and Button which has height=0dp and weight=1. Rest has height=wrap_content.

When I open up the keyboard, the RecyclerView keeps being full-sized. This doesnt look fine. I want it to make space to let the Buttons fit on the screen. It should be like that

I tried to make the RecyclerView height=0dp too, but then the Space doesnt work as it should. Their height would depend on each other weight. When I remove the Space and try to get the Button to the bottom while Recyclerview has height=0dp, it would fill the entire free space, so the ImageButton would stick to the Button instead of to the end of the list.

If I would put the ImageButton as the last element of the RecyclerView, it wouldnt be always on the screen when the List is too long.

Layout-XML-only solution would be appreciated.

@edit: My layout breaks completely when I add more items. This should be fixed with the same approach.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context=".ui.edit.EditActivity">

    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"

        android:hint="@string/enter_name"
        android:inputType="text|textAutoComplete|textAutoCorrect|textShortMessage"
        android:maxLength="25" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:scrollbars="vertical"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager">

    </android.support.v7.widget.RecyclerView>


    <ImageButton
        android:id="@+id/bt_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="8dp"
        android:background="@null"
        app:srcCompat="@drawable/twotone_add_circle_24" />

    <Space
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <Button
        android:id="@+id/bt_save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="save" />

</LinearLayout>
  • I had similar issues with keyboard. I think you need to focus on googling a bit differently, for `windowSoftInputMode` or something similar, cause that determines how your keyboard will behave. Also check [this](https://stackoverflow.com/questions/41582691/push-up-content-except-some-view-when-keyboard-shown/41713627) answer out – Vucko May 28 '18 at 12:35

1 Answers1

0

you can give weight to recyclerview like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">

<EditText
    android:id="@+id/et_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:hint="enter name"

  android:inputType="text|textAutoComplete|textAutoCorrect|textShortMessage"
    android:maxLength="25" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_margin="8dp"
    android:layout_weight="1"
    android:scrollbars="vertical"
    app:layoutManager="android.support.v7.widget.LinearLayoutManager"> 
   </android.support.v7.widget.RecyclerView>

<ImageButton
    android:id="@+id/bt_add"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_margin="8dp"
    android:background="@null"
    app:srcCompat="@drawable/ic_delete" />

<Space
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

<Button
    android:id="@+id/bt_save"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:text="save" />
</LinearLayout>