Note that there are some explanatory texts on larger screens.

plurals
  1. POEmpty space when I return to Activity (Soft Keyboard forced)
    text
    copied!<p>I have an <code>ActionView</code> with menu item on <code>ActionBar</code> (using <em>ActionBarSherlock</em>), I'm able to display an <code>EditText</code> as a search field in it. It's an input to launch another <code>Activity</code> with a <code>CustomView</code> in ActionBar which it displays the same layout (I don't use anything to force the <code>SoftKeyboard</code> to appear in this second activity, there is no problem here). When I want to make the Soft Keyboard appears/disapears automatically when the view collapse in first activity, I use: </p> <p><strong>openKeyboard method</strong> </p> <pre><code>mImm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); </code></pre> <p><strong>closeKeyboard method</strong> </p> <pre><code>mImm.hideSoftInputFromWindow(edittext.getWindowToken(), 0); </code></pre> <p>I use the <code>ActionExpandListener</code> to make the SoftKeyboard appears or disappears when the <code>View</code> expands or collapse. With these two methods above, I have the expected result. I found this on several questions on SO (especially on <a href="https://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard">Close/hide the Android Soft Keyboard</a> and <a href="https://stackoverflow.com/a/15225311/2668136">Showing the soft keyboard for SearchView on ActionBar</a> or <a href="https://stackoverflow.com/questions/2479504/forcing-the-soft-keyboard-open">Forcing the Soft Keyboard open</a>).</p> <p>Just to understand, when I used <code>SHOW_IMPLICIT</code> or <code>SHOW_FORCED</code> alone, it was no effect on lower versions (as 2.+). The EditText was focused but the keyboard didn't show up (so, you guess it was a bad thing). In recent versions (as 4.+ for example), it was a nice effect and no problem. Then, I forced keyboard to show up with the <em>openKeyboard method</em> above.</p> <p>Now, I got some troubles with this...<br> On lower versions, I got "empty" space before and after the keyboard created/destroyed, I can live with this. <strong>BUT</strong> in recent versions, I got "empty" space which it displays when I <strong>return to the first Activity</strong>. And it's here during less than one second, but sufficient to see that!<br> To better understand what happens, see the image below: </p> <p><img src="https://i.stack.imgur.com/hliIx.png" alt="Left=Second Activity: I press the Up Home Button - the keyboard disappears properly. Right=(back to) First Activity: my ListView is covered by a &quot;empty&quot; screen (background color in my application) and it disappears."> </p> <p><strong><em>1.</em></strong> Second Activity: I press the Up Home Button - the keyboard disappears properly.<br> <strong><em>2.</em></strong> (back to) First Activity: my ListView is covered by a "empty" space (background color in my application). And it disappears <em>(this is the same height of the SoftKeyboard, no possible doubt!)</em> </p> <p>I guess it's because <strong>I forced the keyboard to appear</strong> in my first activity <strong>although I also forced the keyboard to hide</strong> when I go the second, but how can I resolve the "empty" space when I return to the first activity?</p> <hr> <p><em>Summary</em><br> 1) A activity => press item in menu > view collapse > show the keyboard > tap text > send it > hide keyboard > launch B activity.<br> 2) B activity => setCustomView in actionbar > show the keyboard only if the edittext is focused/clicked > tap text > send it > hide keyboard > refresh content > press home button > return to A activity<br> 3) A activity => "empty" screen > screen disappears. </p> <p>Any help will be very appreciate.<br> Thanks for your time. </p> <hr> <p><strong>EDIT</strong><br> I add my code of my first class, to see if someone tells me what I'm doing wrong. Maybe it's my code which makes the issue. </p> <blockquote> <p><strong>Menu (ActionView)</strong> </p> </blockquote> <pre><code>ActionBar actionBar; MenuItem itemSearchAction; EditText mSearchEdit; InputMethodManager mImm; @Override public boolean onCreateOptionsMenu(final Menu menu) { getSupportMenuInflater().inflate(R.menu.main, menu); itemSearchAction = menu.findItem(R.id.action_search); View v = (View) itemSearchAction.getActionView(); mSearchEdit = (EditText) v.findViewById(R.id.SearchEdit); itemSearchAction.setOnActionExpandListener(this); return true; } </code></pre> <blockquote> <p><strong>OnActionExpandListener</strong> </p> </blockquote> <pre><code>@Override public boolean onMenuItemActionExpand(MenuItem item) { actionBar.setIcon(R.drawable.ic_app_search); // change icon mSearchEdit.requestFocus(); // set focus on edittext openKeyboard(); // the method above mSearchEdit.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEARCH || (event != null &amp;&amp; event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) { closeKeyboard(); // same method as above // new Intent() to second activity // perform with startActivity(); itemSearchAction.collapseActionView(); // collapse view return true; } return false; } }); // add a clicklistener to re-open the keyboard on lower versions mSearchEdit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { openKeyboard(); } }); return true; } @Override public boolean onMenuItemActionCollapse(MenuItem item) { actionBar.setIcon(R.drawable.ic_app_logo); // change icon again if(!(mSearchEdit.getText().toString().equals(""))) mSearchEdit.setText(""); // reinitial the edittext return true; } </code></pre> <blockquote> <p><strong>OnOptionsItemSelected</strong> </p> </blockquote> <pre><code>// I had this verification when I make new Intent() to // a new activity, just in case (works like a charm) if(itemSearchAction.isActionViewExpanded()) itemSearchAction.collapseActionView(); </code></pre> <blockquote> <p><strong>ActionView (Item + layout)</strong> </p> </blockquote> <pre><code>&lt;item android:id="@+id/action_search" android:icon="@drawable/ic_app_search" android:title="@string/action_search" android:showAsAction="ifRoom|collapseActionView" android:actionLayout="@layout/search_actionview" /&gt; &lt;EditText xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/SearchEdit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="right|bottom" android:gravity="left" android:layout_marginBottom="6dip" android:hint="@string/action_search" android:textColor="@color/white" android:textColorHint="@color/white" android:singleLine="true" android:cursorVisible="true" android:inputType="text" android:imeOptions="actionSearch|flagNoExtractUi" android:imeActionLabel="@string/action_search" android:focusable="true" android:focusableInTouchMode="true" android:background="@drawable/bt_edit_searchview_focused" &gt; &lt;requestFocus /&gt; &lt;/EditText&gt; </code></pre> <hr> <p><strong>UPDATE</strong> </p> <p>I see a lot of similar issues, with <code>EditText</code> in <code>ActionBar</code> which not makes the keyboard appear even the focus has set. I tried this again (even if I already tested several time): </p> <pre><code>/* * NOT WORKING * Sources: https://stackoverflow.com/questions/11011091/how-can-i-focus-on-a-collapsible-action-view-edittext-item-in-the-action-bar-wh * https://stackoverflow.com/a/12903527/2668136 */ int mode = WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE; getWindow().setSoftInputMode(mode); postDelayed() to show: .showSoftInput(mSearchEdit, InputMethodManager.SHOW_IMPLICIT); - 200ms (Not working on lower versions) postDelayed() to hide: .hideSoftInputFromWindow(mSearchEdit.getWindowToken(), 0); - 200ms new Runnable() on edittext =&gt; requestFocus() + showSoftInput(SHOW_IMPLICIT/FORCED/HIDE_NOT_ALWAYS/HIDE_IMPLICIT_ONLY) </code></pre> <p>It seems with me, only <code>SHOW_FORCED|HIDE_IMPLICIT_ONLY</code> can force the keyboard to show automatically when the view collapse. After this, in all versions, I must to make a <code>hideSoftInputFromWindow</code> to 0 for hiding it.<br> <strong>BUT</strong> this undisplays the keyboard even if the edittext is pressed, so I added an <code>ClickListener</code> to force the keyboard to show again (this happens only on lower versions). </p> <hr> <p><strong>UPDATE2:</strong><br> It's clearly weird, when I try to make a little <code>Thread</code> like I saw in many SO answers (with/without ABS), nothing happens in lower versions.<br> I tried a different way. I created the new thread to have a short time before call the new intent for hide the keyboard. I had the keyboard which forced to close, OK. And then I opened the new activity, OK. But now when I return, it's worth! The "empty" space is also on lower versions when I come back. I did this: </p> <pre><code>// close the keyboard before intent closeKeyboard(); // make the intent after 500 ms Handler handler = new Handler(); Runnable runner = new Runnable() { public void run() { // new intent with startActivity() } }; handler.postDelayed(runner, 500); // collapse the View itemSearchAction.collapseActionView(); </code></pre> <p>It gives me headaches! I don't understand why in my case, the tip above not working whereas on other answers, when they use a new thread to show/hide the keyboard, this works perfectly.</p> <p><strong>NOTE:</strong> my tests were on <em>(emulator:)</em> GalaxyNexus, NexusS, NexusOne and <em>(real devices:)</em> Samsung GalaxySL (2.3.6) and Nexus4 (4.4). </p> <p>If someone can help me with this ugly situation. Thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload