I have a settings section in my android application, where the user using radio buttons, is able to change the main activity's background image. The problem is that when I turn the method for storing the selection of the user into static, I can not use this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
to read the preferences.
The method is called setColorOnPreference()
and is lunched on the onCreate method of the main activity to check the last user's selection of background. But the method belongs to the settings activity.
public class Settings extends Activity {
private final static String userSettings="userSettings.txt";
private EditText txtEditor;
static LinearLayout mScreen;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
txtEditor=(EditText)findViewById(R.id.textbox);
mScreen = (LinearLayout) findViewById(R.id.homeScreen);
try {
InputStream in = openFileInput(userSettings);
if (in != null) {
InputStreamReader tmp=new InputStreamReader(in);
BufferedReader reader=new BufferedReader(tmp);
String str;
StringBuilder buf=new StringBuilder();
while ((str = reader.readLine()) != null) {
buf.append(str);
}
in.close();
txtEditor.setText(buf.toString());
}
}
catch (java.io.FileNotFoundException e) {
}
catch (Throwable t) {
Toast.makeText(this, "Exception: "+t.toString(), Toast.LENGTH_LONG).show();
}
}
public void saveClicked(View v) {
try {
OutputStreamWriter out=
new OutputStreamWriter(openFileOutput(userSettings, 0));
out.write(txtEditor.getText().toString());
out.close();
Toast.makeText(this, "The contents are saved in the file.", Toast.LENGTH_LONG).show();
}
catch (Throwable t) {
Toast.makeText(this, "Exception: "+t.toString(), Toast.LENGTH_LONG).show();
}
}
public void saveBgColorPreference()
{
RadioGroup g = (RadioGroup) findViewById(R.id.prefgroup);
int selected = g.getCheckedRadioButtonId();
RadioButton b = (RadioButton) findViewById(selected);
String selectedValue = (String) b.getText();
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putString("bgcolor", selectedValue);
prefsEditor.commit();
}
public static void setColorOnPreference()
{
SharedPreferences myPrefs2 = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
String prefName = myPrefs2.getString("bgcolor", "Navy Blue");
if(prefName.equals("Navy Blue"))
mScreen.setBackground( getResources().getDrawable(R.drawable.mainbg));
else
mScreen.setBackgroundColor(0xffff0000);
}
public void savepreferencesClicked(View v) {
saveBgColorPreference();
setColorOnPreference();
}
The error I am getting is that I can use SharedPreferences myPrefs2 = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
and also on the mScreen.setBackground( getResources().getDrawable(R.drawable.mainbg));
suggesting me that I cannot use this. in a static method.
Sorry for the lots of text, I just wanted to make sure that you understand my issue. Open to any suggestions and implementations
The question here is how will I call a method into another activity?