Skip to content Skip to sidebar Skip to footer

Override Certain Attributes Of A Custom Control

Let's say there's a custom control and related style in an Android library project. The application that uses this library wants to override certain attributes of that control, whi

Solution 1:

Use a different name to your style in app/styles.xml and make the other style as it's parent.

<stylename="newCreditCardInputField"parent="CreditCardInputField"><itemname="android:layout_margin">50dp</item></style>

This will override your layout_margin while restoring background and textStyle.

Solution 2:

I've found a way to achieve what I want through the custom attributes. Not as convenient as with a style, but more flexible. In short, declare custom attributes in the library, read them in the control's code, provide in the app. Here's the almost complete code, maybe this will help someone:

In lib/values/attrs.xml (custom attributes are declared here):

<?xml version="1.0" encoding="utf-8"?><resources><declare-styleablename="test_view"><attrname="field_margins"format="dimension">50dp</attr><attrname="field_background"format="reference">@drawable/border</attr><attrname="name_field_hint"format="reference"/><attrname="number_field_hint"format="reference"/></declare-styleable></resources>

In lib/layout/credit_card_view.xml (this is the custom control's layout):

<?xml version="1.0" encoding="utf-8"?><mergexmlns:android="http://schemas.android.com/apk/res/android" ><EditTextstyle="@style/CreditCardInputField"android:layout_width="match_parent"android:layout_height="wrap_content"/><EditTextstyle="@style/CreditCardInputField"android:layout_width="match_parent"android:layout_height="wrap_content"/></merge>

In lib/java/TestView.java (the custom control itself):

publicclassTestViewextendsLinearLayout {
    publicTestView(Context context) {
        super(context);
    }

    publicTestView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArraya= context.obtainStyledAttributes(attrs,
            R.styleable.test_view, 0, 0);
        intmargins= (int)a.getDimension(R.styleable.test_view_field_margins, 0f);
        intbackground= a.getResourceId(R.styleable.test_view_field_background, R.drawable.border);
        intnameFieldHint= a.getResourceId(R.styleable.test_view_name_field_hint, R.string.name_field_hint_lib);
        intnumberFieldHint= a.getResourceId(R.styleable.test_view_number_field_hint, R.string.number_field_hint_lib);
        a.recycle();

        LayoutInflaterinflater= (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.credit_card_view, this, true);

        setOrientation(LinearLayout.VERTICAL);
        setGravity(Gravity.CENTER_VERTICAL);

        TextViewtitle= (TextView) getChildAt(0);
        title.setHint(nameFieldHint);
        title.setBackgroundResource(background);
        LinearLayout.LayoutParamsp=newLinearLayout.LayoutParams(context, attrs);
        p.setMargins(margins, margins, margins, margins);
        title.setLayoutParams(p);

        TextViewnumber= (TextView) getChildAt(1);
        number.setHint(numberFieldHint);
        number.setBackgroundResource(background);
        number.setLayoutParams(p);
    }
}

And finally in app/layout/main_activity.xml, custom control's usage and configuration:

<LinearLayout 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"
...>

<com.example.testlibrary.TestView
    custom:field_margins="20dp"custom:field_background="@drawable/field_background"custom:name_field_hint="@string/name_field_hint"custom:number_field_hint="@string/number_field_hint"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

Solution 3:

You should use parent attribute e.g.:

<stylename="CreditCardInputField"parent="parentStyle"><itemname="android:layout_margin">10dp</item><itemname="android:background">@drawable/border</item><itemname="android:textStyle">bold|italic</item></style>

Post a Comment for "Override Certain Attributes Of A Custom Control"