android: show font icon as a sequence of number string - android

I want to put a Font-Awesome icon inside textview text. After I set the text, android shows me a string sequence instead of font-icon.
My code is:
String formatedSection = formatedSection + sections.get(i).getContent() +getResources().getString(R.string.icon_ref);
I define icon_ref in string.xml as below:
<string name="icon_ref"></string>
I followed these instructions to add font-icon. What am I doing wrong?

If you did step 5 in that short guid your problem seem to forget that there is a diff between java and XML.
In XML you use XML escape (&...;), in Java you'll probably have to use Java escape (\u...). – Biffen May 20 at 10:10
Try to hard code the string into "\uf075" and it would work like a charm.

Related

Android Charsequence - replacing text

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

How to edit arabic string properly?

<string name="message">هذا المجلد يحتويى على %1$s ملفات. الرجاء التأكد قبل الحذف. الملفات المحذوفة غير قابلة للإسترجاع.</string>
I wanna put "%1$s" in an arabic string, but as you can see here, word, notepad++, utraedit, all failed to get the right string. how you guys edit arabic string?
In android Studio 2, RTL support is not turned on by default,
Configure it Manually:
1. In your Computer, go to the [android-studio2.0]/bins/idea.properties
2. add editor.new.rendering=true to the end of idea.properties
3. restart your android studio.
This is a source of frustration when editing mixed-direction text. What counts is the logical order of the text, not how any of the editors display it. When you finally format the string at run time in the app, the %1$s will be replaced by whatever string you pass to the formatting method. The only thing that matters is how the string will be rendered after the substitution.
The easiest thing to do is to write the message without the %1$s, then position the insertion caret, paste in the format code, and simply ignore how the editors screw up the bidi analysis. (The screw-up is because the editors are using a left-to-right base level. In some editors, you can set the base flow to right-to-left, but then the xml markup ends up being unreadable.)
When I work with RTL text and I need to put a place holder(%1$s) or LTR words, I just write it in MS Word and copy to the IDE.
It works for me in Eclipse and Android Studio.
All you need to check is that the components that displays that string like TextView has the right gravity.
You can try in Activity..
String formatedString = String.format("%1$s", getString(R.string.your_string));

Copy string to strings.xml

Everybody knows that if we have:
ekran3.setText("VAT Tax:");
We may (or even we SHOULD) convert it to:
ekran3.setText(getString(R.string.kwotaVat));
and add in strings.xml:
<string name="kwotaVat">VAT Tax:</string>
But is there some kind of trick to do it automatically? For example by clicking RMB on text and selecting some option? It would be nice to know it in fact it will save us a lot of time than while we're doing it manually.
If you are using Eclipse you may extract the string directly into the strings.xml file by placing the mouse within the string and hitting Ctrl + 1. It will bring up the dialog as followed and you may select "Extract String". You then give it a name (Ex: kwotaVat) and you're done.
hey you do not need to use getString() to convert it to string the values xml file is already having data in string form so you just need to use the following code to set the string
ekran3.setText(R.string.kwotaVat);
where ekran3 is the object of your text view
and kwotaVat is the id of your value string
for more detail od android codes have look here http://grabcodes.blogspot.com/

How to use escape characters in XML

How can I use escape characters in XML?
The situation is, I am a new android developer, and I need a string which need to be printed in 2 lines (other wise no space). This string is in string.xml file. I need to use /n line break character to break the string, but I don't know how to do it in XML file. Please help.
See reference http://developer.android.com/guide/topics/resources/string-resource.html#String
line break symbol is \n
/n - is wrong.
and you can write it in xml like it is.
Example:
<string name="multiline_text">line 1.\nline2.</string>
I'm not familiar with Android development but have you checked out CDATA? Refer to this link.
Basically, you wrap your string value like so:
<![CDATA[string value]]>
You can use double quotes:
<string name="mystring">"One line\nAnother line"</string>
You can use the character entity
to indicate a linefeed.

How to add a line break in an Android TextView?

