Swipe Element Switch instead of clicking on Android Studio

0

Good afternoon, my question is quite simple but I can not find how to make this element work in Android Studio:

Switch activar;
Boolean switchState;

activar.setOnClickListener(new View.OnClickListener() {
  @Override public void onClick(View v) {
     switchState = activar.isChecked ();
                switche();
       }
});

private void switche(){
        if(switchState==false){             Toast.makeText(getApplicationContext(),"Desactivado",Toast.LENGTH_SHORT).show();
        btnaceptar.setEnabled(false); }
        if(switchState==true){    Toast.makeText(getApplicationContext(),"Activado",Toast.LENGTH_SHORT).show();
btnaceptar.setEnabled(false);}

My code works correctly by "CLick" on the Switch element, however if you slide your finger on it to turn it on or off it does not change its status or execute the "switch ()" class, and I understand that it is because I have it with a "setOnClickListener" but, How should I change it to detect if it slides with my finger instead of being clicked?

Thank you in advance.

    
asked by karasu55 25.10.2017 в 03:01
source

2 answers

1

You can implement something like this.

 private static final String TAG = MainActivity.class.getSimpleName();
 Switch sw;


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    sw = (Switch) findViewById(R.id.switch1);

    //Anulas el evento onClick, si haces click
    //Retorna a su estado original
    sw.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.v(TAG,"onClick");
            sw.setChecked(!sw.isChecked());
        }
    });

    //Si mantienes presionado un poco mas de tiempo
    //se ejecuta, pero puedes arrastrar el switch.
    sw.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            Log.v(TAG,"onLongClick");
            sw.setChecked(!sw.isChecked());
            switche();
            return true;
        }


    });

}

Here the switche () method is slightly modified.

private void switche() {


            if (!sw.isChecked()) {
                Toast.makeText(getApplicationContext(), "Desactivado", Toast.LENGTH_SHORT).show();
                //btnaceptar.setEnabled(false);
            } else {
                Toast.makeText(getApplicationContext(), "Activado", Toast.LENGTH_SHORT).show();
                //btnaceptar.setEnabled(false);
            }

    }
    
answered by 25.10.2017 / 23:14
source
0

Thank you very much Gustavo, I already tried the code and it worked perfectly.

private static end String TAG = MainActivity.class.getSimpleName ();  Switch sw;

@Override     protected void onCreate (Bundle savedInstanceState) {         super.onCreate (savedInstanceState);         setContentView (R.layout.activity_main);

sw = (Switch) findViewById(R.id.switch1);

//Anulas el evento onClick, si haces click
//Retorna a su estado original
sw.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Log.v(TAG,"onClick");
        sw.setChecked(!sw.isChecked());
    }
});

//Si mantienes presionado un poco mas de tiempo
//se ejecuta, pero puedes arrastrar el switch.
sw.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View view) {
        Log.v(TAG,"onLongClick");
        sw.setChecked(!sw.isChecked());
        switche();
        return true;
    }


});

}

    
answered by 27.10.2017 в 23:30