How to get attributes with android namespace in custom TextView

In a custom TextView I am trying to get the value of the text attribute (for example).

 TypedArray values = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.TextView); String text = values.getString(com.android.internal.R.styleable.TextView_text); 

But I get this error message:

package com.android.internal.R does not exist

So how to get default attributes for TextView?

+5
source share
3 answers

The internal.R class is not displayed, so you can only read them through their access methods and only after the super constructor has been called.

See TextView source to see how it works internally.

+1
source

If you want to have access to these "android" attributes, you can "override" them: in the declaration of the declared style, for example.

 <attr name="android:padding"/> 

Then you can easily get it this way:

 int padding = a.getInt(R.styleable.CustomView_android_padding, -1); 

For recording only, anwser is inspired by the source code for the TwoWayView layout, implemented by Lucas Rocha. I think this is a good example for analyzing how to implement custom views.

+6
source

To access the resource com.android.internal.R you can use

 Resources.getSystem().getIdentifier(name, defType, defPackage) 

The link may be useful:

0
source

Source: https://habr.com/ru/post/1205177/


All Articles