Skip to content

Instantly share code, notes, and snippets.

@dalwadi2
Last active January 20, 2017 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dalwadi2/21e26f50e962a50daefe71f823fb7cff to your computer and use it in GitHub Desktop.
Save dalwadi2/21e26f50e962a50daefe71f823fb7cff to your computer and use it in GitHub Desktop.
CustomTextView that have functionality to change TextViewDrawable size.
package com.samcomtechnologies.chumchatclient.utils;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* Created by Harsh on 23/9/16.
* edit @attrs.xml file
* <declare-styleable name="TextViewDrawableSize">
* <attr name="compoundDrawableWidth" format="dimension" />
* <attr name="compoundDrawableHeight" format="dimension" />
* </declare-styleable>
* <p>
* use :
* app:compoundDrawableHeight="20dp"
* app:compoundDrawableWidth="20dp"
*/
public class CustomTextView extends TextView {
private int mDrawableWidth;
private int mDrawableHeight;
public CustomTextView(Context context) {
super(context);
init(context, null, 0, 0);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0, 0);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr, 0);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs, defStyleAttr, defStyleRes);
}
private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TextViewDrawableSize, defStyleAttr, defStyleRes);
try {
mDrawableWidth = array.getDimensionPixelSize(R.styleable.TextViewDrawableSize_compoundDrawableWidth, -1);
mDrawableHeight = array.getDimensionPixelSize(R.styleable.TextViewDrawableSize_compoundDrawableHeight, -1);
} finally {
array.recycle();
}
if (mDrawableWidth > 0 || mDrawableHeight > 0) {
initCompoundDrawableSize();
}
}
private void initCompoundDrawableSize() {
Drawable[] drawables = getCompoundDrawables();
for (Drawable drawable : drawables) {
if (drawable == null) {
continue;
}
Rect realBounds = drawable.getBounds();
float scaleFactor = realBounds.height() / (float) realBounds.width();
float drawableWidth = realBounds.width();
float drawableHeight = realBounds.height();
if (mDrawableWidth > 0) {
if (drawableWidth > mDrawableWidth) {
drawableWidth = mDrawableWidth;
drawableHeight = drawableWidth * scaleFactor;
}
}
if (mDrawableHeight > 0) {
if (drawableHeight > mDrawableHeight) {
drawableHeight = mDrawableHeight;
drawableWidth = drawableHeight / scaleFactor;
}
}
realBounds.right = realBounds.left + Math.round(drawableWidth);
realBounds.bottom = realBounds.top + Math.round(drawableHeight);
drawable.setBounds(realBounds);
}
setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment