Avani
Avani

Reputation: 29

Solution for switchcase in multi switch

In this code

CompoundButton.OnCheckedChangeListener multiListener = new CompoundButton.OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton v, boolean isChecked) {
                switch (v.getId()){
                    case R.id.msg:
                        startActivity(new Intent(Mainfunction.this, Get_messages.class));
                        break;
                    case R.id.cnt:
                        Toast.makeText(Mainfunction.this, "Contact In Procces", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.call:
                        startActivity(new Intent(Mainfunction.this, Get_callLog.class));
                        break;
                    case R.id.loc:
                        Toast.makeText(Mainfunction.this, "Location In Procces", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.ring:
                        Toast.makeText(Mainfunction.this, "Ringing In Proccess", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        };

        //on each switch
        ((Switch) findViewById(R.id.msg)).setOnCheckedChangeListener(multiListener);
        ((Switch) findViewById(R.id.cnt)).setOnCheckedChangeListener(multiListener);
        ((Switch) findViewById(R.id.call)).setOnCheckedChangeListener(multiListener);
        ((Switch) findViewById(R.id.loc)).setOnCheckedChangeListener(multiListener);
        ((Switch) findViewById(R.id.ring)).setOnCheckedChangeListener(multiListener);

In switch case my intent activity Get_messages and Get_callLog will call both the time when I switch on or off but i want to call at only switch on for that what can I do ??

Upvotes: 0

Views: 105

Answers (4)

Basil jose
Basil jose

Reputation: 780

Check whether the compound button is checked. use an if() loop.

public void onCheckedChanged(CompoundButton v, boolean isChecked) {
   if (isChecked) {
               /*your code here*/
       } 
}

Upvotes: 0

KuLdip PaTel
KuLdip PaTel

Reputation: 1069

you can do it like this.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
   findViewById(R.id.msg)).setOnCheckedChangeListener(this);
   findViewById(R.id.cnt)).setOnCheckedChangeListener(this);
   findViewById(R.id.call)).setOnCheckedChangeListener(this);
   findViewById(R.id.loc)).setOnCheckedChangeListener(this);
   findViewById(R.id.ring)).setOnCheckedChangeListener(this);
}


public void onCheckedChanged(CompoundButton v, boolean isChecked) {
       if (isChecked) {
        switch (v.getId()){
                case R.id.msg:
                    startActivity(new Intent(Mainfunction.this, Get_messages.class));
                    break;
                case R.id.cnt:
                    Toast.makeText(Mainfunction.this, "Contact In Procces", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.call:
                    startActivity(new Intent(Mainfunction.this, Get_callLog.class));
                    break;
                case R.id.loc:
                    Toast.makeText(Mainfunction.this, "Location In Procces", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.ring:
                    Toast.makeText(Mainfunction.this, "Ringing In Proccess", Toast.LENGTH_SHORT).show();
                    break;
             }
        }
  }
}

Upvotes: 0

Sagar Patel
Sagar Patel

Reputation: 224

CompoundButton.OnCheckedChangeListener multiListener = new CompoundButton.OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton v, boolean isChecked) {
 if (isChecked) {
                switch (v.getId()){
                    case R.id.msg:
                        startActivity(new Intent(Mainfunction.this, Get_messages.class));
                        break;
                    case R.id.cnt:
                        Toast.makeText(Mainfunction.this, "Contact In Procces", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.call:
                        startActivity(new Intent(Mainfunction.this, Get_callLog.class));
                        break;
                    case R.id.loc:
                        Toast.makeText(Mainfunction.this, "Location In Procces", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.ring:
                        Toast.makeText(Mainfunction.this, "Ringing In Proccess", Toast.LENGTH_SHORT).show();
                        break;
                }
              }
            }
        };

        //on each switch
        ((Switch) findViewById(R.id.msg)).setOnCheckedChangeListener(multiListener);
        ((Switch) findViewById(R.id.cnt)).setOnCheckedChangeListener(multiListener);
        ((Switch) findViewById(R.id.call)).setOnCheckedChangeListener(multiListener);
        ((Switch) findViewById(R.id.loc)).setOnCheckedChangeListener(multiListener);
        ((Switch) findViewById(R.id.ring)).setOnCheckedChangeListener(multiListener);

Upvotes: 1

Ben P.
Ben P.

Reputation: 54204

If you want to do something only when the item becomes selected, you could wrap your entire switch statement inside an if:

public void onCheckedChanged(CompoundButton v, boolean isChecked) {
    if (isChecked) {
        switch (v.getId()){
            // cases go here
        }
    }
}

Upvotes: 1

Related Questions