I want to use Seekbar in setting to choose number 1 to 10. Is there any way to do this? I extend PreferenceActivity
for my setting activity.
As I found from here, but I don't know how to create Seekbar inside of the PreferenceScreen
. Thank in advance.
Here is activity
public class UserSettingActivity extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings); }
}
And setting.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="@string/pref_user_profile" >
<EditTextPreference
android:title="@string/pref_user_name"
android:summary="@string/pref_user_name_summary"
android:key="prefUsername"/>
</PreferenceCategory>
<PreferenceCategory android:title="@string/pref_update_setting" >
<CheckBoxPreference
android:defaultValue="false"
android:key="prefSendReport"
android:summary="@string/pref_send_report_summary"
android:title="@string/pref_send_report" >
</CheckBoxPreference>
<ListPreference
android:key="prefSyncFrequency"
android:entries="@array/syncFrequency"
android:summary="@string/pref_sync_frequency_summary"
android:entryValues="@array/syncFrequencyValues"
android:title="@string/pref_sync_frequency" />
</PreferenceCategory>
</PreferenceScreen>
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.
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.
You need to call setOnSeekbarChangeListener() on your seekbar object and let for example your activity implement OnSeekbarChangeListener() and in that function you can do whatever you want.
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.
If you don't mind using the Android.Support.v7.Preference Library, then you can use SeekBarPreference.
You could then use in XML:
<SeekBarPreference
android:key="keyName"
android:title="Property Label"
android:summary="Summary."
android:max="5"
android:defaultValue="0" />
There is currently no SeekBarPreference out of the box, though I have seen some official source code.
Maybe you want to use this library: https://github.com/MrBIMC/MaterialSeekBarPreference
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