I am new to custom view and didn\'t know much about canvas in android, I wanted to align a left drawable to right side of layout along with the text(whether multiline or not) o
try something like this
package views;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;
import com.upsilon.docta.R;
public class MyTempView extends TextView {
public MyTempView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyTempView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
//ContextCompat.getColor(mContext, R.color.back_text_color)
Paint textPaint = getPaint();
Rect bounds = new Rect();
textPaint.getTextBounds(getText().toString(), 0, getText().length(), bounds);
int textWidth = bounds.width();
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.ic_close);
canvas.drawBitmap(bitmap, 0, getHeight() / 2 - bitmap.getHeight() / 2, textPaint);
canvas.translate(bitmap.getWidth(), 0);
super.onDraw(canvas);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/ad_post_tv_noDays"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:drawableLeft="@drawable/ic_cart_final"
android:drawablePadding="5dp"
android:gravity="center_vertical"
android:text="20 days"
android:textColor="@color/blue_btn_bg_color" />
<TextView
android:id="@+id/ad_post_tv_wage"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:drawableLeft="@drawable/ic_cart_final"
android:drawablePadding="5dp"
android:layout_gravity="right"
android:gravity="center_vertical|right"
android:text="$8 - $12/hour"
android:textSize="10dp"
android:textStyle="bold" /></LinearLayout>