Android ContentViews → Android ImageView – Camposha

Android ContentViews

Android ContentViews

Android ImageView

Android ImageView Tutorial.

This is an android imageview tutorial and examples. We start by looking at the imageview, it’s api definition, then discuss why it’s needed, then several methods in this class and hwo to use. Then we look at various quick imageview examples.


What is ImageView?

ImageView is a class that allows us render images in an android app.

Why ImageView?

ImageView is one of the most important widgets in android. It’s even strange to imagine how android apps would be without it. This is because imageview is probably one of those widgets that doesn’t seem to have any viable alternatives.

You can say an alternative to TextView would be EditText, an alternative to Button would be clickable TextView, an alternative to RadioButton would be cleverly synchronized CheckBox, an alternative ListView would be RecyclerView or one column GridView etc.

However for ImageView it is difficult to imagine an alternative. This is because while the others simply render text data, imageview renders binary data.

Hence it becomes very important to master this class because we will use it in almost every practical apps.

Common Applications of ImageView

Here are some of the common use cases of imageview:

  1. While Making a Slider or Gallery.
  2. While Rendering list of products in an ecommerce store. This is commonly done via adapterviews Grid RecyclerView or GridView. The products images get rendered in an imageview and alternative product text and description can be rendered below or beside the imageview.

Quick ImageView Examples and HowTo’s

Lets start by looking at quick custom ImageView examples:

1. How to create a Square ImageView

Let’s say we want to create a square imageview. That is an imageview where both width and height are the same.

First we will have our SquareHelper class:

import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;

import java.lang.annotation.Retention; 
import java.lang.annotation.RetentionPolicy;

import online.sniper.R; 

 * Square view helper class 
public class SquareHelper {    

    public static final int BASE_WIDTH = 0;      
    public static final int BASE_HEIGHT = 1;      
    public static final int BASE_MIN_EDGE = 2;      
    public static final int BASE_MAX_EDGE = 3;      

    public @interface SquareViewEdge { 


    private View mView;
    private int mBaseEdge = BASE_WIDTH;

    public SquareHelper(View view, AttributeSet attrs) {
        mView = view;
        TypedArray a = view.getContext().obtainStyledAttributes(attrs, R.styleable.SquareView);
        try { 
            mBaseEdge = a.getInt(R.styleable.SquareView_baseEdge, BASE_WIDTH);
        } finally { 

    public void setBaseEdge(@SquareViewEdge int baseEdge) {
        mBaseEdge = baseEdge;

    public int getMeasuredSize() { 
        switch (mBaseEdge) {
                return mView.getMeasuredWidth();
                return mView.getMeasuredHeight();
                return Math.min(mView.getMeasuredWidth(), mView.getMeasuredHeight());
                return Math.max(mView.getMeasuredWidth(), mView.getMeasuredHeight());


Then our SquareImageView

import android.content.Context;
import  android.util.AttributeSet ;

public class SquareImageView extends AppCompatImageView { 

    private final SquareHelper mHelper ;     

    public SquareImageView(Context context) {

    public SquareImageView(Context context, AttributeSet attrs) {
        mHelper = new SquareHelper(this, attrs);

    protectedvoid onMeasure(intwidthMeasureSpec,intheightMeasureSpec){     
        int size = mHelper.getMeasuredSize();
        setMeasuredDimension(size, size);



Leave a Reply

Your email address will not be published. Required fields are marked *




Not enough ratings to display