Solved

Keyboard

Posted on 2012-03-16
19
792 Views
Last Modified: 2012-03-23
Is there a way to have a button to check if the Android keyboard is showing and then toggle the keyboard (Show & Hide).


Here's what I'm looking for:

onClick {
  if(keyboard==showing)
       hide keyboard
  else
       show keyboard
}
0
Comment
Question by:InfoTechEE
  • 10
  • 9
19 Comments
 
LVL 8

Expert Comment

by:santhanasamy
Comment Utility
Hi  InfoTechEE,

Create a layout with EditText and a ToggleButton

onCreate(){
.....
mToggleButton = (ToggleButton) findViewById(R.id.togglebutton);
mToggleButton .setTextOn("HIDE");
mToggleButton .setTextOff("SHOW");

mEditText = (EditText) findViewById(R.id.editText);
mToggleButton .setOnClickListener( new OnClickListener(){
              public void onClick(View aView){
                             if(mToggleButton.isChecked()){

                                                mToggleButton..postDelayed(new Runnable() {
                                                            public void run() {
                                                                    InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                                                                    keyboard.showSoftInput(mEditText , 0);
                                                            }
                                                      }, 200
                                                );
                            } else {
                                                mToggleButton..postDelayed(new Runnable() {
                                                            public void run() {
                                                                    InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                                                                    keyboard.hideSoftInputFromWindow(mEditText.getWindowToken() , 0);
                                                            }
                                                      }, 200
                                                );
      
                 }
              }
});

.....

}

If you press the toggle button it will show and hide the Key pad....
0
 

Author Comment

by:InfoTechEE
Comment Utility
When I run it through the imulator it crashes and I have to force close it, and I think I know why.

It's not a button, that I want to toggle ON/OFF the keyboard. It's an ImageView -- image of a small keyboard.

It seems like you cannot say:

mToggleButton = (ToggleButton) findViewById(ImageView in Here);
0
 
LVL 8

Expert Comment

by:santhanasamy
Comment Utility
Hi  InfoTechEE,

In you application you are using keyboard image view to show/hide the keypad. But the purpose of key pad is to enter some data into the EditField.. So i guess at least you should have one edittext in your screen.....

You can use the same code snippet....Instead of using ToggleButton just use the Image view that you are using currently....

Set the click listener to the image view (Better to use image button)....

While dealing with key pad we need to associate some view( EditText.....) with the InputMethodManager.

The reason is the data entered through the key board should be displayed in the UI.
0
 

Author Comment

by:InfoTechEE
Comment Utility
I definitely have an edittext and next to it I have an ImageView (R.id.keyboard) -- image of a small keyboard.

The code I tried doesn't work:

mToggleButton = (ImageView) findViewById(R.id.keyboard)
mToggleButton.setTextOn("Hide");
mToggleButton.setTextOff("Show");

setTextOn and setTextOff and isChecked are underlined red saying these methods are unsupported by ImageView.
0
 

Author Comment

by:InfoTechEE
Comment Utility
I also tried:

mToggleButton = (ToggleButton) findViewById(R.id.keyboard) but that crashes.
0
 
LVL 8

Expert Comment

by:santhanasamy
Comment Utility
Hi  InfoTechEE,


ImageView don't have the following method setTextOn("Hide"),setTextOff("Show");
These methods are in Toggle button....
______________________________________________________________________________________________

Add this code for Imagebutton into your layout.xml file.

<ImageButton
          android:id="@+id/keyboard_image_button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@drawable/keyboard_selector">
</ImageButton>
------------------------------------------------------------------------------------------------------------------------------------------------------

In your java code  add following code

mKeyPadButton = (ImageButton) findViewById(R.id.keyboard_image_button)

------------------------------------------------------------------------------------------------------------------------------------------------------
Add this selector file in your drawable folder....

Use any 2 image to represent key pad visible and hide state with the following names...
key_pad_on,key_pad_off

keyboard_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">    
<!-- pressed -->    
<item android:state_checked="true"   android:drawable="@drawable/key_pad_on" />
<!-- default/unchecked --> </selector>
 <item android:drawable="@drawable/key_pad_off" />
------------------------------------------------------------------------------------------------------------------------------------------------------
0
 

Author Comment

by:InfoTechEE
Comment Utility
Is it not possible for this to work for ImageView? ImageButton looks really weird for my app, but I decided to give it a try anyways.

