piojo
piojo

Reputation: 1929

Making part of layouts always visible when keyboard is shown

I have a DialogFragment with the following layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
android:background="@drawable/rounded_rect_light">

<RelativeLayout
    android:id="@+id/create_note_top_panel"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="#f2f3f5">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="Cancel"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title"
        android:gravity="center"
        android:layout_centerInParent="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="Send"/>
</RelativeLayout>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/quote_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_alignParentTop="true"
            android:src="@drawable/edu4_create_note_quotes"/>

        <TextView
            android:id="@+id/quote_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/quote_image"
            android:scrollbars="vertical"
            android:fadeScrollbars="false"
            android:text="The issue is that soft keyboard is very tricky and this\n quesakdf\njk\nlsd\nfj\nklsdjfklsdjfkl jsdklfj skldjkljfklsdjklfjsdklfjsdkljfkls"
            android:maxLines="5"/>

        <EditText
            android:layout_width="match_parent"
android:layout_height="150dp"
            android:layout_below="@id/quote_text"
            android:layout_margin="10dp"
            android:gravity="top|left"
            android:background="@drawable/rounded_rect_background_white"
            android:padding="5dp"/>
    </RelativeLayout>
</ScrollView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingBottom="15dp"
    android:layout_marginLeft="10dp">

    <TextView
        android:id="@+id/visibleTo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_gravity="center_vertical"
        android:text="Publish"/>

    <ToggleButton
        android:id="@+id/switchPublishToCourse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"/>
</LinearLayout>

I want that when the keyboard is opened, the top panel (android:id="@+id/create_note_top_panel") will be visible on top, and the dialog (it's background) will be above the keyboard, the bottom TextView and ToggleButton will be visible and the rest of the layout will be scrollable.

Upvotes: 0

Views: 396

Answers (1)

piojo
piojo

Reputation: 1929

Found the answer: add android:layout_weight="0" to the parts that you want to stay visible andandroid:layout_weight="1" to the one that can be resized. Here is the full xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
android:background="@drawable/rounded_rect_light">

<RelativeLayout
    android:id="@+id/create_note_top_panel"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="#f2f3f5">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="Cancel"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title"
        android:gravity="center"
        android:layout_centerInParent="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="Send"/>
</RelativeLayout>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_marginTop="10dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/quote_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_alignParentTop="true"
            android:src="@drawable/edu4_create_note_quotes"/>

        <TextView
            android:id="@+id/quote_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            android:fadeScrollbars="false"
            android:layout_toRightOf="@id/quote_image"
            android:text="The issue is that soft keyboard is very tricky and this\n quesakdf\njk\nlsd\nfj\nklsdjfklsdjfkl jsdklfj skldjkljfklsdjklfjsdklfjsdkljfkls\nend of tex"
            android:maxLines="5"/>

        <EditText
            android:id="@+id/create_note_text"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_below="@id/quote_text"
            android:layout_margin="10dp"
            android:gravity="top|left"
            android:background="@drawable/rounded_rect_background_white"
            android:padding="5dp"/>
    </RelativeLayout>
</ScrollView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_weight="0"
    android:gravity="center_vertical"
    android:layout_marginLeft="10dp"
    android:layout_marginBottom="10dp">

    <TextView
        android:id="@+id/visibleTo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_gravity="center_vertical"
        android:text="Publish"/>

    <ToggleButton
        android:id="@+id/switchPublishToCourse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"/>
</LinearLayout>

and in the Fragment class:

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}

Upvotes: 1

Related Questions