Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android EditText keyboard not shown in Fragment

I added EditText in Fragment, but my EditText is not showing keyboard even if I click on EditText.

Here is my code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <EditText
        android:id="@+id/setting_nickname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="8"
        android:gravity="right"
        android:hint="Samantha"
        android:textColorHint="@color/nav_selected"
        android:textSize="14dp"
        android:textColor="@color/nav_selected"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" >
    </EditText>

    <EditText
        android:id="@+id/setting_country"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="8"
        android:gravity="right"
        android:hint="Samantha"
        android:textColorHint="@color/nav_selected"
        android:textSize="14dp"
        android:textColor="@color/nav_selected"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" >
    </EditText>

</LinearLayout>

I try to get listener of EditText

final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
edit_nickname.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        imm.showSoftInput(edit_nickname, 0);

        //edit_nickname.requestFocus();  --> fail
        //imm.showSoftInput(edit_nickname, InputMethodManager.SHOW_IMPLICIT);  --> fail
        //imm.showSoftInput(edit_nickname, InputMethodManager.SHOW_FORCED);  --> fail
    }
});

And this code is fail to...

final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

Also, I try to add LinearLayout to have focusableInTouchMode and focusable, but failed.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginTop="10dp"
    android:padding="10dp"
    android:background="@color/black_transparent"
    android:focusableInTouchMode="true"
    android:focusable="true">
    <EditText .../>
    <EditText .../>
</LinearLayout>

If I use <requestFocus /> than keyboard is shown up but It applies to only one of EditText. How can I do? And why not the keyboard is shown?

like image 632
Soyeon Kim Avatar asked May 11 '15 05:05

Soyeon Kim


1 Answers

Try using this, it works for me

EditText myEd= (EditText)getActivity().findViewById(R.id.myEd);
myEd.requestFocus(); 
InputMethodManager mgr = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(myEd, InputMethodManager.SHOW_IMPLICIT);
like image 105
Neal Ahluvalia Avatar answered Sep 21 '22 08:09

Neal Ahluvalia