I would like to add multiple dynamic buttons. Their texts are saved in SharedPreferences.
LinearLayout layout = view.findViewById(R.id.root);
SharedPreferences mPrefs = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
mPrefs = getContext().getSharedPreferences("k-texts", Context.MODE_PRIVATE);
}
Map<String, ?> allEntries = mPrefs.getAll();
for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
Button btn = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
btn = new Button(this.getContext());
}
btn.setText(entry.getValue().toString());
btn.setTextColor(Color.BLUE);
btn.setBackgroundColor(Color.RED);
layout.addView(btn);
}
The problem is, I get only 1 button with the text ["1","2","3"]. Why is my loop adding only 1 button, instead of 3?