Skip to content Skip to sidebar Skip to footer

Cannot Set Image And Text In Center Vertical Android Pagertabstrip Android

I got problem when I try to set image and text vertically center. So I'm using SpannableStringBuilder and ImageSpan. I try to combile image and text by this code : @Override

Solution 1:

PagerTabStrip: Height - 48dp Padding - 12dp left and right of text, 20dp from bottom for a single line of text, 12dp from bottom for two lines of text

In my case I did it for one line text

<android.support.v4.view.PagerTabStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_gravity="top"
        android:paddingBottom="20dp"
        android:textColor="#ffffff" />

Solution 2:

This solution should work. I have tested it and am using it for sometime. It doesn't consider the ascent and decent but it Aligns the drawable in the center. This solution is

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.text.style.ImageSpan;

import java.lang.ref.WeakReference;

publicclassCustomImageSpanextendsImageSpan {

  /**
   * A constant indicating that the center of this span should be aligned
   * with the center of the surrounding text
   */publicstaticfinalintALIGN_CENTER= -12;
  private WeakReference<Drawable> mDrawable;
  privateint mAlignment;

  publicCustomImageSpan(Context context, finalint drawableRes, int alignment) {
    super(context, drawableRes);
    mAlignment = alignment;
  }

  @OverridepublicintgetSize(Paint paint, CharSequence text,
                     int start, int end,
                     Paint.FontMetricsInt fm) {
    Drawabled= getCachedDrawable();
    Rectrect= d.getBounds();
    if (fm != null) {
      Paint.FontMetricsIntpfm= paint.getFontMetricsInt();
      fm.ascent = pfm.ascent;
      fm.descent = pfm.descent;
      fm.top = pfm.top;
      fm.bottom = pfm.bottom;
    }
    return rect.right;
  }

  @Overridepublicvoiddraw(@NonNull Canvas canvas, CharSequence text,
                   int start, int end, float x,
                   int top, int y, int bottom, @NonNull Paint paint) {
    if (mAlignment == ALIGN_CENTER) {
      DrawablecachedDrawable= getCachedDrawable();
      canvas.save();
      //Get the center point and set the Y coordinate considering the drawable height for aligning the icon verticallyinttransY= ((top + bottom) / 2) - cachedDrawable.getIntrinsicHeight() / 2;
      canvas.translate(x, transY);
      cachedDrawable.draw(canvas);
      canvas.restore();
    } else {
      super.draw(canvas, text, start, end, x, top, y , bottom, paint);
    }
  }

  // Redefined locally because it is a private member from DynamicDrawableSpanprivate Drawable getCachedDrawable() {
    WeakReference<Drawable> wr = mDrawable;
    Drawabled=null;
    if (wr != null) {
      d = wr.get();
    }
    if (d == null) {
      d = getDrawable();
      mDrawable = newWeakReference<>(d);
    }
    return d;
  }
}

Post a Comment for "Cannot Set Image And Text In Center Vertical Android Pagertabstrip Android"