Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package br.com.phonecad.custom;
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.graphics.drawable.Drawable;
- import android.os.Build;
- import android.support.v7.content.res.AppCompatResources;
- import android.support.v7.widget.AppCompatEditText;
- import android.util.AttributeSet;
- import br.com.phonecad.R;
- /**
- * Created by VictorHugo (vhv.sousa@gmail.com) on 06/12/16 at 23:00.
- */
- public class CustomEditText extends AppCompatEditText {
- public CustomEditText(Context context) {
- super(context);
- }
- public CustomEditText(Context context, AttributeSet attrs) {
- super(context, attrs);
- initAttrs(context, attrs);
- }
- public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- initAttrs(context, attrs);
- }
- void initAttrs(Context context, AttributeSet attrs) {
- if (attrs != null) {
- TypedArray attributeArray = context.obtainStyledAttributes(
- attrs,
- R.styleable.CustomEditText);
- Drawable drawableLeft = null;
- Drawable drawableRight = null;
- Drawable drawableBottom = null;
- Drawable drawableTop = null;
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
- drawableLeft = attributeArray.getDrawable(R.styleable.CustomEditText_drawableLeftCompat);
- drawableRight = attributeArray.getDrawable(R.styleable.CustomEditText_drawableRightCompat);
- drawableBottom = attributeArray.getDrawable(R.styleable.CustomEditText_drawableBottomCompat);
- drawableTop = attributeArray.getDrawable(R.styleable.CustomEditText_drawableTopCompat);
- } else {
- final int drawableLeftId = attributeArray.getResourceId(R.styleable.CustomEditText_drawableLeftCompat, -1);
- final int drawableRightId = attributeArray.getResourceId(R.styleable.CustomEditText_drawableRightCompat, -1);
- final int drawableBottomId = attributeArray.getResourceId(R.styleable.CustomEditText_drawableBottomCompat, -1);
- final int drawableTopId = attributeArray.getResourceId(R.styleable.CustomEditText_drawableTopCompat, -1);
- if (drawableLeftId != -1)
- drawableLeft = AppCompatResources.getDrawable(context, drawableLeftId);
- if (drawableRightId != -1)
- drawableRight = AppCompatResources.getDrawable(context, drawableRightId);
- if (drawableBottomId != -1)
- drawableBottom = AppCompatResources.getDrawable(context, drawableBottomId);
- if (drawableTopId != -1)
- drawableTop = AppCompatResources.getDrawable(context, drawableTopId);
- }
- setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom);
- attributeArray.recycle();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement