How to add string parametrs to XML from Main Activity - android

In android can we do some thing like R.string.navigation_item_1 = _userName.getText().toString() where _userName is a TextView and navigation_item_1 is a string name in strings.xml. If not then what is the right procedure to pass a string from MainActivity to strings.xml

You can not customize the resources (no matter if string, drawable or what) in android at run time. The res folder could not be modified programmatically at all. For example you could not change a drawable at run time.
More answers to a similar question here: Change value of R.string programically
If you need a string value to be saved in your app, you could use SharedPrefferences for example

The answer is simple: you can't.
All xml resources files can't be modified during runtime. You can still assign that _userName.getText().toString() to a public instance variable but you can't add it to some resources which reference is created during build process.

Related

Should I save keys of bundle in java constants or res/values/strings.xml or something else in android?

I have a lot of Intent to send message between activities and services in my APP.
Usually they will have some Object to pass by bundle.putString(key, someObject) and bundle.getString(key, someObject).
I want these keys to be well organized so I will be clear with messages being sent.As I think, they should be constants in Java, but for android, would it be better to put them in res/values/strings.xml named as key_xxx_xxx.
Thanks for any answers or comments.
Well, I thought about this matter a few days ago.
Use Java is better than strings.xml, like Android' s permissions are all Java public static final field. If you hava a lot of constants, you can also prepend a package name to be clear and identical, which strings.xml cannot supply.
If your xml file use the the key, I think is better to put the key in the strings.xml. For example, your settings xml file need a string key, and your java code also needs it, if you just put that key in java, you need to set text literal to the settings xml file, that may not good...
in short, it depends on the key used in java or xml or both.
hope it helpful!
It would be good to keep them as static constants. Make a java file as Utils.java and keep all the names there as Static Constants.
Something like this:
public static final String FIRST_KEY = "my_first_key";

How to create a string resource programatically

I am using WebTrends analytics in my app.
(Ref-http://help.webtrends.com/en/android/)
The WebTrends APIs are written in such a way that the initialization happens ONLY from resources file. They provide webtrends.xml with the sdk and all the parameters are set from the values from this xml file.
Now the problem is I have to set a couple of values dynamically based on our server feed.
Here I have only two choices:
(1) Set WebTrends initialization values in the code which looks impossible from WebTrends SDK. Neither the member variables are exposed outside the library nor there are any setters/methods to set the initialization params.
(2) Create resources from webtrends.xml dynamically or at least set the values for resources dynamically which also seems impossible.
Can anyone please suggest the way out of this deadlock?
You cannot do that. When you add a string resource an automatic entry is made for the resource in R.java file at compile time.
Example:
public static final class string
{
public static final int app_name=0x7f040000;
}
where app_name is the name of the string resource. So, it is not possible.

Get String from value folder to class android

I want to use a particular string found in the Values folder into normal classes. This is for my localization process with different languages.
How would I do this?
Use the getString() method on a Context (such as an Activity), specifying the key in this form: R.string.you_key
If you need to get a String outside of an Activity, like in a helper class or something, just make sure you pass in a reference to a context.
Access the value by calling getString(R.string.your_string);
Ex.
String myValue = getString(R.string.your_string);
obviously replace "your_string" with the actual name you gave the string

Android Source Code for android:text attribute processing?

I am trying to find the original android source code, where the android:text attribute from the layout files is resolved to the actual string referred to via #string/ (for instance: android:text="#string/hello"). The reason is the following: I have created a completely different way of setting up my layouts using a custom XML which in turn uses pre-written Widgets, an example:
<dialog name="Homepage">
<field type="Combobox" label="Enter">
</dialog>
What I want to do: I want to be able to change the label attribute in my example to: label="#string/enter" - so I can make use of the localization feature in android.
But in order to handle those #string commands, I need to know, where this is done in the original Android Source Code (on version 2.3 for instance) so I can imitate the behaviour. So far the only way I know of to obtain string resources is to use context.getString(int resID) - trouble is, how would I go about translating the String "#string/enter" to a res id? I assume I can't, which is why i am curious about how android handles this.
I'd be really grateful if someone could point me in the right direction and I hope my explanation wasn't too confusing :) .
[..] trouble is, how would I go about translating the String "#string/enter" to a res id?
You can use Resources.getIdentifier() for that.
Small sample that gets the res id of #string/enter:
Resources r = getResources();
int resId = r.getIdentifier("enter", "string", getPackageName());
Given that the format for resources is always the same, you can parse all three arguments out of this string and fill them into getIdentifier():
#[<package_name>:]<resource_type>/<resource_name>
from Accessing Resources

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