Ripple effect activated on backpressed - android

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.

Related

How can i make 1 method act differently based on button clicked?

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;
}
});
}

Android: click event after Activity.onPause()

There are two buttons, button A starts another activity inside its onClickListener using simple Activity.startActivity() method, button B does some other work inside its onClickListener.
When I click button B and immediately after button A, then new activity is started, onPause() lifecycle event for old activity is fired, but also onClick event for second button is fired, but after Activity.onPause() which leads to some state inconsistencies in my app.
Is there any way to prevent touch/click events from being delivered after onPause() without using isPaused flag?
**Edit:**My code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonA = (Button) findViewById(R.id.activity_button);
buttonA.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, TestActivity.class));
}
});
Button buttonB = (Button) findViewById(R.id.log_button);
buttonB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("TEST", "onClick");
}
});
}
#Override
protected void onPause() {
super.onPause();
Log.e("TEST", "onPause");
}
}
Basically if you are fast enough and click button B right after button A then B's onClick is fired after onPause.
In OnClickListener of button A, disable the button b.
Button.setEnabled(false);
Just enable the button at the of A's onClickListener or at onResume depending on your requirements.
I know this question is very old and far far away from being active. I came across this question via a blog.
A simple solution which I can think off is maintaining a flag globally in Activity A and setting it immediately inside onClick of Button A. This flag can be reset in onResume of Activity A.
This flag should be used at the top of onClick handler and all the clicks of all the views must be ignored. Of course, this requires that there is a single onClick method.
private boolean isOnClickIgnored= false;
#Override
public void onResume() {
super.onResume();
isOnClickIgnored = false;
}
#Override
public void onClick(View v) {
super.onClick(v);
if(isOnClickIgnored){
return;
}
switch (v.getId()){
case R.id.btnA:
isOnClickIgnored = true;
// Do button a things like opening Activity B
break;
case R.id.btnB:
// Do button b things
break;
case R.id.btnZ:
// Do button z things
break;
}
}
This whole concept can be easily solved using ViewModels + lifecycle-aware LiveDatas where LiveData expose events to UI-layer only when it's allowed.

Reset seekbar to beginning on back activity

I have a seekbar in my activity. In the button event when I go to another activity and then use back button, the values of the seekbar are set where the user left it. I would like to reset the seekbar when the user comes back to the seekbar activity. How can I achieve it? Thanks
public void onClick(View v) {
if (seek.getProgress > 0) {
// do something
}
}
simply reset the seek bar in onPause or onResume of your SeekBarActivity, i.e. :
#Override
protected void onPause() {
seek.setProgress(0);
super.onPause();
}
This will be called when you have started another activity and the current one is paused.

How to prevent double-click on refresh button

I'm developing an android app where i've a refresh button in my action bar. This button call a function that re-open the same activity. This activity contains an asyncTask to load the content.
At the moment i'm encountering this problem. When i click on the refresh button it works fine, but if i click on the refresh button when the AsyncTask is still working (i've a progress bar to check the status) the app crashes.
The error that i receive is: NullPointerException
Is it possible to disable that button until the activity (and its AsyncTask) are completely loaded?
In your button's OnClickListener, from where you execute the AsyncTask, add this code:
button.setEnabled(false);
In onPostExecute() method of your AsyncTask, place this:
button.setEnabled(true);
If you also give the 'cancel' option to the user(i.e. if you have overridden the onCancelled() method in your AsyncTask), enable the button in onCancelled().
Edit 1:
Declare a boolean flag in your activity:
boolean menuButtonIsEnabled = true;
In your OnClickListener, set this flag to false:
menuButtonIsEnabled = false;
In onPostExecute() method of your AsyncTask:
menuButtonIsEnabled = true;
Override the onPrepareOptionsMenu(Menu) method in your activity:
#Override
public boolean onPrepareOptionsMenu (Menu menu){
super.onPrepareOptionsMenu(menu);
MenuItem button = menu.findItem(R.id.whatever_menu_button);
if(menuButtonIsEnabled){
button.setEnabled(true);
} else {
button.setEnabled(false);
}
return true;
}
In your onClickListener, the first thing you do is deactivate the button. This way it cannot be clicked again until you reactivate it :
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View target) {
target.setClickable(false);
target.setEnbled(false);
// Start your asynctask
}
}
in your AsyncTask.onPostResult(), you can reactivate the click on the button.
Nota: setClickable(false) prevents the button from reacting to clicks events, but setEnabled(false) also usually changes the appearance of the button.
You can try to disable the button from onPreExecute in AsyncTask and enable the button from onPostExecute in AsyncTask
You can check the status of your task.... On click of refresh button...
if(yourAsyncTaskObject != null && yourAsyncTaskObject.getStatus() != AsyncTask.Status.RUNNING){
then start your activity again here
}

Animation overriding onNavigateUp's default one

I am launching an About Activity with overridePendingTransition() right after it, so I can get an animation of the incoming Activity.
I want the Activity to perform an animation as well upon "leaving", so I have overriden onBackPressed() and it works ok.
The problem comes, as the About activity has the "Up navigation" enabled, on how to perform the animation when the "Up" navigation is tapped --instead of just Back button-- to return to previous activity.
I have tried
#Override
public boolean onNavigateUp() {
overridePendingTransition(R.anim.fadeinltr, R.anim.fadeoutltr);
return super.onNavigateUp();
}
but it does not work, because by the time the overridePendingTransition() method is called, there is no transition to override yet.
Any ideas?
I had the same problem and I solved in this way:
#Override
public boolean onNavigateUp(){
boolean x = super.onNavigateUp();
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
return x;
}

Categories

Resources