In my code, I've got a array of all the apps that are installed on the device, in which I want to find the app I'm running itself. To accomplish this I wrote the following code:
if (tmpPacs[I].packageName.compareToIgnoreCase("com.example.basiclauncher") == 0){
launcherPosition = I;
}
This line works perfectly, however, I want to store "com.example.basiclauncher" in a string in strings.xml like so:
<string name="launcher_package">com.example.basiclauncher</string>
And change the line to:
if (tmpPacs[I].packageName.compareToIgnoreCase("#string/launcher_package") == 0){
launcherPosition = I;
}
This doesn't work, launcherPosition stays 0 throughout the execution of the code and the correct app isn't found. When I revert to the first line, the app is found again. Why is this? And how can I make this work with the strings.xml file?
EDIT:
I just now realised I was trying to use xml code inside my java class, which obviously isn't going to work. Thanks for all the quick replies, getPackageName() is a much more elegant solution!
to retrieve the string from the xml, you have to use getResources().getString(R.string. launcher_package). You can also retrieve the package name through getPackageName()
You need to do
tmpPacs[1].packageName.compareToIgnoreCase(getResources().getString(R.string.launcher_package));
The code you have is literally comparing tmpPacs[I].packageName to the string "#string/launcher_package" instead of the string resource it refers to.
You can't use "#string/launcher_package".
Try this:
tmpPacs[I].packageName.compareToIgnoreCase(
getResources().getString(R.string.
launcher_package))
Hope this helps.
Related
I tried a lot of combinations, but somehow I don't get it.
The id for the text I want to show is generated.
I need to write a variable here instead of a concrete id.
So not:
getString(R.string.id_1)
But something like:
var myId = ...
getString(R.string."$myId")
Do you know what I mean? What ever I tried I got an error that only an Int.
How would you solve this in Kotlin?
Try below code, it will work for you:
fun AppCompatActivity.getString(name: String): String {
return resources.getString(resources.getIdentifier(name, "string", packageName))
}
Usage: val resource = getString($resourceName);
if you want to access string resource dynamically than you have to create pattern something like below
string.xml
<string name="abc">Hello</string>
<string name="abc1">Hello1</string>
<string name="abc2">Hello2</string>
<string name="abc3">Hello3</string>
java code
getString(R.string.abc+<index>)
this is just an idea, i haven't tried it.
Let me know whether it works or fail.
Happy Coding :)
I learn android with sample apps from textbook. In the given sample source R.string.something is recognized:
However, in my own EXACT source code, same R.strings are not recognized- highlighted as errors:
Does anyone know how to fix it?
I've had this many time with eclipse, and it was not a setup / code problem on my side. Generally, cleaning, rebuilding and sometime even stopping and restarting eclipse solved it. And yes, it's a pain in the neck ...
Try a clean of your project, if that does nothing then click on the problems tab and see if there's any build related issues, you may be missing a required jar or something!
These are following reason possible.
1-: You import android.R;
2-: Any error in xml files.
3-: Please check you String.xml file may be any error or declare a string more then two time.
I assume you are new to Android.
If so, there are two ways of using text strings in Buttons, textviews and so on:
1) Hardcoded string - you put the text you want in quotation marks (""), for example:
yourTextview.setText("Hardcoded string");
2) You can call the text from your String resources (res/values/strings.xml).
That is a much better approach seeing is is easier to translate, make changes and so on.
In your strings.xml file you can create all your string values, and call them from there.
Like in you example, if in your strings.xml file you have for example:
<string name="delete">This is String resource</string>
you can then call the string from there, like so :
yourTextview.setText(R.string.delete);
Hope this helps!
I want to be able to set the text of buttons using the strings xml file. I have this code;
Button playVid = (Button)this.findViewById(R.id.vidbutton1);
playVid.SetText(this.getApplicationContext().getString(R.string.play_video));
And this xml
<string name="play_video">Play Video</string>
But I get the compile
error: cannot resolve method settext(java.lang.string)
I am using Android Studio. Everywhere I have read suggest that you can use strings to set text (makes sense, right?), so I am very confused.
This also will not work:
playVid.SetText("Test");
Bug in AS?
Mind your casing. Use setText() instead of SetText().
Also there's an overload setText(int) that takes in a resource id. You can use it to set a value from resources without using getString() to obtain it yourself first.
Methods in Java usually start with a lower-case letter. Maybe that's what your problem is here.
Try playVid.setText("Test"); instead of playVid.SetText("Test");
This works perfectly fine:
Button button = (Button) findViewById(R.id.some_button);
button.setText(R.string.hello_world);
Make sure you imports are correct ;)
In my app I put a certain link in strings.xml file and then I use it in the rest of the application. Here is the way I put in strings.xml file :
<string name="link">http://mylink/</string>
and here in how I get it in my activity :
String link = getResources().getString(R.string.link);
The problem is that this String returns sometimes not the value from strings file. I get often #ff666666 and that's strange. Why sometimes it works fine and sometimes not ?
Has anyone any idea about this?
Thanks in advance.
You need to Clean and Build again before running the project.
This is happening because some times new values are added in the R.java file that doesn't get updated, so you need to clean to force the R.java file to refresh and update itself.
Try debugging into the particular method and inspect the value of R.string.link. Is it the same as you would see in the R.java file? Sometimes you get this problem when shifting around stuff in your resource files. "Project->Clean...->Clean all projects" in Eclipse usually solves it.
It is working fine. Just now i checked with following lines. Make sure sure to clean your project and build it.
<string name="link">http://www.google.com</string>
String name = getResources().getString(R.string.link);
Log.e("Name is:",name);
My string.xml has a string:
<string name="loginLocation">http://website.com/afile.php</string>
In my java file, I am trying to reference it like so:
url = new URL(R.string.loginLocation);
except I am getting the error:
The constructor URL(int) is undefined
I managed to get the error to go away by doing:
url = new URL(Integer.toString(R.string.loginLocation));
except when I make the call to it, I get a Protocol Error
I can do:
url = new URL("http://website.com/afile.php");
and it works fine, but I'd like to define it in the Strings.xml file. Any help is appreciated, thanks!
If you're trying to do it in a method of the Activity subclass, then do the following:
url = new URL(getString(R.string.loginLocation));
getResources() will give you a lot of methods for accessing what's in your /res directory. Among the rest, the method you are looking for is getString(R.string....), which is also available directly from your Context (without the Resources object).
As simple as this:
this.getxt(R.string.btn_menu_logout);
What nobody mentions online is to put .this before gettxt() or getText():