Android set seekbar with decimal - android

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

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(...)

Using seekbars to change RGB values android studio

I was wondering how to change my code to make it work properly:
Red.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekr = Red.getProgress();
seekg = Green.getProgress();
seekb = Blue.getProgress();
button1.setBackgroundColor(
0xff000000
+seekr*0x10000
+seekg*0x100
+seekb
);
button2.setBackgroundColor(
0xff000000
+seekr*0x10000
+seekg*0x100
+seekb
);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
this is the method that i'm using to change the value of the Red parameter, and i'm controlling also the other 2 colors in the same way.
The problem is that when all the seekbars are set on 0 the background color of the buttons is black, and if they are all set at the maximum value the background color is grey; so i can't have the white color.
Why is this code not working properly?
Not sure if you are wanting white or not but could try using:
button1.SetBackgroundColor(Color.argb(255, seekr, seekg, seekb));
button2.SetBackgroundColor(Color.argb(255, seekr, seekg, seekb));

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

Progressbar showing values in android

I have a progressbar that i have defined for accepting the rating
RATINGbar = (SeekBar)findViewById(R.id.RATINGseekBarID); // make seekbar object
RATINGbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
RATINGtextProgress = (TextView)findViewById(R.id.RATINGtextViewProgressID);
RATINGtextProgress.setText("Rating:: "+progress);
seekBar.setMax(5);
}
});
This Bar accepts maximum of 5 number as i have defined !
when i select it and scroll horizontally, it accepts values as
1-2-3-4-5
How to make the progress bar take values like
1-1.5-2-2.5-3-3.5-4-4.5-5
what changes should i need to make here
How to make my progress bar accept values in interval of 0.5
Hope i am clear
How to make my progress bar accept values in interval of 0.5
Short answer:
You can't. ProgressBar takes only int values for actual and max progress and you can't change this behaviour. This how it's designated by OS.
Update: Look also at this thread.
You can't change ProgressBar intervals to float. Instead, try using RatingBar.

Categories

Resources