1. Check out our companion app, Forums for Android! Download from Google Play

Set a background image in a PreferenceScreen

Discussion in 'Application Development' started by Leolicos, Jul 21, 2010.

  1. Leolicos

    Leolicos Well-Known Member
    Thread Starter
    15

    Jun 16, 2010
    54
    0
    15
    Senior Software Engineer
    Chicago, IL
    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
    Thread Starter
    15

    Jun 16, 2010
    54
    0
    15
    Senior Software Engineer
    Chicago, IL
    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
    163

    Jul 13, 2010
    1,934
    458
    163
    Male
    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
    Thread Starter
    15

    Jun 16, 2010
    54
    0
    15
    Senior Software Engineer
    Chicago, IL
    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
    163

    Jul 13, 2010
    1,934
    458
    163
    Male

    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
    Thread Starter
    15

    Jun 16, 2010
    54
    0
    15
    Senior Software Engineer
    Chicago, IL
    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
    163

    Jul 13, 2010
    1,934
    458
    163
    Male
    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
    Thread Starter
    15

    Jun 16, 2010
    54
    0
    15
    Senior Software Engineer
    Chicago, IL
    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
    Thread Starter
    15

    Jun 16, 2010
    54
    0
    15
    Senior Software Engineer
    Chicago, IL
    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
    6

    Dec 4, 2010
    7
    1
    6

Share This Page

Loading...