Alex Ferents
Alex Ferents

Reputation: 298

Autocompletetextview show keyboard doesn't work

I am trying to build android application with fragments, that contains Autocompletetextview , i want the keyboard to show up when the fragment is displayed. I have tried, few things but didn't succeed.

here is my code

xml

<AutoCompleteTextView
    android:id="@+id/editArea"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:completionThreshold="1"
    android:singleLine="true"
    android:textColor="@android:color/primary_text_light" 
    android:gravity="right" >
</AutoCompleteTextView>

My Java fragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    mLayout = inflater.inflate(R.layout.change_area, null, false);
    area = (AutoCompleteTextView) mLayout.findViewById(R.id.editArea);
    showHideKeyboard(true);
    citiesList = singleToneCitiesList.getInstance().getList();


    mAdapter = new CitiesAdapter(getActivity(),
            R.layout.autocommplete_text, citiesList);
    area.setAdapter(mAdapter);
    area.setThreshold(1);
    area.setOnItemClickListener(this);
    return mLayout;
}



private void showHideKeyboard(boolean showHide) {
    if (showHide) {
        InputMethodManager inputMethodManager = (InputMethodManager) getActivity()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        area.requestLayout();

        if (inputMethodManager != null) {
            inputMethodManager.toggleSoftInputFromWindow(getActivity()
                    .getCurrentFocus().getApplicationWindowToken(),
                    InputMethodManager.SHOW_FORCED, 0);
        }

    } else {
        InputMethodManager imm = (InputMethodManager) getActivity()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(area.getWindowToken(), 0);

    }
}

Thanks for reading and helping me with this, have a nice day!

Upvotes: 7

Views: 4089

Answers (3)

Amardeepvijay
Amardeepvijay

Reputation: 1628

use this in xml code :

 <AutoCompleteTextView
    android:id="@+id/editArea"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:completionThreshold="1"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:gravity="right"
    android:singleLine="true"
    android:textColor="@android:color/primary_text_light" >

    <requestFocus />
  </AutoCompleteTextView>

and added following to the activity in manifest:

android:windowSoftInputMode="stateAlwaysVisible"

Upvotes: 3

Piyush
Piyush

Reputation: 18923

Call requestFocus(); for AutoCompleteTextView by coding like,

autocomplete.requestFocus();

OR you can set in xml also.

Upvotes: 1

Imtiyaz Khalani
Imtiyaz Khalani

Reputation: 2053

you need to request for focus like this

AutoCompleteTextView yourView = findViewById(R.id.your_id);
yourView.requestFocus();

Upvotes: 1

Related Questions