You can create custom attributes.
For this you need to declare an Attribute Resource :
In the res/value
folder, create a file named attrs.xml
with the following content:
<resources>
<declare-styleable name="SwitchCustom">
<attr name="cordefundo" format="color" />
</declare-styleable>
</resources>
In the SwitchCustom java code you can access the value of this attribute in the SwitchCustom(Context context, AttributeSet attrs)
constructor:
private int cordefundo;
public SwitchCustom(Context context, AttributeSet attrs){
super(context, attrs);
TypedArray a = context.getTheme()
.obtainStyledAttributes(attrs, R.styleable.SwitchCustom, 0, 0);
try {
cordefundo = a.getInteger(R.styleable.SwitchCustom_cordefundo, 0);
} finally {
a.recycle();
}
}
When you put the SwitchCustom in a layout , to access the attribute, you must indicate in the layout the namespace to which it belongs :
xmlns:custom="http://schemas.android.com/apk/res/com.example.utils"
Example for a LinearLayout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.example.utils">
<com.example.utils.SwitchCustom
android:layout_margin="15dp"
android:layout_width="150dp"
android:layout_height="50dp"
custom:cordefundo="@android:color/blue"/>
</LinearLayout>