Advertisement
Guest User

Counter Class

a guest
Sep 20th, 2018
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.76 KB | None | 0 0
  1. package com.example.will.counterapp;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.graphics.Color;
  6. import android.support.v4.app.Fragment;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.util.TypedValue;
  9. import android.view.Gravity;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.Button;
  13. import android.widget.LinearLayout;
  14. import android.widget.TextView;
  15.  
  16. public class Counter implements View.OnClickListener {
  17.  
  18.     private final int buttonPixels = 100;
  19.     private final int buttonTextSizePixels = buttonPixels / 10;
  20.     private final int addButtonId = View.generateViewId();
  21.     private final int subButtonId = View.generateViewId();
  22.     private int buttonTextSize = 0;
  23.     private int buttonWidth = 0;
  24.  
  25.  
  26.     private Button add;
  27.     private Button sub;
  28.     private TextView disp;
  29.  
  30.  
  31.     private final int countTextSizePixels = 20;
  32.     private final int countTextId = View.generateViewId();
  33.     private int countTextSize = 0;
  34.  
  35.  
  36.     private final int layoutPixels = 100;
  37.     private final int layoutId = View.generateViewId();
  38.     private int layoutHeight = 0;
  39.  
  40.     private ViewGroup mainContainer = null;
  41.     private Activity activity = null;
  42.     private Context context = null;
  43.     private LinearLayout container = null;
  44.     private Button addButton = null;
  45.     private TextView countDisp = null;
  46.     private Button subButton = null;
  47.  
  48.     private Integer count = 0;
  49.     private Integer index;
  50.  
  51. //    private String name = "";
  52.  
  53.  
  54.     //----------------------------------------CONSTRUCTORS------------------------------------------
  55.  
  56.  
  57.     public Counter(Context context, Integer index, ViewGroup mainContainer) {
  58.         this.index = index;
  59.         this.context = context;
  60.         this.mainContainer = mainContainer;
  61.         this.container = new LinearLayout(this.context);
  62.         this.buttonWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, buttonPixels, this.context.getResources().getDisplayMetrics());
  63.         this.countTextSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, countTextSizePixels, this.context.getResources().getDisplayMetrics());
  64.         this.buttonTextSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, buttonTextSizePixels, this.context.getResources().getDisplayMetrics());
  65.         this.layoutHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, layoutPixels, this.context.getResources().getDisplayMetrics());
  66.     }
  67.  
  68.  
  69.     public void createCounter() {
  70.         createLinearLayout();
  71.         createSubButton();
  72.         createTextView();
  73.         createAddButton();
  74.     }
  75.  
  76. //    addCounterButton.setOnClickListener(new View.OnClickListener() {
  77. //        @Override
  78. //        public void onClick(View v) {
  79. //            counterArr.add(new Counter(MainActivity.this, index[0]));
  80. //            counterArr.get(index[0]).createCounter();
  81. //            linLayContainer.addView(counterArr.get(index[0]).getContainer());
  82. //            index[0]++;
  83. //        }
  84. //
  85. //    });
  86.  
  87.  
  88.     private void createLinearLayout() {
  89.         String colorString;
  90.  
  91.         container.setLayoutParams(new LinearLayout.LayoutParams(
  92.                 LinearLayout.LayoutParams.MATCH_PARENT,
  93.                 LinearLayout.LayoutParams.MATCH_PARENT));
  94.         container.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, this.layoutHeight));
  95.         container.setOrientation(LinearLayout.HORIZONTAL);
  96.  
  97.         colorString = (this.index % 2 == 1) ? "#5279b7" : "#2a5fb2";
  98.         container.setBackgroundColor(Color.parseColor(colorString));
  99.     }
  100.  
  101.     private void createSubButton() {
  102.  
  103.         subButton = new Button(context);
  104.         subButton.setLayoutParams(new LinearLayout.LayoutParams(buttonWidth, LinearLayout.LayoutParams.MATCH_PARENT, 0.5f));
  105.         subButton.setText("-");
  106.         subButton.setTextSize(buttonTextSize);
  107.         subButton.setId(subButtonId);
  108.         subButton.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL);
  109.         container.addView(subButton);
  110.  
  111.         sub = mainContainer.findViewById(subButtonId);
  112.         sub.setOnClickListener(new MyOnClickListener() {
  113.             @Override
  114.             public void onClick(View v) {
  115.                 subCount();
  116.                 disp.setText(count.toString());
  117.             }
  118.         });
  119.     }
  120.  
  121.     private void createTextView() {
  122.         countDisp = new TextView(context);
  123.         countDisp.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 2.0f));
  124.         countDisp.setText(this.count.toString());
  125.         countDisp.setTextSize(this.countTextSize);
  126.         countDisp.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL);
  127.         countDisp.setTextColor(Color.BLACK);
  128.         countDisp.setId(countTextId);
  129.  
  130.         container.addView(countDisp);
  131.  
  132.         disp = mainContainer.findViewById(countTextId);
  133.     }
  134.  
  135.     private void createAddButton() {
  136.         addButton = new Button(context);
  137.         addButton.setLayoutParams(new LinearLayout.LayoutParams(buttonWidth, LinearLayout.LayoutParams.MATCH_PARENT, 0.5f));
  138.         addButton.setText("+");
  139.         addButton.setTextSize(buttonTextSize);
  140.         addButton.setId(addButtonId);
  141.         addButton.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL);
  142.  
  143.         container.addView(addButton);
  144.  
  145.         add = mainContainer.findViewById(addButtonId);
  146.         sub.setOnClickListener(this);
  147.     }
  148.  
  149. //-------------------------------------------SETTERS--------------------------------------------
  150.  
  151.     public void addCount() {
  152.         this.count++;
  153.     }
  154.  
  155.     public void subCount() {
  156.         this.count--;
  157.     }
  158.  
  159.     public void resetCount() {
  160.         this.count = 0;
  161.     }
  162.  
  163.     public void deleteCounter() {
  164.  
  165.     }
  166.  
  167.     //    public void setName(String name) {
  168.     //        this.name = name;
  169.     //    }
  170.  
  171.     //-------------------------------------------GETTERS--------------------------------------------
  172.  
  173.  
  174.     public String getCount() {
  175.         return this.count.toString();
  176.     }
  177.  
  178.     public int getIndex() {
  179.         return this.index;
  180.     }
  181.  
  182.     public LinearLayout getContainer() {
  183.         return this.container;
  184.     }
  185.  
  186.     @Override
  187.     public void onClick(View v) {
  188.         if (v.getId() == addButtonId) {
  189.             addCount();
  190.             disp.setText(count.toString());
  191.         } else if (v.getId() == subButtonId) {
  192.             subCount();
  193.             disp.setText(count.toString());
  194.         }
  195.     }
  196.  
  197.  
  198.     //    public String getName() {
  199. //        return this.name;
  200. //    }
  201.  
  202. }
  203.  
  204.  
  205. class MyOnClickListener implements View.OnClickListener {
  206.  
  207.  
  208.     public MyOnClickListener() {
  209.  
  210.     }
  211.  
  212.     @Override
  213.     public void onClick(View v) {
  214.  
  215.     }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement