Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty space when I return to Activity (Soft Keyboard forced)

I have an ActionView with menu item on ActionBar (using ActionBarSherlock), I'm able to display an EditText as a search field in it. It's an input to launch another Activity with a CustomView in ActionBar which it displays the same layout (I don't use anything to force the SoftKeyboard 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:

openKeyboard method

mImm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);  

closeKeyboard method

mImm.hideSoftInputFromWindow(edittext.getWindowToken(), 0); 

I use the ActionExpandListener to make the SoftKeyboard appears or disappears when the View expands or collapse. With these two methods above, I have the expected result. I found this on several questions on SO (especially on Close/hide the Android Soft Keyboard and Showing the soft keyboard for SearchView on ActionBar or Forcing the Soft Keyboard open).

Just to understand, when I used SHOW_IMPLICIT or SHOW_FORCED 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 openKeyboard method above.

Now, I got some troubles with this...
On lower versions, I got "empty" space before and after the keyboard created/destroyed, I can live with this. BUT in recent versions, I got "empty" space which it displays when I return to the first Activity. And it's here during less than one second, but sufficient to see that!
To better understand what happens, see the image below:

Left=Second Activity: I press the Up Home Button - the keyboard disappears properly. Right=(back to) First Activity: my ListView is covered by a "empty" screen (background color in my application) and it disappears.

1. Second Activity: I press the Up Home Button - the keyboard disappears properly.
2. (back to) First Activity: my ListView is covered by a "empty" space (background color in my application). And it disappears (this is the same height of the SoftKeyboard, no possible doubt!)

I guess it's because I forced the keyboard to appear in my first activity although I also forced the keyboard to hide when I go the second, but how can I resolve the "empty" space when I return to the first activity?


Summary
1) A activity => press item in menu > view collapse > show the keyboard > tap text > send it > hide keyboard > launch B activity.
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
3) A activity => "empty" screen > screen disappears.

Any help will be very appreciate.
Thanks for your time.


EDIT
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.

Menu (ActionView)

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;
}  

OnActionExpandListener

@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 && 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;
}  

OnOptionsItemSelected

// 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();  

ActionView (Item + layout)

<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" />  

<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" >

     <requestFocus />
</EditText>

UPDATE

I see a lot of similar issues, with EditText in ActionBar which not makes the keyboard appear even the focus has set. I tried this again (even if I already tested several time):

/*
* 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 => requestFocus() + showSoftInput(SHOW_IMPLICIT/FORCED/HIDE_NOT_ALWAYS/HIDE_IMPLICIT_ONLY)

It seems with me, only SHOW_FORCED|HIDE_IMPLICIT_ONLY can force the keyboard to show automatically when the view collapse. After this, in all versions, I must to make a hideSoftInputFromWindow to 0 for hiding it.
BUT this undisplays the keyboard even if the edittext is pressed, so I added an ClickListener to force the keyboard to show again (this happens only on lower versions).


UPDATE2:
It's clearly weird, when I try to make a little Thread like I saw in many SO answers (with/without ABS), nothing happens in lower versions.
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:

// 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();  

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.

NOTE: my tests were on (emulator:) GalaxyNexus, NexusS, NexusOne and (real devices:) Samsung GalaxySL (2.3.6) and Nexus4 (4.4).

If someone can help me with this ugly situation. Thanks in advance.

like image 266
Blo Avatar asked Dec 13 '13 05:12

Blo


People also ask

How do I turn off soft keyboard on Android?

You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow , passing in the token of the window containing your focused view. This will force the keyboard to be hidden in all situations. In some cases, you will want to pass in InputMethodManager.

What is soft keyboard in Android?

The Android system shows an on-screen keyboard—known as a soft input method—when a text field in your UI receives focus.


2 Answers

Did you tried this one?

setContentView(R.layout.activity_direction_3);
    getWindow().getDecorView().setBackgroundColor(
            android.R.color.transparent);
like image 157
Bhavesh Hirpara Avatar answered Nov 03 '22 14:11

Bhavesh Hirpara


Remove Translucent theme:

android:theme="@style/Theme.AppCompat.Translucent" 

and use your Activity in manifest.xml as:

<activity
     android:name=".SearchActivity"
     android:screenOrientation="portrait">
like image 26
Kuldip Sharma Avatar answered Nov 03 '22 15:11

Kuldip Sharma