I'm trying to have my app display a toast message when "Back" is pressed when R.layout.main.xml is displayed, but not display the message from any other layout.xml . Also, when any other layout is displayed, I want it to return to the previous screen (like a back button should). If I left the back button default, whenever you pressed it, it would exit the application from any layout screen.
Here is my current code..... it keeps force closing and I cant figure out why:
private Toast toast;
private long lastBackPressTime = 0;
#Override
public void onBackPressed()
{
View thisView = getCurrentFocus();
int screen = thisView.getId();
if (screen == R.layout.main)
{
if (this.lastBackPressTime < System.currentTimeMillis() - 4000)
{
toast = Toast.makeText(this, "Press BACK once more to close this application", 4000);
toast.show();
this.lastBackPressTime = System.currentTimeMillis();
}
else
{
if (toast != null)
{
toast.cancel();
}
super.onBackPressed();
}
}
if (screen != R.layout.main)
super.onBackPressed();
}
In my apps I'm using constants for each layout and a currentView variable. Each time I change the content view I reassign this variable, so I'm always aware of where I'm currently being. Hope this helps you.
Related
I have the weirdest bug in my android code.
Basically, I'm streaming video with the Camera, and I have a button to start and stop the stream, call it myButton.
If I click on the button to START the stream, I have code that does myButton.setText("stop"), so that the button now says stop if the user wants to stop the stream.
In my onTouchEvent, I have code that detects a swipe up or a swipe down, and make a menu disappear/appear accordingly.
The swiping up and down animates my menu to slide out of the screen or slide back into the screen. It works perfectly fine before I try to start streaming, but if I ever click the start streaming button WHILE the menu is off the screen (translated -menuView.getHeight() distance away to make it off the screen), then if I try to swipe the menu back down again, it won't appear. However, it definitely receives the onTouchEvent because if I click the stop button (myButton.setText("start") will execute), then the menu immediately pops into the middle of the screen without any animation (it seems as if the animation must have happened invisibly).
Anyone have any insight into what could be causing this? Have been searching around everywhere and looking it up but haven't had any luck. Thanks
Updated with relevant code:
#Override
public void onClick(View v) {
ToggleButton clickedButton;
switch (v.getId()) {
case R.id.start_stop_stream:
if (!recording) {
Thread t = new Thread(){
#Override
public void run(){
startRecording();
}
};
t.start();
startStopButton.setText("Stop");
startStopButton.setTextColor(Color.RED);
} else {
Thread t = new Thread(){
#Override
public void run(){
stopRecording();
}
};
t.start();
startStopButton.setText("Start");
startStopButton.setTextColor(Color.BLACK);
}
break;
}
}
and
public boolean onTouchEvent(MotionEvent touchevent) {
switch (touchevent.getAction()) {
case MotionEvent.ACTION_DOWN:
downTouch = touchevent.getY();
break;
case MotionEvent.ACTION_UP:
if(menuView != null){
releaseTouch = touchevent.getY();
SharedPreferences.Editor editor = sp.edit();
if (downTouch < releaseTouch && Math.abs(downTouch-releaseTouch) > 100) {
menuView.animate().translationY(0);
editor.putString("menuDisplayed", "True");
editor.apply();
return true;
}
if (downTouch > releaseTouch && downTouch-releaseTouch > 100) {
menuView.animate().translationY(-menuView.getHeight());
rtmpURL = rtmpURLText.getText().toString();
editor.putString("rtmpUrl", rtmpURL);
editor.putString("menuDisplayed", "False");
editor.apply();
Log.v("EditText", rtmpURLText.getText().toString());
return true;
}
}
break;
}
return false;
}
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 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
I have an Activity in which i have 3 views.On click of a button i have hidden one view and displayed other one.But my doubt is that i cannot go back to the previous view.How should i do this.
For eg:I am on view A, and i click a button so now view A is hidden and now B is displayed.Now if press back button A should be displayed again but this is not happening,y app is directly closing.
Code
switch (view.getId()) {
case R.id.bt_continue_1:
if (str_first_name.equals("") || str_last_name.equals("") || str_gender.equals("Select Gender") || str_religion.equals("Select Religion") || str_age.equals("Select Age")) {
commonFunctions.showAlert(this, "INVALID", "Please fill all the mandatory details");
personal_info.setVisibility(view.VISIBLE);
professional_info.setVisibility(view.GONE);
contact_info.setVisibility(view.GONE);
} else {
header.setText("Contact Info");
personal_info.setAnimation(commonFunctions.animateLeft());
contact_info.setAnimation(commonFunctions.animateRight());
personal_info.setVisibility(view.GONE);
professional_info.setVisibility(view.GONE);
contact_info.setVisibility(view.VISIBLE);
}
break;
case R.id.bt_continue_2:
if (str_mobile.equals("") || str_state.equals("Select State") || strCity.equals("") || str_area.equals("") || str_address.equals("")) {
commonFunctions.showAlert(this, "INVALID", "Please fill all the mandatory details");
personal_info.setVisibility(view.GONE);
professional_info.setVisibility(view.GONE);
contact_info.setVisibility(view.VISIBLE);
} else {
header.setText("Professional Info");
contact_info.setAnimation(commonFunctions.animateLeft());
professional_info.setAnimation(commonFunctions.animateRight());
personal_info.setVisibility(view.GONE);
contact_info.setVisibility(view.GONE);
professional_info.setVisibility(view.VISIBLE);
}
break;
//
// if( code== KeyEvent.KEYCODE_BACK){
//
// }
case R.id.bt_save:
// AddUpdateJobResource async = new AddUpdateJobResource (personal_info.this, "0", "34588A34-E969-4723-84FE-E5409B66A5B7", "", str_first_name, str_last_name, gender_id, age_substring, nationality_id, "IND", "INDIA", state_code, str_state, str_city, str_area, str_address, str_mobile, profession_id, religion_id, "2", months_substring, yrs_substring, "0.00", "0.00", "hssuraksha");
// async.execute ();
AddResources addResources = new AddResources();
addResources.execute();
}
}
Please do suggest something
I don't understand your problem exactly. Do you mean the back button of the device or the a custom designed button in your application? In the latter case you can simply call the finish()-Method of your Activity when the button is pressed.
case R.id.bt_back:
// finish current view and go back to last view
finish();
It sounds like you might want to make view A and view B into fragments instead of hiding/showing their specific data. If you still want to use your paradigm you could override the onBackPressed to show view A when it is pressed.
#Override
public void onBackPressed() {
Log.d("YOUR_APP", "onBackPressed Called");
//Show/Hide stuff here
}
The back button works on activities, not on views.
The default back logic is (well, more or less) 'close current activity and return to
the one before'.
if you want to override this behavior add code like below to your activity:
#Override
public void onBackPressed() {
if (v1.getVisibility() == view.VISIBLE) {
// hide v1, show v2, v3
v1.setVisibility(view.GONE);
v2.setVisibility(view.VISIBLE);
v3.setVisibility(view.VISIBLE);
}
else if (v2.getVisibility() == view.VISIBLE) {
// hide v2, show v1, v3
v2.setVisibility(view.GONE);
v1.setVisibility(view.VISIBLE);
v3.setVisibility(view.VISIBLE);
}
else if (v2.getVisibility() == view.VISIBLE) {
// close activity
finish();
}
}
or whatever other back-button logic your application requires.
Do you mean that you want to navigate from activity 3 to activity 1? Dependend on your use case you could override the standard task and backstack behavior. http://developer.android.com/guide/components/tasks-and-back-stack.html
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.