How to Read Value from String.Xml in Android

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



Leave a reply



Submit