android does not display "" inside TextView? - android

i am trying to display STRING my car name is "abc" in textview.
it displays &qoute;abc&qoute; after running application.
i have tried decoding into " and then assigning it to TextView but it always convert " to &qoute;
can any one guide me how to solve this issue?
any help would be appriciated.

Escape them; use " to have quotes in XML values.
For e.g. try: android:text="" hi ""

I have done it escaping the "
Example:
<string name="pref_about_text">Estado del Tránsito versión \"%s\"\nCreado por:</string>

For me none of the solutions presented here earlier did not worked.
Sharing my method to do this:
<string name="filter">\"Filtruj\"</string>

Related

Why can't I put "x⁵" on a TextView but I can put "x⁴"?

Why can't I put "x⁵" on a TextView but I can put "x⁴"?
<string name="secante_instru">Enter the following polynomial coefficients \n Ax⁵ + Bx⁴ + Cx³ + Dx² + Ex + F</string>
5 - exists, but it is not visible. It just takes up space. may be a bug with the font. Try to use another. How to set custom font Hope its help.
UPDATE
Try this:
yourTextView.setText(Html.fromHtml("x<sup>5</sup>"));
Does this help?
http://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts
You could use unicode directly.
Example:
\u2075
Have you tried wrapping double quote around it.

how to set apostrophe in TextView programming

