Showing image on ListPreference summary - android

I'm trying to add images on ListPreference and I achieved that but on Android API level 15 or higher it doesn't work. What I'm doing wrong? I have tested this code on 2.2 and 2.3.3 and all works fine!
Here is my code.
private void addSummary(Drawable d, Spannable sp) {
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(Const.IMAGE_ANCHOR);
builder.append(" ");
builder.append(sp);
setTextColor(builder);
ImageSpan imageSpan = new ImageSpan(d);
int end = Const.IMAGE_ANCHOR.length();
builder.setSpan(imageSpan, 0, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
listPreference.setSummary(builder);
}

Instead of using this approach a cleaner approach would be to declare a layout for the preference object and set the android:layout parameter of the preference with the resource id of your custom layout.
Here is a working code example.

Related

Adding multiple smiles in TextView using ImageSpan

I am trying to add multiple smiles in textview using this code.
This is my TextView.
<TextView
android:id="#+id/textViewId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:bufferType="spannable" />
And this is add smiley function.
public void addSmily() {
int resource = R.drawable.smily ;
Spannable spannable = Spannable.Factory.getInstance().newSpannable(" ");
Drawable d = ContextCompat.getDrawable(this, resource);
d.setBounds(0, 0, 40, 40);
ImageSpan smilySpan = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
spannable.setSpan(smilySpan, spannable.length()-1, spannable.length(), 0);
sendText.append(spannable);
}
Smiles are adding perfectly but the problem is when I add lots of smiles did not fit in a single line then the first line of smiles become invisible and they start from the 2nd line.
This is what happening. Plz, someone help me.
Solution:
Try this inside your button:
SpannableString ss = new SpannableString("abc");
Drawable d = ContextCompat.getDrawable(your_activity.this, R.drawable.your_smiley_drawable);
d.setBounds(0, 0, 40, 40);
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
ss.setSpan(span, 0, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
edittext.append(ss);
Note: Also, EditText's inputtype must be textMultiline.
Try it, Works in my lap, Let's Hope it helps to you too.
You can also set smiley by unicode in textview.
how set emoji by unicode in a textview?
int unicode = 0x1F60A;
Which can be used with
public String getEmojiByUnicode(int unicode){
return new String(Character.toChars(unicode));
}
So Textview displays 😊 without Drawable
Try it with http://apps.timwhitlock.info/emoji/tables/unicode
Hope it may help you.

Custom item list view with Image View after the last character of textview

I want custom item list view same image
what can i do to create item list view with small image view just like in the picture
Thank you very much
It looks like you actually want the image to be inline with the text.
In this case, you wouldn't use an ImageView. You would create a Spannable that contains an ImageSpan and assign it to the TextView.
String str = "Thank you very much [icon]";
int start = str.indexOf("[icon]");
int end = start + "[icon]".length();
SpannableString ss = new SpannableString(str);
Drawable d = getResources().getDrawable(R.drawable.headphones);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
ImageSpan span = new ImageSpan(d);
ss.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ss);
Use a textView, create html text with image tag, use Html.fromHtml. Read the docs on fromHtml, as you will still have to replace the images.
Alternatively, you can use a WebView, though I don't like the overhead.

Android: Insert Bitmap in EditText

I would like to insert jpeg images in edittext attached as shown below.
Stackoverflow has recommended this post but the reference projects provided are no longer valid.
how to insert jpeg files in edittext android
Will there be any reference projects?
try this,
EditText text = (EditText)findViewById(R.id.text);
text.setCompoundDrawables(null,null,getResources().getDrawable(R.drawable.check), null);
That's it.
You can use ImageSpan like this...
buildImageSpan(EditText et){
SpannableString ss = new SpannableString("anystring");
Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
d.setBounds(0,0,d.getIntrinsicWidth(), d.getIntrinsicHeight());
ImageSpan span = new ImageSpan(d, "anystring", ImageSpan.ALIGN_BASELINE);
ss.setSpan(span, 0, anystring.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
et.setText(et.getText()+ss);
}
You can set compound drawable top bottom left right using
edittext.setCompoundDrawables(left,top,right, bottom);

Drawable in string resource

I want to show AlertDialog that shows its message with string and icons together.
Is it possible to insert icons/images/drawables in string resource? Is there any way to show drawables with the string in the AlertDialog.
EDIT
If its was not clear, the drawables need to be inside the string. like "click the icon [icon-image] and then click on..."
SpannableString spannableString = new SpannableString("#");
Drawable d = getResources().getDrawable(R.drawable.your_drawable);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BOTTOM);
spannableString.setSpan(span, spannableString.toString().indexOf("#"), spannableString.toString().indexOf("#")+1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
yourTextView.setText(spannableString);
The AlertDialog.Builder class has a method setIcon(int iconRes) or setIcon(Drawable icon) that you can use for this.
EDIT:
If you need it in the middle of the string, you could use an ImageSpan:
String src = "Here's an icon: # isn't it nice?";
SpannableString str = new SpannableString(src);
int index = str.indexOf("#");
str.setSpan(new ImageSpan(getResources().getDrawable(R.drawable.my_icon), index, index + 1, ImageSpan.ALIGN_BASELINE));
AlertDialog.Builder x = new AlertDialog.Builder(myContext);
x.setMessage(str);
You can use custom view or custom title with ImageView or TextView containing compound drawables.

Android add more smileys in one edittext?

I need to add more than one smileys in a single edittext box. For add a single smiley I follow this link
How to add more smileys in a single Edittext box? Thanks in advance..
You can add as many ImageSpans to a Spannable as you like. Just follow the concept laid out by the code you're linking. You probably want to use a SpannableStringBuilder too.
Drawable happySmiley = mContext.getResources().getDrawable(R.drawable.happy);
happySmiley .setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
Drawable sadSmiley = mContext.getResources().getDrawable(R.drawable.sad);
sadSmiley .setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append("Some text [happy_smiley_anchor]");
builder.setSpan(new ImageSpan(happySmiley), builder.length()-"[happy_smiley_anchor]".length(), builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append(". Some more text [sad_smiley_anchor]");
builder.setSpan(new ImageSpan(sadSmiley), builder.length()-"[sad_smiley_anchor]".length(), builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
edittext.setText(builder);
Obviously you can use any anchor text/character you like - it gets replaced when the ImageSpan is inserted. It might even work with an empty char/string, but I didn't try that.
You can add as many ImageSpans to a Spannable as you like. Just follow the concept laid out by the code you're linking. You probably want to use a SpannableStringBuilder too.
SpannableStringBuilder ssb = new SpannableStringBuilder("Some Text");
Bitmap image1 = BitmapFactory.decodeResource( getResources(), R.drawable.yourimage1 );
Bitmap image2 = BitmapFactory.decodeResource( getResources(), R.drawable.yourimage1 ); ssb.setSpan( new ImageSpan( image1 ), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE );
ssb.setSpan( new ImageSpan( image2 ), 2,3, Spannable.SPAN_INCLUSIVE_INCLUSIVE );
deleteButton.setText( ssb, BufferType.SPANNABLE );
I tried above code and it works fine. I added two image span in one text view, likewise you can add as many image span to textview.

Categories

Resources