How to properly reference the Strings.xml file, android - android

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():

Related

Not able to access image in drawable from non-activity class in Android

I have created an Android library project that i am linking with my Android app. In the drawable folder of the library project, I have added a few images. Now I am trying to access these images from a non-activity class in the same project but they are not being accessed when I try to access them using the following snippet
Drawable drawable=context.getResources().getDrawable(R.drawable.image);
The import of R file is correct and the image id is being generated correctly in the R file. The context is correct. I tried refreshing and cleaning/building the project multiple times but nothing seems to work.
Help!
[Note: Answer copied from here.]
Yes you can if you know the package name of your library.
Resources.getIdentifier(...)
You can do:
getResources().getIdentifier("res_name", "res_type", "com.library.package");
ex:
R.id.settings would be:
getResources().getIdentifier("settings", "id", "com.library.package");
I'm not sure I understand the quest completely (the problem you are having is that the method does not return a drawable?
I'm not sure you about how you are getting the context, but you could try this:
Drawable drawable = ContextCompat(getContext(), R.drawable.image);
If you are storing the context in a variable, you can pass the variable instead of using getContext().
Additionally, the method you are using to get the drawable is deprected, so you shouldn't be using it anymore.
Let me know if it helped you and remember to upvote/select this as correct answer if it did, cheers.

Android + Android Studio: cannot resolve method settext(java.lang.string)

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 ;)

compareToIgnoreCase doesn't work with Strings.xml

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.

context.getResources().openRawResource() crashes the app

I'm trying for a while to make android read from a text file that I already have in the R.raw folder.
I have the function void readfile(Context context) that contains the line
InputStream is = context.getResources().openRawResource(R.raw.data);
However when I try to run the app it crashes at that exact line. Can you tell me a workaround? Or another way to read from that file?
There is obviously a problem with the given context. You should post your logcat.
There is 2 ways to solve your problem.
Pass a correct context: see what is wrong with your program and why the given context is not correct.
Extend Application, and save the application context in a static variable: see this post

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