TextView.setText(String with ') , i wanna set a text to the text view but this text keep coming uncompleted in case it has Space or apostrophe
i have tried to use
String SomeString="MacDonald's";
or
SomeString="Fast Food";
and i tried the following
HTML.Fromhtml(SomeString).tostring()
and
SomeString.replace("'","\\\'")
but with no good result
the Result always
MacDon
Fast
any good ideas ?!
Make sure your apostrophe is ' and not special one like ‘
From android developer documentation
you can set apostrophe from XML
Single quote (')
Any of the following:
1. &apos;
2. \'
3. Enclose the entire string in double quotes ("This'll work", for example)
Sample
1. <string name="travelers_details">Traveler&apos;s Information</string>
2. <string name="travelers_details">Traveler\'s Information</string>
Apastrophe cannot be directly displayed. Instead of directly placing Apastrophe use the below when you encounter the apastrophe as below:
if (YourString.contains("'")) {
YourString= YourString.replaceAll("'", "\'");
}
This will help you
I believe, calling yourTextView.setText("MacDonald's"); will simply set MacDonald's inside your TextView. You don't need anything fancy to get it printed.

EdittText Android

I am using EditText control in android, and I want to know if there is any way get the defaul string of the control(I mean the one that is in String.xml).
I use this when i want to modify its string.
e.setText( e.getText().toString + "something").
Now, the problem is that sometimes i get unnecessary information like :
" Name: JhonName: JhonName: JhonName: JhonName: Jhon "
When I just wanna show:
" Name: Jhon "
If i didnt explain properly, let me know :)
e.getText().toString() is return the string of your EditText....
now for the first time when you execute your code ..the above method will not return anything.. that's why your EditText will like "Name: John"..ok
when second time this code executes... the above method will return "Name: John"
and after that you adding "Something" so that why it happens...
just replace you code with this...
e.setText("something")
You dont need to write editText.setText(editText.getText().toString()+"something"), this will definately add "something" in the previous string. You should only write editText.setText("Something"), if you want only to print "something".
I hope that you know about Tag and Hint attributes of edit text. You can set tag and hint in both the ways XML as well as Java. So set the default (prefix) value in hint, append that value whenever you are settext to edit text.
for ex.:
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Name : " />
e.setText(e.getHint() + "John");

R.string.value showing up as a number

For the below code I intended to get the system date and display it as per the formatting of the current locale, it's just that for the R.string.date. In emulator it always shows up as a long number (something like 821302314) instead of "Date: " which I has already externalized in the string.xml. Can anyone help to have a look why this is so?
final TextView mTimeText = (TextView)findViewById(R.id.mTimeText);
//get system date
Date date = new Date();
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTimeText.setText(R.string.date + " " + dateFormat.format(date));
layout.xml
<TextView
android:id="#+id/mTimeText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/date"
/>
strings.xml
<string name="date">Date:</string>
R.string.date is indeed an int, you're missing the call to getText() or getString():
mTimeText.setText(getText(R.string.date) + " " + dateFormat.format(date));
Even better, don't build the string in your code, but use a template with getString(int resId, Object... formatArgs):
mTimeText.setText(getString(R.string.date, dateFormat.format(date)));
and in your string.xml:
<string name="date">Date: %s</string>
Yes, you will get the ID of the String if you use R.string.date.
As stated in the docs
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.
Example:
this.getString(R.string.date);
Read about it here: getString
To get string value from xml, you should call this.getString(R.id.nameOfString). In your case this would be mTimeText.setText(this.getString(R.string.date) + " " + dateFormat.format(date));
To override all "R.string.*" to "getString(R.string.)"* i wrote a little regex.
This regex also ignores the strings who already have a "getString" in front.
((?!getString\() R\.string\.[a-zA-Z1-9_]+)
You just have to press Strg+Shift+R in Android Studio to open the replace terminal and insert the regex above as "Find" and as "Replacement" the regex below.
getString\( $1 \)
Don't forget to set "regular expression" checkbox.
For me this worked perfectly. But the "Find Regex" got one problem it only finds R.string when it starts with a whitespace. I don't know how to solve this because if i delete the whitespace ill find also the R.string that allready have the "getString".
May some can help to improve the regex or has a better way to achieve this.

How to keep the spaces at the end and/or at the beginning of a String?

I have to concatenate these two strings from my resource/value files:
<string name="Toast_Memory_GameWon_part1">you found ALL PAIRS ! on </string>
<string name="Toast_Memory_GameWon_part2"> flips !</string>
I do it this way :
String message_all_pairs_found = getString(R.string.Toast_Memory_GameWon_part1)+total_flips+getString(R.string.Toast_Memory_GameWon_part2);
Toast.makeText(this, message_all_pairs_found, 1000).show();
But the spaces at the end of the first string and at the beginning of the second string
have disappeared (when the Toast is shown) ...
What should I do ?
I guess the answer is somewhere here in this documentation link
or is it something like using &amp ; for the "&" character ??
Even if you use string formatting, sometimes you still need white spaces at the beginning or the end of your string. For these cases, neither escaping with \, nor xml:space attribute helps. You must use HTML entity   for a whitespace.
Use   for non-breakable whitespace.
Use for regular space.
I ran into the same issue. I wanted to leave a blank at the end of a resource string representing an on-screen field name.
I found a solution on this issue report : https://github.com/iBotPeaches/Apktool/issues/124
This is the same idea that Duessi suggests. Insert \u0020 directly in the XML for a blank you would like to preserve.
Example :
<string name="your_id">Score :\u0020</string>
The replacement is done at build time, therefore it will not affect the performance of your game.
This documentation suggests quoting will work:
<string name="my_str_spaces">" Before and after? "</string>
I just use the UTF code for space "\u0020" in the strings.xml file.
<string name="some_string">\u0020The name of my string.\u0020\u0020</string>
works great. (Android loves UTF codes)
This question may be old, but as of now the easiest way to do it is to add quotation marks.
For example:
<string name="Toast_Memory_GameWon_part1">"you found ALL PAIRS ! on "</string>
<string name="Toast_Memory_GameWon_part2">" flips !"</string>
There is possible to space with different widths:
<string name="space_demo">| | | ||</string>
| SPACE | THIN SPACE | HAIR SPACE | no space |
Visualisation:
use "" with the string resource value.
Example :
<string>"value with spaces"</string>
OR
use \u0020 code for spaces.
If you really want to do it the way you were doing then I think you have to tell it that the whitespace is relevant by escaping it:
<string name="Toast_Memory_GameWon_part1">you found ALL PAIRS ! on\ </string>
<string name="Toast_Memory_GameWon_part2">\ flips !</string>
However, I'd use string formatting for this. Something like the following:
<string name="Toast_Memory_GameWon">you found ALL PAIRS ! on %d flips !</string>
then
String message_all_pairs_found = String.format(getString(R.string.Toast_Memory_GameWon), total_flips);
Working well
I'm using \u0020
<string name="hi"> Hi \u0020 </string>
<string name="ten"> \u0020 out of 10 </string>
<string name="youHaveScored">\u0020 you have Scored \u0020</string>
Java file
String finalScore = getString(R.string.hi) +name+ getString(R.string.youHaveScored)+score+ getString(R.string.ten);
Toast.makeText(getApplicationContext(),finalScore,Toast.LENGTH_LONG).show();
Screenshot
here Image of Showing Working of this code
All answers here did not work for me. Instead, to add a space at the end of a string in XML i did this
<string name="more_store">more store<b> </b> </string>
An argument can be made for adding the space programmatically. Since these cases will be often used in concatenations, I decided to stop the madness and just do the old + " " +. These will make sense in most European languages, I would gather.
I've no idea about Android in particular, but this looks like the usual XML whitespace handling - leading and trailing whitespace within an element is generally considered insignificant and removed. Try xml:space:
<string name="Toast_Memory_GameWon_part1" xml:space="preserve">you found ALL PAIRS ! on </string>
<string name="Toast_Memory_GameWon_part2" xml:space="preserve"> flips !</string>
This may not actually answer the question (How to keep whitespaces in XML) but it may solve the underlying problem more gracefully.
Instead of relying only on the XML resources, concatenate using format strings.
So first remove the whitespaces
<string name="Toast_Memory_GameWon_part1">you found ALL PAIRS ! on</string>
<string name="Toast_Memory_GameWon_part2">flips !</string>
And then build your string differently:
String message_all_pairs_found =
String.format(Locale.getDefault(),
"%s %d %s",
getString(R.string.Toast_Memory_GameWon_part1),
total_flips,
getString(R.string.Toast_Memory_GameWon_part2);
Toast.makeText(this, message_all_pairs_found, 1000).show();
There is also the solution of using CDATA. Example:
<string name="test"><![CDATA[Hello world]]></string>
But in general I think \u0020 is good enough.
If you need the space for the purpose of later concatenating it with other strings, then you can use the string formatting approach of adding arguments to your string definition:
<string name="error_">Error: %s</string>
Then for format the string (eg if you have an error returned by the server, otherwise use getString(R.string.string_resource_example)):
String message = context.getString(R.string.error_, "Server error message here")
Which results in:
Error: Server error message here
It does not work with xml:space="preserve"
so I did it the quickest way =>
I simply added a +" "+ where I needed it ...
String message_all_pairs_found = getString(R.string.Toast_Memory_GameWon_part1)+" "+total_flips+" "+getString(R.string.Toast_Memory_GameWon_part2);

Categories

Resources