ImageView To New Activity in Activity - Androidpro.in

 How to open new activity click on imageView ?

In this post we will learn about how to open new activity click on imageView, here I used two activity first where show imageView for click to open next activity and second activity is used for open as new activity after click imageview. for more explain watch video.


ImageView Paste In Your Xml File 

<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="170dp"
android:src="@drawable/image"
android:layout_marginTop="20dp"/>

Set OnClickListener

ImageView imageView;
imageView =(ImageView) findViewById(R.id.image1);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), NextActivity.class);
startActivity(intent);
}
});


Java Code like this

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ImageView imageView;
imageView =(ImageView) findViewById(R.id.image1);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), NextActivity.class);
startActivity(intent);
}
});
}
}


xml Code like this

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="170dp"
android:src="@drawable/image"
android:layout_marginTop="20dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>


Now ImageView Android Studio Projects Ready

 

Post a Comment

0 Comments