Related
I have a on/off switch called audio_switch. In my MainActivity file I want to look if the switch is on, if so, it should set a variable audio = 1. This piece of code mentioned can be found at the bottom of my MainActivity. On the very top after you can see I added a line to find my audio file. A few lines later I used an if(audio == 1) statement to check if the switch is on, so it will play the audio. This however does not work, it does however when I change the statement to audio == audio
package com.joeridukker.main;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.support.constraint.ConstraintLayout;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
public static int audio;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer MansNotHot = MediaPlayer.create(this, R.raw.mans_not_hot);
if(audio == 1){
MansNotHot.start();
}
int secondsDelayed = 1;
Handler start = new Handler();
start.postDelayed(new Runnable() {
public void run() {
Animation wait = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.wait);
ConstraintLayout Entire = (ConstraintLayout) findViewById(R.id.Entire);
ImageView Logo = (ImageView) findViewById(R.id.Logo);
TextView Welcome = (TextView) findViewById(R.id.Welcome);
Welcome.startAnimation(wait);
Entire.startAnimation(wait);
Logo.startAnimation(wait);
// Actions to do after 10 seconds
}
}, 0);
Handler animation = new Handler();
animation.postDelayed(new Runnable() {
public void run() {
Animation slide_up = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_and_slide_upwards);
Animation slide_down = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_and_slide_downwards);
TextView Welcome = (TextView) findViewById(R.id.Welcome);
ImageView Logo = (ImageView) findViewById(R.id.Logo);
Logo.startAnimation(slide_down);
Welcome.startAnimation(slide_up);
// Actions to do after 10 seconds
}
}, 500);
Handler entire_animation = new Handler();
entire_animation.postDelayed(new Runnable() {
public void run() {
Animation fade_in = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_in);
ConstraintLayout Entire = (ConstraintLayout) findViewById(R.id.Entire);
TextView Welcome = (TextView) findViewById(R.id.Welcome);
Entire.startAnimation(fade_in);
ObjectAnimator.ofObject(
Welcome,
"textColor",
new ArgbEvaluator(),
Color.argb(140,0,0,0),
Color.WHITE
).setDuration(500)
.start();
}
}, 500);
new Handler().postDelayed(new Runnable() {
public void run() {
Handler splash = new Handler();
int a = 1;
Intent Intent = new Intent(MainActivity.this, Differentiate.class);
Intent.putExtra("HEADER", a);
startActivity(Intent);
overridePendingTransition(R.anim.fade_in_switch_fast,R.anim.fade_out_switch_fast);
finish();
}
}, secondsDelayed * 2500);
}
public static class SettingsActivity extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.pref_general);
//Check the current value in preference.
SharedPreferences switchPrefStatus = PreferenceManager.getDefaultSharedPreferences(getActivity());
boolean switchPrefValue = switchPrefStatus.getBoolean("audio_switch", false);
Toast.makeText(getActivity(), "Current value: " + switchPrefValue, Toast.LENGTH_SHORT).show();
Preference switchPref = (Preference) findPreference("audio_switch");
switchPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object o) {
boolean isOn = (boolean) o;
//Do what you want with the value!:)
if (isOn) {
audio = 1;
}else{
audio = 0;
}
return true;
}
});
}
}
}
My SettingsActivity
package com.joeridukker.main;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.RingtonePreference;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.text.TextUtils;
import android.view.MenuItem;
import java.util.List;
/**
* A {#link PreferenceActivity} that presents a set of application settings. On
* handset devices, settings are presented as a single list. On tablets,
* settings are split by category, with category headers shown to the left of
* the list of settings.
* <p>
* See <a href="http://developer.android.com/design/patterns/settings.html">
* Android Design: Settings</a> for design guidelines and the <a
* href="http://developer.android.com/guide/topics/ui/settings.html">Settings
* API Guide</a> for more information on developing a Settings UI.
*/
public class SettingsActivity extends AppCompatPreferenceActivity {
/**
* A preference value change listener that updates the preference's summary
* to reflect its new value.
*/
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference.setSummary(
index >= 0
? listPreference.getEntries()[index]
: null);
} else if (preference instanceof RingtonePreference) {
// For ringtone preferences, look up the correct display value
// using RingtoneManager.
if (TextUtils.isEmpty(stringValue)) {
// Empty values correspond to 'silent' (no ringtone).
preference.setSummary(R.string.pref_ringtone_silent);
} else {
Ringtone ringtone = RingtoneManager.getRingtone(
preference.getContext(), Uri.parse(stringValue));
if (ringtone == null) {
// Clear the summary if there was a lookup error.
preference.setSummary(null);
} else {
// Set the summary to reflect the new ringtone display
// name.
String name = ringtone.getTitle(preference.getContext());
preference.setSummary(name);
}
}
} else {
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
}
return true;
}
};
/**
* Helper method to determine if the device has an extra-large screen. For
* example, 10" tablets are extra-large.
*/
private static boolean isXLargeTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
}
/**
* Binds a preference's summary to its value. More specifically, when the
* preference's value is changed, its summary (line of text below the
* preference title) is updated to reflect the value. The summary is also
* immediately updated upon calling this method. The exact display format is
* dependent on the type of preference.
*
* #see #sBindPreferenceSummaryToValueListener
*/
private static void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
// Trigger the listener immediately with the preference's
// current value.
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupActionBar();
}
/**
* Set up the {#link android.app.ActionBar}, if the API is available.
*/
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
// Show the Up button in the action bar.
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
if (!super.onMenuItemSelected(featureId, item)) {
NavUtils.navigateUpFromSameTask(this);
}
return true;
}
return super.onMenuItemSelected(featureId, item);
}
/**
* {#inheritDoc}
*/
#Override
public boolean onIsMultiPane() {
return isXLargeTablet(this);
}
/**
* {#inheritDoc}
*/
#Override
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.pref_headers, target);
}
/**
* This method stops fragment injection in malicious applications.
* Make sure to deny any unknown fragments here.
*/
protected boolean isValidFragment(String fragmentName) {
return PreferenceFragment.class.getName().equals(fragmentName)
|| GeneralPreferenceFragment.class.getName().equals(fragmentName)
|| DataSyncPreferenceFragment.class.getName().equals(fragmentName)
|| NotificationPreferenceFragment.class.getName().equals(fragmentName);
}
/**
* This fragment shows general preferences only. It is used when the
* activity is showing a two-pane settings UI.
*/
static int audio;
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class GeneralPreferenceFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
setHasOptionsMenu(true);
Preference switchPref = (Preference) findPreference("audio_switch");
switchPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object o) {
boolean isOn = (boolean) o;
if (isOn) {
audio = 1;
}else{
audio = 0;
}
return true;
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
startActivity(new Intent(getActivity(), SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
/**
* This fragment shows notification preferences only. It is used when the
* activity is showing a two-pane settings UI.
*/
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class NotificationPreferenceFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_notification);
setHasOptionsMenu(true);
// Bind the summaries of EditText/List/Dialog/Ringtone preferences
// to their values. When their values change, their summaries are
// updated to reflect the new value, per the Android Design
// guidelines.
bindPreferenceSummaryToValue(findPreference("notifications_new_message_ringtone"));
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
startActivity(new Intent(getActivity(), SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
/**
* This fragment shows data and sync preferences only. It is used when the
* activity is showing a two-pane settings UI.
*/
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class DataSyncPreferenceFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_data_sync);
setHasOptionsMenu(true);
// Bind the summaries of EditText/List/Dialog/Ringtone preferences
// to their values. When their values change, their summaries are
// updated to reflect the new value, per the Android Design
// guidelines.
bindPreferenceSummaryToValue(findPreference("sync_frequency"));
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
startActivity(new Intent(getActivity(), SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
}
I´ve succesfully implemented the RangeSeekbar from this website ( http://code.google.com/p/range-seek-bar/) into my App.
The Problem alot of people now have tho, is that we can´t find an onProgressChanged Method for the two thumbs in the code for the RangeSeekbar.
Do i have to write my own onProgressChanged?
I implemented the RangeSeekbar into a DialogFragment. For the normal Seekbar everything worked fine.
http://www.directupload.net/file/d/3582/mokh6mkb_jpg.htm <-- How can I get the two TextViews to the left and right of "to" to show the selected Value of the RangeSeekbar?
My Code so far:
package com.example.test2;
import com.example.test2.RangeSeekBar.OnRangeSeekBarChangeListener;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.TextView;
public class LookingAgeFragment extends DialogFragment implements View.OnClickListener, OnRangeSeekBarChangeListener {
protected static final String TAG = null;
public static String username;
static int lowestAge;
static int highestAge;
static TextView viewTo, viewMaxAge, viewMinAge;
RangeSeekBar<Integer> seekBar;
public void onAttach(Activity activity){
super.onAttach(activity);
}
public void onCeate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view= inflater.inflate(R.layout.fragment_age_looking,null);
getDialog().setTitle(getString(R.string.lookingage_title));
Button button = (Button)view.findViewById(R.id.buttonLeaveFragmentLookingAge);
viewTo = (TextView)view.findViewById(R.id.textView_to);
viewMaxAge = (TextView)view.findViewById(R.id.textView_maxAge);
viewMinAge = (TextView)view.findViewById(R.id.textView_minAge);
button.setOnClickListener(this);
// create RangeSeekBar as Integer range between 20 and 75
seekBar = new RangeSeekBar<Integer>(20, 75, getActivity());
seekBar.setOnRangeSeekBarChangeListener(new OnRangeSeekBarChangeListener<Integer>() {
#Override
public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, Integer minValue, Integer maxValue) {
// handle changed range values
Log.i(TAG, "User selected new range values: MIN=" + minValue + ", MAX=" + maxValue);
}
});
ViewGroup layout = (ViewGroup) view.findViewById(R.id.rangeSeekbarContainer);
layout.addView(seekBar);
setUpLayout();
return view;
}
#SuppressLint("NewApi")
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
Bundle bundle = this.getArguments();
if(bundle != null){
username = bundle.getString("username", null);
}
}
private void setUpLayout() {
// TODO Auto-generated method stub
setCancelable(false);
//bar.setOnSeekBarChangeListener(yourSeekBarListener);
}
public void saveDataChange(String s, String data){
String help = "com.example.test2.PREFERENCE_FILE_KEY_"+username;
Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences(help, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(s, data);
editor.commit();
}
public String getData(String s){
String data;
String help = "com.example.test2.PREFERENCE_FILE_KEY_"+username;
Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences(help, Context.MODE_PRIVATE);
data = sharedPref.getString(s, null);
return data;
}
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
switch(view.getId()){
case R.id.buttonLeaveFragmentLookingAge:
dismiss();
break;
}
}
public void onProgressChanged() {
// TODO Auto-generated method stub
int maxvalue = (Integer) seekBar.getSelectedMaxValue();
viewMaxAge.setText(maxvalue);
int minvalue = (Integer) seekBar.getSelectedMinValue();
viewMinAge.setText(minvalue);
}
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
int maxvalue = (Integer) seekBar.getSelectedMaxValue();
viewMaxAge.setText(maxvalue);
int minvalue = (Integer) seekBar.getSelectedMinValue();
viewMinAge.setText(minvalue);
}
public void onRangeSeekBarValuesChanged(RangeSeekBar bar, Object minValue,
Object maxValue) {
// TODO Auto-generated method stub
viewMaxAge.setText((String)maxValue);
viewMinAge.setText((String)minValue);
}
}
Thank you for your help.
RangeSeekbar is a generic class. if your pass T parameter to it as type of range values, then RangSeekBar pass left thumb Value as minValue and right thumb Value as maxValue in
RangeSeekBar.OnRangeSeekBarChangeListener.(RangeSeekBar bar, T minValue, T maxValue).
change line of your class extends to:
public class LookingAgeFragment extends DialogFragment implements OnRangeSeekBarChangeListener<Integer>
after that you should change Object to Integer in your code at this line
seekBar.setOnRangeSeekBarChangeListener(new RangeSeekBar.OnRangeSeekBarChangeListener<Integer>() {
#Override
public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, Integer minValue, Integer maxValue) {
viewMinAge.setText(String.valueOf(minValue));
viewMaxAge.setText(String.valueOf(minValue));
}
});
I think by this changes your problem will be solved.
for more generic solution when your ages range is not continuous and maybe discrete, I suggest this solution
public class LookingAgeFragment extends DialogFragment implements RangeSeekBar.OnRangeSeekBarChangeListener<Integer> {
RangeSeekBar<Integer> seekBar;
int[] ageValues={10, 20, 30, 50, 100};
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
// create RangeSeekBar as Integer range between 20 and 75
seekBar = new RangeSeekBar<Integer>(0, ageValues.length - 1, getActivity());
seekBar.setOnRangeSeekBarChangeListener(new RangeSeekBar.OnRangeSeekBarChangeListener<Integer>() {
#Override
public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, Integer minValue, Integer maxValue) {
viewMinAge.setText(String.valueOf(ageValues[minValue]));
viewMaxAge.setText(String.valueOf(ageValues[minValue]));
}
});
}
}
I use next code for getting the values on changing event:
rangeSeekBar.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("MinValue", rangeSeekBar.getSelectedMinValue().toString());
Log.d("MaxValue", rangeSeekBar.getSelectedMaxValue().toString());
return false;
}
});
In class ReangeSekkBar you have this if in ACTION_MOVE
if (notifyWhileDragging && listener != null) {
listener.onRangeSeekBarValuesChanged(this, getSelectedMinValue(), getSelectedMaxValue());
}
delete this if
//if (notifyWhileDragging && listener != null) {
listener.onRangeSeekBarValuesChanged(this, getSelectedMinValue(), getSelectedMaxValue());
//}
I am creating a live wallpaper settings activity. It has a few seek bars and an Ad Mob ad view. Every time I touch one of the seek bars the ad temporarily disappears and then reloads a new add. However, the ad does not change when any of the other preferences are touched. How do I stop the ad from reloading when a seek bar preference is touched? I am pretty stumped on this one :-). Code follows:
The ad preference class:
import android.app.Activity;
import android.content.Context;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
public class AdPreference extends Preference {
public AdPreference(Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle);}
public AdPreference(Context context, AttributeSet attrs) {super(context, attrs);}
public AdPreference(Context context) {super(context);}
#Override
protected View onCreateView(ViewGroup parent) {
// this will create the linear layout defined in ads_layout.xml
View view = super.onCreateView(parent);
// the context is a PreferenceActivity
Activity activity = (Activity)getContext();
// Create the adView
AdView adView = new AdView(activity, AdSize.BANNER, "a151390e12917b5");
((LinearLayout)view).addView(adView);
// Initiate a generic request to load it with an ad
AdRequest request = new AdRequest();
request.addTestDevice("23392C83B8B55DE893A18286CB92DDA2");
request.addTestDevice("E1BAA0317138AEE05268B2E4F76B2D3F");
adView.loadAd(request);
return view;
}
}
The seek bar class:
import android.content.Context;
import android.content.res.TypedArray;
import android.preference.Preference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class SeekBarPreference extends Preference implements OnSeekBarChangeListener {
private final String TAG = getClass().getName();
private static final String ANDROIDNS="http://schemas.android.com/apk/res/android";
private static final String ROBOBUNNYNS="http://robobunny.com";
private static final int DEFAULT_VALUE = 50;
private int mMaxValue = 100;
private int mMinValue = 0;
private int mInterval = 1;
private int mCurrentValue;
private String mUnitsLeft = "";
private String mUnitsRight = "";
private SeekBar mSeekBar;
private TextView mStatusText;
public SeekBarPreference(Context context, AttributeSet attrs) {
super(context, attrs);
initPreference(context, attrs);
}
public SeekBarPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initPreference(context, attrs);
}
private void initPreference(Context context, AttributeSet attrs) {
setValuesFromXml(attrs);
mSeekBar = new SeekBar(context, attrs);
mSeekBar.setMax(mMaxValue - mMinValue);
mSeekBar.setOnSeekBarChangeListener(this);
}
private void setValuesFromXml(AttributeSet attrs) {
mMaxValue = attrs.getAttributeIntValue(ANDROIDNS, "max", 100);
mMinValue = attrs.getAttributeIntValue(ROBOBUNNYNS, "min", 0);
mUnitsLeft = getAttributeStringValue(attrs, ROBOBUNNYNS, "unitsLeft", "");
String units = getAttributeStringValue(attrs, ROBOBUNNYNS, "units", "");
mUnitsRight = getAttributeStringValue(attrs, ROBOBUNNYNS, "unitsRight", units);
try {
String newInterval = attrs.getAttributeValue(ROBOBUNNYNS, "interval");
if(newInterval != null)
mInterval = Integer.parseInt(newInterval);
}
catch(Exception e) {
Log.e(TAG, "Invalid interval value", e);
}
}
private String getAttributeStringValue(AttributeSet attrs, String namespace, String name, String defaultValue) {
String value = attrs.getAttributeValue(namespace, name);
if(value == null)
value = defaultValue;
return value;
}
#Override
protected View onCreateView(ViewGroup parent){
RelativeLayout layout = null;
try {
LayoutInflater mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = (RelativeLayout)mInflater.inflate(R.layout.seek_bar_preference, parent, false);
}
catch(Exception e)
{
Log.e(TAG, "Error creating seek bar preference", e);
}
return layout;
}
#Override
public void onBindView(View view) {
super.onBindView(view);
try
{
// move our seekbar to the new view we've been given
ViewParent oldContainer = mSeekBar.getParent();
ViewGroup newContainer = (ViewGroup) view.findViewById(R.id.seekBarPrefBarContainer);
if (oldContainer != newContainer) {
// remove the seekbar from the old view
if (oldContainer != null) {
((ViewGroup) oldContainer).removeView(mSeekBar);
}
// remove the existing seekbar (there may not be one) and add ours
newContainer.removeAllViews();
newContainer.addView(mSeekBar, ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
}
catch(Exception ex) {
Log.e(TAG, "Error binding view: " + ex.toString());
}
updateView(view);
}
/**
* Update a SeekBarPreference view with our current state
* #param view
*/
protected void updateView(View view) {
try {
RelativeLayout layout = (RelativeLayout)view;
mStatusText = (TextView)layout.findViewById(R.id.seekBarPrefValue);
mStatusText.setText(String.valueOf(mCurrentValue));
mStatusText.setMinimumWidth(30);
mSeekBar.setProgress(mCurrentValue - mMinValue);
TextView unitsRight = (TextView)layout.findViewById(R.id.seekBarPrefUnitsRight);
unitsRight.setText(mUnitsRight);
TextView unitsLeft = (TextView)layout.findViewById(R.id.seekBarPrefUnitsLeft);
unitsLeft.setText(mUnitsLeft);
}
catch(Exception e) {
Log.e(TAG, "Error updating seek bar preference", e);
}
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int newValue = progress + mMinValue;
if(newValue > mMaxValue)
newValue = mMaxValue;
else if(newValue < mMinValue)
newValue = mMinValue;
else if(mInterval != 1 && newValue % mInterval != 0)
newValue = Math.round(((float)newValue)/mInterval)*mInterval;
// change rejected, revert to the previous value
if(!callChangeListener(newValue)){
seekBar.setProgress(mCurrentValue - mMinValue);
return;
}
// change accepted, store it
mCurrentValue = newValue;
mStatusText.setText(String.valueOf(newValue));
persistInt(newValue);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
notifyChanged();
}
#Override
protected Object onGetDefaultValue(TypedArray ta, int index){
int defaultValue = ta.getInt(index, DEFAULT_VALUE);
return defaultValue;
}
#Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
if(restoreValue) {
mCurrentValue = getPersistedInt(mCurrentValue);
}
else {
int temp = 0;
try {
temp = (Integer)defaultValue;
}
catch(Exception ex) {
Log.e(TAG, "Invalid default value: " + defaultValue.toString());
}
persistInt(temp);
mCurrentValue = temp;
}
}
}
The xml layout for the settings activity:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:robobunny="http://robobunny.com"
android:key="disco_wormhole_settings" >
<com.package.name.and.AdPreference
android:layout_width="fill_parent"
androidLlayout_height="wrap_content" />
<Preference
android:key="title"
android:summary="#string/settings_summary"
android:title="#string/settings_title" />
<com.package.name.and.SeekBarPreference
android:defaultValue="50"
android:key="flight_speed"
android:max="100"
android:progressDrawable="#drawable/seek_bar_progress"
android:summary="#string/flight_speed_summary"
android:title="#string/flight_speed_title"
robobunny:min="1"
robobunny:unitsLeft=""
robobunny:unitsRight="%" />
<com.package.name.and.SeekBarPreference
android:defaultValue="30"
android:key="num_rings"
android:max="40"
android:progressDrawable="#drawable/seek_bar_progress"
android:summary="#string/num_rings_summary"
android:title="#string/num_rings_title"
robobunny:min="1"
robobunny:unitsLeft=""
robobunny:unitsRight="" />
<com.package.name.and.SeekBarPreference
android:defaultValue="50"
android:key="particle_speed"
android:max="100"
android:progressDrawable="#drawable/seek_bar_progress"
android:summary="#string/particle_speed_summary"
android:title="#string/particle_speed_title"
robobunny:min="1"
robobunny:unitsLeft=""
robobunny:unitsRight="%" />
<Preference
android:summary="#string/colors_summary"
android:title="#string/colors_title" >
</Preference>
<Preference
android:defaultValue="0xff7d9fff"
android:key="color_one"
android:title="#string/color_one" >
</Preference>
<Preference
android:defaultValue="0xffff4b31"
android:key="color_two"
android:title="#string/color_two" >
</Preference>
<Preference
android:defaultValue="0xff64ff46"
android:key="color_three"
android:title="#string/color_three" >
</Preference>
<CheckBoxPreference
android:defaultValue="true"
android:key="use_space_dust"
android:title="#string/use_space_dust_title" />
<Preference
android:key="spacer"
android:title="#string/single_space" />
<Preference>
</Preference>
</PreferenceScreen>
Thanks in advance for your help,
Chris
I think onCreateView will be called multiple times so an ad request will be created every time. Try moving your admob code to something like onBind or onAttached.
i'm trying to work with building my own Preference classes, and having a bit of trouble. it looks like the way of saving data to a preference is via the "persist" group of methods within the Preference class. however, in my preference, i open a color picker dialog, and i need to save the preference from within the dialog's colorChanged override. whenever i run the app and try to change the color preference, i get:
06-05 10:21:46.396: ERROR/AndroidRuntime(516): FATAL EXCEPTION: main
java.lang.IllegalAccessError: tried to access method android.preference.Preference.persistInt:(IIII)V from class android.preference.ColorSelectionPreference$1
at android.preference.ColorSelectionPreference$1.colorChanged(ColorSelectionPreference.java:55)
at android.apis.graphics.ColorPickerDialog.onClick(ColorPickerDialog.java:168)
(UPDATE: 6/5/12 12:20) i tried using callChangeListener to force the onPreferenceChangeListener to trigger, but it crashes with an identical error. without callChangeListener, the preference data is (probably) saved, but the onPreferenceChangeListener doesn't get triggered:
06-05 12:20:23.691: ERROR/AndroidRuntime(2834): FATAL EXCEPTION: main
java.lang.IllegalAccessError: tried to access method android.preference.ColorSelectionPreference.callChangeListener:(IIII)V from class android.preference.ColorSelectionPreference$1
at android.preference.ColorSelectionPreference$1.colorChanged(ColorSelectionPreference.java:52)
at android.apis.graphics.ColorPickerDialog.onClick(ColorPickerDialog.java:168)
here's the actual class:
package android.preference;
import android.apis.graphics.ColorPickerDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.util.AttributeSet;
public class ColorSelectionPreference extends Preference {
private Context mContext;
private int mColor;
public ColorSelectionPreference(Context context) {
super(context);
mContext = context;
}
public ColorSelectionPreference(Context context, AttributeSet attr) {
super(context, attr);
mContext = context;
}
public int getColor() {
return mColor;
}
public void setColor(int color) {
mColor = color;
}
#Override
public void onClick() {
//get original preference
//set ColorPickerDialog to original preference color or default color
ColorPickerDialog dialog = new ColorPickerDialog(mContext, new ColorPickerDialog.OnColorChangedListener() {
public void colorChanged(int a, int r, int g, int b) {
int selectedColor = Color.argb(a,r,g,b);
setColor(selectedColor);
/*** crashes on callChangeListener ***/
//SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
//SharedPreferences.Editor edit = prefs.edit();
//edit.putInt(getKey(), selectedColor);
//edit.commit();
//callChangeListener(selectedColor);
/*** the offending code, error refers to this line ***/
persistInt(selectedColor);
/*** tried this as well by request on IRC ***/
//ColorSelectionPreference.this.persistInt(selectedColor);
}
}, mColor);
dialog.show();
}
}
this is a hacky workaround that uses a Handler to call back to the main class from the inner class. it isn't pretty, but it works.
package android.preference;
import android.apis.graphics.ColorPickerDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
public class ColorSelectionPreference extends Preference {
private Context mContext;
private int mColor;
private final Handler mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
if(msg.getData().containsKey("color")) {
int color = msg.getData().getInt("color");
setColor(color);
}
}
};
public ColorSelectionPreference(Context context) {
super(context);
mContext = context;
}
public ColorSelectionPreference(Context context, AttributeSet attr) {
super(context, attr);
mContext = context;
}
public int getColor() {
return mColor;
}
public void setColor(int color) {
mColor = color;
persistInt(new Integer(color));
}
#Override
public void onClick() {
//get original preference
//set ColorPickerDialog to original preference color or default color
ColorPickerDialog dialog = new ColorPickerDialog(mContext, new ColorPickerDialog.OnColorChangedListener() {
public void colorChanged(int a, int r, int g, int b) {
int selectedColor = Color.argb(a,r,g,b);
Bundle bundle = new Bundle();
bundle.putInt("color", selectedColor);
Message msg = new Message();
msg.setData(bundle);
mHandler.sendMessage(msg);
//setColor(selectedColor);
/*** tried this, but the onPreferenceChangedListener never gets triggered, so this won't work ***/
//SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
//SharedPreferences.Editor edit = prefs.edit();
//edit.putInt(getKey(), selectedColor);
//edit.commit();
//callChangeListener(selectedColor);
/*** the offending code, error refers to this line ***/
//container.
/*** tried this as well by request on IRC ***/
//ColorSelectionPreference.this.persistInt(selectedColor);
}
}, mColor);
dialog.show();
}
}
I just encountered this problem myself. I do not pretend to understand it, but this seems like an easier fix:
// ...why is this necessary? what is special about Preference.persistInt?
#Override protected boolean persistInt(int value)
{
return super.persistInt(value);
}
I want my preference menu to have something to change the duration of a vibration.
There is no slider tag for prefs.xml, so what is the best way to do this?
I improved the link provided by Macarse, so that the value is saved only on ok button click, and so that you can use #string/... values in the XML file.
Here is the code:
/* The following code was written by Matthew Wiggins
* and is released under the APACHE 2.0 license
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Improvements :
* - save the value on positive button click, not on seekbar change
* - handle #string/... values in xml file
*/
package fr.atcm.carpooling.views.utils;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
public class SeekBarPreference extends DialogPreference implements SeekBar.OnSeekBarChangeListener, OnClickListener
{
// ------------------------------------------------------------------------------------------
// Private attributes :
private static final String androidns="http://schemas.android.com/apk/res/android";
private SeekBar mSeekBar;
private TextView mSplashText,mValueText;
private Context mContext;
private String mDialogMessage, mSuffix;
private int mDefault, mMax, mValue = 0;
// ------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------
// Constructor :
public SeekBarPreference(Context context, AttributeSet attrs) {
super(context,attrs);
mContext = context;
// Get string value for dialogMessage :
int mDialogMessageId = attrs.getAttributeResourceValue(androidns, "dialogMessage", 0);
if(mDialogMessageId == 0) mDialogMessage = attrs.getAttributeValue(androidns, "dialogMessage");
else mDialogMessage = mContext.getString(mDialogMessageId);
// Get string value for suffix (text attribute in xml file) :
int mSuffixId = attrs.getAttributeResourceValue(androidns, "text", 0);
if(mSuffixId == 0) mSuffix = attrs.getAttributeValue(androidns, "text");
else mSuffix = mContext.getString(mSuffixId);
// Get default and max seekbar values :
mDefault = attrs.getAttributeIntValue(androidns, "defaultValue", 0);
mMax = attrs.getAttributeIntValue(androidns, "max", 100);
}
// ------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------
// DialogPreference methods :
#Override
protected View onCreateDialogView() {
LinearLayout.LayoutParams params;
LinearLayout layout = new LinearLayout(mContext);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(6,6,6,6);
mSplashText = new TextView(mContext);
mSplashText.setPadding(30, 10, 30, 10);
if (mDialogMessage != null)
mSplashText.setText(mDialogMessage);
layout.addView(mSplashText);
mValueText = new TextView(mContext);
mValueText.setGravity(Gravity.CENTER_HORIZONTAL);
mValueText.setTextSize(32);
params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.addView(mValueText, params);
mSeekBar = new SeekBar(mContext);
mSeekBar.setOnSeekBarChangeListener(this);
layout.addView(mSeekBar, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
if (shouldPersist())
mValue = getPersistedInt(mDefault);
mSeekBar.setMax(mMax);
mSeekBar.setProgress(mValue);
return layout;
}
#Override
protected void onBindDialogView(View v) {
super.onBindDialogView(v);
mSeekBar.setMax(mMax);
mSeekBar.setProgress(mValue);
}
#Override
protected void onSetInitialValue(boolean restore, Object defaultValue)
{
super.onSetInitialValue(restore, defaultValue);
if (restore)
mValue = shouldPersist() ? getPersistedInt(mDefault) : 0;
else
mValue = (Integer)defaultValue;
}
// ------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------
// OnSeekBarChangeListener methods :
#Override
public void onProgressChanged(SeekBar seek, int value, boolean fromTouch)
{
String t = String.valueOf(value);
mValueText.setText(mSuffix == null ? t : t.concat(" " + mSuffix));
}
#Override
public void onStartTrackingTouch(SeekBar seek) {}
#Override
public void onStopTrackingTouch(SeekBar seek) {}
public void setMax(int max) { mMax = max; }
public int getMax() { return mMax; }
public void setProgress(int progress) {
mValue = progress;
if (mSeekBar != null)
mSeekBar.setProgress(progress);
}
public int getProgress() { return mValue; }
// ------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------
// Set the positive button listener and onClick action :
#Override
public void showDialog(Bundle state) {
super.showDialog(state);
Button positiveButton = ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE);
positiveButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (shouldPersist()) {
mValue = mSeekBar.getProgress();
persistInt(mSeekBar.getProgress());
callChangeListener(Integer.valueOf(mSeekBar.getProgress()));
}
((AlertDialog) getDialog()).dismiss();
}
// ------------------------------------------------------------------------------------------
}
EDIT :
Here is a screenshot :
EDIT : on arlomedia's demand, here are all the needed pieces of code (I just recreated a new projet, it is perfectly working. I corrected some bugs in SeekBarPreference class, so don't forget to re-copy/paste it) :
MainActivity :
package fr.at.testsliderpref;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_settings : {
startActivity(new Intent(this, SettingsActivity.class));
break;
}
}
return true;
}
}
SettingsActivity :
package fr.at.testsliderpref;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import fr.at.testsliderpref.utils.SeekBarPreference;
public class SettingsActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// Call super :
super.onCreate(savedInstanceState);
// Set the activity's fragment :
getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
}
public static class SettingsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {
private SeekBarPreference _seekBarPref;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.activity_settings);
// Get widgets :
_seekBarPref = (SeekBarPreference) this.findPreference("SEEKBAR_VALUE");
// Set listener :
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
// Set seekbar summary :
int radius = PreferenceManager.getDefaultSharedPreferences(this.getActivity()).getInt("SEEKBAR_VALUE", 50);
_seekBarPref.setSummary(this.getString(R.string.settings_summary).replace("$1", ""+radius));
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// Set seekbar summary :
int radius = PreferenceManager.getDefaultSharedPreferences(this.getActivity()).getInt("SEEKBAR_VALUE", 50);
_seekBarPref.setSummary(this.getString(R.string.settings_summary).replace("$1", ""+radius));
}
}
}
layout > activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/textview_text" />
</RelativeLayout>
menu > main.xml :
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_settings"
android:title="#string/menu_settings"
android:icon="#android:drawable/ic_menu_preferences"/>
</menu>
xml > activity_settings.xml :
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<fr.at.testsliderpref.utils.SeekBarPreference
android:defaultValue="50"
android:dialogMessage="#string/settings_dialog_message"
android:key="SEEKBAR_VALUE"
android:max="100"
android:summary="#string/settings_summary"
android:text="#string/settings_unit"
android:title="#string/settings_title" />
</PreferenceScreen>
values > strings.xml :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">TestSliderPref</string>
<string name="textview_text">SeekBarPreference test</string>
<string name="menu_settings">Settings</string>
<string name="settings_dialog_message">Here comes a message</string>
<string name="settings_summary">Current value is $1</string>
<string name="settings_unit">Km</string>
<string name="settings_title">Here comes the title</string>
</resources>
Don't forget to add your SettingsActivity to the manifest, and it should be OK.
You could create your own Preference class that extends DialogPreference and shows a SeekBar as the dialog view.
An easy way to achieve this is to add an empty preference to your preferences.xml which uses a layout including a seekbar.
In your preferences.xml add
<Preference android:layout="#layout/sliderlayout" />
under layouts add the sliderlayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
If you already moved to androidX you can simply use androidx.preference.SeekBarPreference. Documentation here and here
Define it in XML like this:
<SeekBarPreference
app:defaultValue="8"
app:key="COUNT_SPEED"
app:title="Fast count speed"
/>
Note: Now in Android Studio (my current version is 3.2.1) auto-suggestions doesn't work for androidx.preference.PreferenceScreen in prefs.xml, but don't worry inflating settings from xml with setPreferencesFromResource() works fine. All needed attributes names and component references you can find here.
This is good Slider Preference component - Android Slider Preference Library
https://github.com/jayschwa/AndroidSliderPreference
One more implementation of this.
All Credit goes to Tim Autin.
I wanted to have the values displayed loaded from XML Arrays
It looks like this
Code - this now extends ListPreference
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.preference.ListPreference;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
public class SeekBarListPreference extends ListPreference implements SeekBar.OnSeekBarChangeListener, View.OnClickListener {
// ------------------------------------------------------------------------------------------
// Private attributes :
private static final String androidns = "http://schemas.android.com/apk/res/android";
private SeekBar mSeekBar;
private TextView mSplashText, mValueText;
private Context mContext;
private String mDialogMessage;
// ------------------------------------------------------------------------------------------
public SeekBarListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
// Get string value for dialogMessage :
int mDialogMessageId = attrs.getAttributeResourceValue(androidns, "dialogMessage", 0);
if (mDialogMessageId == 0)
mDialogMessage = attrs.getAttributeValue(androidns, "dialogMessage");
else mDialogMessage = mContext.getString(mDialogMessageId);
}
// ------------------------------------------------------------------------------------------
// DialogPreference methods :
#Override
protected View onCreateDialogView() {
LinearLayout.LayoutParams params;
LinearLayout layout = new LinearLayout(mContext);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(6, 6, 6, 6);
mSplashText = new TextView(mContext);
mSplashText.setPadding(30, 10, 30, 10);
if (mDialogMessage != null)
mSplashText.setText(mDialogMessage);
layout.addView(mSplashText);
mValueText = new TextView(mContext);
mValueText.setGravity(Gravity.CENTER_HORIZONTAL);
mValueText.setTextSize(32);
params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.addView(mValueText, params);
mSeekBar = new SeekBar(mContext);
mSeekBar.setOnSeekBarChangeListener(this);
layout.addView(mSeekBar, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
setProgressBarValue();
return layout;
}
#Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
// do not call super
}
private void setProgressBarValue() {
String mValue = null;
if (shouldPersist()) {
mValue = getValue();
}
final int max = this.getEntries().length - 1;
mSeekBar.setMax(max);
mSeekBar.setProgress(this.findIndexOfValue(mValue));
}
#Override
protected void onBindDialogView(View v) {
super.onBindDialogView(v);
setProgressBarValue();
}
#Override
public void onProgressChanged(SeekBar seek, int value, boolean fromTouch) {
final CharSequence textToDisplay = getEntryFromValue(value);
mValueText.setText(textToDisplay);
}
private CharSequence getEntryFromValue(int value) {
CharSequence[] entries = getEntries();
return value >= 0 && entries != null ? entries[value] : null;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void showDialog(Bundle state) {
super.showDialog(state);
Button positiveButton = ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE);
positiveButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (shouldPersist()) {
final int progressChoice = mSeekBar.getProgress();
setValueIndex(progressChoice);
}
getDialog().dismiss();
}
}
The usage in the preferences file is now
<com.yourfullpackage.SeekBarListPreference
android:defaultValue="0"
android:dialogMessage="#string/time_limit_pref"
android:entries="#array/timeListArray"
android:entryValues="#array/timeListValues"
android:key="time"
android:summary="Select time limit"
android:title="Time"
/>
And the arrays
<string-array name="timeListArray">
<item>10 Seconds</item>
<item>30 Seconds</item>
<item>1 Minute</item>
<item>2 Minutes</item>
<item>Unlimited</item>
</string-array>
<!--This is going to be in seconds-->
<string-array name="timeListValues">
<item>10</item>
<item>30</item>
<item>60</item>
<item>120</item>
<item>0</item>
</string-array>
As a bonus if you already have ListPreferences, you don't need to add anything extra to display the summary as the current value. So this works just fine
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
SetSummaryForPreferenceKey(key);
}
private void SetSummaryForPreferenceKey(String key) {
Preference preference = findPreference(key);
// This works with our new SeekBarPreference
if (preference instanceof ListPreference) {
ListPreference listPref = (ListPreference) preference;
listPref.setSummary(listPref.getEntry());
}
}
I've made a slight improvement on the code provided in the answer by Tim. This simply makes live adjustments to the output value as the user moves the slider, rather than requiring the user to click the "Ok" button for the changes to be made.
This is useful for things like a Music Volume slider, where the user should be able to hear the adjustment in volume as it's being made.
If the user clicks "OK" the new value is maintained. If the user clicks "Cancel" then the original pre-adjustment value is restored.
Thanks and credit should go to Tim, I just added the extra onClick listener and pushed the value updating into the onChange listener.
package fr.atcm.carpooling.views.utils;
/* The following code was written by Matthew Wiggins
* and is released under the APACHE 2.0 license
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Improvements :
* - save the value on positive button click and/or seekbar change
* - restore pre-adjustment value on negative button click
*/
import android.R;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
public class SeekBarPreference extends DialogPreference implements
SeekBar.OnSeekBarChangeListener, OnClickListener {
// ------------------------------------------------------------------------------------------
// Private attributes :
private static final String androidns = "http://schemas.android.com/apk/res/android";
private SeekBar mSeekBar;
private TextView mSplashText, mValueText;
private Context mContext;
private String mDialogMessage, mSuffix;
private int mDefault, mMax, mValue, mOrig = 0;
// ------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------
// Constructor :
public SeekBarPreference(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
// Get string value for dialogMessage :
int mDialogMessageId = attrs.getAttributeResourceValue(androidns,
"dialogMessage", 0);
if (mDialogMessageId == 0)
mDialogMessage = attrs
.getAttributeValue(androidns, "dialogMessage");
else
mDialogMessage = mContext.getString(mDialogMessageId);
// Get string value for suffix (text attribute in xml file) :
int mSuffixId = attrs.getAttributeResourceValue(androidns, "text", 0);
if (mSuffixId == 0)
mSuffix = attrs.getAttributeValue(androidns, "text");
else
mSuffix = mContext.getString(mSuffixId);
// Get default and max seekbar values :
mDefault = attrs.getAttributeIntValue(androidns, "defaultValue", 0);
mMax = attrs.getAttributeIntValue(androidns, "max", 100);
}
// ------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------
// DialogPreference methods :
#Override
protected View onCreateDialogView() {
LinearLayout.LayoutParams params;
LinearLayout layout = new LinearLayout(mContext);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(6, 6, 6, 6);
mSplashText = new TextView(mContext);
mSplashText.setPadding(30, 10, 30, 10);
if (mDialogMessage != null)
mSplashText.setText(mDialogMessage);
layout.addView(mSplashText);
mValueText = new TextView(mContext);
mValueText.setGravity(Gravity.CENTER_HORIZONTAL);
mValueText.setTextSize(32);
params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.addView(mValueText, params);
mSeekBar = new SeekBar(mContext);
mSeekBar.setOnSeekBarChangeListener(this);
layout.addView(mSeekBar, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
if (shouldPersist())
mValue = getPersistedInt(mDefault);
mSeekBar.setMax(mMax);
mSeekBar.setProgress(mValue);
return layout;
}
#Override
protected void onBindDialogView(View v) {
super.onBindDialogView(v);
mSeekBar.setMax(mMax);
mSeekBar.setProgress(mValue);
}
#Override
protected void onSetInitialValue(boolean restore, Object defaultValue) {
super.onSetInitialValue(restore, defaultValue);
// Set adjustable value
if (restore)
mValue = shouldPersist() ? getPersistedInt(mDefault) : 0;
else
mValue = (Integer) defaultValue;
// Set original pre-adjustment value
mOrig = mValue;
}
// ------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------
// OnSeekBarChangeListener methods :
#Override
public void onProgressChanged(SeekBar seek, int value, boolean fromTouch) {
String t = String.valueOf(value);
mValueText.setText(mSuffix == null ? t : t.concat(" " + mSuffix));
if (shouldPersist()) {
mValue = mSeekBar.getProgress();
persistInt(mSeekBar.getProgress());
callChangeListener(Integer.valueOf(mSeekBar.getProgress()));
}
}
#Override
public void onStartTrackingTouch(SeekBar seek) {
}
#Override
public void onStopTrackingTouch(SeekBar seek) {
}
public void setMax(int max) {
mMax = max;
}
public int getMax() {
return mMax;
}
public void setProgress(int progress) {
mValue = progress;
if (mSeekBar != null)
mSeekBar.setProgress(progress);
}
public int getProgress() {
return mValue;
}
// ------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------
// Set the positive button listener and onClick action :
#Override
public void showDialog(Bundle state) {
super.showDialog(state);
Button positiveButton = ((AlertDialog) getDialog())
.getButton(AlertDialog.BUTTON_POSITIVE);
Button negativeButton = ((AlertDialog) getDialog())
.getButton(AlertDialog.BUTTON_NEGATIVE);
positiveButton.setOnClickListener(cListenPos);
negativeButton.setOnClickListener(cListenNeg);
}
View.OnClickListener cListenPos = new View.OnClickListener() {
public void onClick(View v) {
if (shouldPersist()) {
mValue = mSeekBar.getProgress();
mOrig = mSeekBar.getProgress();
persistInt(mSeekBar.getProgress());
callChangeListener(Integer.valueOf(mSeekBar.getProgress()));
}
((AlertDialog) getDialog()).dismiss();
}
};
View.OnClickListener cListenNeg = new View.OnClickListener() {
public void onClick(View v) {
if (shouldPersist()) {
mValue = mOrig;
persistInt(mOrig);
callChangeListener(Integer.valueOf(mOrig));
}
((AlertDialog) getDialog()).dismiss();
}
};
#Override
public void onClick(View v) {}
// ------------------------------------------------------------------------------------------
}
One small improvement of Tim's answer and its derivates:
protected void onBindDialogView(View v) {
super.onBindDialogView(v);
mSeekBar.setMax(mMax);
mSeekBar.setProgress(mValue);
String t = String.valueOf(mValue);
mValueText.setText(mSuffix == null ? t : t.concat(" " + mSuffix));
}
It will populate current value at start which is otherwise empty until you actually slide the slider.