LibGDX Android SeekBar


I am working on a Live Wallpaper using Libgdx and I needed a seekBar preference that I can set from Android.
As usual I searched the net for something that I can reuse and found a very nice implementation that suits my need .
The first issue that came up while working with Preference Backend and Libgdx is that Libgdx requires a file name from which it will read the preferences
So the following two lines are necessary on the android side.


Here “preferences” is the file name .

Now when I integrated the SeekBarPreference I found that it is able to set the values correctly and Libgdx is able to use them , but when my seek bar is loading it is reading default value every time. A quick check confirmed that it is writing to the preferences mentioned above but it is reading from other preference , maybe the default one.
To fix this I changed the function in

public SharedPreferences getSharedPreferences() {

return mContext.getSharedPreferences("preferences", 0);

protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
super.onSetInitialValue(restoreValue, defaultValue);

if(restoreValue) {
mCurrentValue = getSharedPreferences().getInt(mKey, mCurrentValue);
else {
int temp = 0;
try {
temp = (Integer)defaultValue;
catch(Exception ex) {
Log.e(TAG, "Invalid default value: " + defaultValue.toString());

mCurrentValue = temp;


I am overriding the getSharedPreferences() function to return my shared preference file.

The mCurrent value instead of taking from getPersistedInt I am getting from my preference file.
To get this to work I am getting key and context from the constructor .

I have attached the file for Reference.

Here is how it looks in my sample app.



Siyanatullah Khan

Siyanatullah Khan

Mobile Software consultant with a penchant for blogging.

More Posts

Follow Me:
TwitterLinkedInGoogle Plus

Add a Comment

HTML Snippets Powered By :