Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying style resource programmatically

Tags:

android

I didn't find a way to do that programmatically so I am posting this question here (also i didn't find any question related to this one).

I have a resource style, defined in the res/values/styles.xml. What I am trying to do is apply this style inside my activity using java to a View object that i am manipulating.

Is it possible to achieve this in Android or the style can only be applied to an object using the android:style attribute?

like image 909
Thiago Avatar asked Sep 21 '11 12:09

Thiago


2 Answers

Shared this answer here, but since this has it's own conversational thread, I feel like its relevant here as well.

There is no one line solution to this problem, but this worked for my use case. The problem is, the 'View(context, attrs, defStyle)' constructor does not refer to an actual style, it wants an attribute. So, we will:

  1. Define an attribute
  2. Create a style that you want to use
  3. Apply a style for that attribute on our theme
  4. Create new instances of our view with that attribute

In 'res/values/attrs.xml', define a new attribute:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="customTextViewStyle" format="reference"/>
    ...
</resources>    

In res/values/styles.xml' I'm going to create the style I want to use on my custom TextView

<style name="CustomTextView">
    <item name="android:textSize">18sp</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:paddingLeft">14dp</item>
</style>

In 'res/values/themes.xml' or 'res/values/styles.xml', modify the theme for your application / activity and add the following style:

<resources>
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <item name="@attr/customTextViewStyle">@style/CustomTextView</item>
    </style>
    ... 
</resources>

Finally, in your custom TextView, you can now use the constructor with the attribute and it will receive your style

public class CustomTextView extends TextView {

    public CustomTextView(Context context) {
       super(context, null, R.attr.customTextView);
    }
}

It's worth noting that I repeatedly used customTextView in different variants and different places, but it is in no way required that the name of the view match the style or the attribute or anything. Also, this technique should work with any custom view, not just TextViews.

like image 105
Kevin Grant Avatar answered Oct 07 '22 05:10

Kevin Grant


No, it isn't possible to generically apply a style resources to an existing View instance. Style resources can only be applied to Views during construction time.

To understand why, study the View(Context context, AttributeSet attrs, int defStyle) constructor. This is the only place where central View attributes (like android:background) are read, so there is no way to apply a style after the View has been constructed. The same pattern is used for sub classes of View, like TextView. You will need to apply the style attributes manually using setters.

Note that if you progamatically instantiate the View, you can use any style resource through the defStyle constructor parameter.

like image 25
Martin Nordholts Avatar answered Oct 07 '22 06:10

Martin Nordholts