|
| 1 | +package net.buggy.components; |
| 2 | + |
| 3 | + |
| 4 | +import android.annotation.TargetApi; |
| 5 | +import android.content.Context; |
| 6 | +import android.graphics.drawable.ShapeDrawable; |
| 7 | +import android.graphics.drawable.shapes.RoundRectShape; |
| 8 | +import android.os.Build; |
| 9 | +import android.util.AttributeSet; |
| 10 | +import android.view.Gravity; |
| 11 | +import android.view.View; |
| 12 | +import android.widget.ImageButton; |
| 13 | +import android.widget.ImageView; |
| 14 | +import android.widget.LinearLayout; |
| 15 | +import android.widget.TextView; |
| 16 | + |
| 17 | +import java.util.Arrays; |
| 18 | +import java.util.List; |
| 19 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 20 | + |
| 21 | +import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; |
| 22 | + |
| 23 | +public class Chip extends LinearLayout { |
| 24 | + |
| 25 | + private TextView textView; |
| 26 | + |
| 27 | + private final List<Listener> listeners = new CopyOnWriteArrayList<>(); |
| 28 | + |
| 29 | + public Chip(Context context) { |
| 30 | + super(context); |
| 31 | + init(); |
| 32 | + } |
| 33 | + |
| 34 | + public Chip(Context context, AttributeSet attrs) { |
| 35 | + super(context, attrs); |
| 36 | + init(); |
| 37 | + } |
| 38 | + |
| 39 | + public Chip(Context context, AttributeSet attrs, int defStyleAttr) { |
| 40 | + super(context, attrs, defStyleAttr); |
| 41 | + init(); |
| 42 | + } |
| 43 | + |
| 44 | + @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
| 45 | + public Chip(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 46 | + super(context, attrs, defStyleAttr, defStyleRes); |
| 47 | + init(); |
| 48 | + } |
| 49 | + |
| 50 | + private void init() { |
| 51 | + final int dp4 = ViewUtils.dpToPx(4f, getContext()); |
| 52 | + final int dp13 = ViewUtils.dpToPx(13f, getContext()); |
| 53 | + final int dp32 = ViewUtils.dpToPx(32f, getContext()); |
| 54 | + |
| 55 | + setOrientation(HORIZONTAL); |
| 56 | + setGravity(Gravity.CENTER_VERTICAL); |
| 57 | + setPadding( |
| 58 | + ViewUtils.dpToPx(12f, getContext()), |
| 59 | + 0, |
| 60 | + 0, |
| 61 | + 0); |
| 62 | + |
| 63 | + textView = new TextView(getContext()); |
| 64 | + textView.setGravity(Gravity.CENTER); |
| 65 | + textView.setIncludeFontPadding(false); |
| 66 | + final LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams( |
| 67 | + WRAP_CONTENT, dp32); |
| 68 | + textView.setLayoutParams(textViewParams); |
| 69 | + |
| 70 | + final ImageButton imageButton = new ImageButton(getContext()); |
| 71 | + imageButton.setImageResource(R.drawable.ic_cancel_black_24dp); |
| 72 | + final LinearLayout.LayoutParams imageViewParams = new LinearLayout.LayoutParams( |
| 73 | + WRAP_CONTENT, WRAP_CONTENT); |
| 74 | + imageViewParams.setMargins(dp4, 0, dp4, 0); |
| 75 | + imageButton.setLayoutParams(imageViewParams); |
| 76 | + imageButton.setPadding(0, 0, 0, 0); |
| 77 | + imageButton.setScaleType(ImageView.ScaleType.CENTER_INSIDE); |
| 78 | + imageButton.setAlpha(0.30f); |
| 79 | + imageButton.setOnClickListener(new OnClickListener() { |
| 80 | + @Override |
| 81 | + public void onClick(View v) { |
| 82 | + fireCloseClicked(); |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + addView(textView, textViewParams); |
| 87 | + addView(imageButton, imageViewParams); |
| 88 | + |
| 89 | + final float[] corners = new float[8]; |
| 90 | + Arrays.fill(corners, dp13); |
| 91 | + final RoundRectShape rectShape = new RoundRectShape(corners, null, null); |
| 92 | + final ShapeDrawable background = new ShapeDrawable(rectShape); |
| 93 | + background.getPaint().setColor(0xFFE0E0E0); |
| 94 | + |
| 95 | + ViewUtils.setBackground(this, background); |
| 96 | + } |
| 97 | + |
| 98 | + public void setText(String text) { |
| 99 | + textView.setText(text); |
| 100 | + } |
| 101 | + |
| 102 | + private void fireCloseClicked() { |
| 103 | + for (Listener listener : listeners) { |
| 104 | + listener.closeClicked(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + public void addListener(Listener listener) { |
| 109 | + listeners.add(listener); |
| 110 | + } |
| 111 | + |
| 112 | + public interface Listener { |
| 113 | + void closeClicked(); |
| 114 | + } |
| 115 | +} |
0 commit comments