How to Retrieve Style Attributes Programmatically from Styles.Xml

How to retrieve style attributes programmatically from styles.xml

It is possible to retrieve custom styles from styles.xml programmatically.

Define some arbitrary style in styles.xml:

<style name="MyCustomStyle">
<item name="android:textColor">#efefef</item>
<item name="android:background">#ffffff</item>
<item name="android:text">This is my text</item>
</style>

Now, retrieve the styles like this

// The attributes you want retrieved
int[] attrs = {android.R.attr.textColor, android.R.attr.background, android.R.attr.text};

// Parse MyCustomStyle, using Context.obtainStyledAttributes()
TypedArray ta = obtainStyledAttributes(R.style.MyCustomStyle, attrs);

// Fetch the text from your style like this.
String text = ta.getString(2);

// Fetching the colors defined in your style
int textColor = ta.getColor(0, Color.BLACK);
int backgroundColor = ta.getColor(1, Color.BLACK);

// Do some logging to see if we have retrieved correct values
Log.i("Retrieved text:", text);
Log.i("Retrieved textColor as hex:", Integer.toHexString(textColor));
Log.i("Retrieved background as hex:", Integer.toHexString(backgroundColor));

// OH, and don't forget to recycle the TypedArray
ta.recycle()

Android: Get textColor attribute from a certain style defined in styles.xml

Sorry if I'm a bit late with an answer but I tried your code with a bit of modification, I changed the style to this:

<style name="NotificationTitle" parent="TextAppearance.AppCompat.Notification.Title"/>
<style name="NotificationText" parent="TextAppearance.AppCompat.Notification.Line2"/>

and everything worked well with the rest of your code (and didn't need the v21 for the styles now that I used AppCompat)

I hope this helps.

How to programmatically set style attribute in a view

Generally you can't change styles programmatically; you can set the look of a screen, or part of a layout, or individual button in your XML layout using themes or styles. Themes can, however, be applied programmatically.

There is also such a thing as a StateListDrawable which lets you define different drawables for each state the your Button can be in, whether focused, selected, pressed, disabled and so on.

For example, to get your button to change colour when it's pressed, you could define an XML file called res/drawable/my_button.xml directory like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@drawable/btn_pressed" />
<item
android:state_pressed="false"
android:drawable="@drawable/btn_normal" />
</selector>

You can then apply this selector to a Button by setting the property android:background="@drawable/my_button".

Is it possible to reference attributes from styles.xml file?

Yes, it is definitely possible to add custom attributes and colors to the themes. For this you need to:

  1. Define your custom attribute in your res/values/attrs.xml file:

    <resources>
    <attr name="customColor" format="color" />
    </resources>
  2. Define the attribute's value in your themes:

    <style name="AppTheme" parent="Theme.AppCompat">
    <item name="customColor">#111111</item>
    </style>

    <style name="AppTheme.AnotherColor" parent="Theme.AppCompat">
    <item name="customColor">#222222</item>
    </style>
  3. Use your custom attribute in your styles:

    <style name="CustomActionBar">
    <!-- title text color -->
    <item name="android:textColorPrimary">?attr/customColor</item>
    </style>

Programmatically change one attribute on custom style

What about group these TextView in only one Layout? Then change it programmatically.
In my example I group all of TextViews in only one LinearLayout.

LinearLayout ll = (LinearLayout) findViewById(R.id.layout);
int childCount = ll.getChildCount();
for (int i=0; i<childCount; i++){
((TextView)ll.getChildAt(i)).setTextSize(20);
}

Be sure that you only have TextViews in your layout.

I can't retrieve my custom attributes set in styles.xml when extending MaterialButton

In order to read values set in your default style, you'll need to pass in the correct defStyleAttr to your constructor.

To do this, you should also have a constructor which has just the Context and AttributeSet as arguments. You can call through to your method passing R.attr.materialButtonStyle as defStyleAttr.

The call to obtainStyledAttributes uses the value of defStyleAttr to read the default style that is set in your theme.

How can I set a textview's style programmatically?

You may find this answer by Benjamin Piette handy. To change this into working code for a TextView just change it up a bit:

TextView tv = new TextView (new ContextThemeWrapper(this, R.style.mystyle), null, 0);

EDIT: to set other things like margins, height & width and others, use LayoutParams. To set the params throrin19's answer can be helpful.

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
params.setMargins(left, top, right, bottom);
tv.setLayoutParams(params);


Related Topics



Leave a reply



Submit