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);
Related
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.
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.
Currently I have a spannableString like this, the icon is at the left of the title string. The problem is, is there any way to make the icon above the string , while the icon is align center?
Sample like this:
[] <==== This is the icon
TitleTest <=== This is the title
ABCD
Thanks
Here is my code attempted:
Drawable image = context.getResources().getDrawable(R.drawable.ic_launcher);
image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
// Replace blank spaces with image icon
SpannableString sb = new SpannableString(" " + tabTitles[position]);
ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BASELINE);
sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return sb;
Thanks for helping
I was looking at https://guides.codepath.com/android/Google-Play-Style-Tabs-using-TabLayout today and had the same question. Adding a newline to the SpannableString worked for me, so the following modification will do the trick:
SpannableString sb = new SpannableString(" \n" + tabTitles[position]);
Put the three views (ImageView for icon and 2 TextViews) in a vertical LinearLayout and use the android:layout_gravity property in XML to centre the views in the LinearLayout. Additionally set the android:gravity property on the TextViews to be "center" to centre the text within the TextView.
If you don't want to use XML (which seems to be the case) layout_gravity can be set using the LinearLayout.LayoutParams constructor with parameters for width, height AND GRAVITY. Text alignment can be done using mTextView.setGravity(Gravity.CENTER);
Just use the drawableTop attribute for this.
<TextView
android:drawableTop="#drawable/my_drawable"/>
Or from code:
myTextView.setCompoundDrawablesWithIntrinsicBounds(
0, R.drawable.my_drawable, 0, 0);
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.
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.