I have a seekbar for example: from 0 to 10. How can I make if I want to set seekbar to 5 as default position. So in this example seekbar should start from middle.
Use this one
android:progress="5"
Like "progress", it inherits some of it's attributes from ProgressBar. You find all the attributes at http://developer.android.com/reference/android/widget/SeekBar.html
To get Seekbar at the value of 5
In the xml layout use the max value as 10
android:max="10"
In the java for seekbar to get at progress 5
seekbar = (SeekBar) findViewById(R.id.seekBar1);
seekbar.setProgress(5);
thats it :)
I hope this code surely helps you.
Try this...
float discrete=0;
float start=0;
float end=100;
float start_pos=0;
int start_position=0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start=-10; //you need to give starting value of SeekBar
end=10; //you need to give end value of SeekBar
start_pos=5; //you need to give starting position value of SeekBar
start_position=(int) (((start_pos-start)/(end-start))*100);
discrete=start_pos;
SeekBar seek=(SeekBar) findViewById(R.id.seekBar1);
seek.setProgress(start_position);
seek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "discrete = "+String.valueOf(discrete), Toast.LENGTH_SHORT).show();
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
// TODO Auto-generated method stub
// To convert it as discrete value
float temp=progress;
float dis=end-start;
discrete=(start+((temp/100)*dis));
}
});
}
default value By xml :
android:progress="5"
max value by xml
android:max="10"
final Timer timer = new Timer();
seekBar_efficiency.setMax(40);
seekBar_efficiency.setProgress(35);
timer.schedule(new TimerTask() {
#Override
public void run() {
seekBar_efficiency.setProgress(20);
timer.cancel();
}
}, 200, 50);
Related
i am using a seekbar and its maxvalue is 30 and progress by 1.
i need seekbar to start from value 8 so that if we seek to the left the progress should not go less than 8 and it should remain at value 8
SeekBar seek = (SeekBar)dialog.findViewById(R.id.your_dialog_seekbar);
seek.setProgress(8);
seek.incrementProgressBy(1);
seek.setMax(30);
seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int topsize;
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
TextView t1=(TextView)dialog.findViewById(R.id.df);
t1.setText(String.valueOf(progress));
TextBox = (TextView)findViewById(R.id.t9);
// TextBox.setTextSize(40);
TextBox.setTextSize(TypedValue.COMPLEX_UNIT_SP, progress);
}
});
dialog.show();
XML IS
<SeekBar
android:id="#+id/your_dialog_seekbar"
android:layout_width="0dp"
android:layout_weight="3"
android:paddingTop="7dp"
android:layout_height="wrap_content"
>
</SeekBar>
Basically you can not set min value to seekbar
So you can not do like this seek.setMax(7);
but of course you can handle it see this
how you can handle it
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;
I need a seekbar with value from 1 to 6 and step 0.5. So, values must are : 1 - 1.5 - 2 - 2.5 ...
What is the correct algorithm ?
I would store the possible values in an array and use the current seek-bar value as the element-index for accessing the "desired" value.
Like this
int[] values = {0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4};
int current = values[ seekbar.getValue() ];
Define the seekbar in layout like this
<SeekBar
android:id="#+id/seekbar1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:progress="0"
android:max="5"/>
And add this in your activity
public class MainActivity extends Activity {
private SeekBar seekBar;
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = (SeekBar) findViewById(R.id.seekBar1);
for(i=0;i<seekBar.getMax();i=i+0.5){
Thread.sleep(500);
seekBar.setProgress(i);
}
}
You could do this:
<SeekBar
android:id="#+id/seek"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="12" />
and then access the seek bar with:
mSeekBar = (SeekBar) findViewById(R.id.seek);
mSeekBar.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) {
Toast.makeText(MainActivity.this, "" + (float) progress / 2, Toast.LENGTH_SHORT).show();
}
});
This will give you a toast with every change. You've just to divide each progress by 2.
EDIT 1:
Or you can get the actual value with:
float value = (float) mSeekBar.getProgress() / 2;
I am using a seekbar, and I want to add certain marks in the seekbar with text to use it as a scale. The selected interval points should be highlighted.
The corresponding code is here:
seekbar = (SeekBar) findViewById(R.id.seekBar1);
seekbar.setMax(max);
seekbar.setProgress(50);
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
value.setText("SeekBar value is " + progress);
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
To make the above seek bar you first have to add a measuring scale as a background drawable. Then you'll have to make textboxes for each interval on the measuring scale.
<SeekBar
....
android:progressDrawable="#drawable/progress"
android:thumb="#drawable/thumb"
....
/>
Then
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
progress = ((int)Math.round(progress/interval))*interval;
seekBar.setProgress(progress);
if(progress==5) textBox5.setTextColor(Color.RED);
else if(progress==10) textBox10.setTextColor(Color.RED);
//......... for all the text boxes.
}
Actually, all The above solutions force to select the last progress point, which is not logic.
To make it mire accurete, Seek bar must detect which is the nearest dot and jump to it.
So below is the solution,
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progressStep = getMax() / dotsCount;
int lastDotProgress = Math.round(getProgress() / progressStep) * progressStep;
int nextDotProgress = lastDotProgress + progressStep;
int midBetweenDots = lastDotProgress + (progressStep / 2);
if (getProgress() > midBetweenDots)
seekBar.setProgress(nextDotProgress);
else
seekBar.setProgress(lastDotProgress);
}
You can use same behavior in onProgressChanged() if you want to prevent seeking between dots
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
int s_size=5;
progress = ((int)Math.round(progress/s_size))*s_size;
seekBar.setProgress(progress);
textview.setText(progress + "");
}
what you want to settext get easily get from progress and
textview.settext() value
I'd like to set max and minimum limits of SeekBar to 50 and 20 respectively.
SeekBar has a direct option top provide max value, but how to set its minimum value to 20 rather than 0?
In SeekBar you can set only max value.
<SeekBar android:id="#+id/SeekBar01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="50"/>
And,
You cannot directly set the minimum value to the seekbar.
SeekBar mSeekbar = (SeekBar) findViewById(R.id.SeekBar01);
mSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
length_edit.setText(Integer.toString(progress + 20));
}
public void onStartTrackingTouch(SeekBar seekBar) {}
public void onStopTrackingTouch(SeekBar seekBar) {}
});
As you see min progress you can sum with min which I would like - in your case 20.
Set the max value to 30 and add 20 to the values you read from the SeekBar.
The simplest way to do this is to just limit it in the SeekBar.OnSeekBarChangeListener() for your SeekBar.
First, you need to create field with min or default value. Then set this value when creating SeekBar:
private int fillDefault;
In onCreateView() of a Fragment or onCreate of an Activity:
fillDefault = 20;
fillSeekBar.setProgress(fillDefault);
Second, implement the listener for it:
fillSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// Notification that the progress level has changed.
if (progress < fillDefault){
seekBar.setProgress(fillDefault); // magic solution, ha
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// Notification that the user has started a touch gesture.
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Notification that the user has finished a touch gesture.
}
});
It works. Simple and awesome:D
For example;
public static int LENGTH_MIN_VALUE = 20;
public static int LENGTH_MAX_VALUE = 50;
SeekBar seekBar = (SeekBar) findViewById(R.id.passwordSeekBar);
seekBar.setMax(LENGTH_MAX_VALUE);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(progress <= Constract.PASSWORD_LENGTH_MIN_VALUE){
progress = Constract.PASSWORD_LENGTH_MIN_VALUE + progress;
}
String numberAsString = String.valueOf(progress);
seekNumberView.setText(numberAsString);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
I have answered this question here : How to set seekbar min and max value.
With simple math you can set the min value, the max value and even the step of your seekbar.
Hope this helps !
You can try this:
seekBar.setMax(max_value-min_value);