How can i know if the keyboard is open or not?
This is available on the Configuration class. You can get the current Configuration via getResources().getConfiguration() from your Activity or other Context.
That way =)
public boolean isKeyboardVisible(){
// Checks whether a hardware keyboard is visible
if (getResources().getConfiguration().hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
return true;
} else if (getResources().getConfiguration()..hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
return false;
}
}
Related
How can I keep the keyboard open after the user tap on the "Return" key on the soft keyboard?
I'm calling the focus method on "returnPress" event, which works fine on IOS but not on android:
text() {
let textFieldElement = <TextField>this.textField.nativeElement;
textFieldElement.focus();
}
So turns out I need to overrde "onEditorAction" method on the "OnEditorActionListener" like this:
let tv = <TextField>this.textField.nativeElement;
if (tv.android) {
tv.android.setOnEditorActionListener(new android.widget.TextView.OnEditorActionListener({
onEditorAction: function (callbackType, result) {
if (result == android.view.inputmethod.EditorInfo.IME_ACTION_DONE) {
// do whatever you want when user presses return
}
return true;
}
}));
}
This is something I've been struggling with for quite some time. A lot of Chinese Android manufacturers create Android TV boxes that are controlled using a remote with keypad controls.
I'm looking for a definitive way of detecting if the device uses navigational controls or if it is in fact using touch screen input. Certain devices might emulate touch screen input as mouse input as well, so it's kind of tricky.
Any ideas?
Please read about input controls in the following articles:
http://developer.android.com/training/keyboard-input/index.html
http://developer.android.com/training/keyboard-input/navigation.html
http://developer.android.com/training/tv/optimizing-navigation-tv.html
To see at run-time what types of navigation a user has available, use the Configuration class.
Configuration configuration = context.getResources().getConfiguration();
if (Configuration.NAVIGATION_NONAV == configuration.navigation) {
// Device has no navigation facility other than using the touchscreen.
} else if (Configuration.NAVIGATION_DPAD == configuration.navigation) {
// Device has a directional-pad (d-pad) for navigation.
} else if (Configuration.NAVIGATION_TRACKBALL == configuration.navigation) {
// Device has a trackball for navigation.
} // ... etc
Based on Jozua's answer, I created this simple method which can be used to determine if a device uses navigation controls from a number of factors. The code is written in a way that attempts to fail early.
/**
* Determines if the device uses navigation controls as the primary navigation from a number of factors.
* #param context Application Context
* #return True if the device uses navigation controls, false otherwise.
*/
public static boolean usesNavigationControl(Context context) {
Configuration configuration = context.getResources().getConfiguration();
if (configuration.navigation == Configuration.NAVIGATION_NONAV) {
return false;
} else if (configuration.touchscreen == Configuration.TOUCHSCREEN_FINGER) {
return false;
} else if (configuration.navigation == Configuration.NAVIGATION_DPAD) {
return true;
} else if (configuration.touchscreen == Configuration.TOUCHSCREEN_NOTOUCH) {
return true;
} else if (configuration.touchscreen == Configuration.TOUCHSCREEN_UNDEFINED) {
return true;
} else if (configuration.navigationHidden == Configuration.NAVIGATIONHIDDEN_YES) {
return true;
} else if (configuration.uiMode == Configuration.UI_MODE_TYPE_TELEVISION) {
return true;
}
return false;
}
I have tested this on numerous phones, tablets, emulator configurations and Google TV. A number of devices are controlled using a remote control and a USB mouse. I haven't yet tested whether it works as expected on such devices.
I am building a firmware for visually disabled people. I have to disable touch screen as and when the external key board is connected. And toggle it with Alt + T . For this I have a static volatile flag in View class called misTouchScreenEnabled. Upon plug in of external keyboard I return false from dispatchTouchEvent(). In View Class :
/*
* A register to hold the status of touch screen
*
*{#hide}
*/
public static volatile boolean misTouchScreenEnabled = false;
public boolean dispatchTouchEvent(MotionEvent event) {
if(event == null)
{
return false;
}
Log.d ("CnxsTA","View :: Into dispatchTouchEvent");
// Added by Harsh Vardhan 05102013
Configuration config = getResources().getConfiguration();
if (config.keyboard != Configuration.KEYBOARD_NOKEYS)
{
// And if the device has a hard keyboard, even if it is
// currently hidden, don't pass the touch events to the view
if(!mIsTouchScreenEnabled)
{
Log.d ("CnxsTA","View :: Into dispatchTouchEvent :: Returning False");
return false;
}
}
// Added By Harsh Vardhan
// Some Other Code...
}
In Launcher I catch the ALT + T in dispatchTouchEvent() and toggle the flag using reflection:
case KeyEvent.KEYCODE_T:
{
//Added By Harsh Vardhan 31052013
Log.d ("CnxsTA","Launcher :: onKeyDown :: T pressed");
if ((event.getMetaState() & KeyEvent.META_ALT_ON) == KeyEvent.META_ALT_ON)
{
Object s;
try
{
Log.d ("CnxsTA","Launcher :: In onKeyDown :: In isAltPressed");
s = ToogleTouchScreen(Class.forName("android.view.View"), "mIsTouchScreenEnabled");
if (s instanceof Boolean)
{
boolean v = ((Boolean) s).booleanValue();
//do something
Log.d ("CnxsTA","Launcher :: onKeyDown :: In isAltPressed :: Toggling touchscreen enable flag :: " + v);
if (accessibilityEnabled)
{
if (mTts != null)
{
if (v)
mTts.speak("Touch Screen enabled", TextToSpeech.QUEUE_FLUSH, null);
else
mTts.speak("Touch Screen disabled", TextToSpeech.QUEUE_FLUSH, null);
}
}
}
else if (s == null)
{
//do something
Log.d ("CnxsTA","Launcher :: onKeyDown :: S is Null");
}
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
//return super.dispatchKeyEvent(event);
return true;
//break;
//Added By Harsh Vardhan 31052013
}
This works well for the Launcher and its workspace, ie. the touch gets enabled and disabled when ALT + T is pressed. But the Panel in the bottom containing the back, home, recent apps and notifications remains unresponsive on touch and so is the UI in the application; though in Exploration Mode the Icons names are spoken on touch but the touch is not implemented. And it all retains to normal sate once the external keyboard is removed.
I think the reason behind this could be that the StatusBar Class and Other Classes extending the View which are taking the flag to be false only has cached the whole class or the variables and methods and is not referring to main memory where the flags have been toggled. I am aware that I have made the flag volatile.
I know that my flag gets toggle as I could confirm this from logcat. Please suggest me the direction I could solve this. Thanking in advance.
I wonder what is the value of SIM state returned by TelephonyManager.getSimState() when "airplane mode" is turned on? This seems to be not directly specified anywhere in the SDK specification.
Actually I need to get SIM operator code (i.e. MCC+MNC) using getSimOperator() method, but JavaDoc states that to use that method:
SIM state must be SIM_STATE_READY
UPDATE
I tested it under emulator and it returns SIM_STATE_UNKNOWN (which is described by javadoc as a "transition between states") after airplane mode is switched on. However I would like to know whether it is a common behavior on Android phones?
After searching Android 4.1 sources I found the following code in one of the private classes com.android.internal.telephony.IccCard:
public State getState() {
if (mState == null) {
switch(mPhone.mCM.getRadioState()) {
/* This switch block must not return anything in
* State.isLocked() or State.ABSENT.
* If it does, handleSimStatus() may break
*/
case RADIO_OFF:
case RADIO_UNAVAILABLE:
case SIM_NOT_READY:
case RUIM_NOT_READY:
return State.UNKNOWN;
case SIM_LOCKED_OR_ABSENT:
case RUIM_LOCKED_OR_ABSENT:
//this should be transient-only
return State.UNKNOWN;
case SIM_READY:
case RUIM_READY:
case NV_READY:
return State.READY;
case NV_NOT_READY:
return State.ABSENT;
}
} else {
return mState;
}
Log.e(mLogTag, "IccCard.getState(): case should never be reached");
return State.UNKNOWN;
}
So State.UNKNOWN would be returned whenever radio state is one of RADIO_OFF or RADIO_UNAVAILABLE. Then State.UNKNOWN will be converted to SIM_STATE_UNKNOWN constant by TelephonyManager.getSimState() method.
As the conclusion: when airplane mode is turned on getSimState will return SIM_STATE_UNKNOWN.
yes, this is the common behavior on android phones.
see the implementation of the getSimState() method from the TelephonyManager class:
public int getSimState() {
String prop = SystemProperties.get(TelephonyProperties.PROPERTY_SIM_STATE);
if ("ABSENT".equals(prop)) {
return SIM_STATE_ABSENT;
}
else if ("PIN_REQUIRED".equals(prop)) {
return SIM_STATE_PIN_REQUIRED;
}
else if ("PUK_REQUIRED".equals(prop)) {
return SIM_STATE_PUK_REQUIRED;
}
else if ("NETWORK_LOCKED".equals(prop)) {
return SIM_STATE_NETWORK_LOCKED;
}
else if ("READY".equals(prop)) {
return SIM_STATE_READY;
}
else {
return SIM_STATE_UNKNOWN;
}
}
is there a way to tell if the softkeyboard is shown in an activity or not?
I tried
InputMethodManager manager = (InputMethodManager)
getSystemService(getApplicationContext().INPUT_METHOD_SERVICE);
manager.isActive(v)
but isActive returns false only before the first time the keyboard is shown, but if the kb appears and then dismissed, isActive returns true also.
so is there any other method to check for this issue.
thanks
According to this POST
You cannot detect if soft keyboard is shown or not, but you can indirectly know that a soft key board is shown by knowing that the View of your activity is resized.
Imagine you have a ListView and at the bottom an EditText, you want to go to the bottom of the list when a soft keyboard is shown after user clicks the EditText.
You need to implement a subclass of ListView, then use it in your ListActivity or Activity or View.
public class ThreadView extends ListView {
public ThreadView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
#Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld) {
super.onSizeChanged(xNew, yNew, xOld, yOld);
if (yOld > yNew) {
setSelection(((ListAdapter) getAdapter()).getCount() - 1);
}
}
}
Hope this helps
PS. "check Configuration Changes" only works for hand keyboard.
You can detect the state AND coordinates of the software keyboard, using dumpsys shell command.
Because dumpsys requires permission.android.DUMP, which is a system application permission, you have two options: 1. use a rooted device to grant this permission. 2. override the problem using adb as described in my other answer.
Now, run the following command: dumpsys window InputMethod | grep "mHasSurface" to get the data you are looking for.
This is my idea. It is untested.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks whether a keyboard is available which is not hard keyboard
if ((newConfig.keyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO)&&(newConfig.keyboardHidden != Configuration.KEYBOARDHIDDEN_NO)) {
Toast.makeText(this, "soft keyboard visible", Toast.LENGTH_SHORT).show();
} else if (newConfig.keyboardHidden == Configuration.KEYBOARDHIDDEN_YES) {
Toast.makeText(this, "soft keyboard hidden", Toast.LENGTH_SHORT).show();
}
}
Please check Configuration Changes for your Activity
This for your AndroidManifest.xml
and this for your Activity class http://developer.android.com/reference/android/app/Activity.html#onConfigurationChanged(android.content.res.Configuration)
You will need to #Override the public method onConfigurationChanged(android.content.res.Configuration) of your Activity to be able to handle, for example, these values:
hardKeyboardHidden,
keyboard,
keyboardHidden
For all possible values check http://developer.android.com/reference/android/content/res/Configuration.html
You will see there something like this:
HARDKEYBOARDHIDDEN_NO
HARDKEYBOARDHIDDEN_UNDEFINED
HARDKEYBOARDHIDDEN_YES
KEYBOARDHIDDEN_NO
KEYBOARDHIDDEN_UNDEFINED
KEYBOARDHIDDEN_YES
KEYBOARD_12KEY
KEYBOARD_NOKEYS
KEYBOARD_QWERTY
KEYBOARD_UNDEFINED
Also there you will be able to read something like this:
public int hardKeyboardHidden // A flag indicating whether the hard keyboard
// has been hidden.
public int keyboard The kind of keyboard attached to the device.
public int keyboardHidden A flag indicating whether any keyboard is available.
UPDATE:
Here is a specific example of what I´m talking about:
http://developer.android.com/guide/topics/resources/runtime-changes.html
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
// Checks whether a hardware keyboard is available
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();
}
}