The code I'm trying to use is below. mKeyPadButton.isChecked() is underlined red because isChecked is not an available method for mKeyPadButton.

final ImageButton mKeyPadButton = (ImageButton) findViewById(R.id.imageButton12);
mKeyPadButton.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
            
                        if(mKeyPadButton.isChecked())
                    {
                          mKeyPadButton.postDelayed(new Runnable()
                          {
                                public void run()
                                {
                                InputMethodManager keyboard = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                                keyboard.showSoftInput(txtInput , 0);
                                }
                          }, 200);
                    }
                    else
                    {
                          mKeyPadButton.postDelayed(new Runnable()
                          {
                                public void run()
                                {
                                InputMethodManager keyboard = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                                keyboard.hideSoftInputFromWindow(txtInput.getWindowToken() , 0);
                                }
                          }, 200);
                    }
                        
                  }
              
        });
0
 
LVL 8

Accepted Solution

by:
santhanasamy earned 500 total points
Comment Utility
Hi  InfoTechEE,

Now i have used Image view instead if ImageButton.Change the code accordingly ....

private boolean mIsKeyPadButtonChecked = false;
private ImageView mKeyPadButton;
onCreate(){
mKeyPadButton = ( ImageView)findViewById(R.id.image_toggle_Button);
}
@Override
public void onClick(View v) {

mIsKeyPadButtonChecked  = !mIsKeyPadButtonChecked ;
mKeyPadButton.setActivated(mIsKeyPadButtonChecked);

if(mIsKeyPadButtonChecked )
{
mKeyPadButton.postDelayed(new Runnable()
{
public void run()
{
InputMethodManager keyboard = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
keyboard.showSoftInput(txtInput , 0);
}
}, 200);
}
else
{
mKeyPadButton.postDelayed(new Runnable()
{
public void run()
{
InputMethodManager keyboard = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(txtInput.getWindowToken() , 0);
}
}, 200);
}
}
});


keyboard_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item android:state_activated="true" android:drawable="@drawable/key_pad_on" />
<!-- default/unchecked -->
<item android:drawable="@drawable/key_pad_off" />
</selector>

Add this code for ImageView into your layout.xml file.

<ImageView android:id="@+id/keyboard_image_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/keyboard_selector">
</ImageView >
0
 

Author Comment

by:InfoTechEE
Comment Utility
mKeyPadButton.setActivated(mIsKeyPadButtonChecked);

setActivated is underlined red. Error message says -- "The method setActivated(boolean) is undefined for the type ImageView"

What is the point of setActivated or what role does it play here? I commented it out, and it works great without except if the keyboard is already showing, clicking on it does absolutely nothing the first time. All consequent clicks act normal, but the first one does nothing if keyboard is already showing.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 8

Expert Comment

by:santhanasamy
Comment Utility
Hi InfoTechEE,

1)  
I  used  Android4.0 (API level:14) to test my code. In menifest i used this...
<use-sdk android:minSdkVersion="14">This is working perfectly to me in this android version.
In the recent android version for ImageView this method support might be added .
I think you are using older sdk  version like android2.3(API Level: 9) (<use-sdk android:minSdkVersion="9">)For ImageView this method support is not there in older version.That is why it is throwing exception in older version.
I verified this code in the android:4.0. It is working perfectly to me.

2)
I have used View.setActivated(boolean) just to indicate the state change of the keypad button.
If you gothrough the android doc's you will come to know the difference between View.setSelected() and View.setActivated().
0
 

Author Comment

by:InfoTechEE
Comment Utility
That's the reason. I'm using 2.1. The buttons and EditText fields look nicer...rounder, and the EditText background is white. Just some of the preferences. Anyways, no big deal. I tried with 4.0 and the setActivated feature is working, but I'm not seeing any difference.

Is there an if statement I can run to check if the keyboard is already displayed? I ask because the keyboard button doesn't do anything the first time it's clicked if the keyboard is already displaying.
0
 
LVL 8

Expert Comment

by:santhanasamy
Comment Utility
Hi InfoTechEE,

Put his code at the end of onCreate() ....So while launching first time it will check the key board. If it is active it will hide the keyboard....

InputMethodManager keyboard = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
if(keyboard.isActive()){
   keyboard.hideSoftInputFromWindow(txtInput.getWindowToken() , 0);          
}
0
 

Author Comment

