7

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

Community
  • 1
  • 1
Tikiboy
  • 892
  • 10
  • 20
  • Please bear in mind that there are devices with hardware keyboards. If switching to listview is necessary in your design then it is probably as necessary when the user starts typing on a physical keyboard. Maybe you should rather implement something in the lines with [`AutoCompleteTextView`](http://developer.android.com/reference/android/widget/AutoCompleteTextView.html) than trying to detect the soft keyboard presence. – SnakE Jul 04 '11 at 14:30
  • The purpose of the listview is as a replacement to the dropdown menu of the autocompletetextview. Due to design constrains, i cant use the drop down menu. Instead, i have to implement the list of suggested text to appear on the listview instead. ;/ – Tikiboy Jul 04 '11 at 14:39
  • `AutoCompleteTextView` displays its drop-down when a certain number of characters is typed. I think you should implement your control similarly so it works with any type of keyboard. On a separate note, soft input covers the whole screen in landscape, so your list view won't be visible regardless. – SnakE Jul 04 '11 at 14:45
  • The app is only available in portrait atm. Yeah, currently the listview will only begin filtering when at least 2 characters are typed. ill keep in mind on the hardware keyboard, good point to note, thanks! – Tikiboy Jul 04 '11 at 15:22

2 Answers2

2

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

Community
  • 1
  • 1
Blundell
  • 75,855
  • 30
  • 208
  • 233
  • 1
    hurm, i was thinking of implementing the catch when back is pressed, however, i need to take into account other situations where the on-screen-keyboard may disappear through other methods....also have to keep in mind of the presence of h/w keyboards that snake pointed out. Currently im considering implementing a really bad hack, onClicks everywhere that will forcibly take focus away from the edittext, which subsequently will cause the linearlayout to reappear again. In addition, ill also take the focus away when the keyboard disappears. Still thinking tho, the hack is gonna make things ugly. – Tikiboy Jul 04 '11 at 16:24
1

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

Community
  • 1
  • 1
Marmoy
  • 8,009
  • 7
  • 46
  • 74