Home > Software design >  Counter system implement Inside Dialog in android using Java
Counter system implement Inside Dialog in android using Java

Time:10-13

enter image description here

I have created a dialog and made a counter system, as shown above.

My Code :

pax.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Dialog dialog = new Dialog(getContext());
        dialog.setContentView(R.layout.add_pax);
        dialog.show();

        EditText adultfinalCount = dialog.findViewById(R.id.adultCount);
        EditText childfinalCount = dialog.findViewById(R.id.childCount);
        EditText infantfinalCount = dialog.findViewById(R.id.infantCount);

        Button adultPlus = dialog.findViewById(R.id.adultPos);
        adultPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adultfinalCount.setText("1");
            }
        });

        adultfinalCount.setText(""   adultCounter[0]);
        Button childPlus = dialog.findViewById(R.id.childPos);
        Button infantPlus = dialog.findViewById(R.id.infantPos);
        Button adultMinus = dialog.findViewById(R.id.adultNeg);
        Button childMinus = dialog.findViewById(R.id.childNeg);
        Button infantMinus = dialog.findViewById(R.id.infantNeg);

        Button savebtn =  dialog.findViewById(R.id.saveBtn);
        savebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }
});

I have been try all this . So How can I workable this as counter , Please Help Me. Adult always 1 and cannot more than 9 , Then If adult>1 then Child option and Infant Option will be enabaled.

CodePudding user response:

There are many ways to implement it. One way to do so is: If I understood correctly and you want to achieve that by using Minus & Plus buttons, you should create a global counter - (int adultCounter;) and then increment his value with each click (on the adultPlus button, of course) You should set the adultfinalCount text value to this adultCounter value (as a String). Then, you'll be able to condition this value and manipulate everything you want.

CodePudding user response:

Basically you can implement a switch statement for choosing your buttons. Example:

public class CounterSystem implements OnClickListener {

    Button b1 = (Button) findViewById(R.id.adultPos);   
    Button b2 = (Button) findViewById(R.id.adultNeg); 

    b1.setOnClickListener(this);  
    b2.setOnClickListener(this);

    public void onClick(View click) {

      switch(click.getId()) {

        case R.id.adultPos:

          // your code for counter etc. and button visibility
          break;

        case R.id.adultNeg:

          // your code for counter etc. and button visibility
          break;
      }
   }
}

Afterwards you can implement a counter in your code and use counter in the switch cases on every button click. As soon as the counter reaches 9, use the Button method setVisibility(int visibility) (use if & else depending on the actual value of the counter).

E.g. you can make the button disappear with the method:
b1.setVisibility(View.GONE)

https://developer.android.com/reference/android/view/View#setVisibility(int)

Test this simple approach in a few ways and then build up your code for more elements (will be easier for you, if you have less items in your layout).

  • Related