hosseinAmini
hosseinAmini

Reputation: 2342

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?

Upvotes: 15

Views: 15301

Answers (5)

Bogdan Khrystov
Bogdan Khrystov

Reputation: 81

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" />

Upvotes: 1

tynn
tynn

Reputation: 39873

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);

Upvotes: 42

Krutik
Krutik

Reputation: 732

You can build this using layout like

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
    android:id="@+id/edittext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="My Custom View" />
</RelativeLayout>

Your customview class will be like this

public class CustomEditText extends RelativeLayout {
    LayoutInflater mInflater;

    public CustomView(Context context) {
        super(context);
        mInflater = LayoutInflater.from(context);
        init();

    }

    public CustomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mInflater = LayoutInflater.from(context);
        init();
    }

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mInflater = LayoutInflater.from(context);
        init();
    }

    // Here you can access your edittext text
    public void init() {
        View v = mInflater.inflate(R.layout.custom_view, this, true);
        EditText et = (TextView) v.findViewById(R.id.edittext);
        et.setText(" Custom RelativeLayout");
    }
}

Upvotes: -1

Arefeh Iravanchi
Arefeh Iravanchi

Reputation: 201

I think it's not possible because your customview extends relativeLayout and it doesnt have this attribute. You can access android:text or other attributes if you extend from EditText or TextView directly.

Upvotes: 0

Dany Pop
Dany Pop

Reputation: 3658

I think you cannot do that. Add an id to your edittext in xml and if you want to retrieve value of editText, simply use :

edittext.getText().toString()

Upvotes: 0

Related Questions