I have a PreferenceCategory, xml file and I have defined all preferences in it, I call this from class that extends PreferenceActivity. I am unable to set the background of my settings screen, this screen is displayed with help of xml file shown below. Please see that I have already defined the android:background="#041A37", still the screen remains default color: black.
public class MyPreferenceActivity extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
Context mContext=super.getBaseContext();
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.preference);
//v.setBackgroundColor(Color.rgb(4, 26, 55));
}
}
preference.xml is
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#041A37" >
<PreferenceCategory>
<com.dropcall.SeekBarPreference
android:background="#041A37"
android:defaultValue="5"
android:key="#string/Interference_Delay"
android:progressDrawable="#drawable/seekbardrawable"
android:title="Seconds Delay until intereference" />
<com.dropcall.SeekBarPreference2
android:defaultValue="30"
android:key="#string/Drop_Delay"
android:progressDrawable="#drawable/seekbardrawable"
android:title="Seconds delay until drop" />
<CheckBoxPreference
android:background="#drawable/state_normal"
android:defaultValue="true"
android:key="#string/Drop_Option"
android:title="Close after call drop" />
<CheckBoxPreference
android:background="#drawable/state_normal"
android:defaultValue="true"
android:key="#string/Timer_Option"
android:title="Start timers on launch" />
</PreferenceCategory>
</PreferenceScreen>
Although I have set android:background="#041A37" in every file, the background doesn't turn into navy blue, or any other color for that matter. It remains default color, black. How to change the background color. Please let me know any pointers / hints , if you had faced same issue let me know what changes you made to set the background color.
You can define a theme and then set this for your PreferenceActivity in the manifest. Your theme can then define a a background color or a windowBackground image should you prefer that.
Manifest:
<activity android:label="#string/app_label" android:name=".client.PreferencesActivity"
android:theme="#style/PreferencesTheme">
<intent-filter>
</intent-filter>
</activity>
Then add the theme to your styles.xml
<style name="PreferencesTheme">
<item name="android:windowBackground">#drawable/background_image</item>
<item name="android:background">#FFEAEAEA</item>
</style>
In the above snippet there's both a background color and a background image defined to show how to do it.
This worked for me
getListView().setBackgroundColor(Color.TRANSPARENT);
getListView().setCacheColorHint(Color.TRANSPARENT);
getListView().setBackgroundColor(Color.rgb(4, 26, 55));
Another work-around as far as color goes is that you create a theme for the preferences activity and put the background color of list views as well:
<style name="PrefsTheme" parent="#android:style/Theme.Black.NoTitleBar">
<item name="android:windowBackground">#color/prefs_bg</item>
<item name="android:textColor">#color/text_color</item>
<item name="android:listViewStyle">#style/listViewPrefs</item>
</style>
<style name="listViewPrefs" parent="#android:style/Widget.ListView">
<item name="android:background">#color/prefs_bg</item>
<item name="android:cacheColorHint">#color/prefs_bg</item>
</style>
android:background is not an available attribute, according to the documentation.
It is possible you could theme the PreferenceActivity to achieve your color change, though I have not tried this, because I want my preferences to look like those of the rest of Android, to improve usability of the app.
Or you can also make drawable as your background:
getListView().setBackgroundDrawable(getResources().getDrawable(R.drawable.bluegradient));
Note: setBackgroundDrawable() is deprecated. Use setBackground() instead.
getListView().setBackground(getResources().getDrawable(R.drawable.bluegradient));
I faced with the same requirement (Androidx Preference Screen background for settings fragment).
The below code has worked for me in a fragment. (in themes.xml). I assume that it is gonna work also for an activity.
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
...............
<!-- Add below -->
<item name="preferenceTheme">#style/preference</item>
</style>
<style name="preference" parent="Theme.AppCompat">
<item name="android:background">#color/purple_200</item>
</style>
</resources>
Please specify a linear layout with a textview attached and specify background color and attach this xml to preferencecategory using the layout property.
<PreferenceCategory
android:layout="#layout/preftitle"
>
Where preftitle is an xml which has a linear layout and text view attached.
Go to res>values>styles.xml> and add this code to your <style > </style>
the style should must be app base theme
in this #color/activ is color resources added to colors.
<style name="app_theme" parent="#android:style/Theme">
<item name="android:windowBackground">#color/activ</item>
<item name="android:windowContentOverlay">#drawable/title_bar_shadow</item>
<item name="android:listViewStyle">#style/TransparentListView</item>
</style>
if you use app_theme name of style tag
then add like this to your manifest
<application
android:name=".XXXXXX"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/app_theme" > //here
If you only want to change your background
I am using the PreferenceFragmentCompat, the below solution worked for me.
SettingsScreen
import android.os.Bundle
import android.util.TypedValue
import android.view.View
import androidx.annotation.ColorInt
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.almarai.easypick.R
class SettingsScreen : PreferenceFragmentCompat(),
Preference.OnPreferenceChangeListener {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
//Providing the XML file for the view to be created
setPreferencesFromResource(R.xml.app_settings_preferences, rootKey)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
//Get the Theme specific color
val typedValue = TypedValue()
val theme = requireContext().theme
/*R.attr.colorBackgroundScreenBody is my own attr from attrs.xml file,
you can directly use R.color.red - Or any color from your colors.xml
file if you do not have multi-theme app.*/
theme.resolveAttribute(R.attr.colorBackgroundScreenBody, typedValue, true)
#ColorInt val color = typedValue.data
view.setBackgroundColor(color)
super.onViewCreated(view, savedInstanceState)
}
}
Related
I'm using React Native for building my android application and I've followed this tutorial to set up my splash screen. Here is the result. My main problem is that the status bar color changes to black, I can't solve this by having <item name="colorPrimaryDark">#color/colorPrimaryDark</item> in my styles.xml file, and <color name="blue">#009CD7</color>, <color name="colorPrimaryDark">#009CD7</color> in my colors.xml file.
Bonus question: how to center the image without hardcoding a margin so that it stays in the center regardless of the device the app is running on?
Add colorPrimaryDark one more item in your current splash theme or create a new one.
<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:statusBarColor">#android:color/white</item>
<item name="android:windowLightStatusBar">true</item>
<item name="android:background">#drawable/background_splash</item>
<item name="colorPrimaryDark">#android:color/white</item>
</style>
Then use it as a second parameter while showing SplashScreen.
public class MainActivity extends ReactActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
SplashScreen.show(this, R.style.SplashTheme, true);
super.onCreate(savedInstanceState);
}
}
If you see an error something like that -> "int can not be boolean"
Just add third parameter to MainActivity.java
SplashScreen.show(this, R.style.SplashTheme, true);
Create a style definition for this in android/app/src/main/res/values/styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="SplashTheme" parent="SplashScreen_SplashTheme">
<item name="colorPrimaryDark">your_color</item>
</style>
</resources>
now you have change show method to include your custom style:
SplashScreen.show(this, R.style.SplashTheme);
you are donw now :)
If you wanna change the TaskBar color, just add the activity and the color that you desire in the output function.
fun setTaskBarColored(colored: Int,activity: Activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
activity.window!!.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
activity.window!!.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
activity.window!!.statusBarColor = ContextCompat.getColor(activity, colored)
}
}
And also the same applies for the NavigationBar
fun setNavigationBarColored(colored: Int, activity: Activity){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
activity.window!!.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
activity.window!!.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
activity.window!!.navigationBarColor = ContextCompat.getColor(activity, colored)
}
}
You can simply add this JSX code to the root of your app instead of editing any native files.
<StatusBar backgroundColor="blue" barStyle="light-content" />
Also in order to center your image, wrap it with a view and add the styling “{justifyContent: ‘center’, alignItems: ‘center’}”. If that doesn’t work then add this style directly to your image component “alignSelf: ‘center’”
I am trying to use a custom title to include an image button to the title bar.
I got a lot of help form this post: android: adding button to the title of the app?, but could not get it work for my ListActivity.
In a nutshell, following is what I have:
I hide the titlebar in the AndroidManifest.xml
The specify a relative layout for the custom title (workorder_list_titlebar.xml)
My Activity Class looks like the following:
public class WorkOrderListActivity extends ListActivity {
String[] orders={"WO-12022009", "WO-12302009","WO-02122010", "02152010"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.workorder_list_titlebar);
setContentView(R.layout.workorder_list);
setListAdapter(new ArrayAdapter(this,R.layout.workorder_list, R.id.label,orders));
}
}
When I ran the app, I got AndroidRuntimeException: You cannot combine custom titles with other title features.
Base on the stack trace, the exception was thrown by com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:183), that was triggered by setlistAdapter call.
Does anyone have the same problem with ListActivity?
Also once I manage to get this work, how do I attach listeners to the image button for it to do something?
Thanks in advance.
I had the same issue and I fix it deleting
<item name="android:windowNoTitle">true</item>
from my theme.xml
Make you create custom style in “values” folder. Make sure you code as below.
<style name="CustomTheme" parent="android:Theme">
Don't modify parent parameter.
This did work for me.
Instead of modifying your theme.xml you may also:
create a new XML style file my_theme.xml in values folder like this:
<style name="MyWindowTitleBackground">
<item name="android:background">#444444</item>
</style>
<style name="MyTheme" parent="android:Theme">
<item name="android:windowTitleBackgroundStyle">#style/MyWindowTitleBackground</item>
</style>
You may define other settings as you like in this theme.
Then just use this theme in your manifest within the activity's attributes
android:theme="#style/MyTheme"
Finally set your custom title as always in your activity.java:
final Window window = getWindow();
boolean useTitleFeature = false;
// If the window has a container, then we are not free
// to request window features.
if (window.getContainer() == null) {
useTitleFeature = window
.requestFeature(Window.FEATURE_CUSTOM_TITLE);
}
setContentView(R.layout.screen_main);
if (useTitleFeature) {
window.setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.custom_title);
// Set up the custom title
main_title = (TextView) findViewById(R.id.title_left_text);
main_title.setText(R.string.app_name);
main_title = (TextView) findViewById(R.id.title_right_text);
main_title.setText(R.string.Main_titleInfo);
}
Don't forget to define the custom_title.xml file in your layout folder. For example...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical" >
<TextView
android:id="#+id/title_left_text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:ellipsize="end"
android:singleLine="true" />
<TextView
android:id="#+id/title_right_text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:ellipsize="end"
android:singleLine="true"
android:textColor="#fff" />
</RelativeLayout>
I think notenking is right, that this is a problem in activities within tabs. Since some of my activities can either be stand-alone or within a tab, I've found the following helps:
final Window window = getWindow();
boolean useTitleFeature = false;
// If the window has a container, then we are not free
// to request window features.
if(window.getContainer() == null) {
useTitleFeature = window.requestFeature(Window.FEATURE_CUSTOM_TITLE);
}
setContentView(layoutId);
if (useTitleFeature) {
window.setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
}
May be you find this problem when use it in tab,for there already have a title and you can not add a custom title again.
you should add this custom title in the activity which you get the Tab
I did exactly as Sunny Dasari did but with one small change I put the # before and android in the parent attribute.
So my code looked like this.
<style name="CustomTheme" parent="#android:Theme">
To avoid crashing, you can simply add
android:theme="#style/android:Theme"
to the <Activity> tag in your AndroidManifest.xml:
<activity
android:name="test.TestActivity"
android:label="#string/app_name"
android:theme="#style/android:Theme">
This is because the styles defined in your default theme conflict with FEATURE_CUSTOM_TITLE (such as the attribute android:windowNoTitle). By using another theme, you can avoid such problems.
However, you might further need to define your own theme to change other attributes, such as android:windowTitleSize, background color, text color and font, etc. In this case, you can derive your theme from an existing theme (e.g., Theme.Light) and modify its attributes:
<resources>
<style name="CustomWindowTitleBackground">
<item name="android:background">#323331</item>
</style>
<style name="CustomTheme" parent="#style/android:Theme.Light">
<item name="android:windowNoTitle">false</item>
<item name="android:windowTitleSize">60dip</item>
<item name="android:windowTitleBackgroundStyle">#style/CustomWindowTitleBackground</item>
</style>
</resources>
Try swapping following lines:
setContentView(R.layout.workorder_list);
this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.workorder_list_titlebar);
I have run into this issue as well and it looks like it is an issue with what theme is applied to an activity in the AndroidManifest.xml file. If I use a theme like:
android:theme="#android:style/Theme.Holo
Then it will throw the error
android.util.AndroidRuntimeException: You cannot combine custom titles with other title features
However if I use a different theme like:
android:theme="#android:style/Theme.Black"
then it will not throw the error and subsequently will not crash. However I am trying to use a theme like Theme.Holo. I'm not sure if there is a way around this.
Since, I was trying to compile my program in android 4.0, I was facing the same problem. None of these solutions helped.So, I copied my style contents from values > styles.xml and pasted it in values-v11 styles.xml file and values-v14 styles.xml file. Bingo, the trick worked.
As a beginner most of the answers didn't help me for my case. So here is my answer.
Go to res/values folder in your android project and check for strings.xml (this file may vary in your case, something like themes.xml)
Inside the file under resource tag check whether you have style tags. If you don't find it, add the code below as mentioned below as a child to resources tag
something like below
<resources>
<style name="SomeNameHere">
<item name="android:windowNoTitle">true</item>
</style>
</resources>
if you already have style tag, just add the code below to your style tag
<item name="android:windowNoTitle">true</item>
I'm using a AppCompatPreferenceActivity that uses a PreferenceActivity simplified UI for phones (no fragments), but a two-pane UI with PreferenceFragment when it's a tablet.
Until now, I used only a light theme (my theme's parent is Theme.AppCompat.Light) and no problem, it looks like this when in two-pane mode:
I'm now implementing a dark theme, so this time, my theme's parent is "Theme.AppCompat".
And it looks like this:
As you can see, the title of the preference fragment is black on a dark grey background. How can I set this "title" color?
Note: on Pre-Lollipop, it was easy, we would just have to att that to the preference activity theme:
<item name="android:textColorSecondary">#AAAAAA</item>
... but it doesn't work anymore on Lollipop and Marshmallow.
Any idea?
By checking the PreferenceActivity code, I eventually found that the title on the right pane is not part of the preference fragment, but is the fragment "breadcrumb".
By searching for an answer with this keyword ("breadcrumb"), I saw that someone already asked the same thing, and the accepted answer is great and details all about how to do it, and why it's a little more complicated than just changing a color in a style.
Here's the question: Changing the highlight and title color for fragments in PreferenceActivity
And the direct link to the answer: https://stackoverflow.com/a/27078485/1534408
Did you try like this?
in styles.xml
<style name="PreferenceScreen" parent="YourApplicationThemeOrNone">
<item name="android:textColor">#color/TitleColor</item>
</style>
in Manifest
<activity
android:name="MyPreferenceActivity"
...
android:theme="#style/PreferenceScreen" >
</activity>
I changed it by overriding Category theme.
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
//other stuff
<item name="preferenceTheme">#style/MyPrefTheme</item>
</style>
<style name="MyPrefTheme" parent="#style/PreferenceThemeOverlay">
<item name="preferenceCategoryStyle">#style/CategoryStyle</item>
<item name="android:preferenceCategoryStyle">#style/CategoryStyle</item>
</style>
<style name="CategoryStyle" parent="Preference.Category">
<item name="android:layout">#layout/category_view</item>
</style>
layout/category_view.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="#android:id/title"
style="?android:attr/listSeparatorTextViewStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:textColor="#color/white"
android:layout_height="wrap_content"
/>
to know more visit Preference style.xml
I'm in the process of upgrading my app to Android 5.0 Lollipop design based on AppCompat v21.
Everything works fairly well but I hit a problem with the preferences screen where checkboxes are not visible. They are on the screen and I can see them when the whole section gets highlighted when it's touched. Functionality is fine and preferences are updated successfully.
Everywhere else in the app checkboxes display without any problems.
Also, it works without a problem on Android 2.3.
Checkboxes missing in the 2nd and 3rd setting:
Checkbox visible when touching the row:
Preferences and the code for them is very basic and has nothing custom added.
My xml file with preferences looks like this (only checkboxes copied):
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="show_romaji"
android:title="#string/preferences_romaji_title"
android:summary="#string/preferences_romaji_explanation"
android:defaultValue="true"
android:persistent="true" />
<CheckBoxPreference
android:key="send_statistics"
android:title="#string/preferences_statistics_title"
android:summary="#string/preferences_statistics_explanation"
android:defaultValue="true"
android:persistent="true" />
</PreferenceScreen>
Preferences activity:
public class PreferenceActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SharedPreferenceFragment())
.commit();
}
}
Preferences fragment:
public class SharedPreferenceFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
I'm using a theme which has almost nothing defined as well:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Theme.JlptVocabulary" parent="Theme.AppCompat">
<!-- Set AppCompat’s color theming attrs -->
<item name="colorPrimary">#ffe91e63</item>
<item name="colorPrimaryDark">#color/black</item>
<!-- The rest of your attributes -->
<item name="android:windowBackground">#color/backgroundBlack</item>
</style>
</resources>
I'm stuck with this problem and haven't been able to find a solution after doing some research and experiments. Anyone has a clue?
The checkboxes in preferences for Theme.AppCompat style are black (box outline and fill except for the check mark). The check mark takes color of preferences background. In your case, you are setting black background, so checkboxes do not show since they are now all black. I suggest using a separate style (say: Theme.Pref_JlptVocabulary) for the preferences with its normal grayish background so checkboxes appear.
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Theme.Pref_JlptVocabulary" parent="Theme.AppCompat">
</style>
</resources>
The other way would be to customize the checkboxes background and check mark colors by redefining the style for checkbox views how-to-customize-the-color-of-the-checkmark-color-in-android....
You can also try using one of the ThemeOverlays
<style name="SettingsTheme" parent="ThemeOverlay.AppCompat.Dark">
<item name="android:windowBackground">#color/backgroundBlack</item>
</style>
I found that this gave suitable checkboxes on a black background. However, on Lollipop devices (values-v21) I still use the regular theme extending Theme.Appcompat since ThemeOverlay seemed to override my 'colorAccent' values on Lollipop phones
I found another solution for the issue.
This may cause because if we set colorControlNormal as same as the application background color in MyMaterialTheme.Base theme in style.xml file.
Solution: Change colorControlNormal other than application default background color.
I have listpreference in my app , i want to customize it , i can set theme to prefs activity as bellow :
<activity
android:name=".Prefs"
android:label="#string/app_name"
android:theme="#style/PreferencesTheme">
preference style:
<?xml version="1.0" encoding="utf-8" ?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="PreferencesTheme" parent="android:Theme">
<item name="android:background">#FFDAB9</item>
<item name="android:textColor">#color/red_color</item>
<item name="android:textSize">20sp</item>
</style>
</resources>
The above style changes the ListPreference background and ListPreference title text size and text color BUT it doesn't change ListPreference summary text color or size any way to do that ,
i tried this code but it doesn't work:
<item name="android:textColorSecondary">#color/red_color</item>
any help will be appreciated, thanks
Maybe it's too late to answer, but try to remove your line of color:
<item name="android:textColor">#color/red_color</item>
And add this which are for the primary, secondary and tertiary colors
<item name="android:textColorPrimary">#color/red_color</item>
<item name="android:textColorSecondary">#color/red_color</item>
<item name="android:textColorTertiary">#color/red_color</item>
You can choose the color you want to all those, honestly, I never saw the tertiary color in any preference page, but the primary is for the main and big text, the secondary is for the small text appears below the primary.
Hope it helps.
That sucks because the xml file says it uses textColorSecondary for the summary color.
What I would recommend you do is copy and save the contents of the file above into your own project, edit that file to look the way you want it to (Remember not to change any ids), and display a custom list preference item layout.
You can apply a custom preference layout in xml like so:
<ListPreference
android:key="pref_key"
android:title="#string/my_list_preference"
android:layout="#layout/custom_list_preference" />
I put this line into styles.xml and it works:
<item name="android:textColorSecondary">#00CC00</item>