Lakshmi Narayanan
Lakshmi Narayanan

Reputation: 5362

Method not called during ImageView onClick

I am trying to develop the functionality, where the user can click on the image to select an image to upload on the same view being clicked. I define the ImageView as follows

    <ImageView
     style = "@style/DefaultButton"
     android:id="@+id/choose_img"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_centerHorizontal="true"
     android:layout_marginTop="46dp"
     android:clickable="True"
     android:adjustViewBounds="true"
     android:onClick="chooseImg"
     android:contentDescription="@string/description_logo"
     android:src="@drawable/user2" />

the method chooseImg is as follows

    public void chooseImg(View view){
        Toast.makeText(this, "Choose Image from Gallery", Toast.LENGTH_SHORT).show();
        Intent chimg = new Intent(Intent.ACTION_GET_CONTENT);
        chimg.setType("image/*");
        startActivityForResult(chimg,CHOOSE_IMAGE_REQUEST_CODE);
    }

The method is not being called. please help me out to know the reason. Thanks :).

Upvotes: 0

Views: 430

Answers (2)

Code Droid
Code Droid

Reputation: 10472

Casper is correct. I would add that you can have Activity itself implement View.OnClickListener interface

onClick(View v) { if (v.getId() == (Id for ImageView)) { captureImg(); }

}

Or as an anonymous innner class.

Upvotes: 1

hjweide
hjweide

Reputation: 12533

The method that gets called by android:onClick must match the method that you want to be executed... But your method on onClick is captureImg and your method name in activity is chooseImg. Change these to the same method name and it should solve your problem. Also see my comment.

Upvotes: 2

Related Questions