I have this in a normal activity:
tv.setText(this.getString(R.string.week1));
I think
remoteViews.setTextViewText(R.id.widget_textview,"something");
is the equivalent to
tv.setText("something");
How do I getString from strings.xml in AppWidgetProvider?
There is probably a really easy solution but I tried using google and I'm new to android...
So, when you get a hold of a Context you can just call
context.getString(resIdHere);
onEnable(), onDisable(), onReceive() and onUpdate() methods all pass context reference in to their definitions by default.
Hope this helps,
-serkan
I do not know how to "How do I getString from strings.xml in AppWidgetProvider?" but I think you try to get string from strings.xml
and that could be done like this.
String s = getResources().getString(R.string.-yourString);
Related
I'm having some problems on where and how to place and call the getString().
I tried at the beginning of my activity where I usually define it but always get an error. Should it be only after onCreate()?
Some of my strings I use it very often so I don't want to use getString() every time I use them.
Is this the correct way?
private String helloWorld = getString(R.string.hello_world);
Thank you very much! :)
at the top of your file do:
private String helloWorld;
And then, in onCreate(),
do:
helloWorld = getString(R.String.hello_world).
The problem is that getString needs a reference to the activity context, which has not been associated with your class until onCreate is called.
Is it possible to use a ContentObserver to monitor variable changes (e.g. String) by constructing a Uri and passing it as an argument when registering? If yes, can you provide a functional example?
It won't work by doing Uri.parse on an arbitrary string value.
I don't think it will work at all. What's the use case? It suggests that something outside of your control is modifying a variable. How can this happen?
It should work with string value,
It works with me. I have used Uri.parse like below:
contentResolver.notifyChange(Uri.parse("content://com.example.provider/notification"), null)
I was looking at the widget sample in the google api demos, and i found this line in the strings.xml
<string name="appwidget_text_format"><xliff:g id="prefix">%1$s</xliff:g></string>
and I don't understand what it's doing, I've never seen something like this, can someone please explain to me what's going on here.
I mean the whole, xliff:g and id, and especially the "%1$s". What's it doing?
Thanks very much
xliff here is an additional namespace (should be specified in the xml header) and refers to the XML Localisation Interchange File Format.
The %1$s is a placeholder for a value you can later set in your Activity using the getString(int resId, Object... arg) method.
I believe it is a way to reference a string that is passed in as an argument. Take a look here in the Formatting strings where they give an example using it. Similarly you can do %2$d to reference a decimal.
You better refer how to use Formatter to get more understanding on "%1$s" part.
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.
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.