Alan Kałuża
Alan Kałuża

Reputation: 541

Android - how to switch activity on image click

Here I've got this code

switchact.setOnClickListener(new View.OnClickListener() 
    {
      @Override
      public void onClick(View view) 
      {
        Intent act2 = new Intent(view.getContext(), Activity2.class);
        startActivity(act2);
      }
    });

And i've got image on my activity/layout1. How do I make that when I click on the image it switched to activity 2?

Upvotes: 1

Views: 2531

Answers (3)

Jamalx31
Jamalx31

Reputation: 36

You should set a listener on the image over the onClick event, try something like:

ImageView imageView = (ImageView) rootView.findViewById(R.id.imageView);
imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent act2 = new Intent(view.getContext(), Activity2.class);
            startActivity(act2);
        }
    });

Upvotes: 1

Sagar D
Sagar D

Reputation: 2618

ImageView img = (ImageView) findViewById(R.id.myImageId);
img.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
      Intent act2 = new Intent(Activity1.this, Activity2.class);

      startActivity(act2);
    }
});

Upvotes: 0

Naveen Tamrakar
Naveen Tamrakar

Reputation: 3339

Intent act2 = new Intent(Activity1.this, Activity2.class);

startActivity(act2);

Upvotes: 1

Related Questions