Skip to content Skip to sidebar Skip to footer

Customview Listener(callback) With Multiple Button And Different Id

I am trying to make a SlideButton Library everything is almost complete but there is a problem in onClickListener The problem is when i use customView(i.e SlideButton) in xml two o

Solution 1:

Try to define the callback interface like this:

publicinterfaceSlideListener {
    voidonClick(SlideButton button, boolean active);
}

And everywhere in SlideButton class call the onClick with passing current reference:

if (listener != null) {
    listener.onClick(SlideButton.this, true/false);
}

Then in MainActivity check the id of clicked button as following:

@Overridepublic void onClick(SlideButton button, boolean active) {

    if(button.getId() == R.id.btnUp) {
        if(active){
            Toast.makeText(this, "Active Up", Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(this, "Deactivate Up", Toast.LENGTH_SHORT).show();
        }
    } elseif(button.getId() == R.id.btnDown) {
        if(active){
            Toast.makeText(this, "Active Down", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "Deactivate Down", Toast.LENGTH_SHORT).show();
        }
    }
}

Post a Comment for "Customview Listener(callback) With Multiple Button And Different Id"