I'm trying to reference a formatted String in my strings.xml.
I was googling around and found this good article:
http://developer.android.com/guide/topics/resources/string-resource.html
I followed it to every detail it would seem, but the text printed out isn't what it's supposed to be.
First, in my strings.xml under resources, i defined it as:
<string name="print_binary">This is the printout: %1$s</string>
And in the .java i put:
String fromString = "one";
Resources res = getResources();
onScreen = String.format(res.getString(R.string.print_binary), fromString);
This gives the following text on screen:
This is the printout: %1$s
Please advice
You can use:
onScreen = res.getString(R.string.print_binary, fromString);
Goodness, I'd totally missed inputting something like
TextView show = (TextView) findViewById(R.id.tvDisplay);
show.setText(onScreen);
And then adding an id for my TextView object.
Too late in the night. It seems like the String.format() statement isn't necessary, i'll read up on that.
Thanks for your support!
Related
Due to HTML usage within a string resource, I can't convert this to string from a charsequence (I will lose the formatting otherwise).
<string name="exponent_key">x<sup><small>y</small></sup>/string>
After using getString() I want to replace the 'y' with 'other stuff' but how do you do that? It seems like a simple question but for some reason I can't find anything about it.
Edit: Now that I think about it, can I convert the charsequence to a string that contains the HTML code, and then convert it back to a charsequence later?
Edit: Forgot to mention that the string gets set to a button title, and then retrieved (where it is then used).
There is. Create a function where, as a parameter, you take a string that needs to be formatted. And in function, you just take it through itterator, and after that Html.fromHtml()
in your string.xml
<string name="exponent_key">x<sup><small>%1$d</small></sup></string>
in your code
textView.setText(getString(R.string.exponent_key,2))
Let's break down your question in multiple steps:
Replacing the y with "other stuff" can be done like this:
String.format("%1$", "otherStuff");
If you use getString(), you can do the same thing like that:
<string name="exponent_key">%1$</string>
---
String string = getString(R.string.exponent_key, "otherStuff");
For more than one element, do this way:
you can do that like this:
<string name="string_name">%1$ : %2$</string>
---
getString(R.string.string_name, new String[]{"hello", "world"});
In XML you cannot nest HTML code, since HTML is another type of XML and the parser messes up and cannot recognize what tags are for Android and what are for HTML. But.. there's a trick. You can do that in this way:
<string name="exponent_key"><![CDATA[x<sup><small>%1$</small>/sup>]]></string>
So, with the string above in your XML, just use and you're fine:
getString(R.string.exponent_key, "otherStuff");
Note: if you need to show the HTML in a TextView, just use Html.fromHtml:
textView.setText(Html.fromHtml(getString(R.string.exponent_key, "otherStuff")));
Consider the effect you want to achieve.
you can do like this.
SpannableString ss = new SpannableString("2");
// set superscript
ss.setSpan(new SuperscriptSpan(),0,ss.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// set font size
ss.setSpan(new AbsoluteSizeSpan(12,true),0,ss.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append(ss);
I have a chinese string:
String t = "中文..."
Now I want to display it in some text view:
TextView tv = findViewById(id)
tv.setText(t, null);
But this is showing wrong characters... any idea how could I show it
correctly?
Well, I am asked to post the real code, actually the above code is almost the
real code:
suggestions = new ArrayList<String>();
suggestions.add("今日");
Then I get suggestion first element and assign to t:
tv.setText(t, null);
BTW, when I log it out, I also see wrong characters...
I remember writing strings in Japanese and getting the same problem. I didn't solve it, but have you tried using String resources? If you put Chinese characters in res/values/strings.xml instead of in your source code, they should display as expected.
You can access the Strings in your resource file like so:
String suggestion1 = context.getString(R.string.suggestion1);
To get an array of strings, like in your example, it's convenient to use string-arrays.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="suggestions">
<item>示唆1</item>
<item>示唆2</item>
<item>示唆3</item>
</string-array>
</resources>
And then load them using:
String[] suggestions = context.getResources().getStringArray(R.array.suggestions);
I have a string defined in MainActivity.java:
public String counter1 = String.valueOf(e.getCount());
I would like to use this string in activity_main.xml as:
android:text="#string/counter1"
As you can tell I am very new to this so basic steps would be appreciated.
Thanks
Short answer: you cannot. The resources you set in your xml layouts must be statically defined in resources files, like.-
<string name="counter1">COUNTER VALUE</string>
To dynamically define new strings, you must set them programmatically.-
TextView textView = (TextView) findViewById(R.id.textViewId);
textView.setText(counter1);
What do you want to do if you could get this to work? If you want to set text dynamically, you can use setText:
yourTextView.setText(counter1);
If you want to set something programmatically which you can... you should use
txtview.setText(yourString);
if you want to set a String in XML then you either set it in the XML like you did
android:text="Exercise Name"
or using strings which then you can use programmatically as well
in strings:
<string name="Delete">Delete</string>
in code you can call it with R.string.Delete or getString(R.string.app_name);
of course you can also set from strings in XML with #string...
by the sound of what you are trying to do, you want the TextView to change while program is running so the first option would suit you well
you can not,if you want to acheive this kind of solution:
create string in strings.xml
<string name="counter1">value of counter</string>
and if you want to set the text of counter dynamically then in you activity:
TextView tView = (TextView) findViewById(R.id.textViewId);
tView.setText(counter1);
you should follow this,because all static strings shoul always be defined in strings.xml
I could not find an answer to this question anywhere so I have posted it.
Let's say I have multiple resource strings, like:
<string name="hello">Hello World, MainScreen!</string>
<string name="app_name">My Title</string>
And I reference them in my code like so:
R.values.hello
R.values.app_name
But how can I access them like this?
String test = "hello";
String value = R.values.test;
I am trying to do something like this, but on a much larger scale. Or is there a different, but better way of doing it?
In an Activity, this will work...
String value = getString(R.values.hello);
R.values.hello is an integer used as a 'lookup' for the actual string itself. All resources are handled this way.
The correct way to obtain those values would be
String name =
CurrentClassName.this.getString(R.string.hello);
By using CurrentClassName.this you will assure that you can use it inside functions and nested classes.
I think this link might help you http://steven.bitsetters.com/2007/11/27/accessing-android-resources-by-name-at-runtime/
int id = getResources().getIdentifier("hello", "values", getPackageName());
Ok right , i asked how to create a random number from 1-100 for android and i came to this
TextView tv = new TextView(this);
int random = (int)Math.ceil(Math.random()*101);
tv.setText("Your Number Is..."+ random );
What this does is create the default kinda "hello world" style text view and says "Your Number Is.... [Then Random Number]
My problem is that i cant change the layout of this text , because it is not defined in XML, if someone could tell me how to change the style , or like make the random number into a string so i could use it for any Textview layout that would be great ..
Thanks :)
If by change the style you mean the text color, text size, and you want to change them programmatically, have a look at the setTextColor and setTextSize methods.
More info here
If you want more advanced formatting to set programmatically, see this link.
The below example demonstrates how to make your text bold and italic.
tv.setText("Your Number Is..."+ random, TextView.BufferType.SPANNABLE );
Spannable myText = (Spannable) tv.getText();
myText.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),0,myText.length(),0);
Edit:
Try the below for the android:textSize="100dp" and android:gravity="center" :
tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 100);
tv.setGravity(Gravity.CENTER);
Putting it into a string is easy.
String randomAsAString = Integer.toString(random)
You can then use the XML properties of the TextView to change its formatting, such as android:textSize="30dp" or android:textColor="#900".
By the way, if you're happy with the answer to your previous question, you should go back and mark an answer as "Accepted". That gives 'reputation' points to the person whose answer you accepted and closes the question so that people don't think you're still waiting for a better answer. You can read more about reputation in the FAQ.
Edit:
You can't reference the string entirely in xml while still giving it a random number. This is because the "#string/some_string" format only allows unchangeable strings. The execption to this is using parameters, e.g. setting the string as
<string name="random_number">The random number is %d</string>
Then you could call up that string using something like
yourTextView.setText(this.getString(R.string.random_number, random))
As for your other question about setting a background to a textView, that's also easy.
yourTextView.setBackgroundDrawable(R.drawable.....)
You should take advantage of Eclipse's autocomplete feature... it makes finding these commands a lot easier. For example, simply type the name of your TextView followed by a period, pause half a second for the list of options to come up, then "setB" and it should then filter the list to the three setBackground Drawable/Resource/Color options.
tv.setText(Html.fromHtml("Your number is: <b>" + random + "</b>"));
For basic HTML text-styling tags.
You could also do something like this.
Define your string in strings.xml like:
<string name="your_number_is">Your number is <xliff:g id="number">%s</xliff:g>.</string>
Create a TextView in a layout xml:
<TextView android:id="#+id/your_number_is"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/your_number_is"
android:gravity="center"
android:textSize="100dip"
/>
Then your code would look like:
TextView tv = (TextView) findViewById(R.id.your_number_is);
int random = (int)Math.ceil(Math.random()*101);
tv.setText(getString(R.string.your_number_is, random));
This will make it a lot easier when you later on would like to change your text or maybe localize your app.
if you have thead troubles use this:
new Thread(){
public void run(){
TextView v = (TextView)findViewById(R.id.mytext);
v.setText("TEST");
}
}.start();