I am creating one application in which I am searching for a Perticular word in text view.I have one edit text,one text view and one button,when I am entering any word in edit text and clicking on button it gives me the line position and position of word in that line from entire text file...I have append text file's contain in text view...now my question is can I highlight that all word which is there in text view entered by edit text.?if I can than please tell me how to do it..if any one have idea of it?
You can also do it by using a Spannable, which is convenient as you know the position of the word:
SpannableString res = new SpannableString(entireString);
res.setSpan(new ForegroundColorSpan(color), start, end, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
Where entireString is the string in which the word to highlight exists, color is the color you want the hightlighted text to have, start is the position of the word and end is where the word ends (start+word.length()).
The SpannableString res can then be applied to your textview like a regular string:
textView.setText(res);
Note: If you want the background of the text to get a color rather than the text itself, use a BackgroundColorSpan instead of a ForegroundColorSpan.
Edit:
In your case it would be something like this (you will have to save the value of linecount and indexfound for when you read the entire text):
for(String test="", int currentLine=0; test!=null; test=br2.readLine(), currentLine++){
if(currentLine==linecount){
SpannableString res = new SpannableString(test);
res.setSpan(new ForegroundColorSpan(0xFFFF0000), indexfound, indexfound+textword.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
}
tv.append("\n"+" "+test);
}
Yes You can highlight some portion of the text view by writing HTML
String styledText = "This is <font color='red'>simple</font>.";
textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);
use Spannable to set text in spannable you can setSpan by which you can highlight the text you want
string ="<font color='#ff0000' > <b>hello</b> </font>"
textview.setText(Html.fromHtml(string))
http://developer.android.com/reference/android/text/Html.html
Related
In my android app with Kotlin, I created a layout in which there's a TextView that shows some text. For the text I have an item in strings.xml where I want to change the color of part of this Text, I tried the following code :
<string name="description">the product is <font fgcolor="green"> free </font></string>
But, The color didn't change.
I just want to change the color of "free" to green, can someone explain how I can achieve this?
Use <font color="#008000">free</font> instead. According to the documentation, the correct attribute name is color and it only supports hex codes.
Ben P.'s awesome answer should satisfy your use case. However, I want to present to you another way you can achieve this.
You can use SpannableString to achieve the same effect. With SpannableString, you can set several behaviours (color, font-weight, font-size, click-behaviour, etc) to any part of your String.
For the string in your question, you can do something like this:
// the textview you want to set your coloured text to
TextView textView = (TextView) findViewById(R.id.myTextView);
// declare the string you want to span as a Spannable
Spannable wordtoSpan = new SpannableString("the product is free");
// set the colour span
wordtoSpan.setSpan(new ForegroundColorSpan(Color.GREEN), 15, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// set the text to your TextView
textView.setText(wordtoSpan);
I want to make something like
wow its cool app
in android. so that users can see a bold text in both TextView and EditText like WhatsApp. I don't know what to do. I searched and found something in which we can use HTML tags but some of the users don't know how to use that. Is there a way so that it is easy for users to make some part of text bold and remaining will normal.
Here is the image which will clear more
in this image, users can bold some part of text by enclosing that in *'s.
For you to make text bold? Place it in a StyleSpan with a bold style to it. For the user to make it bold? You'll have to provide some UI for them to do that, that isn't built in.
You have to use HTML code and set to textview or edittextview as following..
TextView textView = (TextView) findViewById(R.id.textView);
String first = "Hello what's up ";
String next = "<B>some bold text </B>";
textView.setText(Html.fromHtml(first + next));
if you want to add bold text after '' character then find that character from textview or edittextview and apply tag after '' this character and set text into textview and edittextview
This may be a ridiculous question ever, but still I want to reduce my effort of creating a lot of text views.
I have text view that contains Name:Value format [Suppose Name:Android] In this case all the attributes for the text view will be same except the color and also the texts are side by side.
In real implementation I have to create two text views, and suppose if I have around 10-15 such pairs, the number of text views will be 20-30 respectively.
So how can I set different color for name and value independently??
Use something like
String str = "<font color=#900000 >Name:</font> <font color=#0000FF>Android</font>";
textview.setText(Html.fromHtml(str));
You can set the text to use html tags to set colors inside the text
String formattedText = "<font color=\"#ff0000\">red</font> <font color=\"#00ff00\">green</font>";
Spanned result = Html.fromHtml(formattedText);
view.setText(result);
Or, use spannable like in Set color of TextView span in Android
I have a edittext and some buttons like bold italics underline bullets. Now when i select some text and press any button then using spannable string i am applying that particular span to that portion of the edittext like bold italics underline etc.
I am trying to put Bulletspan in edittext. Here is the code i use to put bullet span:-
s.setSpan(new BulletSpan(), text.getSelectionStart(), text.getSelectionEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
where text is my reference to the Edittext.
Now the issue is that I want that my bulleted text should come at some leading margin space or indentation from start like it comes in any text editor like MS Word.
Just tell BulletSpan how much leading space you want. This example would give you 16dp leading space on the first and subsequent lines (like in the text editors you mentioned).
new BulletSpan(16);
How to display a Textview with highlighting a specific text like first letter i need bold in style
textview.setText(Html.fromHtml( < b>A< /b>ndroid));