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!
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"
you need to request for focus like this
AutoCompleteTextView yourView = findViewById(R.id.your_id);
yourView.requestFocus();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With