These code examples were ranked by Codota’s semantic indexing as the best open source examples for Activity getCurrentFocus method.
cDef.set(Calendar.MINUTE, 0); return cDef; } public static void hideInput(Activity activity) { if (activity.getCurrentFocus() != null) { InputMethodManager inputMethodManager = (InputMethodManager) activity .getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(activity .getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } } public static Uri saveLocalFile(Context context, int index) { String filename = DownloadUtils.SAVE_PATH + String.format("%d.png", index); File fLocal = new File(filename); if (fLocal.exists()) { return Uri.fromFile(new File(filename));
1: LayoutInflater inflater = getLayoutInflater();
2: View dialoglayout = inflater.inflate(R.layout.radio_btn, (ViewGroup) getCurrentFocus());
3: RadioButton default_btn = (RadioButton) dialoglayout.findViewById(R.id.default_btn);
4: RadioButton external_btn = (RadioButton) dialoglayout.findViewById(R.id.external_btn);
Try this..
Change this..
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getApplicationContext());
to
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
AlarmActivity.this);
EDIT
radio_btn.xml
<?xml version="1.0" encoding="UTF-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/status_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical" >
<RadioButton
android:id="@+id/default_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Default music" />
<RadioButton
android:id="@+id/external_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="External card music" />
</RadioGroup>
JAVa
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.radio_btn, (ViewGroup) getCurrentFocus());
RadioButton default_btn = (RadioButton) dialoglayout.findViewById(R.id.default_btn);
RadioButton external_btn = (RadioButton) dialoglayout.findViewById(R.id.external_btn);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
AlarmActivity.this);
Log.d("TAG","button inside mobile");
alertDialogBuilder
//.setMessage("Click yes to exit!")
.setCancelable(false)
.setView(dialoglayout);
.setPositiveButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
AlarmActivity.this.finish();
}
});
alertDialogBuilder.show();
5: if(mActionModeIsActive && isVisible) {
6: InputMethodManager mImm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
7: mImm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
8: isVisible = false;
9: return true;
You should try to override the Back Key
hardware, and handle the expected behaviour with a boolean
as follows:
// boolean isVisible to retrieve the state of the SoftKeyboard
private boolean isVisible = false;
// isVisible becomes 'true' if the user clicks on EditText
// then, if the user press the back key hardware, handle it:
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// check isVisible
if(isVisible) {
// hide the keyboard
InputMethodManager mImm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
mImm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
isVisible = false;
} else {
// remove the CAB
mActionMode.finish();
}
}
return false;
}
Another solution might be to call dispatchKeyEvent
method which is still called when the CAB
is displayed:
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
// check CAB active and isVisible softkeyboard
if(mActionModeIsActive && isVisible) {
InputMethodManager mImm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
mImm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
isVisible = false;
return true;
// Maybe you might do not call the 'else' condition, anyway..
} else {
mActionMode.finish();
return true;
}
}
return super.dispatchKeyEvent(event);
}
This should do the trick, but I have not tested it. Hope this helps.
Sources: How to Override android's back key when softkeyboard is open - Prevent to cancel Action Mode by press back button
12: // hide the keyboard
13: InputMethodManager mImm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
14: mImm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
15: isVisible = false;
16: } else {
You should try to override the Back Key
hardware, and handle the expected behaviour with a boolean
as follows:
// boolean isVisible to retrieve the state of the SoftKeyboard
private boolean isVisible = false;
// isVisible becomes 'true' if the user clicks on EditText
// then, if the user press the back key hardware, handle it:
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// check isVisible
if(isVisible) {
// hide the keyboard
InputMethodManager mImm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
mImm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
isVisible = false;
} else {
// remove the CAB
mActionMode.finish();
}
}
return false;
}
Another solution might be to call dispatchKeyEvent
method which is still called when the CAB
is displayed:
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
// check CAB active and isVisible softkeyboard
if(mActionModeIsActive && isVisible) {
InputMethodManager mImm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
mImm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
isVisible = false;
return true;
// Maybe you might do not call the 'else' condition, anyway..
} else {
mActionMode.finish();
return true;
}
}
return super.dispatchKeyEvent(event);
}
This should do the trick, but I have not tested it. Hope this helps.
Sources: How to Override android's back key when softkeyboard is open - Prevent to cancel Action Mode by press back button
3:
4: //check if no view has focus:
5: View v=this.getCurrentFocus();
6: if(v==null)
7: return;
You can lose focus by doing the following:
searchView.clearFocus();
You can also force hiding the keyboard on any event you want with the inputManager
.
For example:
InputMethodManager inputManager = (InputMethodManager) this
.getSystemService(Context.INPUT_METHOD_SERVICE);
//check if no view has focus:
View v=this.getCurrentFocus();
if(v==null)
return;
inputManager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
7: InputMethodManager inputMethodManager = (InputMethodManager) this
8: .getSystemService(Context.INPUT_METHOD_SERVICE);
9: inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus()
10: .getWindowToken(), 0);
11: }
Try this
private void showKeyboard(View view) {
InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(view, 0);
}
private void hideKeyboard() {
InputMethodManager inputMethodManager = (InputMethodManager) this
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus()
.getWindowToken(), 0);
}