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.
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 the app's 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>
Here's 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 return -1, so the value is not passed from my layout to the class.
What's wrong here?
The library project has a Customview Spinnerbutton
which extends TextView (in you case it may be different. For testing i extended TextView).
Now if i understand correctly this view is used in android project and you need to set the style to that custom view which can be done as below.
Use the style in android library project as the parent style and then customize the style in android project styles.xml
. Now set the style to the customview.
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>
All the above in library project.
Now in Another Android Project
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>
In 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>
Snap
This is for android project
Now if i run library project as a normal android project
Snap
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With