Pretty simple; The dialog is showing. I press the back button on the phone, nothing happens. I've tried this, but it never gets called:
static void ProgressDialog(Context context)
{
String text = context.getString(R.string.dialog_loading_video);
vDialog = new ProgressDialog(context)
{
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
// Nothing happening here!
}
return super.onKeyDown(keyCode, event);
}
};
vDialog = ProgressDialog.show(context, "", text);
vDialog.getWindow().setGravity(Gravity.TOP);
}
First, get rid of the second assignment to vDialog. Then, you need to make your dialog cancelable by calling setCancelable(boolean).
In line vDialog = ProgressDialog.show(context, "", text); you create new Dialog that doesn't have overriden onKeyDown() method. Replace this line with these code:
vDialog.setTitle("");
vDialog.setMessage(text);
vDoalog.show();
Related
I have an activity where the whole screen is dedicated to sending one message. Being one EditText on the top half, and the SoftKeyboard always visible on the bottom half.
When i press back, the SoftKeyboard hides and i have to press back again to leave the activity.
The behavior that i'm struggling to get is : finishing the activity right away when i press the back button, instead of hiding the keyboard.
You can find this behavior in the twitter app for example, when writing a new tweet.
I tried with overriding the onBackPressed() function, but seems like when the keyboard is visible, the function is not called.
#Override
public void onBackPressed() {
finish();
}
Any help would be really appreciated!
So after trying many things, here something that worked :
Subclass EditText and override the onKeyPreIme() function to send a call back.
Here's the code for the subclass :
OnKeyPreImeListener onKeyPreImeListener;
public void setOnKeyPreImeListener(OnKeyPreImeListener onKeyPreImeListener) {
this.onKeyPreImeListener = onKeyPreImeListener;
}
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
if(onKeyPreImeListener != null)
onKeyPreImeListener.onBackPressed();
Log.d(TAG, "HIDING KEYBOARD");
return false;
}
return super.dispatchKeyEvent(event);
}
public interface OnKeyPreImeListener {
void onBackPressed();
}
Then in your activity for each of your TextView :
EditTextGraphee.OnKeyPreImeListener onKeyPreImeListener =
new EditTextGraphee.OnKeyPreImeListener() {
#Override
public void onBackPressed() {
Log.d(TAG, "CALL BACK RECEIVED");
MyActivity.this.onBackPressed();
}
};
editText.setOnKeyPreImeListener(onKeyPreImeListener);
new answer:
so apparently you don't receive the onBackPressed callback, but that doesn't mean you can't detect the keyboard closing.
Using the technique described here: How to check visibility of software keyboard in Android?
you can detect when the keyboard open/close, so when the keyboard closes you call finish();
deprecated, original answer:
simply override the back press event in the activity:
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
I assume that since the soft keyboard is visible probably an edittext has a focus. So you can catch the back pressed event by adding an OnEditorActionListener on that EditText and finish activity.
yourEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP){
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK){
finish();
}
}
return false;
}
});
You nee to extend EdtText class and implement onKeyPreIme method.
public class MyEditText extends EditText {
/* Must use this constructor in order for the layout files to instantiate the class properly */
public MyEditText(Context context, AttributeSet attrs)
{
super(context, attrs);
// TODO Auto-generated constructor stub
}
#Override
public boolean onKeyPreIme (int keyCode, KeyEvent event)
{
// do your stuff here.
return true;
}
}
Override onBackPressed() method like this :
#Override
public void onBackPressed() {
hideKeyboard();
finish();
}
For hideKeyboard() function please search in the Internet .
I need to minimize the application when back button is pressed.
I use following code to catch hardware back button click event
help me with the code of minimize on back key pressed
#Override
public boolean onKeyDown(int keyCode, keyEvent event) {
switch(keyCode) {
case KeyEvent.KEYCODE_BACK;
//minimize application
return true;
}
return super.onKeyDown(keyCode, event);
}
I think that you need to treat back event as home event. The code below is how I emulate home pressed when User press back button:
public void minimizeApp() {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}
This is a simple code to minimize the application
#Override
public void onBackPressed() {
this.moveTaskToBack(true);
}
try this code, this will minimize Activity.
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{
this.moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
or
If you want to close the activity use this.finish() method to close the current running activity. instead of this.moveTaskToBack(true);
I want way to disable the Home Button & Back Button when a checkbox is checked in my application. I am on version 4.2.2. Here is my code, it does not work, the application stops when the box gets checked:
public void HardButtonOnClick(View v)
{
boolean checked1 = ((CheckBox) v).isChecked();
if(checked1)
{
SQLiteDatabase db;
db = openOrCreateDatabase("Saftey.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
ContentValues values = new ContentValues();
values.put("hardBtn", "YES");
db.update("Setting", values, "id = ?", new String[] { "1" });
Toast.makeText(this, "Hard Button Locked", Toast.LENGTH_LONG).show();
//SharedPreferences pref = getSharedPreferences("pref",0);
//SharedPreferences.Editor edit = pref.edit();
//edit.putString("hard","yes");
//edit.commit();
/* String Lock="yes" ;
Bundle bundle = new Bundle();
bundle.putString("key", Lock);
Intent a = new Intent(Change_setting.this, ChildMode.class);
a.putExtras(bundle);
startActivity(a);*/
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
isLock = true;
}
else
{
SQLiteDatabase db;
db = openOrCreateDatabase("Saftey.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
ContentValues values = new ContentValues();
values.put("hardBtn", "NO");
db.update("Setting", values, "id = ?", new String[] { "1" });
//SharedPreferences pref = getSharedPreferences("pref",0);
//SharedPreferences.Editor edit = pref.edit();
//edit.putString("hard","no");
//edit.commit();
Toast.makeText(this, "Hard Button Un-Locked", Toast.LENGTH_LONG).show();
isLock = false;
}
}
How can I make this work? I do not want to hide the buttons, I only want them to not respond to clicks when the checkbox is checked.
You can override the back key onBackPressed() and let it do nothing, but you can't override the home button but you can add some code to onStop() which is called upon home key press.
here is a piece of code to control backpress
#Override
public void onBackPressed()
{
if ( checked1 ) // if checked do nothing , remeber tomove checked1 to be a class member
return;
super.onBackPressed();
}
#Override
public void onBackPressed() {
}
With this you override back button. Just do nothing in it.
This you can use if you do something special when u press back:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
int vfBackId = viewFlipper.getDisplayedChild();
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
// Do some stuff
}
return false;
}
But you cannot override the home button. It's not provided from the Android system to handle the home button.
you cant disable home button in your application but you can disable back button with override
onBackPressed() and do nothing in that
You cannot disable home button.
There is no way to intercept the home button on Android, unless you make your app the home screen. This is for security reasons, so that malicious apps cannot take over your device by overriding all the buttons that can exit.
Home button is one sure short way to navigate to home screen.
If you want to handle the HOME button, implement a home screen.
Not able disable Home button on specific android devices
//Back key event detect
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
// Quit if back is pressed
if (keyCode == KeyEvent.KEYCODE_BACK)
{
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
Okay so I have a custom view inside a dialog but its onKeyDown never get called. I tried onKeyPreIme too but didn't work and setting dialog's setCancelable to true didn't help either.
edit :
//Removed all unnecessary code
public class CustomView extends LinearLayout
{
#Override
public boolean onKeyDown (int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{
//do stuff here
return true;
}
return super.onKeyDown(keyCode, event);
}
}
public class CustomDialog
{
Dialog dialog;
public class CustomDialog(Context context)
{
dialog = new Dialog(context);
dialog.setContentView(R.layout.test);// the test.xml has CustomView
}
}
Why don't you simply use :
public void onBackPressed() {
//desired functionality here
return;
}
Move your onKeyDown logics to an OnKeyListener implementation and register it in your View's constructor.
I want to do a custom action when pressing on the Menu button on the phone.
Is it possible to set an onClickListener (or similar) on the button and if so, how?
onCreateOptionsMenu is only called the first time the button is pressed - I've already tried this.
Usually you shouldn't override MENU behavior as users expect menu to appear, however you can use something along these lines:
/* (non-Javadoc)
* #see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
*/
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ( keyCode == KeyEvent.KEYCODE_MENU ) {
Log.d(TAG, "MENU pressed");
return true;
}
return super.onKeyDown(keyCode, event);
}
But onPrepareOptionsMenu(..) is called each time. :)
Updated for AppCompat v.22.+
As mentioned in this forum, KeyDown is not called for KEYCODE_MENU button pressed.
The solution is to override dispatchKeyEvent to this way:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int keyCode = event.getKeyCode();
int action = event.getAction();
boolean isDown = action == KeyEvent.ACTION_DOWN;
if (keyCode == KeyEvent.KEYCODE_MENU) {
return isDown ? this.onKeyDown(keyCode, event) : this.onKeyUp(keyCode, event);
}
return super.dispatchKeyEvent(event);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ( keyCode == KeyEvent.KEYCODE_MENU ) {
// do what you want to do here
return true;
}
return super.onKeyDown(keyCode, event);
}
It works until Google developers release a fix for this (or maybe it is not a bug and it works this way from now on).
You could probably hack something in using "OnMenuOpened" or some such, but I really wouldn't recommend it. The menu button is only supposed to be used to show menus, so there is consistency between applications.