Praveenkumar
Praveenkumar

Reputation: 24496

OnClick event in 1.5?

I just changed my application's(2.2) version into 1.5. After, that i've an error in my XML files like this

error: No resource identifier found for attribute 'onClick' in package 'android'

Why i can't use that method. Otherwise, is there any additional jars are available to use this method in Android version 1.5. Anyone tell me.

Upvotes: 2

Views: 1249

Answers (2)

Nguyen  Minh Binh
Nguyen Minh Binh

Reputation: 24433

You should turn to use the simplest way that I always do as below:

@Override
public void onCreate(Bundle savedInstanceState) {
    button1.setOnClickListener(onClickListener);
        button2.setOnClickListener(onClickListener);
        button3.setOnClickListener(onClickListener);
}

private OnClickListener onClickListener = new OnClickListener() {

    @Override
    public void onClick(final View v) {
             switch(v.getId()){
                 case R.id.button1:
                      //DO something
                 break;
                 case R.id.button2:
                      //DO something
                 break;
                 case R.id.button3:
                      //DO something
                 break;
             }

    }
};

Upvotes: 0

Caner
Caner

Reputation: 59238

onClick attribute is not defined for API 3 and less. (Android <= 1.5)

It works since API 4 (Android 1.6)

EDIT

If you want compatibility you can use:

findViewById(R.id.myButton).setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // Do stuff
    }
});

http://android-developers.blogspot.com/2009/10/ui-framework-changes-in-android-16.html

Upvotes: 6

Related Questions