Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 396
Alex Ferents Avatar asked May 06 '14 10:05

Alex Ferents


Video Answer


2 Answers

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"
like image 137
Amardeepvijay Avatar answered Sep 28 '22 16:09

Amardeepvijay


you need to request for focus like this

AutoCompleteTextView yourView = findViewById(R.id.your_id);
yourView.requestFocus();
like image 40
Imtiyaz Khalani Avatar answered Sep 28 '22 15:09

Imtiyaz Khalani