Native Mobile App Development With Java

Android ImageView

15-Sep-2024

Learn how to display and manipulate images with ImageView in Android Studio Project Java

ImageView is a widget that is used to display images. You can use it to load images from various sources, such as resources, files, or the internet. Here's a simple example of using ImageView in an Android application:


Here is the tutorial for Kotlin




There Are Some Common Attributes for ImageView :



id
This ID serves as the control's unique identity.
layout_widthmatch_parent -> Text area contains fullscreen
wrap_content-> Text area contains only text size
layout_widthmatch_parent -> Text area contains fullscreen
wrap_content-> Text area contains only text size
scaleType
You can change Image Set Type
center, centerCrop, centerInside, fitCenter, fitEnd, fitStart, fitXY



Step 1:
File open res/layout/activity_main.xml file −:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_margin="20dp"
android:orientation="vertical">


<ImageView
android:id="@+id/imageView1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="10dp"
android:src="@drawable/profile1" />

<ImageView
android:id="@+id/imageView2"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="10dp"
android:src="@drawable/profile2" />

<ImageView
android:id="@+id/imageView3"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="10dp"
android:src="@drawable/profile2" />

<Button
android:id="@+id/submitButton"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>



</RelativeLayout>



Step 2:

File open com.microappvalley.newtestproject/MainActivity.java file −

Java:


package com.microappvalley.newtestproject;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

ImageView imageView1;
ImageView imageView2;
ImageView imageView3;
Button submitButton;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);



imageView1=findViewById(R.id.imageView1);
imageView2=findViewById(R.id.imageView2);
imageView3=findViewById(R.id.imageView3);

//set Image From Drawable
imageView2.setImageResource(R.drawable.profile2);

//Set Image Form Bitmap
Drawable drawable = ContextCompat.getDrawable(this, R.drawable.profile3);
Bitmap bitmap = drawableToBitmap(drawable);
imageView3.setImageBitmap(bitmap);


}

private Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}

// If the drawable is not a BitmapDrawable, create a new Bitmap
Bitmap bitmap = Bitmap.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
Bitmap.Config.ARGB_8888
);

// Create a canvas and draw the drawable on it
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);

return bitmap;
}
}


Open Device Manager, run the emulator, and then run the application. Next, check the working output and check the output  you declared in your code.


Output:




Comments