DaxHR
DaxHR

Reputation: 683

Button.setEnabled not enabling button when needed

So I want to have two buttons, one for adding 1 on TextView and 1 for substracting.

The result on textview mustn't be smaller then 0.

So I thought to set it disabled if the text is 0 so it can't go in negative values. But that doesn't work like I thought, when I press plus button and add 1 and then press minus button to substract 1 (result on textview is now 0), and then again add one with plus button and then substract, minus button is not working, it's not enabled.

Here's my code

int rezultat;
                rezultat = Integer.parseInt(brojIntervala.getText().toString());

                if (rezultat == 0){
                    homeScreenMinus.setEnabled(false);
                    brojIntervala.setText("0");
                } else if (rezultat > 0){
                    homeScreenMinus.setEnabled(true);
                }
            }
        });



package hiitbydario.com.daroioradecic.hiittraining;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class HomeScreen extends AppCompatActivity {
    Button homeScreenPlus, homeScreenMinus;
    TextView brojIntervala;
    ImageButton homeScreenStart;
    int counter = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_screen);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        homeScreenPlus = (Button) findViewById(R.id.homeScreenPlus);
        homeScreenMinus = (Button) findViewById(R.id.homeScreenMinus);
        homeScreenStart = (ImageButton) findViewById(R.id.homeScreenStart);

        brojIntervala = (TextView) findViewById(R.id.brojIntervala);

        homeScreenPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dodajInterval();
            }
        });

        homeScreenMinus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                oduzmiInterval();
                int rezultat;
                rezultat = Integer.parseInt(brojIntervala.getText().toString());

                if (rezultat == 0){
                    homeScreenMinus.setClickable(false);
                    brojIntervala.setText("0");
                } else if (rezultat > 0){
                    homeScreenMinus.setClickable(true);
                }
            }
        });


    }

    private void oduzmiInterval() {
        counter--;
        brojIntervala.setText(Integer.toString(counter));
    }

    private void dodajInterval() {
        counter++;
        brojIntervala.setText(Integer.toString(counter));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_home_screen, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Upvotes: 2

Views: 2427

Answers (2)

George Mulligan
George Mulligan

Reputation: 11923

I would recommend continuing to use setEnabled()over setClickable() so the user also can see that the button is disabled.

The problem is you are only ever updating the minus button's state when it is clicked. However, you need to update it when the plus button is clicked too so that it is re-enabled when transitioning from 0 to a positive value.

homeScreenPlus.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        dodajInterval();
        homeScreenMinus.setEnabled(counter > 0);
    }
});

Upvotes: 1

Maxim G
Maxim G

Reputation: 1489

Move rezultat to member variables.

homeScreenMinus.setEnabled(mRezultat > 0);
brojIntervala.setText(Integer.toString(mRezultat));

[Edit after full code review] there is no need for mRezultat, use counter directly.

Upvotes: 0

Related Questions