If you are looking to know, How to override home button programmatically in android, then below I have provided the simple solution.
You have to do just a simple thing i.e to override the onKeyDown function in your application, given is the code snippet that opens a dialog box on clicking home button:-
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
showCustomDialog("This is just an example to open a dialog when you press home button, you can do other thing also");
return true;
}
return false;
}
void showCustomDialog(String msg)
{
AlertDialog.Builder alertDialog;
alertDialog= new AlertDialog.Builder(activity.this);
alertDialog.setTitle("");
alertDialog.setMessage(msg);
alertDialog.setPositiveButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which)
{
dialog.cancel();
}
});
alertDialog.show();
}
0 Comment(s)