I am trying to add a line break in the TextView.
I tried suggested \n but that does nothing. Here is how I set my texts.
TextView txtSubTitle = (TextView)findViewById(r.id.txtSubTitle);
txtSubTitle.setText(Html.fromHtml(getResources().getString(R.string.sample_string)));
This is my String: <string name="sample_string">some test line 1 \n some test line 2</string>
It should show like so:
some test line 1
some test line 2
But it shows like so: some test line 1 some test line 2.
Am I missing something?
\n works for me, like this:
<TextView android:text="First line\nNext line"
ok figured it out:
<string name="sample_string"><![CDATA[some test line 1 <br />some test line 2]]></string>
so wrap in CDATA is necessary and breaks added inside as html tags
Android version 1.6 does not recognize \r\n.
Instead, use: System.getProperty("line.separator")
String s = "Line 1"
+ System.getProperty("line.separator")
+ "Line 2"
+ System.getProperty("line.separator");
Linebreaks (\n) only work if you put your string resource value in quotes like this:
<string name="sample_string">"some test line 1 \n some test line 2"</string>
It won't do linebreaks if you put it without quotes like this:
<string name="sample_string">some test line 1 \n some test line 2</string>
yes, it's that easy.
Tried all the above, did some research of my own resulting in the following solution for rendering linefeed escape chars:
string = string.replace("\\\n", System.getProperty("line.separator"));
Using the replace method you need to filter escaped linefeeds (e.g. '\\n')
Only then each instance of line feed '\n' escape chars gets rendered into the actual linefeed
For this example I used a Google Apps Scripting noSQL database (ScriptDb) with JSON formatted data.
Cheers :D
There are two ways around this.
If you use your string as a raw string, you need to use the newline
character. If you use it as html, e.g. by parsing it with Html.fromString,
the second variant is better.
1) Newline character \n
<string name="sample">This\nis a sample</string>
2) Html newline tag <br> or <br />
<string name="sample">This<br>is a sample</string>
This worked for me
android:text="First \n Second"
This worked for me, maybe someone will find out this helpful:
TextView textField = (TextView) findViewById(R.id.textview1);
textField.setText("First line of text" + System.getProperty("line.separator") + "Linija 2");
If you're using XML to declare your TextView use android:singleLine = "false" or in Java, use txtSubTitle.setSingleLine(false);
Used Android Studio 0.8.9. The only way worked for me is using \n.
Neither wrapping with CDATA nor <br> or <br /> worked.
I use the following:
YOUR_TEXTVIEW.setText("Got some text \n another line");
very easy : use "\n"
String aString1 = "abcd";
String aString2 = "1234";
mSomeTextView.setText(aString1 + "\n" + aString2);
\n corresponds to ASCII char 0xA, which is 'LF' or line feed
\r corresponds to ASCII char 0xD, which is 'CR' or carriage return
this dates back from the very first typewriters, where you could choose to do only a line feed (and type just a line lower), or a line feed + carriage return (which also moves to the beginning of a line)
on Android / java the \n corresponds to a carriage return + line feed, as you would otherwise just 'overwrite' the same line
As I know in the previous version of android studio uses separate lines " \n " code. But new one (4.1.2) uses "<br/" to separate lines. For example -
Old one:
<string name="string_name">Sample text 1 \n Sample text 2 </string>
New one:
<string name="string_name">Sample text 1 <br/> Sample text 2 </string>
Also you can add "<br/>" instead of \n.
It's HTML escaped code for <br/>
And then you can add text to TexView:
articleTextView.setText(Html.fromHtml(textForTextView));
Try to double-check your localizations.
Possible, you trying to edit one file (localization), but actually program using another, just like in my case. The default system language is russian, while I trying to edit english localization.
In my case, working solution is to use "\n" as line separator:
<string name="string_one">line one.
\nline two;
\nline three.</string>
You could also use the String-Editor of Android Studio, it automatically generates line brakes and stuff like that...
As Html.fromHtml deprecated I simply I used this code to get String2 in next line.
textView.setText(fromHtml("String1 <br/> String2"));
.
#SuppressWarnings("deprecation")
public static Spanned fromHtml(String html){
Spanned result;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
result = Html.fromHtml(html,Html.FROM_HTML_MODE_LEGACY);
} else {
result = Html.fromHtml(html);
}
return result;
}
The most easy way to do it is to go to values/strings (in your resource folder)
Declare a string there:
<string name="example_string">Line 1\Line2\Line n</string>
And in your specific xml file just call the string like
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/example_string" />
I found another method:
Is necessary to add the "android:maxWidth="40dp"" attribute.
Of course, it may not work perfectly, but it gives a line break.
\n was not working for me. I was able to fix the issue by changing the xml to text and building the textview text property like below.
android:text="Line 1
Line 2
Line 3
DoubleSpace"
Hopefully This helps those who have said that \n did not work for them.
I'm reading my text from a file, so I took a slightly different approach, since adding \n to the file resulted in \n appearing in the text.
final TextView textView = (TextView) findViewById(R.id.warm_up_view);
StringBuilder sb = new StringBuilder();
Scanner scanner = new Scanner(getResources().openRawResource(R.raw.warm_up_file));
while (scanner.hasNextLine()) {
sb.append(scanner.nextLine());
sb.append("\n");
}
textView.setText(sb.toString());
In my case, I solved this problem by adding the following:
android:inputType="textMultiLine"
Maybe you are able to put the lf into the text, but it doesn't display? Make sure you have enough height for the control. For example:
Correct:
android:layout_height="wrap_content"
May be wrong:
android:layout_height="10dp"
I feel like a more complete answer is needed to describe how this works more thoroughly.
Firstly, if you need advanced formatting, check the manual on how to use HTML in string resources.
Then you can use <br/>, etc. However, this requires setting the text using code.
If it's just plain text, there are many ways to escape a newline character (LF) in static string resources.
Enclosing the string in double quotes
The cleanest way is to enclose the string in double quotes.
This will make it so whitespace is interpreted exactly as it appears, not collapsed.
Then you can simply use newline normally in this method (don't use indentation).
<string name="str1">"Line 1.
Line 2.
Line 3."</string>
Note that some characters require special escaping in this mode (such as \").
The escape sequences below also work in quoted mode.
When using a single-line in XML to represent multi-line strings
The most elegant way to escape the newline in XML is with its code point (10 or 0xA in hex) by using its XML/HTML entity
or
. This is the XML way to escape any character.
However, this seems to work only in quoted mode.
Another method is to simply use \n, though it negatively affects legibility, in my opinion (since it's not a special escape sequence in XML, Android Studio doesn't highlight it).
<string name="str1">"Line 1.
Line 2.
Line 3."</string>
<string name="str1">"Line 1.\nLine 2.\nLine 3."</string>
<string name="str1">Line 1.\nLine 2.\nLine 3.</string>
Do not include a newline or any whitespace after any of these escape sequences, since that will be interpreted as extra space.
I would recommend querying the line.separator property, and using that whenever you want to add a line break.
Here is some sample code:
TextView calloutContent = new TextView(getApplicationContext());
calloutContent.setTextColor(Color.BLACK);
calloutContent.setSingleLine(false);
calloutContent.setLines(2);
calloutContent.setText(" line 1" + System.getProperty ("line.separator")+" line2" );

Categories

Resources