Seekbar with 1 decimal - java

I need my seekbar to show 1 decimal, ex 4.5.
It show as a integer now.
seekBar.setProgress((int)Float.parseFloat(Item.getRating()));
Thx.

I think this help you
aSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
float value = ((float)progress / 10.0);
// value now holds the decimal value between 0.0 and 10.0 of the progress
// Example:
// If the progress changed to 45, value would now hold 4.5
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});

Related

How to programmatically delete a seekbar in android studio

I am trying to write a program in which I am deleting the seekbar if the seekbar is set to zero. So far, I have been able to remove the visibility of the seekbar from the android screen, but it is not deleted completely. Can anyone please hep me so that I can completely remove the seekbar when the position of the seekbar is set to zero.
Following is my code snippet:
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if(i==0)
{
sb.setVisibility(View.INVISIBLE);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
You can use sb.setVisibility(View.GONE);

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

Adding multiple seekbar values together in a textview

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

Set the progress in Android

I'm making a project - Android - Arduino.
I have a seekbar to handle the temperature, to set it to 20'-35'.
When i open the program i want the seekbar to be set to the temperature that the sensor is set, not to 0 and let me set it.
This is the code for my seekbar:
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
tvTemp.setText(Double.toString(progress + 20));
}
public void onStartTrackingTouch(SeekBar seekBar) {}
public void onStopTrackingTouch(SeekBar seekBar) {}
});
<SeekBar
android:id="#+id/seek_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="35"
android:progress="20" />
Through xml you can do it as :
android:progress="20" means 20 is default position. Starting point of seek bar.
Or in your code you can do it like:
your_seekBar.setProgress(start_position);
ie.your_seekBar.setProgress(20);
Then you can try :
your_seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int progressChanged = 0;
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
progressChanged = progress;
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(your_activity.this,"seek bar progress:"+progressChanged,
Toast.LENGTH_SHORT).show();
}
});
Just add
seekBar.setProgress(value)
below
seekBar.setOnSeekBarChangeListener
If your value is between 20 and 35, you may also want to specify
android:max="35"
android:min="20"
for your seekbar in your xml layout
SeekBar is inherited from ProgressBar,
So you can use 'setProgress()' method to set progress on startup.
seekBar.setProgress(someValue);
it will work
You can set it in your xml file:
android:progress="20"

Categories