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));
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) {
}
});
}
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(...)
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 want to implement a easy hsv color picker that fit well into my app. As known form color Pickers I would like to change the background of the seekBars according to the value of the other seekBars.
But when I start the activity I am seeing this:
After the first touch of one of the seekBars (no matter which) I get this result and after the first touch the layout stays like this and does not change with further touches (which is also what I would have expected according to my layout, the 2 seekBars on top are 20dip and the 1 below is as high as the thumb):
Like seen in the second picture I would like to have the seekBar from beginning.
Here my layout:
<SeekBar
android:id="#+id/SbColorPickerHue"
android:layout_width="match_parent"
android:layout_height="20dip"
android:max="360"
android:thumb="#drawable/colorpickerthumb" />
<SeekBar
android:id="#+id/SbColorPickerSaturation"
android:layout_width="match_parent"
android:layout_height="20dip"
android:max="255"
android:thumb="#drawable/colorpickerthumb"/>
<SeekBar
android:id="#+id/SbColorPickerValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255"
android:thumb="#drawable/colorpickerthumb"/>
With a OnSeekBarChangeListener in onProgressChanged() and of course in onCreate (to initalize the seekBars) I am calling the following function (To safe you a lot of code only the Hue Gradient is shown):
private void setGradients() {
mHueGradient = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,mColors);
mHueGradient.setGradientType(GradientDrawable.LINEAR_GRADIENT);
mHueGradient.setCornerRadius(mRadius);
mHueGradient.setStroke(mStrokeWidth, mStrokeColor);
mSbHue.setBackgroundDrawable(mHueGradient);
}
I tried many different things already:
set the Gradients twice in onCreate
set the Gradients again in onRescume
as can be seen in the layout xml I set some of the SeekBar to a fixed height (20dp) and others to wrap_content. The behaviour changed a little (as can be seen in the pictures). But still not satisfying.
Invalidate either the seekBar or the Drawable
Force Layout
Measure the drawable
Result is always the same.
What am I doing wrong here? Am I missing somthing?
Edit 1:
The SeekBar and the ChangeListener are defined as follows:
mSbHue = (SeekBar)findViewById(R.id.SbColorPickerHue);
setGradients();
mSbHue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
setGradients();
}
});
If you move setGradients() after the listener it works.
mSbHue = (SeekBar)findViewById(R.id.SbColorPickerHue);
mSbHue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
setGradients();
}
});
setGradients();
I would like to have the color of the progress bar (specifically the secondary progress that is updated with setSecondaryProgress) to change dynamically at runtime when it crosses a certain point.
I am already able to substitute my own drawable into the bar during my Activity's onCreate function (changing color from yellow to green) by calling setProgressDrawable and substituting a resource I've copied from the default Android version of a horizontal progress (SDK/platforms/android-2.1/data/res/drawable/progress_horizontal.xml). However, updating this at runtime (yes, from the UI thread via an AsyncTask) causes the entire progress bar except for the thumbtab to go black or transparent.
I think this is what you are looking for
This can be achived by creating custom drwables for background and progress of seak bar
See this link for sample code.
Pls see the below links also
http://javatechig.com/android/android-seekbar-example/
Change the color of a seekbar on onProgressChanged
SeekColorActivity.java
package com.exercise.seekcolor;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.SeekBar;
public class SeekColorActivity extends Activity {
private int seekR, seekG, seekB;
SeekBar redSeekBar, greenSeekBar, blueSeekBar;
LinearLayout mScreen;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mScreen = (LinearLayout) findViewById(R.id.myScreen);
redSeekBar = (SeekBar) findViewById(R.id.mySeekingBar_R);
greenSeekBar = (SeekBar) findViewById(R.id.mySeekingBar_G);
blueSeekBar = (SeekBar) findViewById(R.id.mySeekingBar_B);
updateBackground();
redSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);
greenSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);
blueSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);
}
private SeekBar.OnSeekBarChangeListener seekBarChangeListener
= new SeekBar.OnSeekBarChangeListener()
{
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
updateBackground();
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
};
private void updateBackground()
{
seekR = redSeekBar.getProgress();
seekG = greenSeekBar.getProgress();
seekB = blueSeekBar.getProgress();
mScreen.setBackgroundColor(
0xff000000
+ seekR * 0x10000
+ seekG * 0x100
+ seekB
);
}
}
A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys.
When SeekBarChange, the onProgressChanged() of OnSeekBarChangeListener will be called, and the background of the screen(LinearLayout) will be updated according to the value (SeekBar.getProgress()).
main.xml