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(...)
Related
Is there a way to set the color of a progressBar in XML, while the progress is going on?
I want to change the color of my progress at 80% to red.
Is there a way to to this in xml? So I just can copy paste my progressBar without coding it manually everytime...
No, you cannot change the color at runtime using xml.
If you can change your ProgressBar to SeekBar, you can easily listen to the progress change & as it reaches 80, you can change the color.
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
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
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));
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;
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) {
}
});