Bhanu Sharma
Bhanu Sharma

Reputation: 5145

EditText soft keyboard always gets open on fragment in view pager

I am getting problem of keypad always open.

Display hierarchy is :

I have activity which place a fragment eg;(Fragment1) and this Fragment1 contains edittext and TabLayout and view pager. now view pager having 4 tabs and 4 different fragments attached to this tabs now.

This tabs fragments update the edittext of parent fragment (Fragment1) which contains this edittext.

Now whenever viewpager fragment loads the data edittext always gets focus and keypad open: here is my xml:

<LinearLayout
    android:id="@+id/search_edittext_linear"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/theme_dark"
    android:paddingBottom="5dp"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:focusableInTouchMode="true"
    android:paddingTop="5dp"
    android:visibility="visible">

    <EditText
        android:id="@+id/search_edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/edittext_bottom_line"
        android:drawableLeft="@drawable/search_icon"
        android:drawablePadding="2dp"
        android:hint="@string/sSearch"

        android:padding="5dp"
        android:textColor="@color/txt_color_wht"
        android:textColorHint="#81ABC1"
        android:textCursorDrawable="@drawable/color_cursor"
        android:textSize="13sp"/>

</LinearLayout>

and Edittext textchange listner in Fragment1:

TextWatcher watcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

i already set the keypad hidden code in oncreate() of Fragment1:

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (s.length() == 0) {
            searchFrag = null;
        }
        Fragment frag = adapter.fraglist.get(curpos);
        if (frag instanceof ContactFragment) {
            ((ContactFragment) frag).OnTextChage(s.toString());
            searchFrag = ((ContactFragment) frag);
        } else if (frag instanceof CallLogsFragment) {
            ((CallLogsFragment) frag).OnTextChage(s.toString());
            searchFrag = ((CallLogsFragment) frag);
        } else if (frag instanceof ManualFregment) {
            ((ManualFregment) frag).OnTextChage(s.toString());
            searchFrag = ((ManualFregment) frag);
        } else if (frag instanceof CallFregment) {
            ((CallFregment) frag).OnTextChage(s.toString());
            searchFrag = ((CallFregment) frag);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
};

I don't want to show keypad until user clicks or touch the edittext.

Upvotes: 1

Views: 620

Answers (1)

chrjs
chrjs

Reputation: 2443

You can try to add these 2 properties to the root view (In your case the LinearLayout?) of each fragment. This should avoid opening the keyboard when displaying the view initially.

android:focusable="true"
android:focusableInTouchMode="true" 

Or try to add this for the activity in your manifest:

<activity android:windowSoftInputMode="stateHidden" ...>

Upvotes: 2

Related Questions