Adding multiple seekbar values together in a textview - java

Having trouble adding values together from multiple Seekbars. Trying to send the total to a total_view but it keeps showing up as 0. Im using two seekbars and the total should change depending on the movement on the bar so Im using onSeekChangeListener
public class CalculateGradeActivity extends Activity {
TextView text_view; //shows percentage
TextView text_view2; //shows percentage
TextView total_view; //show total from text_view and text_view2
SeekBar seek_bar1;
SeekBar seek_bar2;
int exam_grade;
int homework;
int total;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculate_grade_view);
seek_bar1 = (SeekBar) findViewById(R.id.seekBarExam);
seek_bar2 = (SeekBar) findViewById(R.id.seekBarHomework);
text_view = (TextView) findViewById(R.id.percentageSeekbar1);
text_view2 = (TextView) findViewById(R.id.percentageSeekbar2);
total_view = (TextView) findViewById(R.id.numeric_grade_id_output);
seekbar();
}
public void seekbar() {
int progress = seek_bar1.getProgress();
text_view.setText(String.valueOf(progress));
int progress2 = seek_bar2.getProgress();
text_view2.setText(String.valueOf(progress2));
total = exam_grade + homework;
seek_bar1.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
text_view.setText(String.valueOf(progress) + "%");
exam_grade = progress;
total_view.setText(String.valueOf(total));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
seek_bar2.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
text_view2.setText(String.valueOf(progress)+"%");
homework = progress;
total_view.setText(String.valueOf(total));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
}
}

Every time you change a value you have to recalculate the total before you display it.
seek_bar1.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
text_view.setText(String.valueOf(progress) + "%");
exam_grade = progress;
total = exam_grade + homework;
total_view.setText(String.valueOf(total));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
seek_bar2.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
text_view2.setText(String.valueOf(progress)+"%");
homework = progress;
total = exam_grade + homework;
total_view.setText(String.valueOf(total));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
ja

Related

multiple seekbar values in TextView by implementing onSeekBarChangeListener

I'm not able to get multiple SeekBar values in multiple TextViews by implementing OnSeekBarChangeListener interface.
By anonymous class I have implemented this but I want this way for my application.
seekBar1.setOnSeekBarChangeListener(this);
}
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
switch (seekBar.getId()){
case R.id.seek1:{
seek1Val=String.valueOf(2000+i*1000);
tvAmount.setText(seek1Val);}
break;
case R.id.seek2:{
seek2Val = String.valueOf(0+i*.5);
tvRate.setText(seek2Val);
}
break;
}
I expect output in each TextView for different SeekBar but value of progress I is taking same for every SeekBar.
better would be to add different setOnSeekBarChangeListener for each SeekBar.
Or if you don't want that then use tag instead id
Best would be:
seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seek1Val=String.valueOf(2000+i*1000);
tvAmount.setText(seek1Val);}
}
#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) {
seek2Val = String.valueOf(0+i*.5);
tvRate.setText(seek2Val);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});

Multiple seekbar in one java class, need implement each setOnSeekBarChangeListener for each seekbar?

I need to make multiple seek bar in the same Java class. However, I wonder do we need to set each setOnSeekBarChangeListener for each seek bar? The code seems too long if I am doing so (I need to create 10 seek bar in the same class). Is there any other solution which I can shorten the code? Thank you so much for helping. :)
Below is my code
private SeekBar seekbar,seekbar1;
private TextView a,b;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.schiassess);
seekbar = (SeekBar) findViewById(R.id.delubar);
seekbar1 = (SeekBar) findViewById(R.id.disorbar);
a = findViewById(R.id.a);
b = findViewById(R.id.b);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int val = (progress * (seekBar.getWidth() - 2 * seekBar.getThumbOffset())) / seekBar.getMax();
progress++;
a.setText("" + progress);
a.setX(seekBar.getX() + val + seekBar.getThumbOffset() / 2);
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
seekbar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int val = (progress * (seekBar.getWidth() - 2 * seekBar.getThumbOffset())) / seekBar.getMax();
progress++;
b.setText("" + progress);
b.setX(seekBar.getX() + val + seekBar.getThumbOffset() / 2);
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
}
Follow the below steps it's simple
JUST IMPLEMENT SeekBar.OnSeekBarChangeListener in your class/fragment
public class Home extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
}
You don't have to write new OnSeekBarChangeListener for Every Seekbar...
Just Implement
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
} to your Activity/Fragment..
Now you can check for the id and perform action according to it

