Dittimon
Dittimon

Reputation: 1026

How to keep DialogFragment positive/negative buttons above soft keyboard

I am using a DialogFragment. I want the positive and negative buttons to remain above the keyboard when the user interacts with an edit text like in the example pics below from a screen in the Gmail tablet application.

without soft keyboard

With soft keyboard - buttons remain above keyboard as dialog resizes and content scrolls

In my attempt which doesn't work, here's my Dialog Fragments onCreateDialog method

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {   

    LayoutInflater factory = LayoutInflater.from(getActivity());
    final View textEntryView = factory.inflate(R.layout.my_layout, null);

    return new AlertDialog.Builder(getActivity())                
            .setTitle(getString(R.string.paypal))
            .setView(textEntryView)
            .setPositiveButton(R.string.dialog_ok,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {                          

                    }
                }
            )
            .setNegativeButton(R.string.dialog_cancel,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                    }
                }
            )
            .create();
}

and here's the R.layout.my_layout

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:fillViewport="true">


    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical">
        <ImageView android:id="@+id/paypal_button"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:layout_marginTop="15dip"
               android:layout_marginBottom="15dip"

             />
        <EditText
            android:id="@+id/edit_paypal_email"
                style="@style/GeneralEditText"
                android:hint="@string/contact_info_text_email"
                android:inputType="textEmailAddress"
                />

           <View 
            android:id="@id/horizontal_line"
            style="@style/HorizontalLine"
            android:layout_marginTop="@dimen/contact_info_line_margin_top" />

        <!-- Notes -->
        <TextView
            android:text="@string/paypal_info1"
            style="@style/ContactInfoNotes"
            android:paddingBottom="5dip"
            />  

        <TextView
            android:text="@string/paypal_info2"
            style="@style/ContactInfoNotes"
            android:paddingBottom="5dip"
            />

        <TextView
            android:text="@string/paypal_info3"
            style="@style/ContactInfoNotes"
            android:paddingBottom="5dip"
            />
        <TextView
            android:text="@string/paypal_info4"
            style="@style/ContactInfoNotes"
            android:paddingBottom="5dip"
            />

    </LinearLayout> 
</ScrollView>

In my implementation, the soft keyboard comes up and covers the dialogs positive and negative buttons. What am I missing to allow the buttons to remain above the keyboard?

Thanks in advance.

Upvotes: 15

Views: 12425

Answers (5)

CautionHot
CautionHot

Reputation: 1

This is what worked for me.

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


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

    <ScrollView
        android:layout_width="match_parent"
        android:fillViewport="true"
        android:layout_weight="1"
        android:layout_height="300dp" >

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:inputType="textMultiLine|textAutoCorrect|textCapSentences"
             />
    </ScrollView>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
         />

</LinearLayout>

Upvotes: 0

nistv4n
nistv4n

Reputation: 2535

I had the same problem, and i found a solution. The essential part of the layout file:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:layout_weight="1">
 <!-- Form items  -->
</ScrollView>

<!-- Include the layout which contains the buttons -->
<include layout="@layout/fragment_dialog_button_bar" android:layout_weight="0" />

So you need to set the buttons' layout_weight 0, while the scrollview's weight is 1 or bigger.

I hope this will help.

Upvotes: 4

Chiara
Chiara

Reputation: 1887

Just add this on your onViewCreated:

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

Upvotes: 29

Alex Lockwood
Alex Lockwood

Reputation: 83311

Have you tried adding the following attribute to the activity tag in your manifest?

<activity android:name=".SampleActivity"
    android:windowSoftInputMode="adjustResize" 
    ... >
</activity>

Upvotes: 0

Kirk
Kirk

Reputation: 16255

I believe this has to do with the LinearLayout and ScrollView. Remove the fillViewport part from the ScrollView and show the scrollbar in the LinearLayout.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:scrollbars="vertical"
        android:scrollbarAlwaysDrawVerticalTrack="true">
         <!-- Stuff -->

    </LinearLayout>
</ScrollView>

Upvotes: 0

Related Questions