ListView divider not applied when specified in style declaration - android

This is more "Why?" than "How?" question, since I know a simple workaround for the problem. But I'd like to know what logic is behind behavior I'm going to describe.
I've got a style declaraion and some ListViews. I want to change the way divider looks. I used following declaration:
<style name="Theme.MyApp.Dark" parent="#style/Theme.Sherlock">
<item name="android:dividerHeight">4px</item>
<item name="android:divider">#123456</item>
</style>
To my amusement, only the height of the divider was applied. I've tried specifying color with alpha value (#12345678) and gradient drawable, nothing works. ListViews are declared like this:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
So, as you can see there's nothing that could override style declaration. I can change the divider only when I specify it directly in ListView declaration:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list"
android:divider="#123456"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
Using my newly-acquired skill of specifying colors depending on theme selected, I can work around this very easily - I will just have to declare android:divider with custom color attribute for every list.
But why I can't change android:divider in the style, just like the android:dividerHeight? Is there some reason for that? I couldn't find an explanation, bug report or something similar that would let me understand this behavior.
EDIT: The theme is applied globally, in the Manifest file:
<application android:icon="#drawable/icon" android:label="#string/app_name" android:name="org.my.app" android:theme="#style/Theme.MyApp.Dark">

It can be set from a theme but with a little trick:
<style name="MyTheme" parent="AppTheme">
<item name="android:listViewStyle">#style/MyListViewStyle</item>
</style>
<style name="MyListViewStyle" parent="#android:style/Widget.ListView">
<item name="android:divider">#F00</item>
<item name="android:dividerHeight">1px</item>
</style>
Rgrds ;)

You have to mention the style in the Lisview like this
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list"
style = "#style/Theme.MyApp.Dark"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
Otherwise this wont work..

Related

Adding a style for ConstraintLayout

I want to make a style for ConstraintLayout but it seem nothing happened.
This is my style :
<style name="ConstraintLayoutFragment">
<item name="android:layout_marginStart">#dimen/margin_border_screen</item>
<item name="android:layout_marginEnd">#dimen/margin_border_screen</item>
</style>
This is how I applied the style :
<androidx.constraintlayout.widget.ConstraintLayout
style="ConstraintLayoutFragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
Maybe I need to put a parent in the style but I don't found anything for ConstraintLayout.
Oh! I just forget to put a #style/ :
style="#style/ConstraintLayoutFragment"

Android PreferenceFragment title text color

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

How to layout partial overlay ActionBar

I have a problem to implement this situation, Seen here that natively has let the entire layout as belowlayout below.
But I would like to do this, this rectangle Should be a single and clickable item
How to do this ?
If you need to display something over the main content aPopupWindow will be the best choice.
First, you need to create a custom theme that extends an existing action bar theme and set the android:windowActionBarOverlay property to true.
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#android:style/Theme.Holo">
<item name="android:windowActionBarOverlay">true</item>
</style>
</resources>
And add the following in your layout:
android:paddingTop="?android:attr/actionBarSize"
Example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?android:attr/actionBarSize"> // <==== add this one
...
</RelativeLayout>
Read more about this.

Android - use attributes to tweak custom styles

here's my issue. I have defined custom themes and styles, so as to customize various Views, in the relevant .xml files. Here are some code extracts:
themes.xml:
...
<style name="Legacy" parent="android:Theme.NoTitleBar">
<item name="android:buttonStyle">#style/Legacy.Button</item>
...
</style>
styles.xml:
...
<style name="Legacy.Button" parent="#android:style/Widget.Button">
<item name="android:textColor">#ffffff</item>
<item name="android:background">#drawable/button_selector_blue</item>
<item name="android:textSize">15dp</item>
</style>
Let's say I set my application's theme to Legacy. If I use a Button in a layout, it will get my custom default parameters (white text, background is #drawable/button_selector_blue, etc).
Now let's say I want to keep those parameters save for the text size: I'd like to have some buttons with a larger text size, which would be defined in an titleSize attribute in attrs.xml:
...
<attr name="titleSize" format="reference|dimension" />
and which value is set for each theme in my themes.xml file.
So my layout would contain something like:
<Button
android:id="#+idmyButtonId"
android:drawableRight="#drawable/aDrawable"
android:text="#string/someText"
android:textSize="?titleSize"
android:layout_width="fill_parent" android:layout_height="wrap_content">
</Button>
When launching my app I get the following error:
java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x2
So it seems I cannot tweak custom styles using attributes - at least not this way. Is such a thing possible ? If not, what would you use to achieve such a result ?
I'd like to give the user the ability to select among different themes, so I can't just define an additionnal ButtonWithLargeText style and directly use it in my layout.
Thanks for your help !
I finally got it to work. Instead of defining my titles' size in attrs.xml, I used dimens.xml. So now the following works:
<Button
android:id="#+idmyButtonId"
android:drawableRight="#drawable/aDrawable"
android:text="#string/someText"
android:textSize="#dimen/titleSize"
android:layout_width="fill_parent" android:layout_height="wrap_content">
</Button>
While I get my regular text size (which I defined in my styles.xml) on the Button by using this:
<Button
android:id="#+id/idRegularButton"
android:text="#string/regularSizeText"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</Button>

applying themes to my android app dynamically

I have three buttons to change themes. On clicking each button my app theme must change dynamically. How to do it programmatically.
To set the theme dynamically at runtime, call setTheme() in your activity's onCreate() method, before calling setContentView(). To change the theme, you simply need to restart your activity.
Here is a nice tutorial on how dynamically apply themes.
and this one too.
Please Visit this link for it.
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#00FF00"
android:typeface="monospace"
android:text="#string/hello" />
<TextView
style="#style/CodeFont"
android:text="#string/hello" />
By defining an xml theme file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont" parent="#android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
</style>
</resources>
You can also apply styles to all activities of an application:
<application android:theme="#style/CustomTheme">
Or just one activity:
<activity android:theme="#android:style/Theme.Dialog">
Sorry but you can't change styles programmatically.
how to set the Style Attribute Programmatically in Android?
There are certainly other methods to achieve this desired behavior however. You could set onclick listeners for each button, and programmatically change text size, color, background etc of your various view elements
you can use a particular theme for a given xml file.
in graphical layout you can CHANGE the theme of the layout using editing config.
use onclick event to go to next layout and here your theme will be different from the first one.
Vimalatha, to change your background when you click a button just add this code to the onClick function of your button.
myLinearLayout.setBackgroundColor(Color.BLUE);
Assuming that myLinearLayout is your LinearLayout name...

Categories

Resources