mirror of
https://github.com/peterosterlund2/droidfish.git
synced 2024-11-23 11:31:33 +01:00
Use binding in SeekBarPreference
This commit is contained in:
parent
7e4a0314c9
commit
0324409feb
|
@ -18,49 +18,47 @@
|
||||||
|
|
||||||
package org.petero.droidfish.activities;
|
package org.petero.droidfish.activities;
|
||||||
|
|
||||||
import java.util.Locale;
|
|
||||||
|
|
||||||
import org.petero.droidfish.DroidFishApp;
|
|
||||||
import org.petero.droidfish.R;
|
|
||||||
|
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.app.Dialog;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface;
|
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.content.res.TypedArray;
|
import android.content.res.TypedArray;
|
||||||
import android.graphics.Typeface;
|
|
||||||
import android.preference.Preference;
|
import android.preference.Preference;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
import android.view.Gravity;
|
|
||||||
import android.view.KeyEvent;
|
import android.view.KeyEvent;
|
||||||
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.view.View.OnClickListener;
|
|
||||||
import android.view.View.OnKeyListener;
|
|
||||||
import android.widget.EditText;
|
|
||||||
import android.widget.LinearLayout;
|
|
||||||
import android.widget.SeekBar;
|
import android.widget.SeekBar;
|
||||||
import android.widget.TextView;
|
|
||||||
import android.widget.SeekBar.OnSeekBarChangeListener;
|
import android.widget.SeekBar.OnSeekBarChangeListener;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
/** Lets user enter a percentage value using a seek bar. */
|
import org.petero.droidfish.DroidFishApp;
|
||||||
public class SeekBarPreference extends Preference
|
import org.petero.droidfish.R;
|
||||||
implements OnSeekBarChangeListener {
|
import org.petero.droidfish.databinding.SeekbarPreferenceBinding;
|
||||||
|
import org.petero.droidfish.databinding.SelectPercentageBinding;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lets user enter a percentage value using a seek bar.
|
||||||
|
*/
|
||||||
|
public class SeekBarPreference extends Preference implements OnSeekBarChangeListener {
|
||||||
private final static int maxValue = 1000;
|
private final static int maxValue = 1000;
|
||||||
private final static int DEFAULT_VALUE = 1000;
|
private final static int DEFAULT_VALUE = 1000;
|
||||||
private int currVal = DEFAULT_VALUE;
|
private int currVal = DEFAULT_VALUE;
|
||||||
private TextView currValBox;
|
|
||||||
private boolean showStrengthHint = true;
|
private boolean showStrengthHint = true;
|
||||||
|
|
||||||
|
private SeekbarPreferenceBinding binding;
|
||||||
|
|
||||||
public SeekBarPreference(Context context) {
|
public SeekBarPreference(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SeekBarPreference(Context context, AttributeSet attrs) {
|
public SeekBarPreference(Context context, AttributeSet attrs) {
|
||||||
super(context, attrs);
|
super(context, attrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SeekBarPreference(Context context, AttributeSet attrs, int defStyle) {
|
public SeekBarPreference(Context context, AttributeSet attrs, int defStyle) {
|
||||||
super(context, attrs, defStyle);
|
super(context, attrs, defStyle);
|
||||||
}
|
}
|
||||||
|
@ -69,31 +67,30 @@ public class SeekBarPreference extends Preference
|
||||||
protected View onCreateView(ViewGroup parent) {
|
protected View onCreateView(ViewGroup parent) {
|
||||||
super.onCreateView(parent);
|
super.onCreateView(parent);
|
||||||
|
|
||||||
LinearLayout layout = (LinearLayout)View.inflate(getContext(), R.layout.seekbar_preference, null);
|
binding = SeekbarPreferenceBinding.inflate(
|
||||||
TextView name = layout.findViewById(R.id.seekbar_title);
|
LayoutInflater.from(getContext()), null, false);
|
||||||
name.setText(getTitle());
|
binding.seekbarTitle.setText(getTitle());
|
||||||
|
|
||||||
currValBox = layout.findViewById(R.id.seekbar_value);
|
binding.seekbarValue.setText(valToString());
|
||||||
currValBox.setText(valToString());
|
|
||||||
|
|
||||||
final SeekBar bar = layout.findViewById(R.id.seekbar_bar);
|
binding.seekbarBar.setMax(maxValue);
|
||||||
bar.setMax(maxValue);
|
binding.seekbarBar.setProgress(currVal);
|
||||||
bar.setProgress(currVal);
|
binding.seekbarBar.setOnSeekBarChangeListener(this);
|
||||||
bar.setOnSeekBarChangeListener(this);
|
|
||||||
|
|
||||||
TextView summary = layout.findViewById(R.id.seekbar_summary);
|
|
||||||
CharSequence summaryCharSeq = getSummary();
|
CharSequence summaryCharSeq = getSummary();
|
||||||
boolean haveSummary = (summaryCharSeq != null) && (summaryCharSeq.length() > 0);
|
boolean haveSummary = (summaryCharSeq != null) && (summaryCharSeq.length() > 0);
|
||||||
if (haveSummary) {
|
if (haveSummary) {
|
||||||
summary.setText(getSummary());
|
binding.seekbarSummary.setText(getSummary());
|
||||||
} else {
|
} else {
|
||||||
summary.setVisibility(View.GONE);
|
binding.seekbarSummary.setVisibility(View.GONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
currValBox.setOnClickListener(v -> {
|
binding.seekbarValue.setOnClickListener(v -> {
|
||||||
View content = View.inflate(SeekBarPreference.this.getContext(), R.layout.select_percentage, null);
|
SelectPercentageBinding selectPercentageBinding = SelectPercentageBinding.inflate(
|
||||||
|
LayoutInflater.from(SeekBarPreference.this.getContext()),
|
||||||
|
null, false);
|
||||||
final AlertDialog.Builder builder = new AlertDialog.Builder(SeekBarPreference.this.getContext());
|
final AlertDialog.Builder builder = new AlertDialog.Builder(SeekBarPreference.this.getContext());
|
||||||
builder.setView(content);
|
builder.setView(selectPercentageBinding.getRoot());
|
||||||
String title = "";
|
String title = "";
|
||||||
String key = getKey();
|
String key = getKey();
|
||||||
if (key.equals("strength")) {
|
if (key.equals("strength")) {
|
||||||
|
@ -102,19 +99,18 @@ public class SeekBarPreference extends Preference
|
||||||
title = getContext().getString(R.string.edit_randomization);
|
title = getContext().getString(R.string.edit_randomization);
|
||||||
}
|
}
|
||||||
builder.setTitle(title);
|
builder.setTitle(title);
|
||||||
final EditText valueView = content.findViewById(R.id.selpercentage_number);
|
selectPercentageBinding.selpercentageNumber.setText(binding.seekbarValue.getText().toString().replaceAll("%", "").replaceAll(",", "."));
|
||||||
valueView.setText(currValBox.getText().toString().replaceAll("%", "").replaceAll(",", "."));
|
|
||||||
final Runnable selectValue = () -> {
|
final Runnable selectValue = () -> {
|
||||||
try {
|
try {
|
||||||
String txt = valueView.getText().toString();
|
String txt = selectPercentageBinding.selpercentageNumber.getText().toString();
|
||||||
int value = (int) (Double.parseDouble(txt) * 10 + 0.5);
|
int value = (int) (Double.parseDouble(txt) * 10 + 0.5);
|
||||||
if (value < 0) value = 0;
|
if (value < 0) value = 0;
|
||||||
if (value > maxValue) value = maxValue;
|
if (value > maxValue) value = maxValue;
|
||||||
onProgressChanged(bar, value, false);
|
onProgressChanged(binding.seekbarBar, value, false);
|
||||||
} catch (NumberFormatException ignore) {
|
} catch (NumberFormatException ignore) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
valueView.setOnKeyListener((v1, keyCode, event) -> {
|
selectPercentageBinding.selpercentageNumber.setOnKeyListener((v1, keyCode, event) -> {
|
||||||
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
|
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
|
||||||
selectValue.run();
|
selectValue.run();
|
||||||
return true;
|
return true;
|
||||||
|
@ -127,15 +123,15 @@ public class SeekBarPreference extends Preference
|
||||||
builder.create().show();
|
builder.create().show();
|
||||||
});
|
});
|
||||||
|
|
||||||
return layout;
|
return binding.getRoot();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String valToString() {
|
private String valToString() {
|
||||||
return String.format(Locale.US, "%.1f%%", currVal*0.1);
|
return String.format(Locale.US, "%.1f%%", currVal * 0.1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
|
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
|
||||||
if (!callChangeListener(progress)) {
|
if (!callChangeListener(progress)) {
|
||||||
if (currVal != seekBar.getProgress())
|
if (currVal != seekBar.getProgress())
|
||||||
seekBar.setProgress(currVal);
|
seekBar.setProgress(currVal);
|
||||||
|
@ -144,7 +140,7 @@ public class SeekBarPreference extends Preference
|
||||||
if (progress != seekBar.getProgress())
|
if (progress != seekBar.getProgress())
|
||||||
seekBar.setProgress(progress);
|
seekBar.setProgress(progress);
|
||||||
currVal = progress;
|
currVal = progress;
|
||||||
currValBox.setText(valToString());
|
binding.seekbarValue.setText(valToString());
|
||||||
SharedPreferences.Editor editor = getEditor();
|
SharedPreferences.Editor editor = getEditor();
|
||||||
editor.putInt(getKey(), progress);
|
editor.putInt(getKey(), progress);
|
||||||
editor.apply();
|
editor.apply();
|
||||||
|
@ -158,9 +154,11 @@ public class SeekBarPreference extends Preference
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStartTrackingTouch(SeekBar seekBar) {
|
public void onStartTrackingTouch(SeekBar seekBar) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStopTrackingTouch(SeekBar seekBar) {
|
public void onStopTrackingTouch(SeekBar seekBar) {
|
||||||
notifyChanged();
|
notifyChanged();
|
||||||
|
@ -176,7 +174,7 @@ public class SeekBarPreference extends Preference
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
|
protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
|
||||||
int val = restorePersistedValue ? getPersistedInt(DEFAULT_VALUE) : (Integer)defaultValue;
|
int val = restorePersistedValue ? getPersistedInt(DEFAULT_VALUE) : (Integer) defaultValue;
|
||||||
if (!restorePersistedValue)
|
if (!restorePersistedValue)
|
||||||
persistInt(val);
|
persistInt(val);
|
||||||
currVal = val;
|
currVal = val;
|
||||||
|
|
|
@ -1,51 +1,58 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<LinearLayout
|
<layout xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
|
<LinearLayout
|
||||||
android:id="@android:id/widget_frame"
|
android:id="@android:id/widget_frame"
|
||||||
android:orientation="vertical"
|
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="vertical"
|
||||||
android:paddingLeft="16dp"
|
android:paddingLeft="16dp"
|
||||||
android:paddingRight="5dp"
|
|
||||||
android:paddingTop="20dp"
|
android:paddingTop="20dp"
|
||||||
|
android:paddingRight="5dp"
|
||||||
android:paddingBottom="10dp">
|
android:paddingBottom="10dp">
|
||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
android:orientation="horizontal"
|
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content">
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="horizontal">
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/seekbar_title"
|
android:id="@+id/seekbar_title"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:textSize="15dp"
|
android:text=""
|
||||||
android:textColor="@color/White"
|
android:textColor="@color/White"
|
||||||
android:text="">
|
android:textSize="15dp" />
|
||||||
</TextView>
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:gravity="right">
|
android:gravity="right">
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/seekbar_value"
|
android:id="@+id/seekbar_value"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:typeface="monospace"
|
android:text=""
|
||||||
android:textStyle="italic"
|
|
||||||
android:textSize="12dp"
|
android:textSize="12dp"
|
||||||
android:text="">
|
android:textStyle="italic"
|
||||||
</TextView>
|
android:typeface="monospace" />
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
<SeekBar
|
<SeekBar
|
||||||
android:id="@+id/seekbar_bar"
|
android:id="@+id/seekbar_bar"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content">
|
android:layout_height="wrap_content" />
|
||||||
</SeekBar>
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/seekbar_summary"
|
android:id="@+id/seekbar_summary"
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:text="">
|
android:layout_height="wrap_content"
|
||||||
</TextView>
|
android:text="" />
|
||||||
</LinearLayout>
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
</layout>
|
||||||
|
|
|
@ -1,20 +1,24 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<LinearLayout
|
<layout xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
android:orientation="horizontal"
|
android:orientation="horizontal"
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:padding="10dp">
|
android:padding="10dp">
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:text="@string/value_percent"
|
android:layout_height="wrap_content"
|
||||||
android:paddingRight="10dp">
|
android:paddingRight="10dp"
|
||||||
</TextView>
|
android:text="@string/value_percent" />
|
||||||
|
|
||||||
<EditText
|
<EditText
|
||||||
android:id="@+id/selpercentage_number"
|
android:id="@+id/selpercentage_number"
|
||||||
android:layout_width="100dip"
|
android:layout_width="100dip"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:inputType="numberDecimal">
|
android:inputType="numberDecimal" />
|
||||||
</EditText>
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
|
</layout>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user