android settings view from yaml spec
This is an android View
widget that creates a custom settings screen based on a YAML.
The YAML file describes how to draw the settings but doesn’t contain values for them, which means your app code has to:
(Those tasks may become more automatic in future versions of this library. File an issue if you start working on one of them.)
This is a screenshot of the app in the YamlSample folder.
repositories {
maven { url 'https://www.jitpack.io' }
}
dependencies {
compile 'com.github.abe-winter
v0.0.2'
}
<com.github.abe_winter.yaml_settings.YamlSettings
android:id="@+id/yaml_settings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:yamlRawId="@raw/settings"
app:strListDelIcon="@drawable/ic_delete_black_24dp"
android:padding="18dp"></com.github.abe_winter.yaml_settings.YamlSettings>
app:yamlRawId
has to match the res/raw/whatever.yml file you createdapp:strListDelIcon
has to be a drawable in your project. Leave it out if your yaml file doesn’t use the StringList widgetandroid:id
field attr is optional; you use it to access access the settings in a Fragment/Activity:
public class SettingsFragment extends Fragment implements SettingsCallback {
public SettingsFragment() {}
// example populating a settings screen with different types from JSON
void ondataload(JSONObject reply) throws JSONException {
Map<String, View> view_lookup = ((YamlSettings)getView().findViewById(R.id.yaml_settings)).view_lookup;
JSONObject body = reply.getJSONObject("body");
for (Iterator<String> it=body.keys(); it.hasNext();){
String key = it.next();
if (!view_lookup.containsKey(key)){
Log.e(TAG, "unk settings key "+key);
continue;
}
if (body.isNull(key)) continue;
switch (key){
case "profile_picture":{
byte[] bytes = Base64.decode(body.getString(key), Base64.DEFAULT);
((ImageSel)view_lookup.get(key)).m_img.setImageBitmap(BitmapFactory.decodeByteArray(bytes, 0, bytes.length));
break;
}
case "locations":{
StringList sl = (StringList) view_lookup.get(key);
JSONArray arr = body.getJSONArray(key);
for (int i=0;i<arr.length();++i) sl.addElt(getContext(), arr.getString(i));
break;
}
case "connect_with.kind":{
// radio
// todo: this is horrible. the indexes are going to change
int index = Arrays.asList("Singles", "Professionals", "Community").indexOf(body.get(key));
((RadioButton)((RadioGroup)view_lookup.get(key)).getChildAt(index)).setChecked(true);
break;
}
case "email_preferences.match":
case "email_preferences.terms":{
// boolean switch
((Switch)view_lookup.get(key)).setChecked(body.getBoolean(key));
break;
}
default: {
((TextInputEditText)view_lookup.get(key)).setText(body.getString(key));
break;
}
}
}
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// set listener on the YamlSettings instance.
// Doesn't have to be in onActivityCreated, but needs to be after views have been inflated.
((YamlSettings)getView().findViewById(R.id.yaml_settings)).listener = this;
}
// callbacks from SettingsCallback interface
public void btnclick(View sview, View v, SettingsNode node){}
public void textchange(View sview, Editable e, String group, String name, String value) {}
public void radiochange(View sview, CompoundButton buttonView, boolean isChecked, String group, String name, String option){}
public void switchchange(View sview, CompoundButton buttonView, boolean isChecked, String group, String name){}
public void listadd(StringList sl, String name, String val) {}
public void listrm(StringList sl, final View elt, final String name, final String val) {}
public void pickimage(ImageSel sel, String name) {}
}
(see above)
(todo: best example would be a working app)
(Sorry.)