Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: create click listener programmatically with anonymous class

Tags:

java

android

I see a few examples of making a TextView clickable by setting onClick="clickHandler" and clickable="true". Is there a way to use an anonymous class instead of hard coding a clickhandler method inside the activity.

like image 779
prostock Avatar asked Dec 01 '11 06:12

prostock


2 Answers

There you go

TextView tv = (TextView)findViewById(R.id.textview);
tv.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // do whatever stuff you wanna do here
    }
});
like image 163
Pete Houston Avatar answered Nov 19 '22 23:11

Pete Houston


public void setClickable (boolean clickable)

Enables or disables click events for this view. When a view is clickable 
it will change    its state to "pressed" on every click. Subclasses should 
set the view clickable to visually react to user's clicks.
Related XML Attributes

TextView tv = new TextView(this);
tv.setClickable(true);
tv.setOnClickListener(new OnClickListener(){
   public void onClick(View v) {
   }
});
like image 39
Rajdeep Dua Avatar answered Nov 20 '22 00:11

Rajdeep Dua