0

I have tried to build a test application that merely relays a toast message when configuration changes happen. (well thats what its suppose to do, it doesn't work) The purpose in the end is to detect if the user places the tablet into a keyboard cradle or removes it from one. my manifest and main activity are below.. I thought this code would fire a toast when ever the tablet has a configuration change to the uiMode or external keyboard.. but nothing is happening when i dock/undock it.. please help

my manifest:

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk android:minSdkVersion="12" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".UiModeTestActivity"
        android:configChanges="keyboard|uiMode"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

my java:

   package com.eliddell;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.Toast;

public class UiModeTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      Toast.makeText(getApplicationContext(), "new config:"+newConfig, Toast.LENGTH_LONG).show();
    }
}
erik
  • 4,946
  • 13
  • 70
  • 120

1 Answers1

0

Your code looks fine, but...

As source says:

/**
 * The kind of keyboard attached to the device.
 * One of: {@link #KEYBOARD_NOKEYS}, {@link #KEYBOARD_QWERTY},
 * {@link #KEYBOARD_12KEY}.
 */
public int keyboard;

So, I think keyboard will change if KEYBOARD_QWERTY change to KEYBOARD_12KEY

Another part of source says:

  /**
     * A flag indicating whether any keyboard is available.  Unlike
     * {@link #hardKeyboardHidden}, this also takes into account a soft
     * keyboard, so if the hard keyboard is hidden but there is soft
     * keyboard available, it will be set to NO.  Value is one of:
     * {@link #KEYBOARDHIDDEN_NO}, {@link #KEYBOARDHIDDEN_YES}.
     */
    public int keyboardHidden;

So, maybe instead of android:configChanges="keyboard|uiMode" you would try android:configChanges="keyboardHidden|uiMode"

Unfortunately I haven't adapter to plug keyboard to the my device and check my theory. So try it!

pleerock
  • 18,322
  • 16
  • 103
  • 128