how to make reset button automatically play again when its clicked?

another thing is the timer for seekBar its for the duration of the audio file the issue within it is that the audio file do (mini) stop every 1 sec so how to make seek bar update itself every second without doing any stops
seekBar2.setMax(mediaPlayer.getDuration());
seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mediaPlayer.seekTo(progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
seekBar2.setProgress(mediaPlayer.getCurrentPosition());
}
},0,1000);
public void reset(View v) {
mediaPlayer.reset();
}
Try this to Reset Your mediaPlayer
public void reset(View v) {
mediaPlayer.seekTo(0);
mediaPlayer.start();
}
Try this to update your seekbar
private Handler mHandler = new Handler();
//you have to update Seekbar on UI thread
Your.Activity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
if(mediaPlayer != null){
int mCurrentPosition = mediaPlayer.getCurrentPosition() / 1000;
seekBar2.setProgress(mCurrentPosition);
}
mHandler.postDelayed(this, 1000);
}
});
and whenyou need to update the MediaPlayer's position while user drag your SeekBar use below code
seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(mediaPlayer != null && fromUser){
mediaPlayer.seekTo(progress * 1000);
}
}
});

how can i get progress from seekbar?

so i want to take progress from seek bar and use it in button method, but its always 0(counter),doesnt work. I tried seekB.getProgress();,seekbar works great...
int counter;
seekB.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
counter = progress;
seekbarValue.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
//Randomise Button
but.setOnClickListener(new View.OnClickListener() {
int Plength = 6;
char[] i = new char[Plength];
List<Character> pass = new ArrayList<>();
#Override
public void onClick(View v) {
for (int x = 0; x < Plength; x++) {
pass.add(azL[rand.nextInt(azL.length)]);
pass.add(nzL[rand.nextInt(nzL.length)]);
pass.add(AZL[rand.nextInt(AZL.length)]);
i[x] = pass.get(rand.nextInt(pass.size()));
}
textV.setText(String.valueOf(i));
pass.clear();
}
});
if i do textV.setText(String.Valueof(counter)); is going to display 0 all the time.i dont know how to fix it
Try to change it :
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
counter = progress;
seekbarValue.setText(String.valueOf(progress));
}
to
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
counter = progress;
seekbarValue.setText(String.valueOf(new Integer(progress)));
}

Adjust EditText with Seekbar

I have a SeekBar and EditText.I would like to create a % slider that when the user slides the Seekbar, it adjusts the EditText field.
i tried this:
mInterestRateEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
int i = Integer.parseInt(s.toString());
if (i >= 0 && i <= 100) {
mInterestRateSeekBar.setProgress(50);
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
mInterestRateSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mInterestRateEditText.setText(progress + "");
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
but all it does is the second i try sliding the slider, it locks the EditText to 0, and I cannot move the slider?
As I already stated in my comment, you are creating a loop. Changing the Seekbar, changes the text. This changes the seekbar again.
Simple solution: work with a boolean.
boolean changedProgrammatically;
mInterestRateEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
int i = Integer.parseInt(s.toString());
if(!changedProgrammatically){
if (i >= 0 && i <= 100) {
mInterestRateSeekBar.setProgress(50);
}
}else{
changedProgrammatically = false;
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
mInterestRateSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
changedProgrammatically = true;
mInterestRateEditText.setText(progress + "");
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});

Categories