15

I am creating a custom compound view with the following layout

<merge xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="horizontal">

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <EditText
        android:id="@+id/edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:singleLine="true"/>

</merge>

As you can see, it is simply a TextView and a EditText. I want to be able to provide attributes to my custom view that are forwarded on to either the TextView or EditText. For example

<codeguru.labelededittext.LabeledEditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:label="@string/label"
    app:hint="@string/hint"/>

I have figured out how to forward these string attributes to the TextView and EditText, repsectively:

    TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.LabeledEditText,
            0, 0);

    try {
        label.setText(a.getString(R.styleable.LabeledEditText_label));
        edit.setHint(a.getString(R.styleable.LabeledEditText_hint));
    } finally {
        a.recycle();
    }

Now I also want to set the inputType of the EditText. If I create a <attr name="inputType" format="flag"> tag, will I have to populate it with all the possible flag values? Is there a way to reuse the values already declared by EditText?

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • your description is unclear. are you trying to say that you want same attribute tag to be common in all components? – prashantwosti Nov 03 '14 at 04:55
  • @prashantwosti No, I want a `label` attribute which sets the `android:text` of the `TextView` and `hint` attribute that sets the `android:hint` of the `EditText`. These are simple because they are only strings. However, I also want a `inputType` which sets the `android:inputType` for the `EditText`. But I don't want to rewrite code for all of the possible values. – Code-Apprentice Nov 03 '14 at 05:12
  • I'll build a proper SSCCE to illustrate better. – Code-Apprentice Nov 03 '14 at 05:14
  • possible duplicate of [How to use standard attribute android:text in my custom view?](http://stackoverflow.com/questions/18013971/how-to-use-standard-attribute-androidtext-in-my-custom-view) – Code-Apprentice Nov 03 '14 at 22:54

1 Answers1

3

You can get this with:

int[] values = new int[]{android.R.attr.inputType};
TypedArray standardAttrArray = getContext().obtainStyledAttributes(attrs, values);
try {
    mInputType = standardAttrArray.getInt(0, EditorInfo.TYPE_NULL);
} finally {
    standardAttrArray.recycle();
}
Bartek Lipinski
  • 30,698
  • 10
  • 94
  • 132
  • 1
    This seems close to what I want. However, it doesn't actually set the `inputType` of the `EditText` encapsulated in my custom view. Also, I would rather just forward the all of the attributes to said `EditText` and let it extract them itself rather than writing all the lines of code to extract each and every attribute myself. – Code-Apprentice Jan 20 '16 at 20:45