So I'm currently developing a turn based game app. And it displays toasts from a view on who's turn it is after each turn is taken. What's weird though, is if I hit the back button it will go back to the main menu(previous activity) but the toasts will still linger till they time out. Also if I hit back twice and it goes to the home screen, the toasts will still show until they finish. I want to implement a check or some way to cancel those toasts when the back button is pressed. I have to do this in my view too, my view contains all the toasts and all the code to the game, the gameActivity only has onCreate to create the view for the game. Any ideas?
In your Activity you have override method:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// do something
yourView.notifyBackPressed();
return false;
}
return super.onKeyDown(keyCode, event);
}
And in your View you have to implement method, for example notifyBackPressed().
Try This...
Try to use Toast.LENGTH_SHORT or
You should set duration (milliseconds) for custom Toast like this.
Custom toast:
LayoutInflater inflater = getActivity().getLayoutInflater();
View layoutToast = inflater.inflate(R.layout.custom_toast_layout,
(ViewGroup) getActivity().findViewById(R.id.toastcustom));
((TextView) layoutToast.findViewById(R.id.texttoast)).setText("I'm custom toast");
final Toast myToast = new Toast(
(EmployerNominationView) getActivity());
myToast.setView(layoutToast);
myToast.setDuration(300);
myToast.setGravity(Gravity.BOTTOM, 0, 45);
myToast.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
myToast.cancel();
}
}, 1000);
Generate Toast
Toast toast;
toast = Toast.makeText(getBaseContext(), "Messages", Toast.LENGTH_LONG);
toast.show();
Cancel the Toast you may use onKeyDown() or onBackPressed() .
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
toast.cancel();
return true;
}
return super.onKeyDown(keyCode, event);
}
//Try with making Toast Global and cancel the toast on Back Pr
Toast mToast = new Toast(ApplicationContext);
public void onBackPressed(){
mToast.cancel();
}
I found this and will hellp you
Android AppMsg (Crouton) Library
Implementation of in-layout notifications. Based on Toast notifications.
I try the below method to detect the back button pressed on the action bar in activity by the first method and the second one is used to detecting the mobile hardware button back or kill activity button.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onBackPressed() {
setResult(RESULT_CANCELED);
super.onBackPressed();
}
Related
I have created a window and i am showing it on screen through Broadcast receiver.But the problem is that it appears on the screen and i want to dismiss it once the back button is pressed.I am unable to get the event of button press on this view.My code for back press looks as follows-
view.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getKeyCode() == KEYCODE_BACK) {
Log.d("LOG", "back button is pressed");
}
return true;
}
});
But Nothing is happening.I tried to do the same through DISPATCHKEY but it was also of no use.Please help me what i am not figuring out.Wouldn't this work on the view.?
Maintain global reference for Window and override onBackPressed()
Try this:
#Override
public void onBackPressed() {
if (view != null && view.isShowing()) {
view.dismiss();
} else {
super.onBackPressed();
}
}
First thing I want to say, this was done by seeing a tutorial. Here is the Custom Alert Dialog activity part I am calling from a broadcast receiver. The only problem is the back button click. Once the Alert dialog activity got started, when I press the back button it is getting closed.
public class AlertDialogActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setFinishOnTouchOutside(false);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
setContentView(R.layout.activity_inmsgdialog);
}
#Override
public void onBackPressed()
{
super.onBackPressed();
Toast.makeText(getApplicationContext(), "Back Pressed", Toast.LENGTH_SHORT).show();
}
}
I have tried onBackPressed and I'm able to see the toast message but the activity is getting closed.
See here:
#Override
public void onBackPressed()
{
super.onBackPressed(); //Remove this line
Toast.makeText(getApplicationContext(), "Back Pressed", Toast.LENGTH_SHORT).show();
}
Do not call super.onBackPressed(); code if you want to disable back button for activity. So remove this line. Hope it helps.
You can use the below option to handle back button press
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//your code
return true;
} else {
}
}
Don't propagate the event and you should be good.
#Override
public void onBackPressed()
{
//don't call super
}
I am writing an application to animate popup window. It was working great with my code.
I want to close the popup window(i.e to slide-down it), when back button is pressed on device.
But I couldn't listen any one key from device. I used setOnKeyListener of that popup window, I didn't even get log from it.
My Code is given below:
popup_layout = layoutInflater.inflate(R.layout.popup_addchannel, null);
popupWindow = new PopupWindow(popup_layout, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
subscribeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Log.d(TAG,
// "Button is clicked for animation.... Visibility is"
// + subscribeButton.getVisibility());
openMenu(view);
}
});
popup_layout.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.d(TAG, "on key button click called.........");
return false;
}
});
public void openMenu(View view) {
if (!flag) {
popupWindow.setAnimationStyle(R.style.PopupWindowAnimation);
popupWindow.showAtLocation(view.findViewById(R.id.button1),
Gravity.CENTER, 0, 0);
popupWindow.setFocusable(true);
popupWindow.update();
flag = true;
} else {
popupWindow.dismiss();
popupWindow.setFocusable(false);
flag = false;
}
}
What is the problem behind this?
Shall i achieve my requirement?
Please, Guide me.
Thank you in Advance!
try this....
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,true);
...
popupWindow.getContentView().setFocusableInTouchMode(true);
popupMenu.getContentView().setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU &&
event.getRepeatCount() == 0 &&
event.getAction() == KeyEvent.ACTION_DOWN) {
// ... payload action here. e.g. popupMenu.dismiss();
return true;
}
return false;
}
});
The dialog has a property of cancelling the dialog on back press.
dialog.setCancelable(true);
EDIT: Check Qberticus answer on this link: Android popup window dismissal
and you can also see: Issue dismissing popup window
Yes you can use this
dialog_obj.setCancelable(true);
You should override the onKeyPressed() method of your activity as below.
#Override
public void onBackPressed() {
super.onBackPressed();
//your code to close the popup window.
popupWindow.setAnimationStyle(R.style.PopupWindowAnimation);
popupWindow.showAtLocation(view.findViewById(R.id.button1),
Gravity.CENTER, 0, 0);
opupWindow.setFocusable(true);
popupWindow.update();
flag = true;
}
popupWindow.getContentView().setOnClickListener(new OnClickListener()) {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}
});
Try this.
You can't listen a key press in popup window, but when you listen to system back key, you can override dismiss() method to intercept it.
How can I specifically override just the back button while within a dialog to finish the entire activity and not just the dialog.
Using setOnCancelListener and setOnDismissListener do not work because there are other times that I simply close the dialog without closing the whole activity behind it.
Edit
Thanks Shubayu that may work!
I was also able to access just the back button in a dialog through this function.
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK){
finish();
}
return false;
}
});
Override
public void onBackPressed ()
of the activity and put in the way you want the behavior in it. Also set a boolean from your dialog which you use inside onBackPressed() of the Activity. if the boolean is true, run the disabling part of the onBackPressed() code else don't.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK){
// your logic goes here
return true;
}
return super.onKeyDown(keyCode, event);
}
use the above code::
You can use : dialog.setOnCancelListener(.....)
first set dialog.setCancelable(true);
than you can place below code :
dialog.setOnCancelListener(new DialogInterface.OnCancelListener()
{
#Override
public void onCancel(DialogInterface dialog)
{
// add code backpress
}
});
I need to capture the event when my app is showing a dialog and i press the device's back button.
Well it is not definetly a dialog. It is a dropdown list for my spinner.
I tryed:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
Toast myToast = Toast.makeText(getApplicationContext(), "back putton pressed", 1);
myToast.show();
}
// Call super code so we dont limit default interaction
super.onKeyDown(keyCode, event);
return true;
}
Not worked.
I also tryed:
public void onBackPressed()
{
Toast myToast = Toast.makeText(getApplicationContext(), "back putton pressed", 1);
myToast.show();
}
I even tryed this with overriding.
So none of the above sollutions are worked for me. I found both of these on stackoverflow but for some reason they are not working for me.
Of course they work when there is now dialog showing, but when do... they are not run.
Any ideas ?
So my
Try
if (keyCode == KeyEvent.KEYCODE_BACK)
{
Toast myToast = Toast.makeText(getApplicationContext(), "back putton pressed", 1);
myToast.show();
return true;
}
A general way to detect if a dialog has been canceled (back button pressed) is to implement OnCancelListener, not really sure if it works on spinners though!
see this
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//Handle the back button
if(keyCode == KeyEvent.KEYCODE_BACK) {
//write your code...
}
}
try this...
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
Toast myToast = Toast.makeText(getApplicationContext(), "back putton pressed", 1);
myToast.show();
new Handler().postDelayed(new Runnable{
public void run(){
finish();
}
}, 1000);
}
else{
// Call super code so we dont limit default interaction
super.onKeyDown(keyCode, event);
}
return true;
}
try to use dismiss listener. a dialog can be dismissed by pressing a button or back key and using this listener you can know if user has pressed a button or back key (you can listen for onClick listener on your dialog to catch dialog's button' click).