I am attempting to change the background color of my Android app (I'm new to the ADK).
I read in another question that I would have to use another LinearLayout between my main layout (RelativeLayout) and all of the other views in the app, and change the color of that instead. I want a preference to dictate which color the background changes to, and the preference activity and everything is running smoothly; however, when I pass R.id.bg (bg is the LinearLayout's ID) to findViewById(), it returns null and throws an NPE whenever I attempt to change the background color.
EDIT: Y'know what, here's the entire class's source. :P
public class Preferences extends PreferenceActivity implements
OnSharedPreferenceChangeListener {
SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);
}
private void showToast(CharSequence text) {
Context context = getApplicationContext();
showToast(context, text, 3000);
}
private void showToast(Context context, CharSequence text, int duration) {
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key.equalsIgnoreCase("list_color")) {
LinearLayout bg = (LinearLayout) findViewById(R.id.bg);
String color = sharedPreferences.getString("list_color", "White");
if (bg == null) {
showToast("Unable to change background color");
} else {
if (color.equals("White")) {
bg.setBackgroundColor(android.R.color.white);
showToast("Background color set to white");
} else if (color.equals("Black")) {
bg.setBackgroundColor(android.R.color.black);
showToast("Background color set to black");
}
} // end NP test
}
}
}