6

In custom TextView I'm trying to obtain value of a 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'm getting this error message:

package com.android.internal.R does not exist

So how to retrieve "default" attributes of TextView?

reVerse
  • 35,075
  • 22
  • 89
  • 84
Dima
  • 1,490
  • 16
  • 25

4 Answers4

7

If you want have an access to those 'android' attributes you can 'override' them: in your declarable-styleable declare eg.

<attr name="android:padding"/>

Then you can easily obtain it this way:

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

Just for a record anwser is inspired by source code of TwoWayView layout implemented by Lucas Rocha. I think it is good example to analyse how to implement custom views

Krzysztof Skrzynecki
  • 2,345
  • 27
  • 39
1

The internal.R class isn't visible so you can only read them through their accessor methods and only once your super constructor has been called.

See the TextView source to see how it works internally.

reactivemobile
  • 505
  • 3
  • 9
1

Example:

val attrsArray = intArrayOf(android.R.attr.digits, android.R.attr.text)
val ta = context.obtainStyledAttributes(attrs, attrsArray)
val digits = ta.getString(0)
val text = ta.getText(1)
ta.recycle()
Milan Jurkulak
  • 557
  • 3
  • 6
-1

For accessing the resource of com.android.internal.R, you can use

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

The link may be helpful:

Community
  • 1
  • 1
Jerikc XIONG
  • 3,517
  • 5
  • 42
  • 71