How to Get Text and Smiley from Edittext into String? - android

How to Get text and smiley from edittext into String?
Using Following Code I was Add Smiley/Emojis in Edittext but how to get text/smiley from edittext into String Format.
ImageGetter imageGetter = new ImageGetter() {
public Drawable getDrawable(String source) {
Drawable d = getResources().getDrawable(
R.drawable.happy);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
cs = Html.fromHtml(
"<img src='"
+ getResources()
.getDrawable(R.drawable.happy)
+ "'/>", imageGetter, null);
edttxtemoji.setText(cs);

Please use following function.
public static Spannable getSmiledText(Context context, String text) {
SpannableStringBuilder builder = new SpannableStringBuilder(text);
int index;for (index = 0; index < builder.length(); index++) {
for (Entry<String, Integer> entry : emoticons.entrySet()) {
int length = entry.getKey().length();
if (index + length > builder.length())
continue;
if (builder.subSequence(index, index + length).toString().equals(entry.getKey())) {
builder.setSpan(new ImageSpan(context, entry.getValue()), index, index + length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
index += length - 1;
break;
}
}
}
return builder;
}
following for emotions code...
private static final HashMap<String, Integer> emoticons = new HashMap<String, Integer>();
static {
emoticons.put("8-)", R.drawable.s1);
emoticons.put(":-&", R.drawable.s2);
emoticons.put(">:-)", R.drawable.s3).....};
and settext using
tv_msg_send.setText(getSmiledText(getApplicationContext(), edt_msg.getText().toString()));

Use Html.toHtml(Spanned text)
like:
String myString = Html.toHtml(cs);
System.out.println(myString);
edit: iam digging in the dark here but could it be you want the text(string) representation of your Smillie?
like you have:
cs = Html.fromHtml(
"<img src='"
+ getResources()
.getDrawable(R.drawable.happy)
+ "'/>", imageGetter, null);
and you want:
String cs = ":)";
is that right? if not, my previous answer gives you the technical String representation of your Html code

Related

How to highlight some text using html?

working perfect but
when i am highlight text using html then some text can not be view perfect(Hindi text).
android
String str="रिश्ते भले ही कम ही बनाओ लेकिन दिल से निभाओ,\n" +
"क्योंकि आज कल इंसान अच्छाई के चक्कर में अच्छे खो देते है।";
//textview.setText(str);
textview.setText(Html.fromHtml(String.format(colorfulltext(str))), TextView.BufferType.SPANNABLE);
// highlight text
public String colorfulltext(String text) {
String[] colors = new String[]{"#fdc113", "#fdc113", "#fdc113","#fdc113", "#fdc113" ,"#fcfcfc", "#fcfcfc", "#fcfcfc", "#fcfcfc", "#fcfcfc", "#fcfcfc", "#fcfcfc", "#fcfcfc", "#fcfcfc","#fcfcfc","#fcfcfc","#fcfcfc","#fcfcfc"};
StringBuilder finals = new StringBuilder();
int size = colors.length;
int k = 0;
for (int item = 0; item < text.length(); item++) {
if (k >= size) {
k = 0;
}
finals.append("<font color='" + colors[k] + "'>" + text.charAt(item) + "</font>");
k++;
}
return finals.toString();
}
screen
Why do you convert a String to html to apply fontcolor for a static text??
You have to follow the following steps:
Create entries in Strings.xml for each text. For instance, रिश्ते
has a different color and needs to be a separate entry in
strings.xml.
add this to a Util class:
public static void addColoredPart(SpannableStringBuilder ssb,
String word, int color,String... texts) {
for(String text : texts) {
if (word != null) {
int idx1 = text.indexOf(word);
if (idx1 == -1) {
return;
}
int idx2 = idx1 + word.length();
ssb.setSpan(new ForegroundColorSpan(color), idx1, idx2,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
apply style the following way:
String string1 = context.getString(R.string.String_id1)
String string2 = context.getString(R.string.String_id2)
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
spannableStringBuilder.append(string2)
spannableStringBuilder.append(string2)
SpannableUtil.addColoredPart(
spannableStringBuilder,
spannableStringBuilder.toString(), color, string1, string2);

android spannable edititext with cursor manipulation

I am trying to implement code highlighting using spanned text and html.fromhtml() function in an edittext than implements text watcher.
The problem occurs when i try to manipulate cursor for custom brackets, the app crashes due to some spannable string setspan error.
How do i use spannable code highlighting and set cursor position adjusting according to the spanned text.
Edit:
The function:
Spanned matchtext(String s)
{
//Pattern p =Pattern.compile(check[0]);
String a=s;
for(int i=0;i<Constants.keyWords.length;i++) {
a = a.replaceAll(Constants.keyWords[i], "<font color=\"#c5c5c5\">" + Constants.keyWords[i] + "</font>");
//a = s.replaceAll(";", "<font color=\"#c5c5c5\">" + ";" + "</font>");
}
Spanned ab = Html.fromHtml(a);
return ab;
}
And the function call:
mCodeEditText.removeTextChangedListener(tt);
bs = matchtext(s.toString());
mCodeEditText.setText(bs);
mCodeEditText.addTextChangedListener(tt);
Edit 2:
This is my new implementation, I just can't get the highlighting to work.
Spannable matchtext(String s, int pos) {
Spannable abc = new SpannableString(s);
for (int i = 0; i < Constants.keyWords.length; i++) {
if (pos - Constants.keyWords[i].length() >= 0) {
int j = s.indexOf(Constants.keyWords[i]);
if (j != -1) {
if ((s.subSequence(j, pos)).equals(Constants.keyWords[i]))
abc.setSpan(new ForegroundColorSpan(Color.BLUE), j, pos, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
}
}
}
return abc;
}

Linkify any text Android

I want to add underline and colour to textview, but it is a simple text, not link, not phone number, just simple "Hello world", that I want to have with underline and that blue link-like colour.
It failed to do so:
view.setMovementMethod(LinkMovementMethod.getInstance());
Linkify.addLinks(view, Linkify.ALL);
Thank you! I just underlined the text as Const suggested and changed color of textview. But I guess xoxol_89's answer is correct in my case and should be accepted.
Do you try to use Spannable
For example
/**
* Method allocates filtering substring in all contacts yellow color,
* that satisfy the user's search
* #param inputText - DisplayName
* filtText - filtering Text
* #return String with allocating substring (Spannable)
*/
public static Spannable changeBackgroungFiltText(CharSequence inputText, String filtText, int color) {
Spannable str = null;
if(inputText != null)
{
String inputStr = inputText.toString();
String inputLowerCaseStr = inputStr.toLowerCase();
String filtLowerCaseStr = filtText.toLowerCase();
// Spannable str = new SpannableStringBuilder(inputStr);
str = new SpannableStringBuilder(inputStr);
if (filtText.length() != 0)
{
int indexStart = 0;
while (true)
{
int indexCur = inputLowerCaseStr.indexOf(filtLowerCaseStr, indexStart);
if (indexCur != -1) {
int start = indexCur;
int end = indexCur + filtText.length();
int flag = Spannable.SPAN_EXCLUSIVE_EXCLUSIVE;
str.setSpan(new ForegroundColorSpan(color),start, end, flag);
//str.setSpan(new BackgroundColorSpan(highlightColor), start, end, flag);
indexStart = indexCur + 1;
} else {
return str;
}
}
} else {
return str;
}
}
return str;
}
You can do that in 4 ways:
1.Automatically linkifies using android:autoLink=”all”
2.Link text by setMovementMethod
3.Link as html code using Html.fromHtml()
4.Link string by SpannableString
and you can find the examples here
You can use SpannableString like this:
final SpannableString text = new SpannableString("Hello World!");
final int startAt = 0;
final int endAt = text.length();
final int sampleColor = Color.parseColor("#3333ff");
text.setSpan(new UnderlineSpan(), startAt, endAt, 0);
text.setSpan(new ForegroundColorSpan(sampleColor), startAt, endAt, 0);
textView.setText(text);

How to colour new String?

How ist it possible to colour the newText "e"?
public void onClick(View v) {
String text = tw.getText().toString();
int n = 4;
String newText = text.substring(0, n) + "e" + text.substring(n + 1);
tw.setText(newText);
You need to use a ForegroundColorSpan
final String text = "something something text";
final int n = 4;
final String newText = text.substring(0, n) + "e" + text.substring(n + 1);
final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.RED);
final SpannableString ss = new SpannableString(newText);
ss.setSpan(fcs, n, n + 1, 0);
tv.setText(ss);
yes, if you formate the String with html
You need to use Html.fromHtml() to use HTML in your XML Strings. Simply referencing a String with HTML in your layout XML will not work.
For example:
String text = "<font color=#cc0029>Exempl</font> <font color=#ffcc00>e</font>";
myTextView.setText(Html.fromHtml(text);

How to get img tag From Spanned html from editText

I want to get HTML tag from editText.
Setting image to editText code is below.
String path = "images/sample.png";
Drawable drawable = Drawable.createFromStream(context.getAssets().open(path), null);
ImageGetter imageGetter = new ImageGetter() {
#Override
public Drawable getDrawable(String source) {
drawable.setBounds(0, 0, mTextSize, mTextSize);
return drawable;
}
};
String img = "<img src=\"" + drawable.toString() + "\" />";
Spanned spanned = Html.fromHtml(img, imageGetter, null);
Log.i("HTML",spanned.toString());
int start = this.getSelectionStart();
int end = this.getSelectionEnd();
this.getText().replace(Math.min(start, end), Math.max(start, end), spanned, 0, spanned.length());
If I use editText.getText(), result is removed images.
I want to do following:
String htmlTag = getHTML(); // I don't know this part.
myWebView.loadURL("javascript:setHTML(" + htmlTag + ")");
anyone has idea ?

Categories

Resources