28

I would like to create a seekbar for an Android app that allows the user to select a value between -5 and 5 (which maps to "strongly disagree" and "strongly agree"). How do I make a seekbar with discrete values? or is there a better UI widget I could use for this?

Thanks.

einverne
  • 5,828
  • 4
  • 37
  • 83
vee
  • 281
  • 1
  • 3
  • 3

7 Answers7

21

The Seekbar works great for discrete values. We use a Seekbar for discrete data as shown below. To show which item is selected, we just change the font of the selected text view so it is bigger. You could also highlight by changing the background color or something. It works pretty well. You will want to call setMax(11) on the seek bar, and then in your code you need to translate between the range (0 through 11) and (-5 through 5) appropriately.

 <LinearLayout 
           android:orientation="vertical"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:gravity="center_horizontal">
     <LinearLayout 
           android:orientation="horizontal"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:gravity="center_horizontal">
    <TextView android:text="-5"
            android:id="@+id/answerNegative5"
            android:textSize="25sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    <TextView android:text="-4"
            android:id="@+id/answerNegative4"
            android:textSize="25sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    <TextView android:text="-3"
            android:id="@+id/answerNegative3"
            android:textSize="25sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    .....
  </LinearLayout>

  <SeekBar android:id="@+id/intensitySlider"
                    android:layout_weight="0"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" />
</LinearLayout>
Jay Askren
  • 9,793
  • 13
  • 48
  • 73
19

If you want to implement discrete seekbar with number of gaps without using third party library then use style property of seekbar.

<SeekBar
     android:id="@+id/sb"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:max="10"
     android:thumb="@drawable/ic_location"
     android:theme="@style/Widget.AppCompat.SeekBar.Discrete" />

enter image description here

Prashant Jajal
  • 2,856
  • 2
  • 20
  • 34
7

I don't know what is the issue here, you add a seekbar having a range of 0-10. Then you can map these values to -5 if you substract -5 from the selected value.

EDIT

add android:max="10" to the xml definiton and you get a fixed size seekbar.

You maybe should consider to add a textview to denote the current selected value's textual representation such as: Strongly Disagree. To update this view, subscribe to onProgressChanged event and progress parameter will give you the chosen number.

SeekBar s = (SeekBar) findViewById(R.id.SeekBar);
s.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

                    @Override
                    public void onProgressChanged(SeekBar seekBar,
                            int progress, boolean fromUser) {
                                    }
}
Pentium10
  • 190,605
  • 114
  • 394
  • 474
2

I hope this code surely helpes 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));

         }
     });
 }
Nikhil Agrawal
  • 23,885
  • 19
  • 85
  • 117
Balaji Gunasekar
  • 1,323
  • 11
  • 5
  • 4
    Welcome to Stack Overflow! Be careful when posting copy and paste boilerplate/verbatim answers to multiple questions, these tend to be flagged as "spammy" by the community. If you're doing this then it usually means the questions are duplicates so flag them as such instead. – Kev Jul 24 '12 at 23:58
0

1-st step : set maxVal to 10;

2-nd step :

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
        boolean fromUser) 
   {
    switch(seekBar.getId())
    {
    case R.id.mySeekBar:
        int prValue = progress - 5;
        editText.setText(String.valueOf(preValue));
        break;
    }
}
Mickey Tin
  • 2,983
  • 7
  • 35
  • 66
0

Just use the material components library with something like:

<com.google.android.material.slider.Slider
    android:stepSize="1"
    android:valueFrom="-5"
    android:valueTo="5"
    />

enter image description here

Gabriele Mariotti
  • 192,671
  • 57
  • 469
  • 489
-1
      <SeekBar
                android:id="@+id/seekbar"
                style="@style/SeekBarWithoutSteps"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="22dp"
                android:layout_marginRight="22dp"
                android:layout_marginTop="@dimen/margin_10"
                android:max="4"
                android:maxHeight="@dimen/margin_5"
                android:minHeight="@dimen/margin_5"
                android:paddingLeft="@dimen/margin_10"
                android:paddingRight="@dimen/margin_10"
                android:progressBackgroundTint="@color/colorGray"
                android:progressTint="@color/colorGreen"
                android:theme="@style/Widget.AppCompat.SeekBar.Discrete"
                android:thumb="@drawable/filled_green"
                android:thumbOffset="15dp" />
Pratibha Sarode
  • 1,583
  • 14
  • 16