Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get android default attributes in a custom view

I have created a simple custom view that contains a RelativeLayout and an EditText:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/edt_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</RelativeLayout>

Also I have added some custom attributes in res/values/attrs.xml and I retrieve these attributes in my custom view constructor and everything works ok.

Now, I want to retrieve EditText default attributes in my custom view for example I want to get android:text attribute in my custom view.

My custom view class (simplified):

public class CustomEditText extends RelativeLayout {
    private int panCount;

    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs,
            R.styleable.CustomEditText, 0, 0);
        try {
            this.panCount = typedArray.getInt(R.styleable.CustomEditText_panCount, 16);
        } finally {
            typedArray.recycle();
        }
    }
}

How can I do that without redeclaring text attribute in res/values/attrs.xml?

like image 632
hosseinAmini Avatar asked Jun 16 '17 08:06

hosseinAmini


People also ask

What is Attrs XML in Android?

An <attr> element has two xml attributes name and format . name lets you call it something and this is how you end up referring to it in code, e.g., R. attr. my_attribute . The format attribute can have different values depending on the 'type' of attribute you want.

What is the name of the file which is used to declare custom attributes?

Create an XML res/values/attrs. xml file to define new attributes alongwith their data type.


2 Answers

You can add android:text to your declared syleable. But be sure to not redeclare it.

<declare-styleable name="CustomEditText">
    <attr name="android:text" />
</declare-styleable>

And then get this value from the style like you would with any other of your attributes with the index of R.styleable.CustomEditText_android_text.

CharSequence text = typedArray.getText(R.styleable.CustomEditText_android_text);
like image 176
tynn Avatar answered Oct 23 '22 09:10

tynn


You can access attrs from custom view like this:

class CustomView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0,
) : ConstraintLayout(context, attrs, defStyleAttr) {

    private val text = attrs?.getAttributeValue(
        "http://schemas.android.com/apk/res/android",
        "text"
    )

    private val scaleType = ImageView.ScaleType.values().getOrNull(
        attrs?.getAttributeIntValue(
            "http://schemas.android.com/apk/res/android",
            "scaleType",
            -1
        ) ?: -1
    )

    private val adjustViewBounds = attrs?.getAttributeBooleanValue(
        "http://schemas.android.com/apk/res/android",
        "adjustViewBounds",
        false
    )

    private val verticalGapRes = attrs?.getAttributeResourceValue(
        "http://schemas.android.com/apk/res-auto",
        "flow_verticalGap",
        R.dimen.default_res
    )
}

In XML:

<com.example.testcustomview.CustomView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:scaleType="centerCrop"
    android:text="Hello World!"
    app:flow_verticalGap="@dimen/default_res"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
like image 1
Bogdan Khrystov Avatar answered Oct 23 '22 09:10

Bogdan Khrystov