Question

I'm having an issue with dynamic view creation and OnClickListener. I want my dynamic view to do things when I click on the it. To do so, I've implemented OnClickListener:

import android.view.View.OnClickListener;

public abstract class ChordDiagram extends View implements OnClickListener {

...

@Override
public void onClick(View v) {       
//it's gonna be implemented by its sons
}
}

Then, I have another class:

public class KeyboardChordDiagram extends ChordDiagram {

...

@Override
    public void onClick(View v) {               
        Toast.makeText(v.getContext(), "Here's gonna do something :)", Toast.LENGTH_LONG).show();
    }
}   

I'm kinda new to Android and I suspect it has something to do with the context, but I really don't know what it is. I also tried to implement OnClickListener on the activity class and set the my real event to the activity method, but it didn't work either.

Was it helpful?

Solution

Add that line in every constructor of your class: this.setOnClickListerner(this);

For example:

public abstract class ChordDiagram extends View implements OnClickListener
{
    public ChordDiagram(Context context)
    {
        super(context);

        this.setOnClickListerner(this);
    }

    @Override
    public void onClick(View v)
    {
        //it's gonna be implemented by its sons
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top