Program to display images using Image View |Android Program|


Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
tools:context=".MainActivity" >

    <
ImageView
       
android:id="@+id/imageView"
       
android:layout_width="match_parent"
        
android:layout_height="200sp"
       
app:layout_constraintBottom_toBottomOf="parent"
       
app:layout_constraintStart_toStartOf="parent"
       
app:layout_constraintTop_toTopOf="parent"
       
app:srcCompat="@drawable/p1" />

    <
Button
       
android:id="@+id/button1"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_marginBottom="96dp"
       
android:text="image 1"
       
app:layout_constraintBottom_toBottomOf="parent"
       
app:layout_constraintEnd_toEndOf="parent"
       
app:layout_constraintHorizontal_bias="0.278"
       
app:layout_constraintStart_toStartOf="parent" />

    <
Button
       
android:id="@+id/button2"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_marginEnd="84dp"
       
android:layout_marginRight="84dp"
       
android:layout_marginBottom="96dp"
       
android:text="image 2"
       
app:layout_constraintBottom_toBottomOf="parent"
       
app:layout_constraintEnd_toEndOf="parent"
       
app:layout_constraintHorizontal_bias="0.489"
       
app:layout_constraintStart_toEndOf="@+id/button1" />
</
androidx.constraintlayout.widget.ConstraintLayout>

 

MainActivity.java

package com.example.imageview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    Button
b1,b2;
    ImageView
imageView;

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

       
b1=findViewById(R.id.button1);
       
b2=findViewById(R.id.button2);
       
imageView=findViewById(R.id.imageView);

       
b1.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
               
imageView.setImageResource(R.drawable.p1);
            }
        });
       
b2.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
               
imageView.setImageResource(R.drawable.p2);
            }
        });
    }
}



Design

Post a Comment

0 Comments