by:InfoTechEE
Comment Utility
Just to make sure that I'm understanding the code correctly: At first mIsKeyPadButtonChecked is false. onClick turns it true. if its true, show keyboard. Since it is true shows the keyboard. Skip the else statement. Check if keyboard isActive (which it now is), at this point it should be hiding the keyboard right? Yet it doesn't.

Sorry for a million questions, but I'm trying to actually learn this material instead of just saying OK it works.

onClick code:
{
                  mIsKeyPadButtonChecked = !mIsKeyPadButtonChecked;
                  //mKeyPadButton.setActivated(mIsKeyPadButtonChecked);
                  if(mIsKeyPadButtonChecked)
                  {
                        mKeyPadButton.postDelayed(new Runnable()
                        {
                              public void run()
                              {
                                    InputMethodManager keyboard = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                                    keyboard.showSoftInput(txtInput , 0);
                              }
                        }, 200);
                  }
                  else
                  {
                        mKeyPadButton.postDelayed(new Runnable()
                        {
                              public void run()
                              {
                                    InputMethodManager keyboard = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                                    keyboard.hideSoftInputFromWindow(txtInput.getWindowToken() , 0);
                              }
                        }, 200);
                  }
                  InputMethodManager keyboard = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                  if(keyboard.isActive())
                  {
                        keyboard.hideSoftInputFromWindow(txtInput.getWindowToken(), 0);
                  }
            }
0
 
LVL 8

Expert Comment

by:santhanasamy
Comment Utility
Hi InfoTechEE,

   InputMethodManager keyboard = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                  if(keyboard.isActive())
                  {
                        keyboard.hideSoftInputFromWindow(txtInput.getWindowToken(), 0);
                  }
            }

This code snippet i asked to use inside the end of onCreate... While launching the activity if you want to hide the keypad we can use this.....
(keyboard.isActive()) will return true if any  view is the currently active view for the keyboardinput

(keyboard.isActive(View)) will return true if the given view is the currently active view for the keyboardinput . Here you can pass your editText reference. IF it returns true,we can come to know that keyboard is associate with this  edittext. Based on that we can hide the keyboard.....(Actually I didn't tried this one)....

In your cause you no need to use that .......

Other than that this code is working properly to me.....
0
 

Author Comment

by:InfoTechEE
Comment Utility
I'm so sorry, I read your previous post incorrectly. I read it as if to put that code in the end of my onClick method instead of onCreate. OK, let me correct my mistake, and get back to you.
0
 

Author Comment

by:InfoTechEE
Comment Utility
OK, so I did as suggested, but it doesn't do what I need it to do.

Basically the keyboard image -- when clicked does what it is suppose to do. However, if I click on the txtInput first -- the keyboard automatically displays. At that point, if I click the keyboard button, it doesn't do anything the first time. Then if you click on it again, the keyboard disappears.
0
 
LVL 8

Expert Comment

by:santhanasamy
Comment Utility
Hi InfoTechEE,

1) I got your problem..... Then  initially while launching  your activity show the keypad by using following code


onCreate(){

...........
mEditText .postDelayed(new Runnable()
                        {
                              public void run()
                              {
                                    InputMethodManager keyboard = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                                    keyboard.showSoftInput(txtInput , 0);
                                    mIsKeyPadButtonChecked = true;
                  //mKeyPadButton.setActivated(mIsKeyPadButtonChecked);//For Android 4.0

                              }
                        }, 200);

   }
}

and remove the following code from the onCreate method
 if(keyboard.isActive())
                  {
                        keyboard.hideSoftInputFromWindow(mEditText .getWindowToken(), 0);
                  }


2) Other wise set the touch listener for the EditText........If the user touches this view set the
flag "mIsKeyPadButtonChecked  = true"
0
 

Author Comment

by:InfoTechEE
Comment Utility
I implemented #2. It still has one small bug, but I don't think it can be fixed using our logic (unless there is an explicit way of checking if they keyboard is displaying).

The bug is when you touch the keyboard, the flag is set to TRUE. If you click on the back button instead of our keyboard button, the flag is still TRUE. So when you click on the keyboard the first time nothing happens. The second click on keyboard image finally gets it working.

I think it's OK as is, unless you can think of something? Let me know, thanks.
0
 
LVL 8

Expert Comment

by:santhanasamy
Comment Utility
Hi InfoTechEE,

I too thought the same ....
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Working settings for French ISP Orange "Prêt à Surfer" SIM cards for data connections only. Can't be found anywhere else !
A short article about a problem I had getting the GPS LocationListener working.
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now