Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using custom attrs in styles.xml in android

Tags:

android

xml

I made a custom attribute style as

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="EffectsTextView">        
        <attr name="strokeWidth" format="float" />
        <attr name="strokeMiter" format="float" />
        <attr name="strokeColor" format="color" />        
    </declare-styleable>
</resources>

In Layout file, I could use this custom attribute assigning a namespace for this attribute:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
xmlns:effects="http://schemas.android.com/apk/res-auto/com.base.view.EffectsTextView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#40000000" >

 <com.base.view.EffectsTextView
        android:id="@+id/textView1"
        android:layout_width="290dip"
        android:layout_height="80dip"
        android:layout_marginLeft="5dip"
        android:layout_marginTop="16dip"
        android:gravity="center"
        android:text="SETTINGS"
        android:textSize="44dip"
        android:textStyle="bold"  
        effects:strokeWidth="10"  />    <--- custom tag "effects"

However it was not recognizing this custom tag in styles.xml

<style name="EffectsHeaderTextStyle">
     <item name="effects:strokeWidth">10</item>
</style>

Error: No resource found that matches the given name, effects, though I put the namespace same as I did in RelativeLayout file.

Any ideas? Thanks

like image 735
annas8 Avatar asked Apr 05 '13 01:04

annas8


People also ask

How to create custom button in Android using XML styles?

This example demonstrates how do I create custom button in Android using XML Styles. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 – Right Click on res/drawable, ...

How do I create a custom theme in Android?

You can create a theme the same way you create styles. The difference is how you apply it: instead of applying a style with the style attribute on a view, you apply a theme with the android:theme attribute on either the <application> tag or an <activity> tag in the AndroidManifest.xml file.

How do I create a custom style in HTML?

For each style you want to create, follow these steps: Add a <style> element with a name that uniquely identifies the style. Add an <item> element for each style attribute you want to define. The name in each item specifies an attribute you would otherwise use as an XML attribute in your layout.

How to create a custom dialog in Android Studio?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 – Right Click on res/drawable, Select new → Drawable resource file and add the following code in custom_dialog.xml


1 Answers

Change your XML to:

xmlns:effects="http://schemas.android.com/apk/res/com.base.view.EffectsTextView"

And change your style:

<style name="EffectsHeaderTextStyle">
    <item name="strokeWidth">10</item>
</style>

I did something similar with fonts:

https://github.com/androidfu/CodeExamples/tree/master/com.androidfu.CustomFontsDemo

like image 136
Bill Mote Avatar answered Oct 03 '22 14:10

Bill Mote