Reputation: 77
I am trying to write an if-else statement depending on what the user clicks. This is my main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:text="Food #1:"
android:textSize="20dp"
android:textStyle="bold"
android:layout_height="wrap_content"
android:id="@+id/txt_comparing"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true">
</TextView>
<Spinner
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_below="@+id/txt_comparing"
android:id="@+id/spn_1">
</Spinner>
<RadioGroup
android:id="@+id/canteen1"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/spn_1" >
<RadioButton
android:id="@+id/option_0"
android:text="ITAS "/>
<RadioButton
android:id="@+id/option_1"
android:text="Design "/>
<RadioButton
android:id="@+id/option_2"
android:text="Engineering "/>
</RadioGroup>
<TextView
android:text="Food #2:"
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:textStyle="bold"
android:layout_below="@+id/canteen1"
android:layout_height="wrap_content"
android:id="@+id/txt_and"
android:layout_width="wrap_content">
</TextView>
<Spinner
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_below="@+id/txt_and"
android:id="@+id/spn_2">
</Spinner>
<RadioGroup
android:id="@+id/canteen2"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/spn_2" >
<RadioButton
android:id="@+id/option1_0"
android:text="ITAS "/>
<RadioButton
android:id="@+id/option1_1"
android:text="Design "/>
<RadioButton
android:id="@+id/option1_2"
android:text="Engineering "/>
</RadioGroup>
<Button
android:id="@+id/btn_compare"
android:text=" Compare "
android:textStyle="bold"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true">>
</Button>
<Button
android:text=" Clear "
android:layout_height="wrap_content"
android:id="@+id/clear"
android:layout_toLeftOf="@+id/btn_compare"
android:layout_alignTop="@+id/btn_compare"
android:layout_alignBottom="@+id/btn_compare"
android:layout_width="wrap_content">
</Button>
In my main java class, how do I create an if-else statement depending on what the user clicks? I have tried and researched many times but did not get the answer.
Thank you in advance!
Upvotes: 0
Views: 1336
Reputation: 4121
You should assign OnClickListener to each clickable view (Button).
It could be either the same listener or new different instance for each button.
In case of single listener, you can implement button id switcher and got known which button is pressed.
I've spent a little time to prepare illustration:
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
Toast.makeText(getApplicationContext(), "Button 1 pressed", Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
Toast.makeText(getApplicationContext(), "Button 2 pressed", Toast.LENGTH_SHORT).show();
break;
}
}
};
((Button)findViewById(R.id.button1)).setOnClickListener(listener);
((Button)findViewById(R.id.button2)).setOnClickListener(listener);
Upvotes: 1