1

I have an editText and a toolbar. When I focus the editText I hide the toolbar and show the softKeyboard so the user can enter text.

The problem is when pressing the back key, I added an on back pressed method to show again the toolbar:

@Override
    public void onBackPressed() {
        //show toolbar
        toolbar_bottom.setVisibility(View.VISIBLE);

    }

But it first hides the keyboard and is until the second press when my toolbar is visible again. Is there a way for hiding keyboard and making visible the toolbar with just one press?

Gustavo Serna
  • 117
  • 1
  • 12
  • you can wait a bit before everything completes and then show the toolbar ( Handler handler = new Handler(); handler.postDelayed(new Runnable() { public void run() { toolbar_bottom.setVisibility(View.VISIBLE); } }, 500);) – Tasos Sep 05 '16 at 03:03
  • @Tasos like this? `@Override public void onBackPressed() { //show toolbar Handler handler = new Handler(); handler.postDelayed(new Runnable() { public void run() { toolbar_bottom.setVisibility(View.VISIBLE); } }, 500);) }` – Gustavo Serna Sep 05 '16 at 03:06
  • yes stick the code i provided inside onBackPressed() { } -- that should fire up the toobar visible in half a second. Thats about the time the keybard needs to hide – Tasos Sep 05 '16 at 03:13
  • @Tasos it didnt work – Gustavo Serna Sep 05 '16 at 03:18
  • go for 1 second (1000) – Tasos Sep 05 '16 at 03:19
  • @Tasos neither, it works but when I click por the second time, just as the problem I described in my question – Gustavo Serna Sep 05 '16 at 03:20
  • ok, you will have to add all the relevant code in Q to check. I suspect The problem lies somewhere else – Tasos Sep 05 '16 at 03:24

3 Answers3

0

you can override the onKeyDown method

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            HideYourToolbar();
            HideYourSoftKeyboard(); 
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
FrankOy
  • 31
  • 2
0

It's because when the Soft Keyboard is visible, your onBackPressed won't be triggered by pressing Back button because the Keyboard will take the event prior to your Activity and consumes the event.

I think that behavior is very common and Normal in Android Apps and most people will accept that without problem.

Anyway, if you wish to do it in one-click, you have to catch the event when [Keyboard becomes invisible].

This is somewhat tricky in Android.
I think there is no perfect solution, but there are some workarounds.
Check this link.

Community
  • 1
  • 1
Suhyeon Lee
  • 569
  • 4
  • 18
0

I already found the solution to my problem. Hope it helps someone else.

    public class CustomET extends EditText {

        Context context;

        public CustomEditText(Context context, AttributeSet attrs) {
            super(context, attrs);
            this.context = context;
        }

        @Override
        public boolean onKeyPreIme(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                InputMethodManager mgr = (InputMethodManager)         
                    context.getSystemService(Context.INPUT_METHOD_SERVICE);
                mgr.hideSoftInputFromWindow(this.getWindowToken(), 0);
//show toolbar

            }
            return false;
    }

My xml:

<com.limecream.CustomEditText
     android:id="@+id/CEditText"
     android:layout_height="wrap_content"
     android:layout_width="match_parent"/> 

My activity

public class MainActivity extends Activity {
   private CustomEditText editText;

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      editText = (CustomEditText) findViewById(R.id.CEditText);
   }
}
Gustavo Serna
  • 117
  • 1
  • 12