Android examples for User Interface:TextView
set Text Drawable Top to TextView
//package com.java2s; import android.content.Context; import android.graphics.drawable.Drawable; import android.util.DisplayMetrics; import android.widget.TextView; public class Main { public static void setTextDrawableTop(TextView txt, int padDip, int resId) { txt.setCompoundDrawables(null,//from www . jav a 2s . c om getTextDrawable(txt.getContext(), resId), null, null); if (padDip >= 0) { txt.setCompoundDrawablePadding(dp2Px(txt.getContext(), padDip)); } } public static Drawable getTextDrawable(Context context, int resId) { Drawable drawable = context.getResources().getDrawable(resId); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); return drawable; } public static int dp2Px(Context context, float dp) { DisplayMetrics displayMetrics = context.getResources() .getDisplayMetrics(); return (int) (dp * displayMetrics.density); } }