Skip to content

Instantly share code, notes, and snippets.

@ulyssesdotcodes
Created August 14, 2015 21:59
Show Gist options
  • Save ulyssesdotcodes/ff5db4e6e21dd25ac8c5 to your computer and use it in GitHub Desktop.
Save ulyssesdotcodes/ff5db4e6e21dd25ac8c5 to your computer and use it in GitHub Desktop.
An AlertDialog that allows you to adjust variable values
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class ParameterAlertDialog extends DialogFragment {
ArrayList<Param> mParams;
private enum Type {
INTEGER,
FLOAT,
BOOLEAN,
MAP
}
public ParameterAlertDialog() {
mParams = new ArrayList<>();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LinearLayout linearLayout = new LinearLayout(getActivity());
linearLayout.setOrientation(LinearLayout.VERTICAL);
for (Param param : mParams) {
TextView text = new TextView(getActivity());
text.setText(param.name);
linearLayout.addView(text);
TextView value = new TextView(getActivity());
linearLayout.addView(value);
View editView;
switch (param.type) {
case INTEGER:
editView = createIntegerView((IntegerParam) param, value);
break;
case FLOAT:
editView = createFloatView((FloatParam) param, value);
break;
case BOOLEAN:
editView = createBooleanView(param, value);
break;
case MAP:
editView = createMapView((MapParam) param, value);
break;
default:
throw new UnsupportedOperationException("We don't seem to support that parameter type");
}
linearLayout.addView(editView);
}
AlertDialog.Builder builder =
new AlertDialog.Builder(getActivity())
.setView(linearLayout)
.setNeutralButton("Done", null);
return builder.create();
}
public void addParam(String name, int defVal, int min, int max, OnChangeListener<Integer> listener) {
IntegerParam integerParam = setParamFields(new IntegerParam(), Type.INTEGER, name, defVal, listener);
integerParam.name = name;
integerParam.currentValue = defVal;
integerParam.min = min;
integerParam.max = max;
integerParam.onChangeListener = listener;
mParams.add(integerParam);
}
public void addParam(String name, float defVal, float min, float max, OnChangeListener<Float> listener) {
FloatParam floatParam = setParamFields(new FloatParam(), Type.FLOAT, name, defVal, listener);
floatParam.name = name;
floatParam.currentValue = defVal;
floatParam.min = min;
floatParam.max = max;
floatParam.onChangeListener = listener;
mParams.add(floatParam);
}
public void addParam(String name, boolean defVal, OnChangeListener<Boolean> listener) {
mParams.add(setParamFields(new Param<Boolean>(), Type.BOOLEAN, name, defVal, listener));
}
/**
* Map parameters using a drop down menu. Be warned that this uses S::toString() to show the options.
*/
public <S, T> void addParam(String name, S defVal, final Map<S, T> map, final OnChangeListener<T> listener) {
MapParam<S, T> mapParam = setParamFields(new MapParam<S, T>(), Type.MAP, name, defVal,
new OnChangeListener<S>() {
@Override
public void onChange(S s) {
listener.onChange(map.get(s));
}
});
mapParam.values = map;
mParams.add(mapParam);
}
private <S, T extends Param<S>> T setParamFields(T param, Type type, String name, S defVal,
OnChangeListener<S> listener) {
param.type = type;
param.name = name;
param.currentValue = defVal;
param.onChangeListener = listener;
return param;
}
private View createIntegerView(final IntegerParam integerParam, final TextView value) {
value.setText(Integer.toString(integerParam.currentValue));
final float min = integerParam.min;
final float max = integerParam.max;
SeekBar seekBar = new SeekBar(getActivity());
seekBar.setProgress((int) (100 * (((float) integerParam.currentValue - min) / (max - min))));
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int newValue = integerParam.min + (int) (((float) progress * 0.01f) * (max - min));
integerParam.onChangeListener.onChange(newValue);
value.setText(Integer.toString(newValue));
integerParam.currentValue = newValue;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
return seekBar;
}
private View createFloatView(final FloatParam floatParam, final TextView value) {
value.setText(Float.toString(floatParam.currentValue));
final float min = floatParam.min;
final float max = floatParam.max;
SeekBar seekBar = new SeekBar(getActivity());
seekBar.setProgress((int) (100 * ((floatParam.currentValue - min) / (max - min))));
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
float newValue = floatParam.min + (((float) progress * 0.01f) * (max - min));
floatParam.onChangeListener.onChange(newValue);
value.setText(Float.toString(newValue));
floatParam.currentValue = newValue;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
return seekBar;
}
private View createBooleanView(final Param<Boolean> param, final TextView value) {
value.setText(Boolean.toString(param.currentValue));
CheckBox checkBox = new CheckBox(getActivity());
checkBox.setChecked(param.currentValue);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
param.onChangeListener.onChange(isChecked);
param.currentValue = isChecked;
value.setText(Boolean.toString(isChecked));
}
});
return checkBox;
}
private <S, T> View createMapView(final MapParam<S, T> mapParam, final TextView value) {
value.setText(mapParam.currentValue.toString());
final List<S> keys = new ArrayList<>(mapParam.values.keySet());
Spinner spinner = new Spinner(getActivity());
ArrayAdapter<S> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, keys);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setSelection(keys.indexOf(mapParam.currentValue));
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
S val = keys.get(position);
mapParam.onChangeListener.onChange(val);
mapParam.currentValue = val;
value.setText(val.toString());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return spinner;
}
private class Param<T> {
Type type;
OnChangeListener<T> onChangeListener;
String name;
T currentValue;
}
private class IntegerParam extends Param<Integer> {
int min;
int max;
}
private class FloatParam extends Param<Float> {
float min;
float max;
}
private class MapParam<S, T> extends Param<S> {
Map<S, T> values;
}
public interface OnChangeListener<T> {
void onChange(T t);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment