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
Related
This question already has answers here:
How to set seekbar min and max value
(15 answers)
Closed 3 years ago.
i have this code to make some editor text and custom font size with seekbar, how to make a minimum value for int progres seekbar?
seekerFont.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
fontsize.setText(""+ progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
You can set minimum or maximum progress
eekerFont.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
public void onStopTrackingTouch(SeekBar seekBar) {
//TODO My code goes here
int CurrentLevel = eekerFont.getProgress();
if (CurrentLevel < 30) CurrentLevel = 30;
eekerFont.setProgress(CurrentLevel);
}
public void onStartTrackingTouch(SeekBar seekBar){
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser){
}});
In XML Also you can set
android:progress="10"
eekerFont.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int progressChanged = minimumValue;
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progressChanged = minimumValue+ progress;
}
});
Link: Android SeekBar Minimum Value
Another better way is to do with little logic, For example you want to set values between 50 to 100. Then Do like this
seekBar.setMax(50);
int selectedProgress = seekBar.getProgress() + 50; // or in change listener
I want to create seek bar, I want increment like this seek bar
minvalue = 5, maxvalue = 10
seek bar value increments : 0 1 2 3 4 5 6
I want seek bar values like this : 5.10, 5.20, 5.30... 9.90, 10
How to achieve this.?
aSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
float value = ((float)progress / 20.0);
String yourprogress = String.valueOf(value+5.0f);
// yourprogress is your desiger output
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
aSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
float value = ((float)progress / 20.0);
String progress = String.valueOf(value+5.0f);
// 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) {}
});
this is impossible with float.
You have to make a workaround by multiply your increment.
see this response :
duplicate
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 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);
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);