Create a value xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="toastText">
<attr name="toast" format="string"/>
</declare-styleable>
</resources>
in the layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res/your.app.package.name"
style="@style/wrapContent"
android:orientation="vertical" android:gravity="center_horizontal">
<your.app.package.name.OwnButton
android:id="@+id/button"
style="@style/wrapContent"
android:text="ABC"
yourapp:toast="abc" />
</LinearLayout>
extend button
public class OwnButton extends Button {
public OwnButton(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.toastText);
String toastText = a.getString(R.styleable.toastText_toast);
Toast.makeText(getContext(), toastText, Toast.LENGTH_SHORT).show();
}
}