14

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?

tynn
  • 38,113
  • 8
  • 108
  • 143
hosseinAmini
  • 2,184
  • 2
  • 20
  • 46
  • it is confusing you are not using your view in xml code...! are you sure you have post the latest code. Check this answer: https://stackoverflow.com/a/8090772/7134908 it might solve your problem to set value in xml and get into Java vode – Ajeet Choudhary Jun 16 '17 at 09:17
  • Could you add your `styleable` file for `CustomEditText`? – tynn Jun 16 '17 at 10:50
  • 2
    Possible duplicate of [How to get attributes with namespace "android" in custom TextView](https://stackoverflow.com/questions/26486791/how-to-get-attributes-with-namespace-android-in-custom-textview) – Mir-Ismaili Nov 21 '17 at 12:39

5 Answers5

39

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);
tynn
  • 38,113
  • 8
  • 108
  • 143
1

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

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()
Dany Pop
  • 3,590
  • 2
  • 21
  • 28
0

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.

-1

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");
    }
}
Krutik
  • 732
  • 6
  • 19