1

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

        }

    }
}
ChaoticWeg
  • 291
  • 2
  • 15

2 Answers2

1

You cannot fetch a View directly from preferences in the traditional manner.

The closest you can do is get the Preference using findPreference():

Preference myPreference = findPreference("key");

If you need the View (which I assume you do), you can try this:

View v = myPreference.getView(null, null);

And that should return the View container which represents the Preference.

Cat
  • 66,919
  • 24
  • 133
  • 141
  • Thanks, I'll look at it again. If the preference is a ListPreference, how do I get the selected value from the Preference? – ChaoticWeg Nov 25 '12 at 01:02
  • Cast it to a [`ListPreference`](http://developer.android.com/reference/android/preference/ListPreference.html), using `ListPreference pref = (ListPreference) ...;`. – Cat Nov 25 '12 at 01:06
  • Oh okay, there we go. So now when I change the background color _preference_, the message says "Background changed to ...", but the background doesn't change. I'm assuming this just means that either I can't do it, or I'm grabbing the wrong View using `myPreference.getView(null, null);`. – ChaoticWeg Nov 25 '12 at 01:16
  • Also, it gives me both Toast popups whenever I change the preference value: always "black" first, and then "white". – ChaoticWeg Nov 25 '12 at 01:18
  • 1
    Take a look here: http://stackoverflow.com/questions/3551169/change-background-color-of-preference ... if those methods don't work, I would assume it cannot be done (and may vary by API). Additionally, that toast glitch would only make sense if the preference was changed twice. `registerOnSharedPreferenceChangeListener` may be called twice. Call `unregisterOnSharedPreferenceChangeListener` in `onDestroy()`. – Cat Nov 25 '12 at 01:54
  • I removed the `else` from the `bg == null` check, and made it return instead. That fixed the Toast glitch. I'll check out that link, thanks man. – ChaoticWeg Nov 25 '12 at 02:35
  • Well, the link tells me how to modify the Preferences activity's layout, but not the Main activity's layout using preferences. – ChaoticWeg Nov 25 '12 at 06:40
  • Oh, you're trying to change the main activity from the preferences? Ahhhhhhh, okay. What you want to do then is load the preference (using `SharedPreferences`) in the *main* activity, then set your background color appropriately. (I thought you were trying to change the preferences activity background...) – Cat Nov 25 '12 at 17:04
  • Oh, alright. Sorry, re-reading shows I _was_ being pretty vague. XD But thanks, and your answers for the preferences activity helped too anyway. :) – ChaoticWeg Nov 27 '12 at 01:59
-1

Show your onCreate method. Maybe you forget to add setContentView(R.layout.your_layout);:

super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);

or import incorrect R file.

Artyom Kiriliyk
  • 2,513
  • 1
  • 17
  • 21
  • Well, it's in the PreferenceActivity extending class, but I'll do it anyway. Do I need to set the content view to the LinearLayout or the RelativeLayout? (RelativeLayout is the main layout of the app, the LinearLayout is only for the background color.) – ChaoticWeg Nov 25 '12 at 00:37
  • If it's PreferenceActivity you need to add something like this in onCreate: `addPreferencesFromResource(R.xml.preferences);` – Artyom Kiriliyk Nov 25 '12 at 00:40
  • Yeah, addPreferencesFromResource(R.xml.preferences); is in onCreate(). But in onCreate() do I need to add setContentView(R.layout.main); ? – ChaoticWeg Nov 25 '12 at 00:46
  • 2
    You do not add `setContentView` in a preferences activity. – Cat Nov 25 '12 at 00:49
  • So what am I missing? I know I'm doing something wrong, I just don't know what. :P – ChaoticWeg Nov 25 '12 at 00:50
  • **setContentView(R.layout.your_layout);** addPreferencesFromResource(R.xml.preferences); – Artyom Kiriliyk Nov 25 '12 at 01:19
  • 1
    I did `setContentView(R.layout.activity_main)` (my main layout), and all it did was crash the app. – ChaoticWeg Nov 25 '12 at 01:41