Closed bryanchun closed 7 years ago
New working example using GSON library:
LinearLayout rows = (LinearLayout) findViewById(R.id.rows); //gets the container linear layout
String json = "";
//Retrieve the string of the keypad.json file
InputStream in_s = getResources().openRawResource(R.raw.keypad);
try {
byte[] b = new byte[in_s.available()];
in_s.read(b);
json = new String(b);
} catch (IOException e) {
}
//Initialize a GSON JsonObject and parse the json string
JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
//Convert the jsonObject to pre-defined, compatible Java classes
JsonHelper.KeypadRows keypadRows = gson.fromJson(jsonObject, JsonHelper.KeypadRows.class);
//add the keys in loop
for (JsonHelper.Key[] keys : keypadRows.rows) {
LinearLayout row = new LinearLayout(this);
row.setOrientation(LinearLayout.HORIZONTAL);
row.setLayoutParams(new LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT));
for (JsonHelper.Key key : keys) {
Button btn = new Button(this);
btn.setText(key.key);
btn.setLayoutParams(new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT, 1f));
row.addView(btn);
}
rows.addView(row);
}
public class JsonHelper {
public class KeypadRows {
Key[][] rows;
}
public class Key {
String key;
String style;
String shift;
String alpha;
}
}
{
"rows":[
[
{
"key": "shift",
"style": "Fn"
},
{
"key": "alpha",
"style": "Fn"
},
{
"key": "constant",
"style": "Fn"
},
{
"key": "mode",
"style": "Fn"
},
{
"key": "derivative",
"style": "Fn",
"shift": "integration"
},
{
"key": "function",
"style": "Fn_Orange"
}
],
[
{
"key": "complex_fraction",
"style": "Fn",
"shift": "improper_fraction"
},
{
"key": "square_root",
"style": "Fn"
},
{
"key": "x_squared",
"style": "Fn"
},
{
"key": "power",
"style": "Fn",
"shift": "x_root"
},
{
"key": "log",
"style": "Fn",
"shift": "10_power_x"
},
{
"key": "natural_log",
"style": "Fn",
"shift": "exp_power"
}
],
[
{
"key": "negation",
"style": "Fn",
"shift": "angle"
},
{
"key": "sexagesimal",
"style": "Fn",
"shift": "sexagesimal_back"
},
{
"key": "hyperbolic",
"style": "Fn"
},
{
"key": "sin",
"style": "Fn",
"shift": "arc_sin"
},
{
"key": "cosine",
"style": "Fn",
"shift": "arc_cosine"
},
{
"key": "tangent",
"style": "Fn",
"shift": "arc_tangent"
}
],
[
{
"key": "recall",
"style": "Fn",
"shift": "store"
},
{
"key": "engineering",
"style": "Fn",
"shift": "engineering_back"
},
{
"key": "left_parenthesis",
"style": "Fn",
"shift": "percent"
},
{
"key": "right_parenthesis",
"style": "Fn",
"shift": "absolute"
},
{
"key": "comma",
"style": "Fn",
"shift": "semicolon"
},
{
"key": "memory_plus",
"style": "Fn",
"shift": "memory_minus"
}
],
[
{
"key": "7",
"style": "Large_More",
"shift": "constant"
},
{
"key": "8",
"style": "Large"
},
{
"key": "9",
"style": "Large_More",
"shift": "clear"
},
{
"key": "delete",
"style": "Purple_Large_Text_More",
"shift": "insert"
},
{
"key": "all_clear",
"style": "Purple_Large_Text"
}
],
[
{
"key": "4",
"style": "Large"
},
{
"key": "5",
"style": "Large"
},
{
"key": "6",
"style": "Large"
},
{
"key": "multiply",
"style": "Large",
"shift": "permutation"
},
{
"key": "divide",
"style": "Large",
"shift": "combination"
}
],
[
{
"key": "1",
"style": "Large"
},
{
"key": "2",
"style": "Large"
},
{
"key": "3",
"style": "Large"
},
{
"key": "add",
"style": "Large",
"shift": "polar"
},
{
"key": "subtract",
"style": "Large",
"shift": "rectangular"
}
],
[
{
"key": "0",
"style": "Large",
"shift": "round"
},
{
"key": "dot",
"style": "Large",
"shift": "random"
},
{
"key": "exponential",
"style": "Large_Text",
"shift": "pi"
},
{
"key": "answer",
"style": "Large_Text",
"shift": "angle_units"
},
{
"key": "execute",
"style": "Large_Text"
}
]
]
}
Json is updated. The above comment is no longer relevant. Please find the json in res/raw/keypad.json
Instead of statically define layout elements.