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
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
}
I have a method which on button click needs to perform in a specific way and if no button is clicked, it should alternatively act differently.
Basically on clicking on the button, it should destroy my service. And if I don't click on it, it will eventually be destroyed. The thing is that I want the destroy method act differently based on these 2 scenarios.
You could add the clicked View as method argument:
myOnClick(View view){
if(view != null){
//Button was clicked
....
}else{
//No Button was clicked
.....
}
}
Is the method performing periodically? Then just add a bool to see if the button was clicked before.
like:
Main:
boolean clicked = false;
onCreate:
Button b = (Button)findViewById(R.id.buttonName);
b.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
if(clicked){
clicked = false;
}else{
clicked = true;
}
});
}
I want my btnAppShare button to be clicked even if the button is invisible, I have written this code after button 1 click listener which is visible. Basically, I want that after completely executing setOnClickListener event of button1 my button2 gets automatically clicked and perform its `setOnClickListener.
btnAppShare = (Button) findViewById(R.id.btnAppShare);
btnAppShare.setVisibility(View.INVISIBLE);
btnAppShare.performClick();
btnAppShare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (TextUtils.isEmpty(regId)) {
Toast.makeText(getApplicationContext(), "RegId is empty!",
Toast.LENGTH_LONG).show();
} else {
Intent i = new Intent(getApplicationContext(),
MainActivity.class);
i.putExtra("regId", regId);
startActivity(i);
finish();
}
}
});
Use View#callOnClick():
Button b = (Button)findViewById(R.id.button);
//set here listener
b.callOnClick();
Unlike performClick() it calls onClickListener method directly, without view related stuff. Button has to have listener BEFORE you call click action!
Make a method and call that after setting the button invisble, then inside the onClick method of the listener, call that same method.
btnAppShare = (Button) findViewById(R.id.btnAppShare);
btnAppShare.setVisibility(View.INVISIBLE);
myMethod();
btnAppShare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0)
myMethod();
}
});
public void myMethod()
{
if (TextUtils.isEmpty(regId)) {
Toast.makeText(getApplicationContext(), "RegId is empty!",
Toast.LENGTH_LONG).show();
} else {
Intent i = new Intent(getApplicationContext(),
MainActivity.class);
i.putExtra("regId", regId);
startActivity(i);
finish();
}
}
You cannot click something that is not visible but what you can do is make that button background transparent with this attribute in its xml
android:background="#android:color/transparent"
this way button will not be visible but clickable.
Hope it helps
if you want to
view.performClick()
something like that, you should write your setOnclick method upper the performclick method. Because codes flow up to down and button should generate setOnclick method before calling.
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();
}
}
I have 2 activities, A and B.
In activity A I have radiogroup, a CHECK button and NEXT button .
before I can press the NEXT button , I want to choose first one of the radio button in the radio group and CHECK it first so that I can know if its correct or incorrect before I can press the next button.
here's my code:
btnNext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (radio1.isChecked() || radio2.isChecked() || radio3.isChecked()) {
if (btnCheck.isPressed()) {
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(i);
}
} else {
return;
}
}
});
I can't press the NEXT button even I have chosen one of the radio button and check it.
Please help.
Your answers will be appreciated.
Thank You.
I'm not sure what the problem is, but you could just create a boolean to check if you're concerned that isPressed() isn't working as you'd expect.
boolean hasChecked = false;
btnCheck.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// put conditions here to check if it was pressed at the right time
hasChecked = true;
}
});
btnNext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (radio1.isChecked() || radio2.isChecked() || radio3.isChecked()) {
if (hasChecked) {
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(i);
}
}
}
});
if (radio1.isChecked() || radio2.isChecked() || radio3.isChecked()) {
//Do something here.
}
The above if you want to check if any one of the radio buttons is checked.
if (radio1.isChecked() && radio2.isChecked() && radio3.isChecked()) {
//Do something here.
}
The above if you want to check if all the radio buttons are checked.
If I understand you correctly, you also want to check some btnChk's click, before you press NEXT. So put a FLAG (Global Variable) in your btnChk's onClick method, if you want to log that if that particular button was presed.
If you want anyone of the radio button checks to be true, use the || condition in the if statement. Then to check if btnChk was pressed do the following:
Declare a Global FLAG at just below the class declaration:
public int FLAG = 0;
Set a buttonclick event on your button in onCreate():
Button btnChk = (Button)findViewById(R.id.YourId);
btnChk.setOnClickListener(btn);
private View.OnClickListener btn = new View.OnClickListener() {
#Override
public void onClick(View v) {
FLAG =1;
}
};
Then in your NEXT button check the following:
if (radio1.isChecked() || radio2.isChecked() || radio3.isChecked()) {
if(FLAG ==1){
//Do something here.
}
}
What the FLAG does: FLAG is init to zero initially, as soon as you press btnChk, FLAG becomes 1, if btnChk is not pressed, FLAG remains 0. so in the button click of Next button you check the value of FLAG and do your stuff accordingly.