Can't make Android on-screen keyboard show / hide with viewpager

In my application, I use viewPager to give me good swipey looks. I want the keyboard to be hidden on two pages, but always crossed out on one page where I have a text box.

I tried various ways for the keyboard to display, but it just doesn't work. I think I should call the display keyboard code in the wrong place.

@Override public Object instantiateItem( View collection, int position ) { LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View layout = null; if(position==0){ layout=inflater.inflate(R.layout.other, null); //new PC().create(layout, context); ((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(collection.getWindowToken(), 0); }else if(position==1){ layout=inflater.inflate(R.layout.main, null); new BlurayRemote().create(layout,context); ((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(collection.getWindowToken(), 0); }else if(position==2){ layout=inflater.inflate(R.layout.text, null); new TextInput().create(layout,context); ((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInputFromInputMethod(collection.getWindowToken(), 0); } ((ViewPager) collection).addView(layout); return layout; } 

Any help would be great as it drives me crazy!

+4
source share
2 answers

I am sure there is a better way to do this, but I had the same problem, and I circumvented it by setting the parent View to custom. Thus, everything that causes the appearance of a soft keyboard will not receive focus when scrolling between pages.

 <!-- Dummy item to prevent your View from receiving focus --> <LinearLayout ... android:focusable="true" android:focusableInTouchMode="true" /> <!-- The view(s) that are causing the keyboard to pop up each time you swipe --> <EditText ... /> </LinearLayout> 
+2
source

I found that using the wrong context usually soaks things up. Try it.

 @Override public Object instantiateItem( View collection, int position ) { LayoutInflater inflator = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View layout = null; if(position==0){ layout=inflater.inflate(R.layout.other, null); //new PC().create(layout, context); ((InputMethodManager)collection.getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(collection.getWindowToken(), 0); }else if(position==1){ layout=inflater.inflate(R.layout.main, null); new BlurayRemote().create(layout, collection.getContext()); ((InputMethodManager)collection.getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(collection.getWindowToken(), 0); }else if(position==2){ layout=inflater.inflate(R.layout.text, null); new TextInput().create(layout,collection.getContext()); ((InputMethodManager)collection.getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInputFromInputMethod(collection.getWindowToken(), 0); } ((ViewPager) collection).addView(layout); return layout; } 
+1
source

All Articles