Android private styles using - android

Trying to inherit Widget.TextView.ListSeparator style, but now aapt doesn´t allow to do this:
No resource found that matches the given name
'Widget.TextView.ListSeparator
because google made it private. But how can I combine two styles : ListSeparator and margins?
Style 1
<style name="settings_plain_text">
<item name="android:layout_marginTop"> 10sp </item>
<item name="android:layout_marginBottom"> 10sp </item>
<item name="android:textSize"> 18sp </item>
Style 2
style="?android:attr/listSeparatorTextViewStyle"

I copy the answer from this link:
Hello all. I did some investigating with the frameworks team who's in charge of aapt.
What is happening is that some styles, like WindowTitle are not public (you won't find them in android.R.style). You should not be extending non public resources. aapt used to let you do that but it was a bug which was fixed in platform-tools r6.
The issue is that once compiled, resources are assigned an integer. In this case your custom style is assigned an integer and its parent is referenced through the parent integer.
For the framework, only public resources are guaranteed to only have the same integer, build after build. The integer of private resources integer will change from build to build.
This means that your custom style is referencing a parent that will not be valid once installed on a device. It'll referenced either another resources or none at all, and it won't do what you want.
If you wish to reuse a style that is private, you should copy the content of that style into your own instead of extending it.
The style I have found googling is that one:
<style name="Widget.TextView.ListSeparator">
<item name="android:background">#android:drawable/dark_header_dither</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">?textColorSecondary</item>
<item name="android:textSize">14sp</item>
<item name="android:gravity">center_vertical</item>
<item name="android:paddingStart">8dip</item>
</style>
From that you can modify margins.

Related

ListPreference text color

I'm having a hard time trying to style a ListPreference.
I've applied a main theme which declares a preferenceTheme and both of them link to a dialogTheme (and alertDialogTheme respectively). It works except that the text color of the items doesn't change - but the color of all other texts does. I cannot rely on a workaround because I'm using the v7 preferences and thus cannot override the dialog methods in a custom class.
For me it looks like the rows ignore the text color value, but maybe someone else has a solution for this. Otherwise this might be a bug?
Main style:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- [...] -->
<!-- Some color values -->
<item name="android:dialogTheme">#style/DialogTheme</item>
<item name="android:alertDialogTheme">#style/DialogTheme</item>
<item name="dialogTheme">#style/DialogTheme</item>
<item name="alertDialogTheme">#style/DialogTheme</item>
<item name="preferenceTheme">#style/PreferenceTheme</item>
</style>
PreferenceTheme:
<style name="PreferenceTheme" parent="PreferenceThemeOverlay.v14.Material">
<!-- [...] -->
<!-- Some color values -->
<item name="android:textColor">#color/preference_primary_color</item>
<item name="android:textColorPrimary">#color/preference_primary_color</item>
<item name="android:textColorSecondary">#color/preference_primary_color</item>
<item name="android:textColorHighlight">#color/preference_primary_color</item>
<item name="android:editTextColor">#color/preference_primary_color</item>
<item name="android:dialogTheme">#style/DialogTheme</item>
<item name="android:alertDialogTheme">#style/DialogTheme</item>
<item name="preferenceTheme">#style/PreferenceTheme</item>
</style>
DialogTheme:
<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:textColor">#EEEEEE</item>
<item name="android:textColorPrimary">#EEEEEE</item>
<item name="android:textColorSecondary">#EEEEEE</item>
<item name="android:textColorHighlight">#EEEEEE</item>
<item name="android:textColorTertiary">#EEEEEE</item>
<item name="android:textColorAlertDialogListItem">#EEEEEE</item>
<item name="android:editTextColor">#EEEEEE</item>
<item name="color">#EEEEEE</item>
</style>
This is how it looks.The text should be #EEEEEE. I've snipped it but the text colors are applied in each of the given styles.
You did everything right, except one thing: do not use the android prefix when overriding textColorAlertDialogListItem because this is not the framework version of AlertDialog.
This statement is generally true for almost all attributes that belong to the support widgets / views. The reason is pretty straightforward: not all attributes are available on the older platforms. Such example is android:colorControlActivated which was introduced in API 21. The AppCompat lib declares its own colorControlActivated so it's available on older API levels, too. In this case the developer should not use the android prefix when defining the style in the theme as that would point to the platform version of the attribute instead of the AppCompat one.
TL;DR: Do not use the android prefix for support widgets unless you have to (i.e. you get compilation error).
P.S.: I have created a fix / extension to the support preferences-v7 lib's annoying things that you might want to check out.
Also for radio buttons color add <item name="colorAccent">#000000</item> to your style.
Be careful, it's not android:colorAccent but colorAccent

Styling CardView groups

For a small tool, I have to write an Android app. There are no requirements on portability, it's sufficient when the app only runs on android version 6 or later.
I would love to group dialog elements into CardViews and I would love to have some reasonable layout (spacing, colors, etc). Is there a way to use a theme, standard layout, style, etc. that I could use without the need to apply "android:padding", "card_view:cardElevation", ect. attribute to every CardView?
If it's not possible to use some already existing defaults, I could use styles. When I use styles (following Google's "Styles and Themes" API Guide), I get error messages, when I move some attributes from the CardView definition from the layout xml to the style xml. For attributes that I move to the style xml, that begin with "android:", there is no error. For other attributes, I get an `No resource found that matches the given name-error.
<style name="CardGroups">
<item name="xmlns:card_view">"http://schemas.android.com/apk/res-auto"</item>
<item name="card_view:cardElevation">3dp</item>
<item name="android:layout_margin">3dp</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>
results in: Error:(20, 5) No resource found that matches the given name: attr 'card_view:cardElevation'.
You are correct that styles are the way to go to provide a common set of attributes to multiple Views.
You should not use custom namespaces in your style definitions. For custom attributes provided by libraries (such as cardElevation), you simply do not provide a namespace.
Thus your style should look like this:
<style name="CardGroups">
<item name="cardElevation">3dp</item>
<item name="android:layout_margin">3dp</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>

Why can't I use #android:style/DialogWindowTitle

I needed to change title size and simple solution was to create a new style, setting parent to #android:style/DialogWindowTitle and then modifiy it. However, Eclipse keep showing an error:
no resource found
I checked in SDK styles.xml, it exists there in every version.
Why can't I override it? I want to use system default for all parameters, and just change the size.
I also got an error while trying to do this:
<style name="CenterJustifyDialogTitle" parent="#android:style/DialogWindowTitle" >
<item name="android:gravity">center|center_vertical</item>
<item name="android:textColor">#000000</item>
</style>
The error dissapeared when I changed this to:
<style name="CenterJustifyDialogTitle" parent="#android:style/TextAppearance.DialogWindowTitle" >
<item name="android:gravity">center|center_vertical</item>
<item name="android:textColor">#000000</item>
</style>
Hope it helps to anyone interested.

Change the line color used in listSeparatorTextViewStyle

I have the following code
<TextView
android:text="#string/hello"
style="?android:attr/listSeparatorTextViewStyle" />
and I will get the following effect.
However, I am not happy with the color line. I would like to have something like
I would like it to have blue color line as in holo. I try the following custom style.
<style name="MyOwnListSeperatorTextViewStyle">
<item name="android:background">#android:drawable/list_section_divider_holo_light</item>
<item name="android:textAllCaps">true</item>
<!-- Copy from Widget.TextView.ListSeparator -->
<item name="android:background">#android:drawable/dark_header_dither</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">?textColorSecondary</item>
<item name="android:textSize">14sp</item>
<item name="android:gravity">center_vertical</item>
<item name="android:paddingLeft">8dip</item>
</style>
But it won't work, as I get the following error.
error: Error: Resource is not public. (at 'android:background' with value '#android:drawable/dark_header_dither').
Have idea how can I change the line color used in listSeparatorTextViewStyle?
I needed to do this to override the typical Holo Spinner style (I didn't want the underlined item - i just wanted the arrow), and I think this can be overridden in precisely the same manner:
First off, you want to find the item you wish to override in the android styles source. There is an incredibly useful SO answer that contains all of the styles (and the names to override them) right here: Set Dialog theme to parent Theme in Android
I believe yours is the following line:
<item name="listSeparatorTextViewStyle">#android:style/Widget.Holo.Light.TextView.ListSeparator</item>
This takes us on a journey to find the style Widget.Holo.Light.TextView.ListSeparator which should live somewhere on your very own computer! But I'll make it easy and just c&p it:
<style name="Widget.Holo.Light.TextView.ListSeparator" parent="Widget.TextView.ListSeparator">
<item name="android:background">#android:drawable/list_section_divider_holo_light</item>
</style>
Now, you probably want to leave well enough alone, and just look at that background drawable. You will find it is a grey 9patch file that looks like the sinister grey line you seek to avoid.
We need to override this. I am sure there are a number of ways to do this, but I do so by customizing the theme of the application. Here is the themes.xml file:
<style name="AppTheme" parent="#android:style/Theme.Holo.Light.NoActionBar">
<item name="android:listSeparatorTextViewStyle">#style/MyOwnListSeperatorTextViewStyle</item>
</style>
<style name="MyOwnListSeperatorTextViewStyle" parent="Widget.TextView.ListSeparator">
<item name="android:background">#drawable/make_your_own_blue_9_patch_here</item>
</style>
Notice how we used the listSeparatorTextViewStyle from that previous SO post? And the parent of the custom style is the Widget.TextView.ListSeparator from android's style source? All very important.
Now you just need to apply this theme to your app, but I am assuming you have a theme already. If you haven't already, you will need to make your own 9patch but I would just look at the list_section_divider_holo_light.9.png file on your computer, and make the grey parts blue, and then make a copy and place it into your own drawables folder.
Hope this works and is helpful!

Want to overload Button style

I want to overload how an android Button looks, except I want to overload only one attribute i.e. the android:background. I know I could write something like:
<style name="App_TextButtonStyle" parent="???">
<item name="android:background">#drawable/filled_roundededges_nostroke</item>
</style>
Where parent="???" specifies which style I inherit from. My question is which style should I inherit from do that I get everything from the android default style for buttons and just define a new background.
I have used style for buttons without specifying "parent" attribute
<style name="BigButtonStyle">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_gravity">center_horizontal</item>
<item name="android:background">#drawable/backbutton</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">8pt</item>
<item name="android:textStyle">bold</item>
<item name="android:layout_margin">10pt</item>
</style>
I think it could be enough for you to define your style without "parent".
This is mostly for future visitors, I found a solution to the problem:
<style name="App_TextButtonStyle" parent="#android:style/Widget.Button">
<item name="android:background">#drawable/filled_roundededges_nostroke</item>
</style>
#android:style/Widget.Button references the default style of the button in the currently selected theme. You could also use the holo widget button style #android:style/Widget.Holo.Button (available since api 11).
You might want to look in <android-sdk-install-folder>/platforms/android-XX/data/res/values for the files styles.xml and themes.xml to check out all styles android uses for a given platform version.

Categories

Resources