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.
In Android Discrete SeekBar is just an advancement of progressBar just like the SeekBar, the only difference in SeekBar and discrete SeekBar being that in discrete SeekBar, we can only set the value only to discrete values like 1, 2, 3, and so on.
android.widget.SeekBar. A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys. Placing focusable widgets to the left or right of a SeekBar is discouraged.
Step1: Create a new project. After that, you will have java and XML file. Step2: Open your xml file and add a SeekBar and TextView for message as shown below, max attribute in SeekBar define the maximum it can take. Assign ID to SeekBar And TextView.
In Android, SeekBar is an extension of ProgressBar that adds a draggable thumb, a user can touch the thumb and drag left or right to set the value for current progress. SeekBar is one of the very useful user interface element in Android that allows the selection of integer values using a natural user interface.
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>
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With