Finding the value of the reference name to R - android

I am doing some debugging in my application, mainly loading custom styles from styles.xml when my custom view is given a style="#styles/CustomStyle", and attributes such as custom:attribute="custom value"
I looked into the TextView source to see how Android loads styles/attributes and I am mimicking that. However I am not being passed any of my R.styleables through some of the calls to my constructors and so I am trying to peek in there to see which resources are coming in.
I am using obtainStyledAttributes() to load these key/value pairs into a TypedArray, however I am wondering if there is an easy way to convert the R.styleable.CustomWidget_customAttribute from the int that R reads, to its referenced name.
In essence, I want LogCat to say, "We've been given R.styleable.xxx" and not "We've been given 1487214712442"

Look at this method: http://developer.android.com/reference/android/content/res/Resources.html#getResourceName(int)
Return the full name for a given resource identifier. This name is a single string of the form "package:type/entry".

You most likely are not able to do this explicitly, as all resources are stored in a generated java class with no accessible reference to the original strings.
However, your best bet is override the toString() method for the R class.
See if something like that works.
Hope this helped!

Related

Why does the R class not contain the field type?

Whenever we want to inflate a view or get a resource we have to cast it in run-time. views, for example, are used like so:
In the past, we would have needed to cast it locally
(RelativeLayout) findViewById(R.id.my_relative_layout_view)
Now, we use generics
findViewById<RelativeLayout>(R.id.my_relative_layout_view)
my question is why doesn't the compiler(or whoever generates the R class) doesn't also keep some kind of a reference to the type of the element(doesn't matter if it's a string or an int or any other type) that way casting problems should not occur
We cannot really speculate on that, that would be a design choice.
It might be that they wanted to avoid bloating the APK. Every ID would need a full package name to the class. So would each ID in android.R too. Since R is packaged in every APK.
Solutions
However, if you are using Kotlin, you can even do away with the generics check. Kotlin will determine it automatically.
val view = findViewById(R.id.my_relative_layout_view)
view.method()
Or event simpler, if you use synthetics:
my_relative_layout_view.method()
Also, if you are using data bindings, you can just access it like this:
binding.my_relative_layout_view.method()

Is it possible to get an attribute value of a widget as declarated in the android layout.xml file?

Is there a way in an android application to retrieve the attribute value of a widget declared in a layout.xml?
for example in my layout.xml file i have :
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/clean_filesWithoutVideo"
android:checked="#{uiprefs.switchButtonEditable}"
android:id="#+id/checkbox_updateable" />
I would like to retrieve the literal value for android:checked, eg I want to retrieve "#{uiprefs.switchButtonEditable}" at runtime.
I searched in android.content.res.Resources class using getResources() from an Activity without success :
I cant get the xml file as stream.
Parsing attributes throught the Resources.getLayout() doesn't restitute the attribute value.
Maybe this value is hidden somewhere in the CheckBox instance but I can't find it using inspection at debug time...
Note : The value I want to retrive are especially databinding literals. Maybe I could retrieve this through the databinding API ?
Every UI component has it’s own getters and setters through which you can retrieve attributes that are currently set or set what you want to. If the attributes are common among the views, for example height/width, the getter will be available in View class.
In your case, you can get checked attribute through:
CheckBox checkbox = (CheckBox) view.findViewById(R.id.checkbox_updateable);
boolean initialState = checkbox.isChecked();
In Android Studio, you can get the methods available for a particular view. Enter var name (say checkbox) then enter dot (.) and it will show you all available methods. Start typing that you think you are looking for, it will show you all relevant methods.
UPDATE
I think AttributeSet is what you are looking for. You can do this :
XmlPullParser parser = resources.getXml(myResource);
AttributeSet attributes = Xml.asAttributeSet(parser);
From doc:
The implementation returned here, unlike using the implementation on
top of a generic XmlPullParser, is highly optimized by retrieving
pre-computed information that was generated by aapt when compiling
your resources. For example, the getAttributeFloatValue(int, float)
method returns a floating point number previous stored in the compiled
resource instead of parsing at runtime the string originally in the
XML file.
This interface also provides additional information
contained in the compiled XML resource that is not available in a
normal XML file, such as getAttributeNameResource(int) which returns
the resource identifier associated with a particular XML attribute
name.

