get progress in onStopTrackingTouch not onProgressChanged in seekbar - java

I'm new to android development ,
I use onProgressChanged to get current progress as progress is a parameter of the function .
but I want to get only the final value of progress when user release the seekbar not the Immediate value.
Thanks in advance.

The onStopTrackTouch method defined in OnSeekBarChangeListener is called when the user stops sliding the SeekBar (i.e., has finished the touch gesture) and provides the "final value".

Related

Activity not paused quickly enough

In my AppCompatActivity, I am showing an AlertDialog at a specific event.
Once the AlertDialog is shown, the user should not be able to click on anything in my AppCompatActivity anymore.
I tried to disable the activity in onPause(): getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
And to check whether it is not paused: getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)
But it seems that onPause() is not called quickly enough for that.
I could set a static boolean to true whenever I pause the AppCompatActivity, set it on false in onResume() and check it in every single onClickListener/onTouchListener etc., but is there a better way to do it?
Every help is appreciated!
Edit:
Thanks for your comments! I found out, that when I am clicking on the screen while the UI thread is started (for opening the AlertDialog), the click is put in a queue. That's why the click event is executed while the AlertDialog is running. Do I need to save the AlertDialog and check if it is running or is there a better way?
I believe that you should let us know more about your code. However, I think that I can guess what you're saying. There is a setEnabled(false) method for every view component in android, and if you call it on a view component java object, it'll be disabled and no longer clickable. So you can simply disable your components whenever you want to show the AlertDialog and then enable them again by calling setEnabled(true) if you wish. I'm pretty sure pausing the activity is not what you should do. Because doing it manually, is not best practice anyway, at all.
I ended up saving an instance and writing a method to check if the Activity should not be active:
private void canRun() {
return (dialog == null || !dialog.isShowing()) && getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED);
}

Android: startActivity in onResume() method has delay

I am creating a simple button application for Android. The application controls a very expensive machine so I have to make sure that user cannot accidentally click on any button. So I created a "lock" screen as an activity and start it whenever the application becomes active (when onResume() method is called). But when I am inside the app and just lock the phone and then unlock it I can see the activity for about half a second before the "lock" screen jumps in.
I was trying to start it when onPause() is called, but when the back button is pressed, it will navigate to "lock" screen instead of going out of the app.
I was wondering if I can put the activity to the front but activate it when onResume() method is called.
Thank you for your answers.

Android screensaver in app if it's not used

I created an android app. The screen never turns off:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Now I want to show a picture if the screen is not pressed for 5 minutes or something else. The app should not be closed, when pressing on the image the app should be open.
How can I realize that?
I would discourage you from taking this approach. Users expect to have a consistent user experience between various apps on their devices, and likely have a preference to how their device sleeps, either by having specified a sleep timeout or displaying a daydream as introduced in Android 4.2.
If you'd like to provide users with the option to display a screensaver associated with your app, I suggest including a Daydream in your app and otherwise acknowledging the user's preferences.
That being said, if you cannot use Daydream, you could observe if the app is being used or not. Two things come to mind:
Have the root view of your activity intercept touch events to observe if any of its children have been touched.
Observe the activity's onPause() and onResume() to acknowledge that the activity is still being displayed.
You could then invoke a Runnable by posting it to a view using postDelayed(Runnable action, long delayMillis), being wary to remove it when the activity is paused or the timer should be reset using removeCallbacks(Runnable).
I solved the problem!!!
I used that event:
public boolean dispatchTouchEvent(MotionEvent ev)
{
super.dispatchTouchEvent(ev);
// cancel my Timer
return true;
}
Thanks!!

Set a fixed value for a Progress Bar

I'm developing an application which will use a Progress Bar.
The problem is that I don't want to use the method setProgressBar(), just because I want to regulate the loading amount manually.
In fact when I use, for instance, setProgress(20), the bar will keep updating by 20 every time.
I mean, I need to call a kind of "set" method which take an input as parameter and set the fixed amount on the progress bar without change it time after time.
Is it possible?
As the documentation states:
setProgress(int progress)
Set the current progress to the specified value.
Meaning you can set the progress of the progressbar to the fixed value you want. The only thing you need to do is call it on the right time.
You should write like this!
ProgressDialog progressdialog = new ProgressDialog(this);
progressdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressdialog.setCancelable(false);
progressdialog.show();
Finally, you should set the progress wtih this method
progressdialog.setProgress(1);

"removedialog" issue when displaying 2 dialogs at the same time

In the method "OnCreate" of my actvity, I'm displaying a little progress dialog while loading data. It's done every time the "OnCreate" method is called so even when the screen orientation change.$
In the nominal case, there is no problem even if the user change the screen orientation.
But, if the user opens another dialog (used to select an item in a list), then change the orientation, the progress dialog is displayed behind the "list" dialog and is not impacted by the "removeDialog".
Do you have any clues on that behavior ?
Thanks
call "removeDialog" or "dismissDialog" before "startDialog" with try-catch and so on. You can call "removeDialog" or "dismissDialog" several times...

Categories