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
Asked by: Miller190 | Posted: 24-01-2022
Answer 1
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.
Answered by: Elian369 | Posted: 25-02-2022Answer 2
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.
Answered by: Brooke378 | Posted: 25-02-2022Answer 3
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.
Answered by: Stuart534 | Posted: 25-02-2022Similar questions
android - Programmatically populating a sub-PreferenceScreen already contained by a PreferenceFragment XML
Sorry for the weird title.
I want to programmatically add a PreferenceCategory to one of my PreferenceScreens that already exist within this file (a cropped version of my preferences.xml):
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Monitored Zones -->
<P...
Still can't find your answer? Check out these communities...
Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android