SquareView.java 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package com.khmer9.lotto.view;
  2. import android.content.Context;
  3. import android.content.res.TypedArray;
  4. import android.util.AttributeSet;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.widget.TextView;
  8. import androidx.annotation.NonNull;
  9. import androidx.annotation.Nullable;
  10. import androidx.constraintlayout.widget.ConstraintLayout;
  11. import com.khmer9.lotto.R;
  12. public class SquareView extends ConstraintLayout {
  13. TextView tvNumber;
  14. TextView tvRangNumber;
  15. public SquareView(@NonNull Context context) {
  16. super(context);
  17. init();
  18. }
  19. public SquareView(@NonNull Context context, @Nullable AttributeSet attrs) {
  20. super(context, attrs);
  21. init();
  22. setStyle(attrs);
  23. }
  24. public SquareView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  25. super(context, attrs, defStyleAttr);
  26. init();
  27. setStyle(attrs);
  28. }
  29. public SquareView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
  30. super(context, attrs, defStyleAttr, defStyleRes);
  31. init();
  32. setStyle(attrs);
  33. }
  34. private void init() {
  35. View view = LayoutInflater.from(getContext()).inflate(
  36. R.layout.view_square, this, true);
  37. tvNumber = view.findViewById(R.id.tv_number) ;
  38. tvRangNumber = view.findViewById(R.id.tv_range);
  39. }
  40. private void setStyle(AttributeSet attrs){
  41. TypedArray a = getContext().getTheme().obtainStyledAttributes(
  42. attrs,
  43. R.styleable.SquareView,
  44. 0, 0);
  45. try {
  46. String textNumber = a.getString(R.styleable.SquareView_numberValue);
  47. String textRangeNumber = a.getString(R.styleable.SquareView_rangNumber);
  48. tvNumber.setText(textNumber);
  49. tvRangNumber.setText(textRangeNumber);
  50. } finally {
  51. a.recycle();
  52. }
  53. }
  54. }