How can I handle a user pressing the back button, whilst a ShowcaseView is showing? I want to be able to hide the ShowcaseView when they press back.
As linked from https://github.com/amlcurran/ShowcaseView/issues/376
ShowcaseView allows you to query if it is showing or not. So, if a user presses back and it is showing, simply hide it using Activity#onBackPressed():
#Override
public void onBackPressed() {
if (sv.isShowing()) {
sv.hide();
} else {
super.onBackPressed();
}
}
Try this :
#Override
public void onBackPressed() {
// in if condition check showcaseview is show or hide
if(isShowcaseViewShow==true) {
// here hide your showcase view
}
else{
// here perform back action if view already hide
super.onBackPressed();
}
}
Related
In my application I am using navigation drawer menu. I need to close this menu and get back to previous activity, when user clicks on back button.
I wrote below code :
#Override
public void onBackPressed() {
if (toNightSearchView.isSearchOpen()) {
toNightSearchView.closeSearch();
} else if (toNightDrawerLayout.isShown()) {
toNightDrawerLayout.closeDrawer(Gravity.END);
} else {
super.onBackPressed();
}
}
The problem is when menu is closed app does not back to previous activity.
How can I fix this?
// use `isOpen()` to check the drawer state
if (toNightSearchView.isSearchOpen() || toNightDrawerLayout.isOpen()) {
//close if any of the views are open
if (toNightSearchView.isSearchOpen())
toNightSearchView.closeSearch();
if (toNightDrawerLayout.isOpen())
toNightDrawerLayout.closeDrawer(Gravity.END);
// Gravity.END is used for closing right drawer and start for left one
} else{
super.onBackPressed(); // go back to previous activity
}
or better use isDrawerOpen
if (toNightSearchView.isSearchOpen() || toNightDrawerLayout.isDrawerOpen(Gravity.START)) {
if (toNightSearchView.isSearchOpen())
toNightSearchView.closeSearch();
if (toNightDrawerLayout.isDrawerOpen(Gravity.START))
toNightDrawerLayout.closeDrawer(Gravity.START);
} else{
super.onBackPressed(); // go back to previous activity
}
as title says, how to make my ripple effect on ImageButton (ripple effect assigned to this ImageButton) activated on backpressed() without back to the previous activity
with a bit of logic you could achieve that easily. what i thought of is that you have a method lets call it doSomething();
boolean onBackpressed = false;
private void doSomthing() {
if(onBackpressed){
finish();
}else{
// do anything else that the button wants to do;
}
}
now onBackpressed() you could do this
#Override public void onBackpressed() {
onBackpressed = true;
myImageButton.performClick(); // myImageButton.callOnClick()
}
P.S: that i didn't call super.onBackpressed(); so we can control the back press without existing the app.
of course your imageButton click listener calls doSomething() method.
I have a menu button on the bottom and I have display menu icons from another activity like MenuTask.
But how to close that MenuTask activity if the user clicks again in the menu?
imgMenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (key == 0) {
// I want to show menu here
key = 1;
Intent intent = new Intent(MainActivity.this, MenuAction.class);
startActivityForResult(intent, 2);
setResult(0);
} else if (key == 1) {
// I want to Delete menu here
key = 0;
//onBackPressed();
finish();
}
}
});
Note: I think if i click second time , button could not fire. it means else part not execute.
Problem:
I have Clicked, right top menu button and open new activity menu action. but if i want to click again same menu button, i want to disappers that menu action. but menu button could not clicked.
How to make MenuButton Clickable? any idea? any other menthod? But when i click mobile backbutton menuaction layout disappeared.
Thanks in advance.
I thing this is not best way. You must use Fragment
If you want only kill activity MenuAction.this.finish();
#Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
// handler to delay
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
you can go though this link Clicking the back button twice to exit an activity
same question
In our application we have one parent linear layout which contains another linear layout as child.Child linear layout is nothing but treating as dialog box with fixed width and height. As expected if i click on back key of device entire activity is getting removed from stack. I just want if dialog box is open then only dialog box should get removed i.e. only child linear layout should be gone.How to dismiss only view not the activity on back key press?
Please do needful help.
Thanks,
AA.
You need to override Activity.onBackPressed() and do what you need to do:
#Override
public void onBackPressed() {
if (mDialogView.getVisibility() == View.VISIBLE) {
mDialogView.setVisibility(View.GONE);
} else {
super.onBackPressed();
}
}
Well, you could override onBackPressed to something like:
#Override
public void onBackPressed() {
if(!closeDialogsAndStuff()) {
super.onBackPressed();
}
}
private boolean closeDialogsAndStuff() {
/**
* check if dialogs should be closed and if so, close them and return true
* otherwise return false
* */
return true;
}
Implement onBackPressed method(), and inside that method dismiss your dialog
#Override
public void onBackPressed()
{
// code here to show dialog
super.onBackPressed(); // optional depending on your needs
dialog.dismiss();
}
You can ask if you have any further queries :)
put this code in your activity
public boolean onKeyDown(int keyCode, KeyEvent event) { //if user click back from device
if (keyCode == KeyEvent.KEYCODE_BACK) {
if(dialog.isShowing()) dialog.dismiss(); //dismiss dialog if shown
else finish(); //finish current activity
}
return true;
}
I have 1 edit text in my activity in that when i press a button soft keyboard opens and on hardware backbutton the softkeyboard is closed and cursor should be visible gone..i have implemented as below..Please help me for it..
public void onBackPressed()
{
brand.setCursorVisible(false);
brand.clearFocus();
finish();
}
}
Try this way
public void onBackPressed()
{
if(brand.isCursorVisible()){
brand.setCursorVisible(false);
brand.clearFocus();
}else{
super.onBackPressed();
}
}