Passing style resource identifier from XML to class does not work

I have my own widget in the library project (Spinnerbutton) that I want to use in the application project. The custom widget contains a TextView, and I want to pass the style to this TextView from my application project.

This is my attrs.xml (in the library project):

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="Spinnerbutton">
        <attr name="myTextAppearence" format="reference" />
    </declare-styleable>

</resources>

And application layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/lib/com.example.spinnerbuttonlib"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.example.spinnerbuttonlib.spinnerbutton.Spinnerbutton
        android:id="@+id/sbp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        custom:myTextAppearence="@style/SmallTextGray" />

</RelativeLayout>

This is how I try to read my custom attribute in the Spinnerbutton class:

public Spinnerbutton(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;

    TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.Spinnerbutton);

    int textStyleId = a.getResourceId(
            R.styleable.Spinnerbutton_myTextAppearence, -1);

    a.recycle();

}

textStyleId always returns -1, so the value is not passed from my layout to the class.

What is wrong here?

+4
source share
5 answers

Customview Spinnerbutton, TextView ( . TextView).

, , android, , , .

Android , android styles.xml. .

package com.example.customviewattributes.p1;


import com.example.customviewattributes.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

public class Spinnerbutton extends TextView{
    public Spinnerbutton(Context context) {
        this(context, null);
    }
    public Spinnerbutton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Spinnerbutton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // real work here
        TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.Spinnerbutton,
                0, 0
        );

        try {
            int textStyleId = a.getResourceId(
                    R.styleable.Spinnerbutton_myTextAppearence, -1);
            Log.i("................ID is",""+textStyleId);
            // to make sure i logged the id 
            this.setText("hello");
            this.setTextAppearance(context,textStyleId);
            // set the style to text
           } finally {
               // release the TypedArray so that it can be reused.
               a.recycle();
           }
    }
 }

styles.xml

<resources>
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <item name="android:textViewStyle">@style/QText</item> 
    </style>

    <style name="AppTheme" parent="AppBaseTheme">
    </style>
   <style name="QText" parent="@android:style/TextAppearance.Medium"> 
        <item name="android:textSize">20sp</item> 
        <item name="android:textColor">@color/ccolor</item> 
        <item name="android:textStyle">bold</item>
        <item name="android:typeface">sans</item>
    </style>
</resources>

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Spinnerbutton">
        <attr name="myTextAppearence" format="reference" />
 </declare-styleable>

</resources>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="ccolor">#ff3232</color>

</resources>

.

Android

activity_main.xml

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
     >

    <com.example.customviewattributes.p1.Spinnerbutton
        android:id="@+id/sbp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
         />

</RelativeLayout>

MainActivity.java

setContentView(R.layout.activity_main);
com.example.customviewattributes.p1.Spinnerbutton cv = (Spinnerbutton) findViewById(R.id.sbp);
cv.setTextAppearance(this,R.style.QText1);
cv.setText("hello");

styles.xml

<resources>
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <item name="android:textViewStyle">@style/QText</item> 
    </style>
    <style name="AppTheme" parent="AppBaseTheme">
    </style>
   <style name="QText1" parent="@style/QText"> 
        <item name="android:textSize">50sp</item> 
        <item name="android:textColor">@color/ccolor</item> 
        <item name="android:textStyle">bold</item>
        <item name="android:typeface">sans</item>
    </style>
</resources>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="ccolor">#4F47B7</color>

</resources>

enter image description here

,

enter image description here

+5

, xmlns: custom , :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res/com.example.yourapppackage"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.example.spinnerbuttonlib.spinnerbutton.Spinnerbutton
        android:id="@+id/sbp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        custom:myTextAppearence="@style/SmallTextGray" />

</RelativeLayout>

, xmlns , :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.example.spinnerbuttonlib.spinnerbutton.Spinnerbutton
        android:id="@+id/sbp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        custom:myTextAppearence="@style/SmallTextGray" />

</RelativeLayout>
0

I think you're lucky if your own XML namespace:

xmlns:custom="http://schemas.android.com/apk/res-auto"
0
source

change layout:

android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<com.example.spinnerbuttonlib.spinnerbutton.Spinnerbutton
    xmlns:custom="http://schemas.android.com/apk/res/android"
    android:id="@+id/sbp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    custom:myTextAppearence="@style/SmallTextGray" />

0
source

try with this in attrs.xml from

<attr name="myTextAppearence" format="reference" />

to that

<attr name="myTextAppearence" format="integer" />

and

 int textStyleId = a.getResourceId(
            R.styleable.Spinnerbutton_myTextAppearence, -1); in Spinnerbutton() constructor 
to
int textStyleId = a.getInt(
            R.styleable.Spinnerbutton_myTextAppearence, -1);

he will work

0
source

All Articles