Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: When the on-screen keyboard appears and disappears, are there any listeners that are automatically called?

I was wondering if there is any way to be notified automatically by android when the on-screen keyboard is shown and when it disappears.

For example when we click on a edittext, the ime appears. Will there be any event calls? And when it disappears when we press back, similarly will there be any even calls?

I found this thread Android: which event fires when on screen keyboard appears? , however no answers have been reached yet.

The purpose is because i need an event to automatically manipulate visibility. I have an activity with an edittext on the top of the screen, below it, a listview and a linearlayout which are sitting on top of each other. To control what the user sees, i manipulate the visibility. By default, the linearlayout is shown initially, however, when the user is entering text, the listview should be shown instead. The listview should disappear when the user has finished typing, which in this case, the on-screen-keyboard will be closed.

I tried acomplishing the change of visibility using onFocusChange, however, even when the on-screen keyboard disappears, the edittext still retains focus and the linearlayout never reappears.

Below is my implementation of the onFocusChange

@Override
public void onFocusChange(View v, boolean hasFocus) 
{
    if(v.getId()==R.id.search_screen_keyword_textbox)
    {
        if(hasFocus)
        {
            filterSection.setVisibility(View.GONE);
            autoComSection.setVisibility(View.VISIBLE);
        }
        else
        {
            filterSection.setVisibility(View.VISIBLE);
            autoComSection.setVisibility(View.GONE);
        }
    }       
    else if(v.getId()==R.id.search_screen_location_textbox)
    {
        if(hasFocus)
        {
            filterSection.setVisibility(View.GONE); 
            autoComSection.setVisibility(View.VISIBLE);
        }
        else
        {
            filterSection.setVisibility(View.VISIBLE);
            autoComSection.setVisibility(View.GONE);
        }
    }
    else
    {
        filterSection.setVisibility(View.VISIBLE);
        autoComSection.setVisibility(View.GONE);
    }
}

If anyone has any idea about it do let me know. :D

like image 629
Tikiboy Avatar asked Jul 04 '11 13:07

Tikiboy


2 Answers

You can catch the back button when in an edittext, this is what would make the keyboard disappear. Using this method:

 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
   if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
           // Do your thing here
           return false;
   }
   return super.dispatchKeyEvent(event);
  }

Search is great: onKeyPreIme or Android API

like image 103
Blundell Avatar answered Oct 23 '22 05:10

Blundell


It seems that this thread has a solution using onConfigurationChanged: How to capture the "virtual keyboard show/hide" event in Android?

like image 32
Marmoy Avatar answered Oct 23 '22 05:10

Marmoy