How to simultaneously change the value of one seekbar based on other - android

Have three seekbars on one Activity. All of them have max of 100. I need to sum of all three was 100.
When seek1=50 and seek2=45 then seek3 must be 5.
When I slide seek1 to 55 seek3 must have 0. etc.
How can I code this?
Sorry, here is my code:
//01
final SeekBar seekBar01 = (SeekBar)findViewById(R.id.seek01);
final TextView seekBar01Value = (TextView)findViewById(R.id.txt01val);
seekBar01.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekBar01Value.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
//02
SeekBar seekBar02 = (SeekBar)findViewById(R.id.seek02);
final TextView seekBar02Value = (TextView)findViewById(R.id.txt02val);
seekBar02.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekBar02Value.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
//03
SeekBar seekBar03 = (SeekBar)findViewById(R.id.seek03);
final TextView seekBar03Value = (TextView)findViewById(R.id.txt03val);
seekBar03.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekBar03Value.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
I can show values on respective TextViews, but don't get how to change another seebar progress from other seekbar onProgressChanged.

Set OnSeekBarChangeListener to your seekbars. When one changes, set the values to the others with setProgress

Trouble was because of late declarations of SeekBar's.
If gather all SeekBar seekBarXX = (SeekBar)findViewById(R.id.seekXX); sooner than Change listeners all works as expected.
Sorry for stupidity.
Here is code:
final SeekBar seekBar01 = (SeekBar)findViewById(R.id.seek01);
final SeekBar seekBar02 = (SeekBar)findViewById(R.id.seek02);
final SeekBar seekBar03 = (SeekBar)findViewById(R.id.seek03);
seekBar01.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekBar02.setProgress(100-seekBar01.getProgress());
}
...
});

Related

running a method when a seekbar changes in any way

I need to run a method every time the position of my seekbar is changed.
It does not matter:
how far it has moved
the direction
if its at the end or the start.
just that it has changed
is there a equivalent to onclickListener() for a seekbar.
Use this method provided by SeekBar class:
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//progress changed
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
The seekbar has a setOnSeekBarChangeListener. Check this for more details https://developer.android.com/reference/android/widget/SeekBar.OnSeekBarChangeListener.html
seekBar.setOnSeekBarChangeListener(
new OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar,
int progresValue, boolean fromUser) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});

Implementation of seekbar start code function write in stop fuction in media player

i want to convert write onprogress function into stop. means when is release seekbar then volume is up and down.
sB.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i/*progress*/, boolean b) {
aM.setStreamVolume(AudioManager.STREAM_MUSIC,i,0);
// Log.i("Progress","Called"+i);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
Log.i("Start","Called");
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
Log.i("Stop","Called");
}
});
Do like this::
private int volumLevel = 0;
sB.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i/*progress*/, boolean b) {
if(b){
volumLevel = i;
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
Log.i("Start","Called");
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
aM.setStreamVolume(AudioManager.STREAM_MUSIC,volumnLevel,0);
Log.i("Stop","Called");
}
});
asper our knowledge onStopTrackingTouch event is called after releasing seekbar, so you can update your volume when called.
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
//update your volume using
seekBar.getProgress();
}

Changing textview's value using 2 seekbar

I have 2 seekbars for setting up a timer one for hours and 1 for minutes. I want to show the total duration of the timer by getting the progress of the 2 seekbar and inserting it in 1 textview with a hh:mm format without using any button.
here are my seekbars:
hseek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int shours;
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
shours = progress;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
hours = shours;
}
});
mseek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int smins;
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
smins = progress;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
mins = smins;
}
});
You are saving the progress value of both of the seek bars in two variables, set the textview's text in OnStopTrackingTouch in both of the listeners
like this
yourTextview.setText(String.format("%02d:%02d", hours, mins));

Multiple vertical seekbar

I want to implement 3 vertical SeekBar in a page so that it looks like a bar chart. When user drags any one of the SeekBar the value corresponding to the y axis should be retrieved for that. Other SeekBar should remain in their original state.
Our idea is to implement interactive bar chart with dragging feature. Since that is not possible in open library, we decided to use seek bar instead by increasing the width.
Any sample app with this will be helpful.
public class multiverticleseek extends Activity {
/** Called when the activity is first created. */
TextView textView3,textView2,textView1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.multiseek);
textView1 = (TextView)findViewById(R.id.textView1);
textView2 = (TextView)findViewById(R.id.textView2);
textView3 = (TextView)findViewById(R.id.textView3);
bar1=(VerticalSeekBar)findViewById(R.id.seekBar1);
bar2=(VerticalSeekBar)findViewById(R.id.seekBar2);
bar3=(VerticalSeekBar)findViewById(R.id.seekBar3);
bar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
textView1.setText(Integer.toString(progress)+"%");
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
bar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
textView2.setText(Integer.toString(progress)+"%");
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
bar3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
textView3.setText(Integer.toString(progress)+"%");
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}

two different seekbars with different tasks

I have one SeekBar which does specific task. I want to add another SeekBar with different task.
How can I put
seekbar.setOnSeekBarChangeListener(this);
unique for each SeekBar. Now when I touch either seekbar it results same.
You have a couple options:
1) The common practice is to use an anonymous class. Like this:
curSlider.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
Log.d("slider","stop tracking");
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
});
2) If you want to use this, you need to use some logic to figure out which SeekBar is being moved (using ids or object references). Like:
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if (seekBar == mySeekBar1) {
} else if (seekBar == mySeekBar2) {
}
}
you can add any no. of seekbars and every one to do something
I have a sample code with 3 seekbars every one send its progress value and all together create a colore to set as a background to imageview
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;
public class MainActivity extends Activity {
SeekBar seekBar1, seekBar2, seekBar3;
ImageView imageView;
public int pro1,pro2,pro3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) this.findViewById(R.id.imageView);
seekBar1 = (SeekBar) this.findViewById(R.id.seekBar1);
seekBar2 = (SeekBar) this.findViewById(R.id.seekBar2);
seekBar3 = (SeekBar) this.findViewById(R.id.seekBar3);
seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
pro1= progress;
imageView.setBackgroundColor(Color.argb(200, pro1, pro2, pro3));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
pro2= progress;
imageView.setBackgroundColor(Color.argb(200, pro1, pro2, pro3));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
seekBar3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
pro3=progress;
imageView.setBackgroundColor(Color.argb(200, pro1, pro2, pro3));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
this youtube
https://youtu.be/AM6lJnze9nA

Categories

Resources