|
| 1 | +package br.com.bloder.magic.view; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.res.TypedArray; |
| 5 | +import android.graphics.Color; |
| 6 | +import android.graphics.drawable.Drawable; |
| 7 | +import android.graphics.drawable.GradientDrawable; |
| 8 | +import android.util.AttributeSet; |
| 9 | +import android.view.LayoutInflater; |
| 10 | +import android.view.View; |
| 11 | +import android.widget.FrameLayout; |
| 12 | +import android.widget.ImageView; |
| 13 | +import android.widget.LinearLayout; |
| 14 | +import android.widget.TextView; |
| 15 | + |
| 16 | +import br.com.bloder.magic.Magic; |
| 17 | +import br.com.bloder.magic.R; |
| 18 | + |
| 19 | +/** |
| 20 | + * Created by bloder on 28/07/16. |
| 21 | + */ |
| 22 | +public class MagicButton extends FrameLayout { |
| 23 | + |
| 24 | + |
| 25 | + private TextView text; |
| 26 | + private LinearLayout button; |
| 27 | + private LinearLayout buttonIcon; |
| 28 | + private ImageView icon; |
| 29 | + private Drawable drawable; |
| 30 | + private TypedArray typedArray; |
| 31 | + |
| 32 | + private String buttonText; |
| 33 | + private int expandableButtonColor; |
| 34 | + private int iconButtonColor; |
| 35 | + private OnClickListener onClickListener; |
| 36 | + |
| 37 | + public MagicButton(Context context) { |
| 38 | + super(context); |
| 39 | + initView(context); |
| 40 | + } |
| 41 | + |
| 42 | + public MagicButton(Context context, AttributeSet attrs) { |
| 43 | + super(context, attrs); |
| 44 | + typedArray = context.obtainStyledAttributes(attrs, R.styleable.MagicButton); |
| 45 | + if(typedArray != null) { |
| 46 | + buttonText = typedArray.getString(R.styleable.MagicButton_hide_text); |
| 47 | + expandableButtonColor = typedArray.getColor(R.styleable.MagicButton_expandable_area_color, Color.parseColor("#FFE6E4E4")); |
| 48 | + iconButtonColor = typedArray.getColor(R.styleable.MagicButton_icon_button_color, Color.parseColor("#FFE6E4E4")); |
| 49 | + drawable = typedArray.getDrawable(R.styleable.MagicButton_button_icon); |
| 50 | + } |
| 51 | + initView(context); |
| 52 | + } |
| 53 | + |
| 54 | + public void setMagicButtonClickListener(OnClickListener onClickListener) { |
| 55 | + this.onClickListener = onClickListener; |
| 56 | + } |
| 57 | + |
| 58 | + private void initView(Context context) { |
| 59 | + LayoutInflater.from(context).inflate(R.layout.magic_button, this, true); |
| 60 | + button = (LinearLayout) findViewById(R.id.expandable_button); |
| 61 | + buttonIcon = (LinearLayout) findViewById(R.id.icon_button); |
| 62 | + text = (TextView) findViewById(R.id.btn_text); |
| 63 | + icon = (ImageView) findViewById(R.id.btn_icon); |
| 64 | + setButtonContent(); |
| 65 | + baseAction(); |
| 66 | + } |
| 67 | + |
| 68 | + private void setButtonContent() { |
| 69 | + if(drawable != null) { |
| 70 | + icon.setBackground(drawable); |
| 71 | + } |
| 72 | + ((GradientDrawable) button.getBackground()).setColor(expandableButtonColor); |
| 73 | + ((GradientDrawable) buttonIcon.getBackground()).setColor(iconButtonColor); |
| 74 | + text.setText(buttonText); |
| 75 | + setSizes(); |
| 76 | + } |
| 77 | + |
| 78 | + private void setSizes() { |
| 79 | + button.getLayoutParams().width = (int) typedArray.getDimension(R.styleable.MagicButton_magic_button_size, 50); |
| 80 | + button.getLayoutParams().height = (int) typedArray.getDimension(R.styleable.MagicButton_magic_button_size, 50); |
| 81 | + buttonIcon.getLayoutParams().width = (int) typedArray.getDimension(R.styleable.MagicButton_magic_button_size, 50); |
| 82 | + buttonIcon.getLayoutParams().height = (int) typedArray.getDimension(R.styleable.MagicButton_magic_button_size, 50); |
| 83 | + icon.getLayoutParams().width = (int) typedArray.getDimension(R.styleable.MagicButton_button_icon_width, 25); |
| 84 | + icon.getLayoutParams().height = (int) typedArray.getDimension(R.styleable.MagicButton_button_icon_height, 25); |
| 85 | + text.setTextSize(typedArray.getDimension(R.styleable.MagicButton_magic_button_size , 20) / 7); |
| 86 | + } |
| 87 | + |
| 88 | + private void baseAction() { |
| 89 | + this.button.setOnClickListener(new View.OnClickListener() { |
| 90 | + @Override |
| 91 | + public void onClick(View v) { |
| 92 | + if(v.getWidth() == v.getHeight()) { |
| 93 | + text.setVisibility(VISIBLE); |
| 94 | + } else { |
| 95 | + text.setVisibility(GONE); |
| 96 | + if(onClickListener != null) onClickListener.onClick(button); |
| 97 | + } |
| 98 | + new Magic().doWith(v); |
| 99 | + } |
| 100 | + }); |
| 101 | + } |
| 102 | +} |
0 commit comments