How to modify SpannableString to string below icon? - android

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

Related

signe textview and show last 2digits with different colour in android

I have used one textview and value set for it is like ex:11,234.45 and after decimal values like 45 should be shown with reduced front like 12sp and different colour like grey and 11,234 with black colour with front 16sp. how to achieve this using single textview in android?
You can achieve that if you follow this steps
First you need to split you string into 2 strings with this code lines
String s ="11,234.45";
String[] split = s.split(".");
String firstSubString = split[0];
String secondSubString = split[1];
Then you change the font size and color of secondSubString like this
SpannableString ss= new SpannableString(secondSubString);
ss.setSpan(new RelativeSizeSpan(2f), 0, 5, 0); // set size
ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);// set color
You can change firstSubString size and color like this
SpannableString ss1= new SpannableString(firstSubString);
ss1.setSpan(new RelativeSizeSpan(2f), 0, 5, 0); // set size
ss1.setSpan(new ForegroundColorSpan(Color.BLACK), 0, 5, 0);// set color
Finally you set your string to the TextView
TextView tv= (TextView) findViewById(R.id.textview);
tv.setText(ss1 + "." + ss);

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.

PagerTabStrip is clipping off the icon

The design called for just icon on the tab of the PagerTabStrip.
Here's the XML:
<android.support.v4.view.PagerTabStrip
android:id="#+id/tbstrp_album"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foregroundGravity="bottom"
android:padding="0dp"
android:background="#color/offWhite">
Here's my getPageTitle inside the adapter:
#Override
public CharSequence getPageTitle(int position) {
// This is "generic" string that we will use as the title to be replaced.
String title = "title";
Drawable myDrawable = oContext.getDrawable(R.drawable.alpha);
// initiate the SpannableString builder
SpannableStringBuilder spanBuilder = new SpannableStringBuilder(title); // space added before text for convenience
// set the drawable's size...if it could be too big or too small for display
myDrawable.setBounds(0, 0, 50, 50);
// turn the Drawable into ImageSpan and align it along the baseline
ImageSpan imageSpan = new ImageSpan(myDrawable, ImageSpan.ALIGN_BASELINE);
// CRUCIAL: this is where we replace the "title" w/ the image
// 0: we start from the beginning
// title.length(): we are replacing the entire string
// the last flag doesn't do anything in our case
spanBuilder.setSpan(imageSpan, 0, title.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return spanBuilder;
}
The output looks like this:
What's causing the strip clipping the image? Thanks!
It turns out this statement is the culprit:
myDrawable.setBounds(0, 0, 50, 50);
I changed the second argument which is the "top" to 30 and the icon was not clipped.
The unanswered question is still...why do I have to specify a value for the "top" argument.

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

Categories

Resources