Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing style resource id from XML to class not working
    primarykey
    data
    text
    <p>I have a custom widget in a library project (Spinnerbutton) that I want to use in an application project. The custom widget contains a TextView and I want to pass a style to that TextView from my app project. </p> <p>This is my attrs.xml (in the library project): </p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;resources&gt; &lt;declare-styleable name="Spinnerbutton"&gt; &lt;attr name="myTextAppearence" format="reference" /&gt; &lt;/declare-styleable&gt; &lt;/resources&gt; </code></pre> <p>And the app's layout: </p> <pre><code>&lt;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" &gt; &lt;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" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>Here's how I try to read my custom attribute in the Spinnerbutton class: </p> <pre><code>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(); } </code></pre> <p>textStyleId always return -1, so the value is not passed from my layout to the class. </p> <p>What's wrong here?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload