Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background from PreferenceActivity is not applied to sub-PreferenceScreen

I am testing my application on a Nexus One and i have some problems. My theme is Light and when an inner sub PreferenceScreen is displayed, the window background becomes black instead of keeping the PreferenceActivity's one.

<PreferenceScreen android:title="main preferences">
    ...
    <PreferenceScreen android:title="sub screen">
    </PreferenceScreen>
</PreferenceScreen>

What is the problem?

Wouter

like image 818
wouter88 Avatar asked Jan 27 '10 14:01

wouter88


3 Answers

Use this:

Create theme in style.xml file

<style name="Theme.SettingsBackground" parent="@android:style/Theme.NoTitleBar">
<item name="android:windowBackground">@android:color/black</item>
</style>

and then in manifest file use:

<activity android:name=".Settings" android:theme="@style/Theme.SettingsBackground"></activity>

Do this for all sub activities which you want.

like image 52
shivanna y Avatar answered Nov 09 '22 17:11

shivanna y


To best understand what is happening here you can refer to this piece of code from the source code for the PreferenceScreen class:

 @Override
    protected void onClick() {
        if (getIntent() != null || getPreferenceCount() == 0) {
            return;
        }

        showDialog(null);
    }

    private void showDialog(Bundle state) {
        Context context = getContext();
        ListView listView = new ListView(context);
        bind(listView);

        // Set the title bar if title is available, else no title bar
        final CharSequence title = getTitle();
        Dialog dialog = mDialog = new Dialog(context, TextUtils.isEmpty(title)
                ? com.android.internal.R.style.Theme_NoTitleBar
                : com.android.internal.R.style.Theme);
        dialog.setContentView(listView);
        if (!TextUtils.isEmpty(title)) {
            dialog.setTitle(title);
        }
        dialog.setOnDismissListener(this);
        if (state != null) {
            dialog.onRestoreInstanceState(state);
        }

        // Add the screen to the list of preferences screens opened as dialogs
        getPreferenceManager().addPreferencesScreen(dialog);

        dialog.show();
    }

The way that I work around it is to set the parent background color by overriding onCreateView in the first preference added to the preference screen. Of course this requires some custom code but it's not terribly complicated, for instance to set a white background:

package com.justinbuser.livewallpapers;

import android.preference.PreferenceCategory;

public class VideoChooserPreferenceCategory extends PreferenceCategory{

    public VideoChooserPreferenceCategory(Context context) {
        super(context);
    }

    @Override
    protected View onCreateView(ViewGroup parent)
    {
        parent.setBackgroundColor(0xFFFFFFFF);
        return super.onCreateView(parent);
    }
}

You would then of course need to use that custom category by altering your xml, i.e.:

<PreferenceScreen android:title="main preferences">
    <PreferenceScreen android:title="sub screen">
    <com.justinbuser.livewallpapers.VideoChooserPreferenceCategory android:title="sub screen category" />
    </PreferenceScreen>
</PreferenceScreen>

Also, if you notice the android PreferenceScreen changes the theme based on whether or not a title is set, i.e. if a title exists it enables a theme that includes the title bar. So if you want no title bar you should avoid setting the preferencescreen title and set it statically in xml or dynamically through code.

like image 37
Justin Buser Avatar answered Nov 09 '22 18:11

Justin Buser


Have you tried this?

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference){
        super.onPreferenceTreeClick(preferenceScreen, preference);
        if (preference!=null)
            if (preference instanceof PreferenceScreen)
                if (((PreferenceScreen)preference).getDialog()!=null)
                    ((PreferenceScreen)preference).getDialog().getWindow().getDecorView().setBackgroundDrawable(this.getWindow().getDecorView().getBackground().getConstantState().newDrawable());
        return false;
    }

Add this method in your PreferenceActivity.

At comment #35 from this source.

like image 1
Anggrayudi H Avatar answered Nov 09 '22 16:11

Anggrayudi H