Android Vertical Text view - display character one below another as show below - android

I want to display the text in vertical text view (i.e. characters should be placed one below another as below)
a
b
c
d
I wish to do this in a single text view.
We can turn the entire text by using android:toDegrees="-90" or "90" but i want to display as shown above which is not achieved with android:toDegress tag.
Appreciate your help!
UPDATE
I will get the String from another app through AIDL which is subjected to change as per the sender, in addition, he will send me a flag to display the text in a vertical or horizontal direction. Based on the flag I should display accordingly. I have no problem with a horizontal view as it is the default one but to display it in vertical(as shown above) I need help.

If String is Dynamic then you can add \n after each character.
private String build(String str){
if(!TextUtils.isEmpty(str)) {
StringBuilder builder = new StringBuilder();
builder.append(str.charAt(0));
for (int i = 1; i < str.length(); i++) {
builder.append("\n"+str.charAt(i));
}
return builder.toString();
}
return "";
}
textView.setText(build("abcd"));
It will show vertically. This way text can go out of Bound so you should make TextView scrollable in this case.

You can add line break to your string with either \n or <br>
With \n:
<string name="sample_string">a\nb\nc\nd</string>
textView.setText(R.string.sample_string)
With <br>:
<string name="sample_string"><![CDATA[a<br />b<br />c<br />d]]></string>
textView.setText(Html.fromHtml(context.getString(R.string.sample_string))

Related

EditText force textDirection to ltr doesn't work, can any body advice?

I have an edit text field, which I need to format every word to hashtag
Example if I type ABC it should be formatted to #ABC
I’ve implemented it with input filter but I’m not able to manage it when user enter Arabic text, #abc# مرحبا #مرحبا appears like this, I assume I can solve the problem if I can force the EditText android:textDirection="ltr", but even after this change android EditText behaving rtl when input is Arabic text.
So my question is, how to force an EditText to behave ltr even for rtl text inputs like Arabic?
To make for both direction automatically use below codes
android:layout_gravity="start"
android:textAlignment="viewStart"
Please refer it on the following site for more description Very nice blog by: Sven Bendel or his github page
/**
* Reformat the String as LTR to ensure that it is laid out from
* left to right, but it doesn't affect overall layouting of
* TextViews etc..
*/
public static String makeLtr ( String string ) {
if (checkRtl(string)) {
/* prepend the string with an LTR control sign (so
that Android's RTL check returns false) and an RTL
control sign (so that the string itself is printed in
RTL) and append an LTR control sign (so that if we
append another String it is laid out LTR). */
return "\u200E" + "\u200F" + string + "\u200E";
} else {
return string;
}
}
/**
* Check if the given String is probably written in RTL by
* checking if the very first character is within the range of
* RTL unicode characters.
*/
public static boolean checkRtl ( String string ) {
if (TextUtils.isEmpty(string)) {
return false;
}
char c = string.charAt(0);
return c >= 0x590 && c <= 0x6ff;
}

Html Styling in textview goes wrong Android

I am selecting a part of the TextView and on click of a "highlight" button, I am sending the start and the end index of selection to the database. Then I am loading all the start and end indexes from db and changing the color of text between them.
The problem is after once or twice, the app is changing the color of text that is not in selection.. and the selected part remains unchanged.
MY CODE:
When user selects and presses the highlight button
int i=contentText.getSelectionStart();
int j=contentText.getSelectionEnd();
db.insertHiglightIndex(String.valueOf(i),String.valueOf(j));
setHighlightedText();
The setHighlightedText() method..
String fullText=contentText.getText().toString();
for(int i=0; i<db.getAllStartIndex().size();i++){
String a=fullText.substring(Integer.parseInt(db.getAllStartIndex().get(i)),Integer.parseInt(db.getAllEndIndex().get(i)));
fullText = fullText.replace(a, "<font color='red'>"+a+"</font>");
}
contentText.setText(Html.fromHtml(fullText), TextView.BufferType.SPANNABLE);
MY SCREENSHOTS.
The selection:
The Result:
Clearly the selected area is from "Garrick" to "Bart", and the result is from "entity" to "2012"
I am not able to understand why is this happening. I think there is some problem with this <font color='red'>"+a+"</font> line.
Thank you
It got wrong indexed because There is already added <font color='red'> in the beginning, So that in second time This tag is also counted as a part of string, So I suggest creating a new temporary String, assign same text to the String but after replacing the previous font tag it held. Use this syntax to remove previous font tag from originalString
String tempString = originalString.replaceAll("[<](/)?font[^>]*[>]", "");
After that work with only tempString. That means again add every previous font tag you have to tempString and set that text.
In next time again do the same first remove all font tag and again add all of them back in tempString as well as current selection using same loop you are using currently.
You have wrong indexes because you are modifying the fullText content within the loop.
Taking a look at this example you can figure it:
final TextView tv = (TextView) findViewById(R.id.textView);
tv.setText( "abcdefghijklmnopqrstuvwxyz0123456789");
String fullText= tv.getText().toString();
// your first iteration
String a = fullText.substring(1,3);
// a contains "ab"
fullText = fullText.replace(a, "<font color='red'>"+a+"</font>");
After the first iteration full text contains now
a<font color='red'>bc</font>defghijklmnopqrstuvwxyz0123456789"
Then the substring() in the second iteration won't returns the substring base on your initial content.
If you want to be able to have multiple substrings colored in red you can try this:
String fullText = contentText.getText().toString();
StringBuilder result = new StringBuilder();
for(int i=0; i < db.getAllStartIndex().size(); i++){
fullText = applyFont(result, fullText, Integer.parseInt(db.getAllStartIndex().get(i)), Integer.parseInt(db.getAllEndIndex().get(i)));
}
// Add here the remaining content
result.append(fullText);
contentText.setText(Html.fromHtml(result.toString()), TextView.BufferType.SPANNABLE);
private String applyFont(StringBuilder result, String source, int from, int to){
result.append(source.substring(0, from));
result.append("<font color='red'>");
result.append(source.substring(from, to));
result.append("</font>");
return source.substring(to, source.length());
}

TextView with mixed languages

I have a TextView with 2 lines. first line rtl language (let's say hebrew), second line is ltr language (let's say english)
The View result is something like:
אחת שתיים שלוש
one two three
what i want: align rtl in that case
אחת שתיים שלוש
one two three
I've tried using setTextDirection() with TEXT_DIRECTION_FIRST_STRONG
but alas the results were the same. Also tried TEXT_ANY_RTL without success
myTextView.setTextDirection(View.TEXT_DIRECTION_FIRST_STRONG);
if i'm using TEXT_DIRECTION_RTL it's working as expected but this is not really a solution because most of the time the TextView will contain only one language.
Is this solvable?
--- UPDATE ---
How i'm populating the TextView
SpannableStringBuilder ssb = new SpannableStringBuilder(titleText);
int end = titlText.length();
ssb.append("\n").append(otheText);
ssb.setSpan(new AbsoluteSizeSpan(size), end, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ssb);
Why not just use two TextViews?
I've managed to solve this problem using Character.getDirectionality.
The first char that is a directional char will signify the TextView direction
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static int getTextDirection(String text) {
final int length = text.length();
for (int i = 0; i < length; i++) {
final char c = text.charAt(i);
final byte directionality = Character.getDirectionality(c);
if(directionality == Character.DIRECTIONALITY_LEFT_TO_RIGHT){
return View.TEXT_DIRECTION_LTR;
}
else if(directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT){
return View.TEXT_DIRECTION_RTL;
}
}
return View.TEXT_DIRECTION_ANY_RTL;
}
and then:
textView.setTextDirection(textDirection);
I Strongly believe that TEXT_DIRECTION_FIRST_STRONG is supposed to do the exact same thing according to the docs. sadly it's not the case.
I'm not accepting my answer in hope that someone will suggest better solution
What About TEXT_DIRECTION_ANY_RTL
This text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains any strong RTL character, otherwise it is LTR if it contains any strong LTR characters. If there are neither, the paragraph direction is the view's resolved layout direction.

Q - Display Text like Command Prompt in Android

Is there a way to display text strings on the screen in a similar look to command prompt for windows, i.e. text fills the bottom-most line and stays on the screen as new lines are added below it? I feel like Canvas.drawTextOnPath can be used for this?
There's probably a bunch of ways to do this, depending on what you're using it for. Here's what I would do:
Add a LinearLayout with android:gravity="bottom", and add your TextView to that layout.
When you add text to it, use something like:
public ArrayList<String> text = new ArrayList<String>();
int lines = 20;
TextView textField = (TextView)findViewById(R.id.textview);
private String concatLines(){
String txt = "";
for(String s: text){
txt += s+"\n";
}
return txt.substring(0, txt.length()-1);
}
private void appendLine(String line){
text.add(line);
if(text.size() > lines){
text.remove(0);
}
textField.setText(concatLines());
}
appendLine("> A new line");
The android:gravity="bottom" should keep your text field aligned at the bottom of the layout. Make sure the TextView's height is wrap_content.

In Android, how do I get the text on a button to descend vertically?

I am trying to get the text within my buttons to descend vertically like below. I'd prefer to have this happen within the main.xml file.
M
Y
T
E
X
T
I don't want the text to go sideways.
Use "\n" between the letters, that tells the compiler to add a return character between the characters.
A little helper function for this (i have no better idea, then adding the newlines):
public static final String rotateString(String str)
{
final StringBuilder sb = new StringBuilder();
for(int i=0; i<str.length(); i++)
sb.append(str.charAt(i)).append(i==str.length()-1? "" :'\n');
return sb.toString();
}
And then if you want to use it:
yourTextView.setText(rotateString("Hello Text"));
At the moment i could only test it on an emulator with 4.0.3, but it seems to work nicely.

Categories

Resources