1

Below is my code i am trying to display toast message on Keyboard on and off on device, my code running but i am unable to display toast message please tell me where is the issue.

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
            Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
        } else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
            Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
        }

    }
}


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.keyboard.MainActivity"
        android:configChanges="orientation|screenSize|keyboardHidden"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

below is manifest file code so please tell me where am doing wrong i am unable to display toast when i enable and disable keyboard.

W I Z A R D
  • 1,224
  • 3
  • 17
  • 44
user2794306
  • 175
  • 3
  • 5
  • 24
  • in your `Manifest` file, add this to your activity definition: `android:configChanges="keyboard|keyboardHidden"` and then try.. – M D Oct 14 '14 at 05:53
  • possible duplicate of [Any android event when keyboard slide out](http://stackoverflow.com/questions/5046748/any-android-event-when-keyboard-slide-out) – M D Oct 14 '14 at 05:57
  • have you tried putting debug points at your `toast`? Because I think it might not getting there.. – Darpan Oct 14 '14 at 07:17
  • yes but its not working when open and off Key .. – user2794306 Oct 14 '14 at 07:20

3 Answers3

0

Add this

<application  android:configChanges="keyboard|keyboardHidden" ... />

in your manifest.xml file

Edit: From here here i figured it out that this solution will not work for soft keyboard :(

Community
  • 1
  • 1
Waqar Khan
  • 468
  • 4
  • 18
0

If you want to keep the keyboard hidden you can use this in your Android Manifest XML file.

<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateHidden"/>

And if you want to hide the keyboard on touch outside the soft keyboard area you can use this in your java class.

EditText myEditText = (EditText) findViewById(R.id.myEditText);  
InputMethodManager imm = (InputMethodManager)getSystemService(
      Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

And you can try this link to detect the on/off of the soft keyboard. ref:http://www.phonesdevelopers.com/1758842/

Hope this Helps :)

Ankit Arora
  • 117
  • 4
0

You are checking hard keyboard state, which is the state of a real physical keyboard if it's present. You should check any keyboard state:

if (newConfig.keyboardHidden == Configuration.KEYBOARDHIDDEN_NO) {
    Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
else if (newConfig.keyboardHidden == Configuration.KEYBOARDHIDDEN_YES) {
    Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
}
Anton Savin
  • 40,838
  • 8
  • 54
  • 90