how to read value from string.xml in android?
Try this
String mess = getResources().getString(R.string.mess_1);
UPDATE
String string = getString(R.string.hello);
You can use either getString(int)
or getText(int)
to retrieve a string. getText(int)
will retain any rich text styling applied to the string.
Reference: https://developer.android.com/guide/topics/resources/string-resource.html
Getting a string value from the strings.xml resource file and set it in an interface class
If you are trying to access a string on the strings.xml file, you need to get it through getString(R.string. default_username);
In your case,
String username = getString(R.string. default_username);
how to get integer value from strings.xml in android
First, don't put them in strings.xml
. Put them in integers.xml
.
Second, you should use context.getResources().getInteger(R.integer.GUI_OK)
, where context
is an instance of a Context object, such as an Activity or Service.
R.integer.GUI_OK
is simply a resource value which Android uses to retrieve the actual value of that resource.
pass value to string.xml value that have a link inside it
I think SpannableStringBuilder
is what you are looking for.
TextView linkTv = (TextView) findViewById(R.id.link_tv);
linkTv.setMovementMethod(LinkMovementMethod.getInstance());
Spannable span = (Spannable) linkTv.getText();
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget){
//open the link
}
};
span.setSpan(clickableSpan, 0, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//for bold
span.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), 0, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
If you want to make only certain part clickable then toggle the values of 0
and span.length()
in setSpan()
.
Unable to retrieve string value from strings.xml to Java
Make sure you are using the correct R class - the one of your project, not android's. Check the imports.
It should be something like this
import com.yourapp.R;
and not like
import android.R;
Related Topics
Updating Android UI Using Threads
Android 2.1 Programmatically Unmount Sdcard
Android Webview - Webpage Should Fit the Device Screen
Hide/Show Bottomnavigationview on Scroll
How to Handle Handler Messages When Activity/Fragment Is Paused
Why Doesn't Logcat Show Anything in My Android
How to Convert Dp, Px, Sp Among Each Other, Especially Dp and Sp
Notification to Restore a Task Rather Than a Specific Activity
Creating a Preference Screen with Support (V21) Toolbar
How to Start an Activity from a Service
"Failed to Install the Following Android Sdk Packages as Some Licences Have Not Been Accepted" Error
Error Message 'Java.Net.Socketexception: Socket Failed: Eacces (Permission Denied)'
Android Studio Error "Installed Build Tools Revision 31.0.0 Is Corrupted"
Creating Temporary Files in Android
Can't Create Handler Inside Thread Which Has Not Called Looper.Prepare()