Pliskin Hunter
Pliskin Hunter

Reputation: 15

How to code a button with differents effects when pressed multiple times

I may be asking a basic question, but to be honest, I have no real developement or code knowledge. I've been requested to make a prototybe of some basic app, that is supposed mainly to be buttons on screens, activable or desactivable. I've written some kind of TL;DR in case my explanations are bad

I've been coding this on Android Studio 3.0, I (hardly) managed to place PNGs files on the screen, making it looking like a button. Thing is, while some parts of the app are mainly constituted with independants togglable button. There a part where pressing a button must deselect the others. AND, if this button is pressed a second time open another activities.

Here's part of my code I'm using.

This one for independants buttons

indbutton1.setOnTouchListener(new View.OnTouchListener(){
        // track if the image is selected or not
        boolean isSelected = true;

        public boolean onTouch(View v, MotionEvent event) {
            if(isSelected) {
                indbutton1.setImageResource(R.drawable.indbutton1slct);
            } else {
                indbutton1.setImageResource(R.drawable.indbutton1nosl);
            }
            // toggle the boolean
            isSelected = !isSelected;
            return false;
        }
    });

And this one for going into other activities

  movements.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent gestesActivity = new Intent (getApplicationContext(), movements.class);
            startActivity(movementsActivity);
            finish();
        }
    });

TL;DR How should I proceed to have a mix of pressing button, disabling others enabled, then, when pressed a second time, I go to another activity.

Thank you for any help :) -Pliskin

Upvotes: 1

Views: 118

Answers (1)

Whole Brain
Whole Brain

Reputation: 2167

Here is how I would do that. Let's say you have 4 buttons.

// your class fields
boolean [] alreadyTouched = new boolean[4];

For each of the 4 buttons :

button0.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if(!alreadyTouched[0]){
            setAlreadyTouched(0);
            // one click actions here
        }else{
            // second click's action
        }       


    }
});

Now you make a private method in your class :

private void setAlreadyTouched(int index){
    for (int i = 0; i< alreadyTouched.length; i++)
        alreadyTouched[i] = false;
    if(index != -1)
        alreadyTouched[index] = true;
}

And to reset your boolean array when the button looses the focus :

button0.setOnFocusChangeListener(new View.OnFocusChangeListener(){
    @Override
    public void onFocusChange(View view, boolean hasFocus){
        if(!hasFocus)
            setAlreadyTouched(-1);
    }
});

You can do exactly the same thing but with an array of int if you want more than two clicks with some slight modifications. For example :

// your class fields
int[] alreadyTouched = new int[4];

Your privat method :

private void setAlreadyTouched(int index){
    if(index == -1){
        for (int i = 0; i< alreadyTouched.length; i++)
            alreadyTouched[i]=0;
    }else
        alreadyTouched[index] = alreadyTouched[index] +1 ;
}

Then just add some if in your onClickListeners :

button0.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        setAlreadyTouched(0);
        switch(alreadyTouched[0]){            
            case 1:
            // one click actions here
            break:
            case 2:
            // second click's action
            break:
            // ... and so on
            default:
            // action for max number of clicks here.
        }                   
    }
});

Upvotes: 1

Related Questions