chandra chandra - 5 months ago 11
Android Question

can we use one button for two or more conditions

I am beginner for android this is my first app so i am getting this much of doubts. Can we use one Button for three or more conditions.I did like this but its not working help me(in this source and destination are two different auto complete textview)

enter code here

String[] source ={"Chennai","Hyderabad","Bangalore"};
String[] destination ={"Chennai","Hyderabad","Bangalore"};

if(source[0].equals("Chennai") &&destination[1].equals("Hyderabad"))
{
Button button=(Button)findViewById(R.id.searchtrain_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,Main2Activity.class);
startActivity(i);
}
});

}
else if (source[0].equals("Chennai") && destination[2].equals("Bangalore"))
{
Button button1=(Button)findViewById(R.id.searchtrain_button);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i1=new Intent(MainActivity.this,Main3Activity.class);
startActivity(i1);
}
})

Answer

Dont you think you have to check your autocompletetextview text instead of your array because your array condition will be true always so it will never going to else condition.

Button button=(Button)findViewById(R.id.searchtrain_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(yourSourceAutoCompleteTextView.getText().toString().equals("Chennai") && yourDestinationAutoCompleteTextView.getText().toString().equals("Hyderabad")) {
                    Intent i=new Intent(MainActivity.this,Main2Activity.class);
                    startActivity(i);
                }
                else if(yourSourceAutoCompleteTextView.getText().toString().equals("Chennai") && yourDestinationAutoCompleteTextView.getText().toString().equals("Bangalore")){
                    Intent i=new Intent(MainActivity.this,Main3Activity.class);
                    startActivity(i);
                }
            }
        });
Comments