Set a background image in a PreferenceScreen


Last Updated:

  1. Leolicos

    Leolicos Well-Known Member This Topic's Starter

    Joined:
    Jun 16, 2010
    Messages:
    54
    Likes Received:
    0
    I would like to set my own background in my preference screen, but the method I know doesn't work...

    RelativeLayout
    ImageView
    PreferenceScreen

    This just crashes my app.
     

    Advertisement
  2. Leolicos

    Leolicos Well-Known Member This Topic's Starter

    Joined:
    Jun 16, 2010
    Messages:
    54
    Likes Received:
    0
    I've looked into this more, and I guess the doc says I need to use a style, but that doesn't seem to work either. Here's my styles.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <resources>
    <style name="prefVerticalTheme">
    <item name="android:windowBackground">@drawable/backvert</item>
    </style>
    <style name="prefHorizontalTheme">
    <item name="android:windowBackground">@drawable/backhorz</item>
    </style>
    </resources>

    then in my preferenceactivity.onCreate

    int orientation = getRequestedOrientation();
    if ( orientation == Configuration.ORIENTATION_PORTRAIT )
    setTheme(R.style.prefVerticalTheme);
    else
    setTheme(R.style.prefHorizontalTheme);

    addPreferencesFromResource(R.xml.preferences);

    That doesn't seem to work either. How do I set the background image to a preference page???
     
  3. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    All you have to do is in your layout file for the pref screen, set the android:background property of the parent layout view to the drawable you want.
     
  4. Leolicos

    Leolicos Well-Known Member This Topic's Starter

    Joined:
    Jun 16, 2010
    Messages:
    54
    Likes Received:
    0
    what do you mean by parent layout?

    Here's what I have.

    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/background">
    </PreferenceScreen>

    then in code I simply

    addPreferencesFromResource(R.xml.preferences);

    that still doesn't set the background to my image...
     
  5. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458

    Err... Sorry, I wasn't aware that PreferenceScreen was a pre-defined XML tag.

    Well, in that case, the only thing i can think of would be to wrap the <PreferenceScreen></PreferenceScreen> in a LinearLayout and set the android:background property to your background.
     
  6. Leolicos

    Leolicos Well-Known Member This Topic's Starter

    Joined:
    Jun 16, 2010
    Messages:
    54
    Likes Received:
    0
    That just crashed the app...

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/background">
    <PreferenceScreen>
    </PreferenceScreen>
    </LinearLayout>

    Does it make a different that my class extends a PreferenceActivity, not a regular activity?
     
  7. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    I am honestly not sure. I have never messed with PreferenceScreens. Is it just a predefined preference activity? If so, why not just make your own?
     
  8. Leolicos

    Leolicos Well-Known Member This Topic's Starter

    Joined:
    Jun 16, 2010
    Messages:
    54
    Likes Received:
    0
    How would I do what the PreferenceScreen does? I looked up the ListView and all that, but then I found that it's much easier to just do a PreferenceActivity and use a PreferenceScreen. How do I do it without a PreferenceActivity?
     
  9. Leolicos

    Leolicos Well-Known Member This Topic's Starter

    Joined:
    Jun 16, 2010
    Messages:
    54
    Likes Received:
    0
    Is there a tutorial handy to set up a ListView in the same way a PreferenceScreen works but not use a PreferenceActivity and a PreferenceScreen?
     
  10. Justin Buser

    Justin Buser Member

    Joined:
    Dec 4, 2010
    Messages:
    7
    Likes Received:
    1

Share This Page

Loading...