Limit SeekBar Progress Within 20<Range<5000 to one popup - android

My SeekBar thumb must move within range 20 to 5000.
I need to prevent the thumb to move below 20. when this amount is reached, a toast message appear telling the user "minimum value reached".
However when i slide to minimum value and forced slide on limit, that is multiple slide towards 20, several popup appears onscreen. How can i limit to only one popup on screen on multiple slide towards minimum or maximum value.
Here is my .setOnSeekBarChanged listener :
sbMensualite.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progress = progress / 1;
progress = progress * 1;
if (progress<20){
progress=20;
}
else {}
if (duree_int>=Constant.DUREE_MAX_VALUE){
Toast.message="durée maximum atteinte";
m.startActivity(new Intent(m, Toast.class));
}else if (duree_int<=Constant.DUREE_MIN_VALUE){
Toast.message="durée minimum atteinte";
m.startActivity(new Intent(m, Toast.class));
}else {
//set progress duree android
sbDDuree.setProgress(duree_int);
//set amount of month for duree
txDDure.setText(Util.doubleNoDp(duree));
}
//set mensualite amount
txMensualite.setText(Util.decimalFormat(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});

I need to prevent the thumb to move below 20. when this amount is reached, a toast message appear telling the user "minimum value reached".
First. Make the range of your slider be 0-4980. And every time you read the value add +20. If you want to set the value programmatically from a variable add -20.
Second, you really should NOT show a toast to tell that the minimum value was reached, because, using the method described in the previous paragraph, this will be obvious.
EDIT:
Would limiting the duree_int variable work for you???
if (duree_int>Constant.DUREE_MAX_VALUE){
duree_int = Constant.DUREE_MAX_VALUE;
Toast.message="durée maximum atteinte";
m.startActivity(new Intent(m, Toast.class));
}else if (duree_int<Constant.DUREE_MIN_VALUE){
duree_int = Constant.DUREE_MIN_VALUE;
Toast.message="durée minimum atteinte";
m.startActivity(new Intent(m, Toast.class));
}else {

To cancel your existing toast and show a new one after, try this:
Toast myToast = new Toast(getApplicationContext());
sbMensualite.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progress = progress / 1;
progress = progress * 1;
if (progress<20){
progress=20;
}
if (duree_int>=Constant.DUREE_MAX_VALUE){
myToast.cancel(); //hide existing toast
myToast.setText("durée maximum atteinte");
myToast.setDuration(Toast.LENGHT_SHORT);
myToast.show();
}else if (duree_int<=Constant.DUREE_MIN_VALUE){
myToast.cancel(); //hide existing toast
myToast.setText("durée minimum atteinte");
myToast.setDuration(Toast.LENGHT_SHORT);
myToast.show();
}else {
//set progress duree android
sbDDuree.setProgress(duree_int);
//set amount of month for duree
txDDure.setText(Util.doubleNoDp(duree));
}
//set mensualite amount
txMensualite.setText(Util.decimalFormat(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});

Related

Setting alpha of background of layout using setAlpha doesn't work

I am a bit confused.
I have this :
this.opacitySeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
// updating opacity of layout
RelativeLayout ledColor;
for(int index = 0; index < selectedLedsIndices.size(); index++) {
ledColor = (RelativeLayout)ledsLayout.getChildAt(index).findViewById(R.id.led_color);
ledColor.setAlpha(((float)i) / 255);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
I am trying to change the opacity of a layout using the progress value of a seekbar. The value of the seekbar goes from à to 255. But when running, the opacity of the layout doesn't change. I'm getting no error trace. It just doesn't want to change. How that?
I am running the app on api 24 (and my app should target api from 19 to 27).
Use ledColor.getBackground().setAlpha(...)

Android set seekbar with decimal

So I have a seekbar that I want to display or setProgress the value of that I get from SharedPreferences String, but since I can't use decimals, I am not 100% sure how to do this, can someone point me in the right direction?
String BarValue = "3.5";
Bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
float value = 0;
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
value = ((float)progress / 5);
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
SharedPreferences.Editor e = sp.edit();
e.putString("barValue", String.valueOf(value));
e.apply();
}
});
Android Seekbar does not support float, so use integer in Seekbar and convert that to float as you want. I think that's the best way.
For example if you want to use single decimal place values like 11.6 you can set 116(11.6 * 10) to the Seekbar, and on retrieval divide that by 10. It will do the job.
Android default SeekBar doesn't provide decimal value, if you want than you can use any custom SeekBar from following,
https://github.com/woxingxiao/BubbleSeekBar
https://github.com/syedowaisali/crystal-range-seekbar
https://github.com/vashisthg/StartPointSeekBar

How to get seekbar default progress position on activity created

I have the following code in onCreate() Activity:
SeekBar vSeekbar = (SeekBar)findViewById(R.id.seekBar1);
final TextView tvText = (TextView) findViewById(R.id.textView);
//sets seekbar tab position from a randomly generated number...
//when the acitivty is first created I would like the SeekBar position to be set in the `textView`
tvText.setText(vSeekbar.getProgress()); //this is not showing anything...
//the onchangelistener works correctly...
vSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar arg0)
{
}
#Override
public void onStartTrackingTouch(SeekBar arg0)
{
}
#Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
tvText.setText(String.valueOf(progress)); //this works fine...
}
});
The seekBar1 position is set by a random number generated when the view is created so no way for me to know beforehand.
How can I get the seekBar1 position when the view is created and set it in the textView.
After random value set to seekbar call vSeekbar.getProgress() method. before you called then only you got zero value.
vSeekbar.setProgress(randomValue());
tvText.setText(vSeekbar.getProgress()+"");
You better limit your random values to the ProgressBar maximum value.
For example if your maximum value is 100 then use the following code: vSeekbar.setProgress(randomValue() % 100);
It will limit the random values to 0 to 99

Android SeekBar Addition Issues

EDIT: I also have pre-existing elements within my app that also add to the counter, so assigning counter = progress would just override the sum instead of adding to.
seekBar = (SeekBar) findViewById(R.id.seekBarDemo);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekBarTextView.setText("Tracking: " + progress + "/" + seekBar.getMax());
counter += progress;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
I'm having an issue with getting a counterto obtain a value off the seekbar. It is fine when you drag it to where you want to at first, but lets say from a scale of 1 - 10 and you are changing your mind consistently from 1 to 4 to 10 to 7.
7 appears on my TextView but the value I get when passing it to my counter is the sum of all of the numbers, where in this example, I would get 22.
Please let me know what I'm doing wrong. Thank you in advance.
counter += progress;
Is the same as counter = counter + progress.
First pass: counter = 0 + progress // let's say progress = 7
Second pass: counter = 7 + progress // let's say progress = 5, counter is now = 12...
If you just want the current value of the slider, you want to use counter = progress;

seekBar off by one driving me crazy

This seekBar is driving me nuts! Help me do this efficiently? I would like to have my starting value at 0 and max number displayed in textView $1,000,000. I would like to increment by $5,000. I also have tickers (plus and minus buttons) but they are off too because of my progress changed / textView display. it seems my textView is always 5,000 less than my actual progress. I can only get to 995,00 0 or do it differently and it skips from 10k to 0.
textView23 = (TextView) view.findViewById(R.id.textView23);
seekBar1.setMax(199);
seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser)
{
{
int newValue = seekBar.getProgress()* (5000);
String nValue = NumberFormat.getInstance().format(newValue);
textView23.setText("$" + nValue);
}
tempHouse = progress;
Calculate();
}
}
Since 1000000 = 200 * 5000, it seems that
seekbar.setMax(200)
would be the correct value ;)

Categories

Resources