Do anyone know how to highlight a certain word in a TextView?
With highligt I mean to use an italic typeface for the word "Hua Hin" in the following string.
The sandy beaches of Hua Hin, popular resort town in Thailand.
Must I use a textView in another textView to acomplish this?
I set the text in the onPostExecute-method of AsyncTask:
protected void onPostExecute(Bitmap[] bitmap) {
for (int i = 0; i < nmbrOfImages; i++) {
imageView[i].setImageBitmap(bitmap[i]);
textView[i].setText(scrollText[i]);
textView[i].setVisibility(TextView.VISIBLE);
}
loadAlertDialog();
}
I have all my text in an xml-file
If only italic you want then you can go for
textView[i].setText(Html.fromHtml(<i>some part</i> some other text);
else you can go for Spnnable
Here is an example in this answer.
Related
I wanted to search a word in a textview which contains a large amount of data. To accomplish it i added a search option in action bar and tried using setOnQueryTextListener. However i am not able to use method tv.getFilters().filter() on text view. This method works fine on array adapter.
Could you please suggest how to search text in text view
Thanks for your help!!
First call this method on onQueryTextChange(). See below.
#Override
public boolean onQueryTextChange(String s) {
filterListByPattern(s, List);// List is TextView Large amount of data convert each word as a list item.
return true;
}
In filterListByPattern(),
private void filterListByPattern(String s, ArrayList<String> mainList)
{
for(int i = 0; i < mainList.size(); i++)
{
String word = mainList.get(i);
if(word.toLowerCase().toString().contains(s.toLowerCase().toString()))// if search word found in list
{
Spannable wordtoSpan = new SpannableString(word);
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE));// change the color of the word in to blue.
mainList.set(i, wordtoSpan.toString())//color changed word update to the List.
break;
}
}
for(int i = 0; i < mainList.size(); i++)
{
String word = mainList.get(i);
tv.append(word);//append each word in List. search word will be blue color
}
}
I am making a Mad Libs game of sorts for practice, and so I will have things like EditTexts so the user can input data.
However right now the only way I can get this to sort of work is if I use a horizontal LinearLayout and then do something like a TextView followed by an EditText followed by another TextView, but then if the user enters something long in the EditText, the text does not "wrap nicely" but rather it squishes the rightmost TextView. This is to be expected, but it is not my end-desired behavior.
Is there a better way to put EditTexts inside of TextViews?
Just have one TextView and then you can use a ClickableSpanand even add multiple ClickableSpan(s) to a given TextView. This gives you the effect of multi-line TextView with certain spots acting like an EditText.
If an object of this type is attached to the text of a TextView with a
movement method of LinkMovementMethod, the affected spans of text can
be selected. If clicked, the onClick(View) method will be called.
SpannableString madLibString = new SpannableString("Our school cafeteria has really adjective food");
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View textView) {
Toast.makeText(getApplicationContext(), "The text 'adjective' has been clicked. Do something.",
Toast.LENGTH_SHORT).show();
}
#Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
// NOTE: set styling here to give UI feedback that this is text that can be clicked and edited.
ds.setColor(getResources().getColor(android.R.color.holo_red_dark));
}
};
madLibString.setSpan(clickableSpan, 32, 41, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(madLibString);
textView.setMovementMethod(LinkMovementMethod.getInstance());
I'm trying to implement search functionality in listview like text search in browser.
I'm succeeded to search input words in cursor data. But how can i change corresponding word's background color when match is found ?
This is search code in bindview
if (searchEnabled) {
cursor.moveToFirst();
while (cursor.moveToNext()) {
String cursorText = cursor.getString(indexMessage);
if (s.equals(searchText.toString())) {
//Change matched word's(textview) background color here
holder.textMessage.setBackgroundColor(Color.WHITE);
Log.i("Cursor Scan:", cursorText);
}
}
}
After calling notifyDataSetChanged(), it changes color of only last row's textview.
I tried search by getting listview text from getChildAt() and partially succeeded, but this view is not consistent and it comes to it's original state on scrolling (since, this listview is inflated from cursor).
I tried following code for searching in listview text:
private void updateListView(String textForSearch) {
LocalStoreDB localDB = new LocalStoreDB(getApplicationContext());
localMessageDB.openDB();
Cursor cursorForSearch = localDB.selectMessageDB();
cursorForSearch.moveToFirst();
while (cursorForSearch.moveToNext()) {
int position = cursorForSearch.getPosition();
View v = listview.getChildAt(position);
TextView tv = (TextView) v.findViewById(R.id.textMessage);
SpannableString cursorString = new SpannableString(tv.getText());
Pattern p = Pattern.compile(textForSearch);
Matcher m = p.matcher(cursorString);
while (m.find()) {
cursorString.setSpan(new BackgroundColorSpan(Color.WHITE),
m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Log.i("m.find()", cursorString.toString());
}
tv.setText(cursorString);
}
localDB.closeDB();
}
You can set background color of a textview like this
yourTextView.setBackgroundColor("0xff0000ff");
I cannot see rest of the code but maybe you have to notify the changes you made after setting color.
Hope it helps
In your case I would try something like this:
TextView tv;
for(int i=0;i<listview.get.getCount();i++){
tv =(TextView) listview.getChildAt(i).findViewById(R.id.textMessage);
if(tv.getText().equalsIgnoreCase(searchstring)){
tv.setBackgroundColor("0xff0000ff");
}
else{
tv.setBackgroundColor("0xffffffff");
}
}
listViewAdapter.notifyDataSetChanged();
Sorry guys for late answer.
I've answered this question in another thread. Please follow this link:
https://stackoverflow.com/a/34287029/4688535
Is it somehow possible to achieve that?
In example: we have listView with 20 items, every item has some text. User want to select half of ending text from item 1. and the half of another item text (same behaviour like in webView or selectable textView). Did someone think about that feature? Where should I search the solution?
This topic will be updated when solution will be found.
ps. I know you will say "show us code first". I do not have it yet.
In your istview on item click listener code like this
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3)
{
// TODO Auto-generated method stub
TextView tv;
String text_from_tv, finalText;
tv= (TextView) view.findViewById(R.id.textview_id);
text_from_tv = tv.getText();
if(!firstPartSelected)
finalText += text_from_tv.substring(0,text_from_tv.length()/2);
else
finalText += text_from_tv.substring(text_from_tv.length()/2,text_from_tv.length());
//Save this finalText String to any string array and use those values
}
});
and that string array contains the second halfs of the selected word, if it helpful up vote the answer!!
In your list_item.xml for your list view. You will want to set,android:textIsSelectable="true" and make android:clickable="true" for that same item.
I won't give any code, just how I would do it :
boolean firstPartSelected false;
String finalText ="";
// ListView Definition
// OnItemClickListener
OnItemClick(...){
if(!firstPartSelected)
finalText += TextFromItem.substring(0,TextFromItem.length()/2)
else
finalText += TextFromItem.substring(TextFromItem.length()/2,TextFromItem.length())
}
This is not some real code, just an idea of how to implement it. Is that what you wanted ?
Try handle OnFocusChangeListener for your EditText, when focus will be change, color selected text in EditText using spans (Android: Coloring part of a string using TextView.setText()?). If you have large count of EditText you can use view.setTag(etNumber) and (Integer)view.getTag() for each EditText and use this information while concat output string in loop (look for more info Android - get children inside a View?)
P.S. EditText is inheritor of TextView, what yo can do with TextView you will can do with EditText
How to change the color of a paragraph word by word with timer. like at first second i want to change color of The in the below paragraph, after 5th second change color of text, after 8th second change color of will be and so on....
The text will be wrapped in tags, and displayed in a monospaced font.
just use Timer and change the font color of your edit text accordingly and stop timer in focus lost.
i think you can do something like this :
Split the paragraph to words by using the method :
split(String separator);// it will return an array of Strings
//in your case you will do somthing like this
myWords = paragraph.split(" ");// the separator is the space
And then , you can use the method to colorate what ever you want of those words by using the method :
myNewParagraph.append(HTML.fromHtml("<font color...>... Your HTML Code to colorate your Text"));
and when you finish coloring each word , you update your textView to display the new text colored
Hope it helps
You can use spans to control the appearance of text. Take a look at Spannable and CharacterStyle.
Here is an example. Of course you would need to put this in some sort of timer.
Spannable spannableText = getSpannableText(yourTextView);
spannableText.setSpan(new TextAppearanceSpan(...), wordStart, wordEnd)
yourTextView.setText(spannableText);
private Spannable getSpannableText(TextView tv) {
CharSequence cs = tv.getText();
if (cs instanceof Spannable) {
return (Spannable)cs;
} else {
return SpannableString.valueOf(cs);
}
}