Unknown character in textview text - android

I have set text in text view
TextView text = view.FindViewById<TextView>(Resource.Id.details);
text.SetText(_text, TextView.BufferType.Normal);
it sets unknown characters when displays
how to write unicode characters correct ?
must be text like this

Try this,
text.setText(Html.fromHtml(_text));

Define your characters/string, which you want to display, in res/values/strings.xml. Load it in textview like
strings.xml
<string name="unicodechar">my unicode char is here</string>
in Java:
String _text = context.getResources().getString(R.string.unicodechar);
textview.settext(_text);

Try using a method like this to convert the string:
public String encodeUTF(String str) throws UnsupportedEncodingException {
byte[] utf8Bytes = str.getBytes("UTF-8");
String encodedStr = new String(utf8Bytes, "UTF-8");
return encodedStr;
}
Normally it will either need to be UTF-8 or ISO 8859-1

Use a font like DejaVuSans.ttf. Copy this in assets folder. Then set the typeface of the text view as below. After this try setting the text.
tv=(TextView)findViewById(R.id.textView1);
Typeface font= Typeface.createFromAsset(getAssets(), "DejaVuSans.ttf");
tv.setTypeface(font);

Related

InputType doesn't work when text is set with setText

I have set android:inputType="text|textCapWords" to my EditText. When I type something in the field, the first letter is correctly capitalised, but if I set a full capitalised (or full lowercase) text using the setText() method, the text remains fully capitalised (or in lowercase).
How can I use setText for the text to comply with the input type?
As #Amarok suggests
This is expected behavior. You have to format the text yourself before
using the setText() method
But if you want to format your text just like android:inputType="text|textCapWords you can use the following method:
public static String modifiedLowerCase(String str){
String[] strArray = str.split(" ");
StringBuilder builder = new StringBuilder();
for (String s : strArray) {
String cap = s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();
builder.append(cap + " ");
}
return builder.toString();
}
and use it like
textView.setText(modifiedLowerCase("hEllo worLd"));
It will convert it like :
Hello World
You can check for your editText Type constants, and add simple .toUpperCase() to the text you're adding, like this:
if(mEditText.getInputType() == TYPE_TEXT_FLAG_CAP_CHARACTERS+ TYPE_CLASS_TEXT)
mEditText.setText("Some text".toUpperCase());
else
mEditText.setText("some text");
More for input types constants can be found here
Use this
EditText.getText().clear();
EditText.getText().append("test");
rather than
EditText.setText("test");
so that the text that has been set follows the input type behavior

Put emoji code in a string

I am using this emoji library
https://github.com/hani-momanii/SuperNova-Emoji
This library has a custom textview which can render emojis. How do I set text of that textview so it displays emojis ?
For example I tried this and it did not work :
String happy = " Feeling happy U+1F601 ";
emojitextview.setText(happy);
Switch out the 'U+' for '0x':
int unicode = 0x1F601;
String happy = "Feeling happy " + getEmojiByUnicode(unicode);
And put it through a helper function:
public String getEmojiByUnicode(int unicode){
return new String(Character.toChars(unicode));
}
p.s. if it still doesn't work, you may have to set the textView to use a typeface that supports emoji characters
from:
how set emoji by unicode in android textview

Unicode to text in java

How can I convert a Unicode string like
"\u0646\u0627\u0645"
to a human-readable string that I can put in a TextView?
No need to do anything more than this
TextView text = (TextView) findViewById(R.id.yourTextView);
text.setText("\u0646\u0627\u0645");

replace a character from only font style bold

I'm new to android and I'm trying to replace a character from bold style at a specific character in a arraylist string. What I'm doing is:
String myName = "76492";
This is my search string and i want to search this string in to the arraylist then i want to bold those character when getting on the arraylist.Arraylist string is 78758,3660,56798,6514,90679.
TextView txtf = (TextView)findViewById(R.id.textView4);
TextView txtS = (TextView) findViewById(R.id.textView5);
TextView txtT = (TextView)findViewById(R.id.textView6);
TextView txt1 = (TextView) findViewById(R.id.textView9);
TextView txt2 = (TextView) findViewById(R.id.textView13);
String str= myName.replaceAll("6", "<b>6</b>");
Spanned text = Html.fromHtml(str);
txtf.setText(text);
txtS.setText(text);
txtT.setText(text);
txt1.setText(text);
txt2.setText(text);
But this is replace all Textview String by the 76492 string with match charater bold style. 1879 search string this is replace old string but i donot want this type replace .iI want only specific position character change in bold style
<style name="boldText">
<item name="android:textStyle">bold</item>
</style>
Paste this code snippet in your style.xml which is in valuse folder under layout.
txtf.setTextAppearance(this, R.style.boldText);
This line of code is for your text view appear as a bold.
Use spans. Then you can specify parts of your string and assign any format or color you want. Have a look at this.
Edit: For clarification: With spans you don't replace the characters. Use some string method to find the position of the characters you want to change to bold and create a span on these positions and assign bold format to that span.

Arabic text appearing reverse when Arabic text comes with digit

I need to print the below string which is in Arabic on TextView in Android. It is printing good but when the Arabic text and digits falls in a same string, Android put the digit at end!
here is my code
String str = "مقر 44";
TextView textView = (TextView)findViewById(R.id.test);
textView.setText(str);
Here is the output
you can try with this workaround
String str = "44 "+"مقر ";
TextView textView = (TextView)findViewById(R.id.txt_title);
textView.setText(str);
its not Android who put digit at the end, its because of Arabic writing standard

Categories

Resources