Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. package br.com.phonecad.custom;
  2.  
  3. import android.content.Context;
  4. import android.content.res.TypedArray;
  5. import android.graphics.drawable.Drawable;
  6. import android.os.Build;
  7. import android.support.v7.content.res.AppCompatResources;
  8. import android.support.v7.widget.AppCompatEditText;
  9. import android.util.AttributeSet;
  10.  
  11. import br.com.phonecad.R;
  12.  
  13. /**
  14. * Created by VictorHugo (vhv.sousa@gmail.com) on 06/12/16 at 23:00.
  15. */
  16.  
  17. public class CustomEditText extends AppCompatEditText {
  18. public CustomEditText(Context context) {
  19. super(context);
  20. }
  21.  
  22. public CustomEditText(Context context, AttributeSet attrs) {
  23. super(context, attrs);
  24. initAttrs(context, attrs);
  25. }
  26.  
  27. public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
  28. super(context, attrs, defStyleAttr);
  29. initAttrs(context, attrs);
  30. }
  31.  
  32. void initAttrs(Context context, AttributeSet attrs) {
  33. if (attrs != null) {
  34. TypedArray attributeArray = context.obtainStyledAttributes(
  35. attrs,
  36. R.styleable.CustomEditText);
  37.  
  38. Drawable drawableLeft = null;
  39. Drawable drawableRight = null;
  40. Drawable drawableBottom = null;
  41. Drawable drawableTop = null;
  42. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  43. drawableLeft = attributeArray.getDrawable(R.styleable.CustomEditText_drawableLeftCompat);
  44. drawableRight = attributeArray.getDrawable(R.styleable.CustomEditText_drawableRightCompat);
  45. drawableBottom = attributeArray.getDrawable(R.styleable.CustomEditText_drawableBottomCompat);
  46. drawableTop = attributeArray.getDrawable(R.styleable.CustomEditText_drawableTopCompat);
  47. } else {
  48. final int drawableLeftId = attributeArray.getResourceId(R.styleable.CustomEditText_drawableLeftCompat, -1);
  49. final int drawableRightId = attributeArray.getResourceId(R.styleable.CustomEditText_drawableRightCompat, -1);
  50. final int drawableBottomId = attributeArray.getResourceId(R.styleable.CustomEditText_drawableBottomCompat, -1);
  51. final int drawableTopId = attributeArray.getResourceId(R.styleable.CustomEditText_drawableTopCompat, -1);
  52.  
  53. if (drawableLeftId != -1)
  54. drawableLeft = AppCompatResources.getDrawable(context, drawableLeftId);
  55. if (drawableRightId != -1)
  56. drawableRight = AppCompatResources.getDrawable(context, drawableRightId);
  57. if (drawableBottomId != -1)
  58. drawableBottom = AppCompatResources.getDrawable(context, drawableBottomId);
  59. if (drawableTopId != -1)
  60. drawableTop = AppCompatResources.getDrawable(context, drawableTopId);
  61. }
  62. setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom);
  63. attributeArray.recycle();
  64. }
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement