android onclick method want image path

1.5k views Asked by At

I display image from the web in my application. It is running perfectly

My Question is :

what is method,function to take current image URL when I click on image.? (I passed url as string for display).

I used this method:

private String getAnImageUrl() {
        i++;
        if(i >= images.length){
            i = 0;
        }

public class ImageExampleXML extends Activity {
private String reviewImageLink;

private final String images[] = {"http://clients..../britney.jpg",
                                     "http://client...../evala.jpg", 
                                     "http://clients.......ashlee.jpg",
                                     "http://clients.......mple_1.jpg",
                                                                         };
    private int i = 0;

    private String imgUrl;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        final Button loadImage = (Button) findViewById(R.id.button);

        final LoaderImageView image = (LoaderImageView) findViewById(R.id.loaderImageView);

        loadImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                image.setImageDrawable(getAnImageUrl());
            }
        });



      image.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {

             imgUrl = getAnImageUrl();
                Toast.makeText(getApplicationContext(), imgUrl+ " this one", Toast.LENGTH_LONG).show();     
            // TODO Auto-generated method stub

        }
    });  

        return images[i];
    }

I changed image through the button. there is problem when I clicked next time it will change URL but image remain same.

please see the method image.setOnClickListener in that onclick due to getAnImageUrl();

i changed the display name when clicked .. please tell me how I can find current image url...

Note: I used toast only to check what i got ?

Thanks in advance.

1

There are 1 answers

1
Arslan Anwar On BEST ANSWER

Simply what you can do is

loadImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgUrl = getAnImageUrl();
                image.setImageDrawable(imgUrl);
            }
        });

      image.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
                Toast.makeText(getApplicationContext(), imgUrl+ " this one", Toast.LENGTH_LONG).show();     
            // TODO Auto-generated method stub

        }

Or you can also User setTag(Object object); method of view e.g

loadImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String url = getAnImageUrl();
                    image.setImageDrawable(url);
                    image.setTag(url);
                }
            });

          image.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), image.getTag()+ " this one", Toast.LENGTH_LONG).show();     
                // TODO Auto-generated method stub

            }

And If you still have problem that image is not change. Then Please share your code of LoaderImageView.java as the image request , update is handled in that.