what does the "#+android:id/title" mean?

In normal, we should use #+id/ to define an id and use #id to reference an id. Today I found #+android:id/title in apps/settings/res/layout/preferenc_progress.xml.
How to understand it and how to use it?
It is used for resources that are shipped with the SDK.
You can take a look at them by browsing to
[PATH TO ANDROID SDK]/platforms/android-[VERSION]/data/res
By using the android in android.R.whatever you just specify the R file to look up. For more information you should read Accessing Platform Resources.
That belongs to the app preferences activity screen definition.
title and summary are standard Android fields of a TextView preference item.
I think it does the same thing. It's just a more formal way of saying it by specifying where the namespace is.
I've never met this way of giving id, but in theory this means adding new id title to android package. So you'll be able to use it in your code like android.R.id.title. But I'm not sure resource compiler will really create any id in android package. I think it can be used only with predefined ids. But I'll give you more precise answer later, when I'll be able to check it.
EDIT: I've checked it and found some differences. Firstly, if you define Android's id using #+android:id/some_id, which is already present in SDK, this id will not be defined in your R.java file. If it's not present in SDK, it will be defined in R.java, but with different kind of value. Secondly, if you'll try to convert id from its string representation to int value, Resources.getIdentifier() method will return 0 in case of #+android:id format.
This means it will create an id in your resource file.

Problems accessing my strings.xml items… i got numbers and not the string value

i found this problem some time ago, but i solve it using this: getString(), or this: getResources().getString()
but now, for this case, it doesn't works, i think it's because i need to get the string values on a NON ANDROID ACTIVITY CLASS. I need the resource values on a remote connection class, that doesn't extends any kind of activity or service.
how i can acces to the variables from my strings.xml on this normal class?
this is the code where i get the error (it gets an integer, and not the string value)
String a =R.string.totalpermission;
Take a look at these two answers (are the same XD):
How to obtain AssetManager without reference to Context?
How can I get a resource content from a static context?
Just an advice: try to read some basic concepts... it seems you don't understand what the R class is and how to use it. Trust me, you waste less time studying than trying to figure out how things work.
I'll add something to existing answers since I found it very useful.
To get your strings you have to use a Context. Your activity will work just great.
String string = getString(R.string.myString);
But if you have something more complex... for exemple
R.string.result -> "You %1$s %2$d cats"
String result = getString(R.string.result, killed ? "killed": "saved", count);
That would give you a result like that:
You saved 10 cats or You killed 2 cats... and so on. You can pass parameters and positional arguments in strings will get replaced by your arguments in getString.
All Android resources are referenced via a resource ID, like R.string.totalpermission. You can see those numbers in R.java (although there's no reason to ever do that).
In cases of strings, you can easily get those using Context.getString. Bonus: You can even pass optional arguments and add dynamic strings that way. You always have a context - how are you getting called? If you really don't have a context, you can create one for the package your resources are in.

Cannot get resource from android application

I am building an Android app, and I have a problem to get resource from string.xml.
I need to have a URL in String.xml that will be used several times through the app.
I tried Resources.getText("my_url"), but this does not work. Eclipse complains when I use this.
Do you think this is the best way to do ?
What you probably want is:
String myUrl = getString(R.string.my_url);
The getString() method is in Context which means that it's available directly in your Activity class since Activity is a subclass of Context. (The getString() method is also in Resources but it's easier to call on it directly on your Activity.)
What happens with your XML resources is that each is given a unique integer ID and this is added to the automatically generated R class as a public static final int. You then use these IDs to reference the resources. Have a look in the gen folder in your Eclipse project and you'll find the R class in there.
Do you ever refer this page: https://developer.android.com/guide/topics/resources/available-resources.html ?
If you want to retrieve the String represented by a resource ID, you can call the Context.getString() method.
Or, you have to post Eclipse's complains.

Categories

Resources