Android PreferenceFragment title text color - android

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

Related

Changing the Color of the the Menu Items does not work

I have created a menu and now I want to change the text color of the individual menu titles.
To do this, I first created a style that should contain the corresponding attribute and then called this style in my Activity_home_drawer.xml. However, this only changes the menu TextColor when the corresponding menu title is clicked but is not permanent as I would like it to be.
What do I have to do so that the text color in my menu changes permanently to white and not Black anymore? Is my way of doing it the right way or is there a more elegant way to change the TextColor?
Thanks for any help!
Part of my Activity_home_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view"
android:theme="#style/TextAppearance44">
<group android:id="#+id/category_group">
<item
android:id="#+id/nav_menu"
android:icon="#drawable/ic_store_black_24dp"
android:title="#string/menu_menu"
android:theme="#style/TextAppearance44"
/>
</group>
My TextAppearance44 style
<style name="TextAppearance44">
<item name="android:textColor">#color/colorWhite</item>
<item name="android:actionMenuTextColor">#color/colorWhite</item>
<item name="android:textSize">16sp</item>
<item name="android:titleTextColor">#color/colorWhite</item>
</style>
You need to change your ToolBar style. Check these two tutorials:
Toolbar assigning to activity and styling:
https://android-developers.googleblog.com/2014/10/appcompat-v21-material-design-for-pre.html?m=1
Changing style and items colors in the Toolbar:
https://www.murrayc.com/permalink/2014/10/28/android-changing-the-toolbars-text-color-and-overflow-icon-color/
In your case, you are looking mainly for (code fragment from second tutorial):
<!-- android:actionMenuTextColor is the color of the text of
action (menu) items in the Toolbar, at least in the
Theme.AppCompat theme.
For some reason, they already get the textColorPrimary
when running on API 21, but not on older versions of
Android, so this is only necessary to support older
Android versions.-->
<item name="actionMenuTextColor">#color/abc_primary_text_material_light</item>
I solved it by myself; I added these to lines to my NavigationView in my ActivityHome (where the Menu comes up).
app:itemTextColor="#color/colorWhite"
app:itemIconTint="#color/colorWhite"
Simple mistake, didnot know that there was such a attribute.

How to set PreferenceFragmentCompat accentColor?

