set image in imageview android programmatically

set image in imageview android programmatically

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#d3d3d3"
    tools:context=".MainActivity">

    <!--
    android:src
       Sets a drawable as the content of this ImageView.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

https://developer.android.com/guide/topics/graphics/vector-drawable-resources#vector-drawables-backward-solution
-->
    <!-- Set ImageView image by XML-->

    <ImageView
        app:srcCompat="@drawable/ic_png_image"
        android:id="@+id/iv1"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_margin="20dp"
        android:src="@drawable/ic_png_image"/>

    <!-- Set Vector drawable as the content of this ImageView-->
    <ImageView
        android:id="@+id/iv2"
        android:layout_toRightOf="@id/iv1"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_margin="20dp"
        app:srcCompat="@drawable/ic_vector_drawable_android"
        android:padding="3dp" />


    <!-- Set ImageView image programmatically-->
    <ImageView
        android:id="@+id/iv3"
        android:layout_below="@id/iv1"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_margin="20dp"
        android:padding="3dp"
        />
    <!-- Set ImageView image as drawable programmatically-->
    <ImageView
        android:id="@+id/iv4"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_margin="20dp"
        android:layout_below="@id/iv2"
        android:layout_toRightOf="@id/iv3"
        android:padding="3dp"
        />

    <!-- Set ImageView image as bitmap programmatically-->
    <ImageView
        android:id="@+id/iv5"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_margin="20dp"
        android:layout_below="@id/iv3"
        android:padding="3dp"
        />
</RelativeLayout>


MainActivity.java

package com.android.kotlin.changedrawablecolor;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.content.res.AppCompatResources;
import android.widget.ImageView;


public class MainActivity extends AppCompatActivity {
    Context context;

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

        ImageView iv1 = (ImageView) findViewById(R.id.iv1);
        ImageView iv2 = (ImageView) findViewById(R.id.iv2);
        ImageView iv3 = (ImageView) findViewById(R.id.iv3);
        ImageView iv4 = (ImageView) findViewById(R.id.iv4);
        ImageView iv5 = (ImageView) findViewById(R.id.iv5);

        iv3.setImageResource(R.drawable.image3);

        iv4.setImageDrawable(AppCompatResources.getDrawable(context,R.drawable.image4));

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.image5);
        iv5.setImageBitmap(bitmap);
    }
}


app build.gradle (Module:app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.espl.changedrawablecolor"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

No comments:

Post a Comment

Popular Posts