How can I set the minimum value of a SeekBar?
I need a minimum of 435 and a max of 5435, but I don't know how to add the minimum value in this case.
My current code is here:
private void bindSeekAndEditText() {
mSeekBar = (SeekBar) mRootView.findViewById(R.id.sb_value);
mSeekBar.setMax(5435);
mEtOtherValue = (EditText) mRootView.findViewById(R.id.et_value);
mSeekBar.setProgress((int) (mSeekBar.getProgress() == 0 ? 5435 : mSeekBar.getProgress()));
mEtOtherValue.addTextChangedListener(mEtOtherValue);
mEtOtherValue.setText((double) (mSeekBar.getProgress() == 0 ? 5435 : mSeekBar.getProgress());
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
mSeekBar.setOnSeekBarChangeListener(new OnSeekBarStepChangeListener());
}
private final class OnSeekBarStepChangeListener implements OnSeekBarChangeListener {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progress = ((int) Math.round(progress / 50)) * 50;
mSeekBar.setProgress(progress);
mEtOtherValue.setText(FormatingUtil.parseMoneyToBr((double) progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
Can anyone please help me?
As of API Level 26 the method setMin(int) was added to class android.widget.AbsSeekBar (see here). Since SeekBar is a subclass of AbsSeekBar, this method can also be used in SeekBar.
Related
Code Run with no errors. But seekbar not working. It is the center but not able to slide on touch.
timesTablesSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
int min =1;
int timesTableNumber;
if(i<1){
timesTableNumber = min;
}else{
timesTableNumber = i;
}
Log.i("Seekbar Value", Integer.toString(timesTableNumber));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
I've implemented a list of seekbars. According to my functionality, I want the sum of all seekbar not to exceed 100% for example if I drag first seekbar to 60% the next one will not exceed 40%.
I have made it work but whenever I drag the first one to some value (let's say 60 %) and then the second one to let's say 40% and then again when I try to drag the first one to decrease its value resets to 0. What is the problem with my code?
holder.shelfShareSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
remainingShelfShare = 100 - shelfShareSum;
holder.percentTextView.setText(String.valueOf(progress));
productShelfShareMap.get(((Products)list.get(position)).getProductID());
if(progress > remainingShelfShare){
holder.shelfShareSeekbar.setProgress(remainingShelfShare);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
list.get(position).setSetSlider(seekBar.getProgress());
shelfShareSum = 0;
productShelfShareMap.put(((Products)list.get(position)).getProductID(), Integer.parseInt(holder.percentTextView.getText().toString()));
for (HashMap.Entry<Integer, Integer> entry : productShelfShareMap.entrySet()){
shelfShareSum += entry.getValue();
}
}
});
Use SetMax function of seekbar to setvalue in SecondSeekbar.
int step = 1; // set it accordingly if you want data in 3.1,3.2 range
int max = 100;
int min; // if you want to set min criteria too.
shelfShareSeekbar.setOnSeekBarChangeListener(
new OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar seekBar) {}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser)
{
// Ex :
// And finally set value of next seekbar based on firstone
secondseekbar.setMax((max - progress) / step);
}
}
);
I've got a small issue in my code:
I've a SeekBar and an EditText in a layout.
What I want to do is to set the EditText value when the SeekBar changes and set the SeekBar progress when the EditText changes.
So far, I've the following code:
SeekBar seek_coagulation = (SeekBar) findViewById(R.id.seek_coagulation);
EditText edit_coagulation = (EditText) findViewById(R.id.edit_coagulation);
seek_coagulation.incrementProgressBy(1);
seek_coagulation.setMax(1000);
seek_coagulation.setOnSeekBarChangeListener( new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
double value = progress / 100.0;
edit_coagulation.setText(String.valueOf(value));
}
public void onStartTrackingTouch(SeekBar seekBar)
{
}
public void onStopTrackingTouch(SeekBar seekBar)
{
}
});
edit_coagulation.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s)
{
double value = (s != null && s.toString().length() > 0) ? Double.parseDouble(s.toString().replace(',', '.')) : 0D;
int progress = (int) (value * 100);
seek_coagulation.setProgress(progress);
}
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
});
The first case (SeekBar to EditText) works well, but not the second.
The problem is that it is a loop (SeekBar to EditText to SeekBar to EditText etc.).
Is there a way to fix that ?
I suggest to use two boolean parameters to lock changes, like this:
boolean mIsTextLocked = false;
boolean mIsSeekBarLocked = false;
seek_coagulation = (SeekBar) findViewById(R.id.seek_coagulation);
EditText edit_coagulation = (EditText) findViewById(R.id.edit_coagulation);
seek_coagulation.incrementProgressBy(1);
seek_coagulation.setMax(1000);
seek_coagulation.setOnSeekBarChangeListener( new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
if(!mIsSeekBarLocked){
mIsTextLocked = true;
double value = progress / 100.0;
edit_coagulation.setText(String.valueOf(value));
}
}
public void onStartTrackingTouch(SeekBar seekBar)
{
}
public void onStopTrackingTouch(SeekBar seekBar)
{
mIsTextLocked = false;
}
});
edit_coagulation.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s)
{
if(!mIsTextLocked){
mIsSeekBarLocked = true;
double value = (s != null && s.toString().length() > 0) ?Double.parseDouble(s.toString().replace(',', '.')) : 0D;
int progress = (int) (value * 100);
seek_coagulation.setProgress(progress);
mIsSeekBarLocked = false;
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
});
I haven't tested it but it should work.
I have made use of seekbar for other requirements, now I have having trouble with a scale of this scope. I would like my max to be 350,000 (dollars) and my "min" or I should say "1" value to be 5,000. How would I implement this?
If i understand correctly you want the seekbar to move by 5,000 up to 350,000.
350,000 / 5,000 = 70
final TextView textView = findViewById(R.id.some_text);
seekBar.setMax(69);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
seekBar.setProgress(progress);
textView.setText(String.valueOf((progress+1)*5000));
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
Please tell me if I misunderstood :)
int min=5000;
int max=350000;
int value = (seekBar.getProgress()*(max-min))+min;
My seekbar range is 2-15 , i want to increase the progress by 0.5 for each click on the seekbar. I have browsed and could not find a way.
in oncreate()
sbTaux.setProgress(0) //default value.
sbTaux.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progress = progress / 1;
progress = progress * 1;
if (progress<=2){
progress=2;
sbTaux.setProgress(0);
}
else {}
txTaux.setText(Util.decimalFormat(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
Make range 4-30 and then just divide the result by 2 should do the trick
int myProgress;
SeekBar seekBar = (SeekBar)layout.findViewById(R.id.seekbar);
TextView seekBarValue = (TextView)layout.findViewById(R,id.TextView1);
seekBar.setProgress(2);
seekBar.incrementProgressBy(1/2);
seekBar.setMax(15);
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
myProgress = progress;
seekBarValue.setText(myProgress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}