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;
Related
I'd like to set a max progress value in a Seekbar that is not the same as the max value.
For example max would be 100 and max progress 50, so the progress slider could only be dragged to the middle. Is there a way to do that?
You can try the following
int seekBarMaxValue = 100;
int seekBarDraggableMaxValue = 50;
seekBar.setMax(seekBarMaxValue );
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangetListener() {
public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) {
seekBar.setProgress(progress > seekBarDraggableMaxValue ? seekBarDraggableMaxValue : progress);
}
});
Hope it helps.
It seems like you want three numbers only (ex. 0; 50; 100); just pretend each number is represents another number or multiply the progress by the common divisor:
Example
0 = 0 or (0 * 50)
1 = 50 or (1 * 50)
2 = 100 or (2 * 50)
int int_actual_number = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
seek_sample.setMax(2);
seek_sample.setProgress(0);
seek_sample.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int_actual_number = progress * 50;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
You can use different common divisors and amount of numbers to test this method for your needs.
I have been trying to create a simple app like this but I failed.
What should I do based on this answer? The code I use:
MainActivity.java
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SeekBar fseekbar = (SeekBar) findViewById(R.id.fseekBar2);
fseekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}
My relative Layout.xml
<LinearLayout
android:id="#+id/linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<SeekBar
android:id="#+id/fseekBar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
The problem is that I cannot create a window manager and set to layoutParams the value of the brightness.
Try this...
MainActivity.java
public class MainActivity extends Activity {
private SeekBar brightbar;
//Variable to store brightness value
private int brightness;
//Content resolver used as a handle to the system's settings
private ContentResolver cResolver;
//Window object, that will store a reference to the current window
private Window window;
TextView txtPerc;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Instantiate seekbar object
brightbar = (SeekBar) findViewById(R.id.brightbar);
txtPerc = (TextView) findViewById(R.id.txtPercentage);
//Get the content resolver
cResolver = getContentResolver();
//Get the current window
window = getWindow();
//Set the seekbar range between 0 and 255
brightbar.setMax(255);
//Set the seek bar progress to 1
brightbar.setKeyProgressIncrement(1);
try
{
//Get the current system brightness
brightness = System.getInt(cResolver, System.SCREEN_BRIGHTNESS);
}
catch (SettingNotFoundException e)
{
//Throw an error case it couldn't be retrieved
Log.e("Error", "Cannot access system brightness");
e.printStackTrace();
}
//Set the progress of the seek bar based on the system's brightness
brightbar.setProgress(brightness);
//Register OnSeekBarChangeListener, so it can actually change values
brightbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onStopTrackingTouch(SeekBar seekBar)
{
//Set the system brightness using the brightness variable value
System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness);
//Get the current window attributes
LayoutParams layoutpars = window.getAttributes();
//Set the brightness of this window
layoutpars.screenBrightness = brightness / (float)255;
//Apply attribute changes to this window
window.setAttributes(layoutpars);
}
public void onStartTrackingTouch(SeekBar seekBar)
{
//Nothing handled here
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
//Set the minimal brightness level
//if seek bar is 20 or any value below
if(progress<=20)
{
//Set the brightness to 20
brightness=20;
}
else //brightness is greater than 20
{
//Set brightness variable based on the progress bar
brightness = progress;
}
//Calculate the brightness percentage
float perc = (brightness /(float)255)*100;
//Set the brightness percentage
txtPerc.setText((int)perc +" %");
}
});
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Slide the bar to change the brightness:"
android:id="#+id/txtPercentage" />
<SeekBar
android:id="#+id/brightbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dip"
android:layout_marginRight="30dip" >
</SeekBar>
</LinearLayout>
Happy coding...
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 have a seekbar, while moving it I want to change values from 0 to 200. I have a TextView, where I display those values while moving the seekbar. But I don't want to have every value from that interval(0,1,2,3,4,5...), but to have 10, 20, 30...so on. So when I move the seekbar, I want to display 10,20,30,40....to 200. Can somebody give me a hint or an example how to do this?
Try below code
SeekBar seekBar = (SeekBar)layout.findViewById(R.id.seekbar);
seekBar.setProgress(0);
seekBar.incrementProgressBy(10);
seekBar.setMax(200);
TextView seekBarValue = (TextView)layout.findViewById(R.id.seekbarvalue);
seekBarValue.setText(tvRadius.getText().toString().trim());
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progress = progress / 10;
progress = progress * 10;
seekBarValue.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
setProgress(int) is used to set starting value of the seek bar
setMax(int) is used to set maximum value of seek bar
If you want to set boundaries of the seekbar then you can check the progressbar value in the onProgressChanged method. If the progress is less than or greater than the boundary then you can set the progress to the boundary you defined.
The easieset way I can think of, is simply defining:
SeekBar yourSeekBar = (SeekBar)findViewById(R.id.yourSeekBarId);
yourSeekbar.setMax(20);
Next, override those methods (the empty methods are also required, even if they are empty):
yourSeekbar.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) {
if (fromUser) {
if (progress >= 0 && progress <= sizeSeekBar.getMax()) {
String progressString = String.valueOf(progress * 10);
yourTextView.setText(progressString); // the TextView Reference
seekBar.setSecondaryProgress(progress);
}
}
}
});
The idea is to only define 20 values for the seekbar, but always multiply the value by 10 and display that value.
If you do not really need 200 values, then there is no point in using 200 values.
You can use the Slider provided by the Material Components Library using the android:stepSize attribute:
<com.google.android.material.slider.Slider
android:valueFrom="0"
android:valueTo="200"
android:stepSize="10"
..>
You should use the SeekBar.OnSeekBarChangeListener for tracking the progress change. Call mseek.setMax(200). Do a modulo operation on the current value to decide if the textview should be updated or not.
SeekBar.OnSeekBarChangeListener() {
void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (progress % 10 == 0) {
textview.setText(""+progress);
}
}
}
You can also achieve this by defining custom SeekBar class as below
public class CustomSeekBar extends AppCompatSeekBar {
int SEEKBAR_MULTIPLIER = 1;
public CustomSeekBar(Context context) {
super(context);
}
public CustomSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setSeekBarConfig( int SEEKBAR_MULTIPLIER, int SEEKBAR_MAX){
setMax(SEEKBAR_MAX);
this.SEEKBAR_MULTIPLIER = SEEKBAR_MULTIPLIER;
}
#Override
public int getProgress() {
return super.getProgress() * SEEKBAR_MULTIPLIER;
}
}
And can use by following manner
CustomSeekBar mCustomSeekBar = (CustomSeekBar) findViewById(R.id.customSeekBar);
mSeekBar.setSeekBarConfig(10,20);
In xml you should add CustomSeekBar instead of SeekBar
<*.CustomSeekBar
com.quemb.qmbform.view:setSeekBarConfig="10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/seekBar" />
So that you can reuse the custom seekBar in entire App with out repeating whole extra calculation.
for the minimum issue, you can set an offset on your progress. This way, the user can't go under your minimal value.
int offset = 10;
seekBar seekBar = (SeekBar)layout.findViewById(R.id.seekbar);
seekBar.setProgress(0);
seekBar.setMax(190); //with the offset, you need to adapt the max value
TextView seekBarValue = (TextView)layout.findViewById(R.id.seekbarvalue);
seekBarValue.setText(""+(seekBar.getProgress() + offset));
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progress += offset;
seekBarValue.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
use Math.round function
fun round(n: Int): Int {
// Smaller multiple
val a = n / 10 * 10
// Larger multiple
val b = a + 10
// Return of closest of two
return if (n - a > b - n) b else a
}
One possible solution would be to set seekBar.setMax(20) (or android:max="20" in XML), and whenever you use or display the value, multiply it by 10.
The SeekBar would then appear to move at least 20 at a time.
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);