The image is from an app called kakao story.
Suppose there's a post with a list of comments like any sns apps.
When you click a comment, it inserts the user name of the commenter in the edit-text to indicate my new comment is a reply to the user.
(You can't add the same name more than once.)
When you hit backspace to delete the name, the entire characters that make up the name(e.g., chabeau in the example) will be deleted by 1-backspace.
I'm trying to mimic the behavior and want some pointers how to implement it or what to search for.
If you are in search of bubble view. You can achieve it by creating a subclass of android.text.style.DynamicDrawableSpan.ImageSpan which will convert a portion of EditText string into formatted span.
This SO Question will give you some basic idea about creating formatted span.
This is a good tutorial for customizing editext with spans.
And for deleting whole word at once, you can use SPAN_EXCLUSIVE_EXCLUSIVE property.
Below code will format the first four character of the string, Hope this will give you some hint.
final SpannableStringBuilder sb = new SpannableStringBuilder("your text here");
final ForegroundColorSpan fcs
= new ForegroundColorSpan(Color.rgb(158, 158, 158));
// Span to set text color to some RGB value
sb.setSpan(fcs, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
yourTextView.setText(sb);
EditText et = (EditText) findViewById(R.id.edit1);
et.setTextColor(Color.parseColor("yourColorCodeHere"));
Related
I am trying to set span on a SpannableStringBuilder using flag SPAN_EXCLUSIVE_EXCLUSIVE and I am facing problem on further editing the text to which I am setting span.
Expected behaviour
1: Original text.
2: Text added before.
3: Text added after with space.
Unexpected Behaviour on adding text after styled text
I don't want the added text to be styled, and want to know what am I doing wrong.
EDIT 1:
The issue is happening on Moto X Play, but is not reproduced on Nexus 5X. Still testing on other devices.
You just probably add text not the way you should. Use .insert() and .append() methods of SpannableStringBuilder to add additional text.
I just tried what you try to achieve and here is the result:
TextView hratkyTextView = (TextView) findViewById(R.id.spannableHratkyTextView);
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); // Span to make text bold
// "Test text" part (in bold)
SpannableStringBuilder builder = new SpannableStringBuilder("Test text");
builder.setSpan(bss, 0, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// Prepending "before" (non-bold)
builder.insert(0, "before");
// Appending " after_with_space" to the end of the string
builder.append(" after_with_space");
hratkyTextView.setText(builder);
Result:
Nexus 7 Emulator running MainActivity with this code
TL;DR: Using some IMEs, like Gboard, when adding a char directly after a word (without space) the IME will replace the whole word tric with trick instead of just appending the c.
Detailed asnwer: How IMEs work with editors.
How some IMEs dictate commands to editors
IMEs communicate with editors (e.g. EditText) through InputConnection interface where they can send commands following user input, and get current text.
Gboard IME works in the following way:
gets text before and after cursor
detects the currently "composing" word and asks the editor to highlight and remember it (usually results in the word being underlined - check screenshot below)
Being aware of the currently composing word enables many features like suggesting words or auto-correcting spelling.
Whenever a char is inputted by the user, Gboard will ask the editor to set the currently composing text to a new value, i.e. replace trick by tricky
After a space is inputted, Gboard will do a final replace of currently composing region, eventually auto-correcting spelling
Currently composing region is reset to the next word.
This unfortunately breaks what we would normally expect from SPAN_EXCLUSIVE_EXCLUSIVE.
Good Day i want to hide some specified or certain part of text in textview!Important: Im not talking about hide the full textview with TextView.setVisibility(View.Gone) I'm not talking about transparent of TEXT in textview!im not talking about hiding full text in textview!So please help me to hide some text.
Example: lets say i have a textview with this text (10-Sporting Goods)
I want to hide the (10-) and show only Sporting Goods text.Any help will be appreciated!Thank you very much beforehand!
Although even i would appreciate for your case to strongly go with DroidWorm/Gabriella approach , just for the information of all the other folks who may see this in future.
If you really wish to hide just a portion of your textview which has the entire string in itself, you should use a SpannableString , as below:-
tvHello = (TextView) findViewById(R.id.tvHello);
SpannableString customText = new SpannableString("10-Sporting Good");
customText.setSpan(new RelativeSizeSpan(.1f), 0, 3, 0);
tvHello.setText(customText);
This code will technically HIDE the 10- from 10-Sporting Good without using a substring.
You could try to get the whole text like
String text = textView.getText().toString();
and then make substring of it like this:
String wantedSubstr = text.substring(4); //for example - everything from the 4th index to the end
then set this substring as text of your textView like this:
textView.setText(wantedSubstr);
There is one the possible solution of it is that..First you have to find the index(position) of "-" and than split the string according to it therefore use below code
String text = textView.getText().toString();
int position=text.indexOf('-');
String wantedSubstr = text.substring(position+1);
textView.setText(wantedSubstr);
Will there always been "10_" in front of it? Or will there always be 3 characters before the text you want? Or will there always be a "-" or "_" before the text you want?
If so, you could just do a simple method which takes the substring and then updates the textview. If so I can help you write a simple method
You cannot hide part of textView, instead you can make a substring of the specific string and setText using it.
Do it like:
String originalString = "10-Sporting Goods";
String subString = originalString.substring(3);
textView.setText(asubstring);
Edit:
my app allows users to post comments for all other users to see. I want to allow users to make certain words in their comment bold or italic etc, so that when they post a comment other people will also be able to see the bold words etc.
At the moment i am able to make the words bold but when the text is saved to my remote database server it is saved as normal text and i am unable to find out which words were bold and which arn't.
How can I preserve the bold/italic/underlined... formatting when saving the text from an EditText?
There is one way to achieve this.
You can store HTML content and display HTML content in TextView.
Just look at this Answer.
Here is Sample link.
Quite easily.
Use EditText.getText to get the text, which implements Spanned interface.
Then you need to somehow mark bold parts within text, for this you may use good old HTML format, which means that you'll end up with text (storable in database) with some formatting.
Use Html.toHtml to turn Spanned to (HTML) String. Save result to DB or where you need.
Reverse process is to get your HTML String and convert it to Spanned, then set it bact to EditText. Use Html.fromHtml to accomplish this.
final SpannableStringBuilder sb = new SpannableStringBuilder("Text from the edit Text");
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); // Span to make text bold
final StyleSpan iss = new StyleSpan(android.graphics.Typeface.ITALIC);//Span to make text italic
sb.setSpan(bss, 0, 10, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make first 10 characters Bold
sb.setSpan(iss, 10, 20, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make last 10 characters Italic
//Set text or save it in db
etx.setText(sb);
so this image basicly shows what I want to do:
I want to add static text (that orange A) on a button which already has text on it. But I don't have any idea how.
You could try Html.fromHTML(htmlString). Something like:
myTextButton.setText(Html.fromHtml("<font color='#FFA500'>A: </font> whatever text you need here"));
With resp to #blackbelt
Spannable wordtoSpan = new SpannableString("A: Text");
wordtoSpan.setSpan(new ForegroundColorSpan(Color.ORANGE), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
button.setText(wordtoSpan);
where button is your Button object..
where
.setSpan(what,start,end,flag);
where
what has ForegroundColorSpan or StyleSpan or UnderlineSpan object. ForegroundColorSpan take one integer argument for color and StyleSpan also take one argument for style but UnderlineSpan don’t take any argument.
start has starting point of string where would you start string index for color of style.
end has ending point of string where would you end string index for color of style.
flag can have 1 or 0.
You can also change Fontsize to part of String.. refer this link for ref
I am having a notes app,in that i have to take smilies into my edittext where the cursor i point, for that i have to take list of smilies placed at top or pin to any place to emulator in edittext Activity.how come i disign UI for taking smilies(list of pngs,shown like grid view).or if any idea let me know?
task is: while typing in edit text, place the corsor to insert smilies and open any pin(button)to show smilies list and do select on one smilies and has to place in my text.
You can convert smily image into spannableText which in turn you have to set into edittext
here code hints:
public SpannableStringBuilder addSmily(Drawable dd) {
happySmileys.setBounds(0, 0, dd.getIntrinsicWidth(),
dd.getIntrinsicHeight());
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(":-)");
builder.setSpan(new ImageSpan(dd), builder.length()
- ":-)".length(), builder.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return builder;
}
above function return a spannableString which u can set like this
Drawable dd=getResources().getDrawable(R.id.ursmily_icon);
edittext.settext(addSmily(dd));
enjoy with coding!
here is the reference project adding sppannable to Edittext