3

Is there a way to programmatically get name of styleable attribute:

<declare-styleable name="TextView">
    <attr name="DataContext" format="string" />
    <attr name="Text" format="string" />
</declare-styleable>

For example I want to get "Text" from R.styleable.TextView_Text integer value.

1 Answer 1

2

Your Styleable is:

<declare-styleable name="TextView">
    <attr name="DataContext" format="string" />
    <attr name="Text" format="string" />
</declare-styleable>

Call it where you want with this:

custom:DataContext="@string/xxxx"

In your Code:

TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.TextView);


  int n = a.getIndexCount();
    for (int i = 0; i < n; i++) {
        int attr = a.getIndex(i);
        switch (attr) {
        case R.styleable.TextView_DataContext:
            title = a.getString(attr);
            break;

        case R.styleable.TextVie_Text:
            //Any you want
            break;
        default:
            Log.d("TAG", "Unknown attribute for " + getClass().toString() + ": " + attr);
            break;
        }
    }

    a.recycle();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.