Oliver
Oliver

Reputation: 65

How to tie If Statements together into one bigger If Statement in Java

I have a basic program. I wish to set the background of individual buttons, based on user click count, then IF all buttons have the same background, the user will proceed to the next activity. Simple enough, or so I thought... I just can't seem to figure out how to compare the IF statements to each-other. I've spent like an hour writing and rewriting code, trying to find an answer. There has to be an easy answer I'm missing and just can't wrap my mind around.

public class MainActivity extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        addListenerOnButton();
    }

    Button a1;
    Button a2;
    Button a3;
    Button a4;

    static int count1=0;
static int count2=0;
static int count3=0;
static int count4=0;


    public void addListenerOnButton() {



        final Button a1 = (Button) findViewById(R.id.a1);
        a1.setOnClickListener(new OnClickListener()                
                @Override
                public void onClick(View v) {
                    count1=count1+1;
                    if(count1==1||count1==4||count1==8)
                    {   a1.setBackgroundColor(Color.RED);
                        Toast.makeText(getApplicationContext(),"Button clicked !", Toast.LENGTH_LONG).show();}
                    else if(count1==2||count1==6)
                    {a1.setBackgroundColor(Color.BLUE);
                        Toast.makeText(getApplicationContext(),"Button clicked" +count1, Toast.LENGTH_LONG).show();}
                    else{a1.setBackgroundColor(Color.YELLOW);
                        Toast.makeText(getApplicationContext(),"Button clicked" +count1, Toast.LENGTH_LONG).show();
                    }}});
        final Button a2 = (Button) findViewById(R.id.a2);
        a2.setOnClickListener(new OnClickListener() {
                int clickcount=0;
                @Override
                public void onClick(View v) {
                    clickcount=clickcount+1;
                    if(clickcount==2||clickcount==4||clickcount==7)
                    {   a2.setBackgroundColor(Color.RED);
                        Toast.makeText(getApplicationContext(),"Button clicked first time!", Toast.LENGTH_LONG).show();}
                    else if(clickcount==1||clickcount==3)
                    {a2.setBackgroundColor(Color.BLUE);
                        Toast.makeText(getApplicationContext(),"Button clicked" +clickcount, Toast.LENGTH_LONG).show();}
                    else{a2.setBackgroundColor(Color.YELLOW);
                        Toast.makeText(getApplicationContext(),"Button clicked" +clickcount, Toast.LENGTH_LONG).show();
                    }}});
        final Button a3 = (Button) findViewById(R.id.a3);
        a3.setOnClickListener(new OnClickListener() {
                int clickcount=0;
                @Override
                public void onClick(View v) {
                    clickcount=clickcount+1;
                    if(clickcount==1||clickcount==2||clickcount==6)
                    {   a3.setBackgroundColor(Color.RED);
                        Toast.makeText(getApplicationContext(),"Button clicked first time!", Toast.LENGTH_LONG).show();}
                    else if(clickcount==4||clickcount==5||clickcount==7)
                    {a3.setBackgroundColor(Color.BLUE);
                        Toast.makeText(getApplicationContext(),"Button clicked" +clickcount, Toast.LENGTH_LONG).show();}
                    else{a3.setBackgroundColor(Color.YELLOW);
                        Toast.makeText(getApplicationContext(),"Button clicked" +clickcount, Toast.LENGTH_LONG).show();
                    }}});

        final Button a4 = (Button) findViewById(R.id.a4);
        a4.setOnClickListener(new OnClickListener() {
                int clickcount=0;
                @Override
                public void onClick(View v) {
                    clickcount=clickcount+1;
                    if(clickcount==3||clickcount==5||clickcount==7||clickcount==8)
                    {   a4.setBackgroundColor(Color.RED);
                        Toast.makeText(getApplicationContext(),"Button clicked first time!", Toast.LENGTH_LONG).show();}
                    else if(clickcount==2||clickcount==9)
                    {a4.setBackgroundColor(Color.BLUE);
                        Toast.makeText(getApplicationContext(),"Button clicked" +clickcount, Toast.LENGTH_LONG).show();}
                    else{a4.setBackgroundColor(Color.YELLOW);
                        Toast.makeText(getApplicationContext(),"Button clicked" +clickcount, Toast.LENGTH_LONG).show();
                }}});           



        }


}
        public void checkFunc(){
        if(count1==1)       

            {   
                Intent browserIntent =
                    new Intent(Intent.ACTION_VIEW,     Uri.parse("https://www.google.com/"));
                startActivity(browserIntent);

        }
    }


    }

One way I have considered achieving my goal is something along the lines of

if((a1.setBackgroundColor(Color.BLUE)==true)||    (a2.setBackgroundColor(Color.BLUE)==true))

Edit** So after trying a few things I edited my code a bit and still cant get it to work. Any further suggestions?

Upvotes: 0

Views: 95

Answers (2)

Aditya Mulgundkar
Aditya Mulgundkar

Reputation: 51

You can do the comparison using static variables instead. Also declare the variables globally. It's not a great approach, but here goes:

//global static vars
static int count1=0;
static int count2=0;
static int count3=0;
static int count4=0;

//make sure you include your conditions. I've added this just for reference
public void addListenerOnButton() {  
  a1.setOnClickListener(new OnClickListener() {  
@Override public void onClick(View v)  
  {
  count1++;
  }
}

a2.setOnClickListener(new OnClickListener() {  
  @Override public void onClick(View v)  
 {
  count2++;
  }
}

//And so on...
}   

 public void checkFunc()
{
if(count1==count2==count3==count4)
  {
  //do something
  }
}

Upvotes: 1

Black Glix
Black Glix

Reputation: 709

I suggest that, declare clickCount variables inside class, not inside click handler. So you can reach each button's click count every clickHandler method. So, write a method that compares clickCount's, and call this method inside every clickHandler.

private int clickCount1, clickCount2, clickCount3, clickCount4;
private void compareClickCounts(){
    // Compare clickCounts and change buttons properties
}

Upvotes: 0

Related Questions