Keyboard hiding EditTexts in Fragments

假装没事ソ 提交于 2019-11-30 03:35:47
Yenchi

Try to call InputMethodManager.hideSoftInputFromWindow() during your fragment's onActivityCreated() function mentioned in this SO answer?

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    AppUtil.hideKeyboard(getActivity(), getView());
}

where hideKeyboard() looks like this

public static void hideKeyboard(Activity activity, View viewToHide) {
    InputMethodManager imm = (InputMethodManager) activity
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(viewToHide.getWindowToken(), 0);
}
Vishwa

Try to use WindowsSoftInputMode as adjustpan only.

For ex:

android:windowSoftInputMode="adjustPan" 

You can try to put this in onActivityCreated of your fragment:

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

This should not make the keyboard upon loading or starting of your activity with fragment.

Yvette Colomb

I solved the problem by installing another soft keyboard, via the google play store.

It was Petey's comments that made me explore this solution.

The IME you used has it's own "floating" layout, can you try your code with built-in IMEs like Google Keyboard or set your IME to a normal full-width layout?

So I figured, no amount of playing with the view layout was going to affect a soft keyboard that has an independent layout.

I used this link https://code.google.com/p/softkeyboard/wiki/HowTo

It even works with Android Manifest:

android:windowSoftInputMode="stateHidden"

I then use imeoptions to control the flow of the editor cursor within the view.

edit update

I just tried the app on a no-brand android tablet, which comes with comes with a generic android keyboard and it works, without having to change a thing. It seems the Samsung default keyboard is painful.

Change this to LinearLayout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

to

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

(and the closing tag). Note the orientation.

Update the manifest Remove this:

<application ... >
    <activity
      android:windowSoftInputMode="stateVisible|adjustResize|adjustPan">
        ...
    </activity>
    ...
</application>

Change this (height):

<ScrollView
    android:layout_height="wrap_content"
    android:layout_width="match_parent">
Saikat1529

I have tried this code and resolve my issue.

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

keep in mind that hide keyboard does not work with getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

What worked for me was using the ScrollView the same way as you in my FrameLayout, in which I added this property: android:windowSoftInputMode="adjustPan". At the end, it looks like this and it is working. I did not need to change anything programatically:

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ProductFragment"
    android:windowSoftInputMode="adjustPan">

    <ScrollView
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
      ...
    </ScrollView>
</FrameLayout>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!