I'd like to override the accentColor used by v14 PreferenceFragmentCompat.
I'm using a pink accent color for the outer frame of my Android app. This creates problems in many situations as it causes standard controls to use an accent color that's close enough to red that the effect is disturbing. Be that as it may, I like the effect of having a pink FAB and button controls on the frame.
For child activities, I use a them with the standard teal accent color. However, I have a PreferenceFragment compat in a drawer on the main activity, and I cannot figure out how to override the main activity's accent color.
Things I have tried (none of which work):
Setting a theme on the frame of the fragment that receives the PreferenceFragmentCompat (doesn't work):
<FrameLayout android:id="#+id/preferenceFragmentFrame"
android:layout_width="match_parent" android:layout_height="0dp"
android:layout_weight="1"
android:theme="#style/AppTheme.TealAccentColor"
/>
where the AppTheme.TealAccentColor style provides an explicit teal acccentColor.
Setting the accentColor in a preference theme (doesn't work):
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
...
<item name="preferenceTheme">#style/MyPreferenceThemeOverlay</item>
</style>
<style name=MyPreferenceThemeOverlay parent="PreferenceThemeOverlay.v14.Material>
<item name="colorAccent">#color/colorAccentTeal</item>
</style>
Adding an accent color to preference-v14's PreferenceThemeOverlay (doens't work):
<!-- use the library's theme-->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
...
<item name="preferenceTheme">PreferenceThemeOverlay.v14.Material</item>
</style>
<!-- but add an accentColor item to the library's theme -->
<style name="PreferenceThemeOverlay.v14">
<item name="colorAccent">#color/colorAccentTeal</item>
</style>
No matter what I do, PreferenceFragmentCompat seems to take the pink accent color from the Activity's theme instead.
I'm sure it has something to do with a disconnect between the Activity's theme and a Fragment's theme. But there's no xml element for the fragment, since PreferenceFragmentCompat provides its own layout.
Maybe there's a way to do it programmatically with an override in the class that extends PreferenceFragmentCompat, but if there is, I can't imagine what it would be. Most of the attack points I can think of either have access to the internally-created layout, or have access to the layout after it has been created, which is too late.
A picture might help:
Have you tried overriding android:colorAccent instead of colorAccent?
Use the method you described as "Setting the accentColor in a preference theme", just change the attribute name.
The preference support library doesn't take into consideration appcompat at all, so
forget about appcompat attributes
color* attributes will only work on API 21, I think
If you want consistent behavior you can use my library Android Support Preference which aims to connect preference and appcompat support libraries.
Then you original style with colorAccent (unprefixed) will work as expected.

App crash when android:theme is aplied to a specific button

i am doing a simple calculator aplication for android (my first app) and i have problems that are breaking my head when i try to apply a specific android:theme to a button.
The problem comes out when the button with the specific theme try to execute an activity method in onclick event. Based on what I 've searched in StackOverflow, is like the "context" of the button with the specific theme is different from the activity context, and because of that it cant find my method that handles the onclick written in the activity.
There is my style.xml where i define my aplication theme and my specific button theme:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorButtonNormal">#dc000000</item>
<item name="android:background">#dc262626</item>
<!-- Customize your theme here. -->
</style>
<style name="contextButtonTheme" parent="AppTheme">
<item name="colorButtonNormal">#color/contextButtonsColor</item>
</style>
</resources>
There is my button in the layout xml:
<Button
android:layout_width="0dp"
android:layout_weight="25"
android:layout_height="match_parent"
android:text="X"
android:id="#+id/multButton"
android:textSize="11pt"
android:theme="#style/contextButtonTheme"
android:onClick="onClickButton"/>
The solution i read here is that change "android:theme" with "style", although this solves the crash, the colorButtonNormal new color is dont applied :(.
Pls help me D:
PD: Sorry for my bad english
I've answered a similar question here, where you can get a bit more background.
A possible solution to your problem is to not use android:onClick="onClickButton" but set an onClickListener in code. This way you can keep your themed Button. The official docs have an example how to do this.

TextView style in dialog, can not see text in froyo but can see in ics

I have a dialog box that I create to display messages in android. It basically contains a text view in a scrollview like this
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/about_msg"
android:text="#string/about_msg"
android:autoLink="web"
android:padding="10dip"
style="#style/DialogTextSmall" />
</ScrollView>
As you can see I have applied a style to TextView the style looks like this
<style name="DialogTextSmall">
<item name="android:textSize">#dimen/text_size_small</item>
</style>
The application theme set is like this
<style name="AppTheme" parent="android:Theme.Light">
The Problem:
On ICS api-15 it shows fine black text on white background of TextView.
The problem is When I show dialogbox in Froyo its the text does'nt seem to show even though it seems to have taken space - My guess is the color of text is same as background (greyish black)
I know I can quick fix by hard-coding black background and white text, but Is it not possible to have the default colors of platform for the text color and background of the TextView to appear, without me having to hardcode them ?
You can inherit a parent style and then only change the values you want to change. Try changing your XML to this:
<style name="DialogTextSmall" parent="#android:style/Widget.TextView>
<item name="android:textSize">#dimen/text_size_small</item>
</style>
The list of styles you can inherit can be found in the AOSP source on Github here.
EIDT:
By default text views have black text and transparent background, so you will need to set one or the other if the background behind the text view (which, again, is transparent) is black.
Inheritance from Textview style did not really help. It is a little quirky problem and here is one way to do it
http://blog.andromo.com/2011/fixing-text-colours-on-an-alertdialog-when-using-theme-light/
In my case I did it another way
Solved it for theme I inherited it from default android theme
<style name="Theme" parent="android:Theme"></style>
<style name="Theme.AppTheme" parent="#android:style/Theme.Light"/>
and
<style name="DialogTextSmall">
<item name="android:textSize">#dimen/text_size_small</item>
<item name="android:textColor">#android:color/white</item>
</style>
This way for all platforms , froyo, gingerbread and above, the dialog boxes are black and text is white on them

How can i be generic my title bar?

i have a title bar one of my project and i want to use my all screen.I think that i can do be a general( means i want a something and i will call everywhere like a style tag or theme tag) i have no idea.What can i use for?
My title bar is here:
RelativeLayout
android:layout_height="44dip"
android:layout_width="320dip"
android:background="#drawable/merhaba_header_320_44dip">
<ImageView
android:layout_height="32dip"
android:layout_width="121dip"
android:background="#drawable/merhaba_logo_121_32dip"
android:layout_centerInParent="true">
</ImageView>
<ImageView
android:layout_height="30dip"
android:layout_width="64dip"
android:background="#drawable/merhaba_btn_nedir_64_30dip"
android:layout_margin="5dip"
android:layout_alignParentRight="true">
</ImageView>
</RelativeLayout>
Hi DuyguK are you asking for a way to have a title bar that you can apply as a theme or something to all the activities in your app?
If that's so, I would recommend you to do the following.
First define a style in your res/values/styles.xml
<resources>
<style name="MyTheme" parent="android:style/Theme.Light">
<item name="android:windowTitleSize">44dip</item>
//whatever item you want/need to edit for your custom title bar
</style>
</resources>
This allows you to have a theme that is applyable to your whole app. To do this you need to go to your AndroidManifest.xml file an inside the application tag add the following:
<application android:label="#string/app_name"
android:theme="#style/MyTheme">
The android:theme tells the app to use the theme named "MyTheme" that can be found in the res/values/styles.xml file in your project.
That way you can apply correctly your custom title bar to all your activities.
Hope this answers your question if not, please specify!
An additional recommendation is to take a look at the ActionBarCompat project that comes with the samples in Android SDKs. The Android Developers page shows the project:
http://developer.android.com/resources/samples/ActionBarCompat/index.html
It has the advantage of being compatible with pre-API11 devices, is applied as a theme to your app and it is being used by lots of apps available in the store.
If you have any questions/trouble with it please tell me.
create your own style which can ofcourse extend from existing from an existing theme.
change the windowNoTitle to true.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="noTitleBarStyle" parent="android:Theme.Black">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#color/darkGrey</item>
<item name="android:colorForeground">#ff3333</item>
</style></resources>

Categories

Resources