Reputation: 325
I am very new to android and this is my first day doing it. I am creating this app which has 2 buttons and is responsible for changing the color of the layout once it is pressed. As the button is pressed, I am using Toast class to display a message saying that the color is changed. But I want the logic to be such that the toast message for a particular button is displayed only once and not for subsequent same button presses. To do this I used a boolean flag and as soon as the button is clicked, changed the boolean variable to false and then not display the toast message. But there is a problem in this logic. The app will not display the toast message for future clicks. I would really appreciate if anyone would help me a little bit in this logic so that the app would know be smart enough to handle future presses.
public class MainActivity extends AppCompatActivity {
private Button myblue_button;
private Button mygreen_button;
private LinearLayout backGround;
private TextView textArea;
boolean forBLue = true;
boolean forGreen = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textArea = (TextView) findViewById(R.id.textID); //id for text area to later change it
backGround = (LinearLayout) findViewById(R.id.linearlayoutID);
myblue_button = (Button) findViewById(R.id.blue_button);
myblue_button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
backGround.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_bright));
textArea.setText("Now we feel blue");
if (forBLue == true){
Toast.makeText(MainActivity.this,"Now we feel blue",Toast.LENGTH_SHORT).show();
forBLue = false;
}
}
});
mygreen_button = (Button) findViewById(R.id.green_button);
mygreen_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
backGround.setBackgroundColor(getResources().getColor(android.R.color.holo_green_light));
textArea.setText("Now we feel green");
if (forGreen == true) {
Toast.makeText(MainActivity.this, "Now we feel green", Toast.LENGTH_SHORT).show();
forGreen = false;
}
}
});
}
}
Following is the XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearlayoutID"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id = "@+id/textID"
android:text="Click the button that best represents your mood"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<Button
android:id="@+id/blue_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BLUE"
android:layout_weight="1"
/>
<Button
android:id="@+id/green_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GREEN"
android:layout_weight="1"
/>
</LinearLayout>>
Upvotes: 0
Views: 2520
Reputation: 2401
Do it like this:
boolean shouldShowToastOnBlue = true;
boolean shouldShowToastOnGreen = true;
...
myblue_button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
backGround.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_bright));
textArea.setText("Now we feel blue");
if (shouldShowToastOnBlue){
Toast.makeText(MainActivity.this,"Now we feel blue",Toast.LENGTH_SHORT).show();
shouldShowToastOnBlue = false;
}
shouldShowToastOnGreen = true;
}
});
mygreen_button = (Button) findViewById(R.id.green_button);
mygreen_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
backGround.setBackgroundColor(getResources().getColor(android.R.color.holo_green_light));
textArea.setText("Now we feel green");
if (shouldShowToastOnGreen) {
Toast.makeText(MainActivity.this, "Now we feel green", Toast.LENGTH_SHORT).show();
shouldShowToastOnGreen = false;
}
shouldShowToastOnBlue = true;
}
});
Upvotes: 0
Reputation: 155
You need to do this :
if (forBLue == true){
Toast.makeText(MainActivity.this,"Now we feel blue",Toast.LENGTH_SHORT).show();
forBLue = false;
forGreen = true ;//edit here
}
and :
if (forGreen == true) {
Toast.makeText(MainActivity.this, "Now we feel green", Toast.LENGTH_SHORT).show();
forGreen = false;
forBlue = true;//edit here
}
Upvotes: 1
Reputation: 235
private int blue, green = 0;
myblue_button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
backGround.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_bright));
textArea.setText("Now we feel blue");
if (blue == 0){
Toast.makeText(MainActivity.this,"Now we feel blue",Toast.LENGTH_SHORT).show();
blue = 1;
green = 0;
}
}
});
mygreen_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
backGround.setBackgroundColor(getResources().getColor(android.R.color.holo_green_light));
textArea.setText("Now we feel green");
if (green == 0) {
Toast.makeText(MainActivity.this, "Now we feel green", Toast.LENGTH_SHORT).show();
green = 1;
blue = 0;
}
}
});
}
}
Here is a code this will fix your problem.
Upvotes: 1