I have a problem with saving the ImageButton in SharedPreferences.
Basically, I have a special REFRESH button:
bbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {... }
and when clicking on it, it creates another ImageButton in a HorizontalScrollView's LinearLayout with background called "a" from drawables folder. That new generated ImageButton will have its own onClick function. However, before creating it there is no ImageButton in that activity, it will appear in a "LinearLayoutScrollView" only after clicking on the special REFRESH button with a couple of "if and else if" statements which will generate ImageButtons with different drawable backgrounds.
All I want is that after creating new ImageButton (with its own background from drawables) it would stay in the app till the user deletes the app or the ImageButton itself. However, I want to see saved ImageButton even after restarting the app.
What I am trying to do now is shown below but it does not work so probably I am doing something wrong. Maybe anyone knows what I need to change?
Right in the end I am using SharedPreferences code to save newly created ImageButton with background of drawable "a". And after that else if follows a few more else if's which are basically the same as the one in the top only with different drawable backgrounds for different selections which I get as url's from intents in another activity...
EDITED according to your advice (but still it doesnt save the image):
final SharedPreferences prefs = getSharedPreferences("MySavedHomeFile", MODE_PRIVATE);
final LinearLayout Row = (LinearLayout) findViewById(R.id.LinearLayoutScrollView);
bbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent a = getIntent();
Intent b = getIntent();
String url1 = a.getStringExtra("url1");
String url2 = b.getStringExtra("url2");
if (url1 == null && url2 == null && url3 == null) {
Toast.makeText(getApplication(), "No new clients added!", Toast.LENGTH_SHORT).show();
} else if (url1 != null) {
int backgroundRes = prefs.getInt("savedImageButton" , R.drawable.a);
final ArrayList<String> Keys = new ArrayList<String>();
for(int ii = 0; ii < 1; ii ++){
Keys.add("Keys is : " + String.valueOf(ii));
}
LinearLayout Row = (LinearLayout) findViewById(R.id.LinearLayoutScrollView);
int width = 240;
int height = 240;
final ImageButton[] my_button = new ImageButton[Keys.size()];
for (int bt = 0; bt < Keys.size(); bt ++) {
final int Index = bt;
my_button[Index] = new ImageButton(HomePageNews.this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(width, height);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, R.id.imageButton11);
my_button[Index].setLayoutParams(lp);
my_button[Index].setId(Index);
my_button[Index].setBackgroundResource(backgroundRes);
my_button[bt].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (my_button[Index].getId() == ((ImageButton) v).getId()) {
}
}
});
Row.addView(my_button[Index]);
}
SharedPreferences saveNewHome = getSharedPreferences("MySavedHomeFile", MODE_PRIVATE);
SharedPreferences.Editor editor = saveNewHome.edit();
editor.putInt("savedImageButton", R.drawable.a);
editor.apply();
}else if (url2 != null) {
int backgroundRes = prefs.getInt("savedImageButton" , R.drawable.b) ;
final ArrayList<String> Keys2 = new ArrayList<String>();
for(int ee = 0; ee < 1; ee ++){
Keys2.add("Keys is : " + String.valueOf(ee));
}
LinearLayout Row2 = (LinearLayout) findViewById(R.id.LinearLayoutScrollView);
int width = 240;
int height = 240;
final ImageButton[] my_button2 = new ImageButton[Keys2.size()];
for (int bt2 = 0; bt2 < Keys2.size(); bt2 ++){
final int Index2 = bt2;
my_button2[Index2] = new ImageButton(HomePageNews.this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(width, height);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, R.id.imageButton11);
my_button2[Index2].setLayoutParams(lp);
my_button2[Index2].setId(Index2);
my_button2[Index2].setBackgroundResource(backgroundRes);
my_button2[bt2].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (my_button2[Index2].getId() == ((ImageButton) v).getId()){
}
}
});
Row2.addView(my_button2[Index2]);
}
SharedPreferences prefs = getSharedPreferences("MySavedHomeFile", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("savedImageButton", R.drawable.b);
editor.apply();