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:
There Are Some Common Attributes for ImageView :
id | This ID serves as the control's unique identity. |
layout_width | match_parent -> Text area contains fullscreen wrap_content-> Text area contains only text size |
layout_width | match_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.kt file −
package com.microappvalley.newtestproject
import android.annotation.SuppressLint
import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.Switch
import android.widget.Toast
class MainActivity : AppCompatActivity() {
@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val imageView1:ImageView=findViewById(R.id.imageView1)
val imageView2:ImageView=findViewById(R.id.imageView2)
val imageView3:ImageView=findViewById(R.id.imageView3)
// Image Set From drawable
imageView1.setImageResource(R.drawable.profile1)
//Set Iamge From Bitmap
val bitmap = BitmapFactory.decodeResource(resources, R.drawable.profile2)
imageView2.setImageBitmap(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.