how do make UI for getting smilies into my edittext? - android

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

Related

How to remove editText bold Span beyond a particular point in android?

I am working on a text editor . I want to make text bold and normal(unbold) on a button click . When bold button has been pressed every next typed text is bold, but i am unable to make every next typed text normal once bold button has been unchecked .
editText.getText().setSpan(boldSpan, start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
this line makes the every next typed text bold. i want to make text normal afterwards as soon as the bold button has been unchecked.
what i have tried:
I have tried setting Typeface.NORMAL on the coming text but this doesn't remove the boldSpan.
I have tried removing end part of the span , but this removes the whole span , so at the end no text is bold at all .
I have tried for days, trying different approaches , but none of them worked . Any approach to get my job done would be highly appreciated .
check below code if its helpful for you.instead of string you can use your editText here dude ;)
String normalBefore= "First Part Not Bold ";
String normalBOLD= "BOLD ";
String normalAfter= "rest not bold";
String finalString= normalBefore+normalBOLD+normalAfter;
Spannable sb = new SpannableString( finalString );
sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), finalString.indexOf(normalBOLD)+ normalBOLD.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //bold
sb.setSpan(new AbsoluteSizeSpan(intSize), finalString.indexOf(normalBOLD)+ normalBOLD.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

How can I capture the formatting of my EditText text, so that bold words show as bold next time I display them in a TextView

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);

How do I put 2 different texts in one button in android?

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

how to add text with format into edit text?

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"));

Android: Is it possible to have a Spanned with part of it's text focusable within a TextView?

I have this currently.
ClickableSpan span = new ClickableSpan(...){...};
String text = "I am some <b>awesome</b> text";
Spanned spanned = Html.fromHtml(text);
SpannableStringBuilder builder = new SpannableStringBuilder(spanned);
int start = builder.nextSpanTransition(0, builder.length(), StyleSpan.class);
int stop = builder.nextSpanTransition(start, builder.length(), StyleSpan.class);
builder.setSpan(span, start, stop, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(builder);
The TextView renders with the text that has the word "awesome" bolded and underlined (Yay). However in my view, I cannot focus the subregion of text I specified in the clickablespan. I can click on it with a touch event, but I cannot focus it. I am testing this on Android 1.5 + 2.1. I have also tried UrlSpan as well.
I have also tried instead of using a ClickableSpan, to actually attach an onClick listener to the entire block of text but that doesn't give the region focus, just makes clicking easier. Please help
Ok I just figured it out. I originally was looking at the UrlSpan documentation and then just blindly implemented ClickableSpan.
textView.setMovementMethod(LinkMovementMethod.getInstance());
And magically through the powers of this undocumented class ... it works. So basically what I think is going on, is the MovementMethod is a way to supply a textview with a strategy to handle cursor input.

Categories

Resources