Question

I'm new on android when I try to create object of OnClickListener and import android.view.view I have an error "Create view class in package android.view" and when I import android.view.view.OnClickListener I have an error "Create class OnClickListener in package android.view.view".

Was it helpful?

Solution

/this is the creation of an OnClickListener/

View.OnClickListener myOnClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //do the things u need to do when u click the view
            }
        };

/this is the part when u set the on click listener/

myButton.setOnClickListener(myOnClickListener);

Another way to do that is to implement the on click listener

public class MyActivity extends Activity implements OnClickListener{
   //the code  of your activity
   @Override
   public void onCreate(...){
       myButton.setOnClickListener(this);
    }
    //some other methods
    @Override
    public void onClick(View v) {
        if(v.getId == R.id.theIdOfTheViewInXML)
        //do the things u need to do when u click the view
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top