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.
Related
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))
How to get a text from a multi line EditText line by line as it displayed on the screen?
For example, if you insert long text in EditText (without \n) it will be displayed in few lines. How to know where system insert line wrap?
It is possible if using EditText.getLayout():
String text = editText.getText().toString();
List<String> lines = new ArrayList<>();
for (int i = 0; i< editText.getLayout().getLinesCount(); i++) {
lines.add(text.substring(editText.getLayout().getLineStart(i),
editText.getLayout().getLineEnd(i));
}
Also EditText.getLayout() has methods for returning size of line:
getLineWidth()
getLineBounds()
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());
}
I have a news feed in my app. In a feed item I have a text view which may have url links and hashtags. I need the both the urls and the hashtags to be clickable which is implemented.
protected void setTextClickable(TextView txtView) {
L.i(getClass().getSimpleName(), "SET TEXT CLICKABLE ENTERED" + "LINES: " +String.valueOf(mTextViewLines));
String text = txtView.getText().toString();
ExpandableTextViewClickableSpan clickSpan = null;
ExpandableTextViewClickableSpan clickableSpan = null;
final SpannableString hashTagInText = new SpannableString(text);
String regexURL = "\\(?\\b((http|https)://|www[.])[-A-Za-z0-9+&##/%?=~_()|!:,.;]*[-A-Za-z0-9+&##/%=~_()|]";
String regexHashTag = "#([A-Za-z0-9_-]+)";
Matcher matcherURL = Pattern.compile(regexURL).matcher(hashTagInText);
Matcher matcherHashTag = Pattern.compile(regexHashTag).matcher(hashTagInText);
int color = view.getResources().getColor(R.color.tinted_green_colour);
while (matcherURL.find()) {
clickSpan = new ExpandableTextViewClickableSpan(currentFragment, false, color, hashTagInText,
matcherURL.start(), matcherURL.end(), MenuUtils.sURL_LINKID);
hashTagInText.setSpan(clickSpan, matcherURL.start(), matcherURL.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
while (matcherHashTag.find()) {
clickableSpan = new ExpandableTextViewClickableSpan(currentFragment, false, color, hashTagInText,
matcherHashTag.start(), matcherHashTag.end(), MenuUtils.sHASHTAG_LINKID);
hashTagInText.setSpan(clickableSpan, matcherHashTag.start(), matcherHashTag.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
However, there is also the requirement that if the message is longer than 4 lines then I ellipsis and show a view more/less option.
Since introducing the method to colour and set clickable the urls and hashtags (which works with spannables which then renders the ellipsis ineffective).
So now my ellipsis doesn't work. I came across this Spannable Ellipsis Issue on SO. However this requires a line count which I can't get unless it's done in a global layout listener.
Which I have done and successfully get the line count. The problem is however that the bind data method is called in which the spannable is applied and layout populated (where I want to apply my own ellipsis method) before the onGlobalLayout method is called, which then leaves me with 0 line count.
Is there a simple way to combine both ellipsis and spannable? Any help is appreciated.
I'm generating ImageButtons programmatically and I'm not able to change the size of the items using XML. So, I would like to do it programmatically too.
How can I do that?
My code :
String[] separated = result.split("%");
ImageButton [] txt =new ImageButton[20];
for(int i=0;i<txt.length;i++)
{
String getname = separated[i];
txt[i]=new ImageButton(hphotos.this);
Picasso.with(getBaseContext()).load("http://www.mywebsite.com/" + getname).into(txt[i]);
txt[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
scroll.addView(txt[i]);
Have you tried
yourView.setLayoutParams(new LayoutParams(width,height));
?