pprados
pprados

Reputation: 1127

Android ViewPager show soft keyboard in the wrong place

I use a ViewPager with 3 fragment. The first one have only text. The second, a input field. the third, only text.

When the ViewPager is initialized, the soft keyboard is show, because the focus is set the the input field. If I change the order of fragment, the soft keyboard is not shown.

How I can control the focus et soft keyboard with ViewPager ?

Regards

Upvotes: 5

Views: 4316

Answers (3)

Alexander M.
Alexander M.

Reputation: 3478

Thanks to everyone, Timmmm was very helpful. I've finally wrapped everything up and got a full soft-keyboard hiding solution for tab swiping. I have 4 tabs with editTexts on each one and I need to hide keyboard when swiping. I've added this to the fragment layout:

<!--Fixes keboard pop-up-->
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@android:color/transparent"
    android:focusable="true"
    android:focusableInTouchMode="true">
</LinearLayout>

This was added to the Activity Code (notice a little difference with a Timmmm's answer: I don't have

mViewPager.getCurrentItem() == 0

here, because I need to hide the keyboard for every view:

// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
    @Override
    public void onPageSelected(int position) {
        if (actionBar != null) {
            actionBar.setSelectedNavigationItem(position);
        }
    }
    @Override
    public void onPageScrollStateChanged(int state) {
        if (state == ViewPager.SCROLL_STATE_IDLE) {
            // Hide the keyboard.
            ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
            .hideSoftInputFromWindow(mViewPager.getWindowToken(), 0);

        }
    }
});

And here is an activity in AndroidManifest.xml:

<activity
        android:name=".TestActivity"
        android:label="@string/title_activity_test"
        android:parentActivityName=".MainActivity"
        android:windowSoftInputMode="stateHidden">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.atrinax.test.MainActivity" />
</activity>

Upvotes: 0

Timmmm
Timmmm

Reputation: 96832

The best solution I've found so far is to use android:windowSoftInputMode="stateHidden" in your activity's manifest, and then add this to your activity.

@Override
public void onPageScrollStateChanged(int state)
{
    if (state == ViewPager.SCROLL_STATE_IDLE)
    {
        if (mViewPager.getCurrentItem() == 0)
        {
            // Hide the keyboard.
            ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE))
                .hideSoftInputFromWindow(mViewPager.getWindowToken(), 0);
        }
    }
}

I didn't use onPageSelected() because the hide-keyboard animation screws with the swiping animation. And I didn't use the android:focusable trick because the keyboard isn't hidden when you swipe back to an input-less fragment. Although I suppose you could combine that with the above code.

Upvotes: 6

Alex Lockwood
Alex Lockwood

Reputation: 83311

I'm sure there is a better way to do this, but I was having the same problem and I got around it by setting the parent View to focusable. That way, whatever is causing the soft keyboard from popping up will not receive focus when you swipe between pages...

<!-- Dummy item to prevent your View from receiving focus -->
<LinearLayout
    ...
    android:focusable="true" 
    android:focusableInTouchMode="true" />

    <!-- The view(s) that are causing the keyboard to pop up each time you swipe -->
    <EditText ... />

</LinearLayout>

Upvotes: 1

Related Questions