A
A
Artem2013-12-06 11:46:12
Android
Artem, 2013-12-06 11:46:12

Custom Preference change position after value change?

My application settings use a custom Preference - SeekBarPreference. It looks like this:
051b0b9082b6.jpg
The problem is that after each click on the engine (the value does not have to be changed), Control1 and Control2 are swapped:
23a378e1e7bc.jpg
This happens in the 2.1 emulator. In the emulator with 4.2.2, this problem does not appear. Question: why can this happen and what is the difference in this regard between 2.1 and 4.2.2?
Here is the preference code:

public class SeekBarPreference extends Preference implements OnSeekBarChangeListener {

  private TextView valueTextView;
  private int currentValue;
  private int max;
  private int min;
  final private String NAMESPACE="http://schemas.android.com/apk/res/ru.bartwell.myapp";
  private int defaultValue;

  public SeekBarPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
    Log.d("SeekBarPreference", "Init");
    max = attrs.getAttributeIntValue(NAMESPACE, "max", 99);
    min = attrs.getAttributeIntValue(NAMESPACE, "min", 0);
    defaultValue = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android", "defaultValue", min);
    if(defaultValue<min) defaultValue=min;
  }
  
  @Override
  protected View onCreateView(ViewGroup parent) {
    currentValue = getPersistedInt(defaultValue)-min;
    
    RelativeLayout layout = (RelativeLayout) LayoutInflater.from(getContext()).inflate(R.layout.seek_bar_preference_layout, null);

    ((TextView) layout.findViewById(R.id.title)).setText(getTitle());
    ((TextView) layout.findViewById(R.id.summary)).setText(getSummary());

    SeekBar bar = (SeekBar) layout.findViewById(R.id.seekBar);
    bar.setMax(max-min);
    bar.setProgress(currentValue);
    bar.setOnSeekBarChangeListener(this);

    valueTextView = (TextView) layout.findViewById(R.id.value);
    valueTextView.setText(String.valueOf(currentValue+min));

    return layout;
  }

  public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    valueTextView.setText(String.valueOf(progress+min));
    valueTextView.invalidate();
  }

  public void onStartTrackingTouch(SeekBar seekBar) {}

  public void onStopTrackingTouch(SeekBar seekBar) {
    currentValue = seekBar.getProgress();
    persistInt(currentValue+min);
    notifyChanged();
  }

}

Used like this (prefs.xml):
<ru.bartwell.myapp.SeekBarPreference
  android:key="Control1"
  android:summary="@string/control1_description"
  android:title="@string/control1"
  android:defaultValue="12"
  sbp:min="1"
  sbp:max="20" />

<ru.bartwell.myapp.SeekBarPreference
  android:key="Control2"
  android:summary="@string/control2_description"
  android:title="@string/control2"
  android:defaultValue="3"
  sbp:min="1"
  sbp:max="15" />

PrefActivity.java:
public class PrefActivity extends PreferenceActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    addPreferencesFromResource(R.xml.prefs);
  }

}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question