Android ViewPager displays soft keyboard in the wrong place

I am using ViewPager with 3 fragments. The first has only text. The second is the input field. third, only text.

When the ViewPager is initialized, a soft keyboard is displayed because the focus is set in the input field. If I change the order of the fragment, the soft keyboard does not appear.

How can I control focus and soft keyboard using ViewPager?

Hi

+5
source share
3 answers

The best solution I've found so far is to use android:windowSoftInputMode="stateHidden"activity in your manifest, and then add it 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);
        }
    }
}

onPageSelected(), . android:focusable, , . , .

+5

, , , , View . , , , ...

<!-- 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>
+1

, . , , . 4 editTexts , . :

<!--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 has been added to Code Activity (note the slight difference with Timmmm's answer: I don't have

mViewPager.getCurrentItem() == 0

here because I need to hide the keyboard for each 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 the 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>
0
source

All Articles