Square image view is used to show a non square image into the form of square.
Steps to use SquareImageView in your project.
Steps to use SquareImageView in your project.
- Crate a project(SquareImageDemo) in android studio.
- Create a class SquareImageView.Java and update it-
package com.pankaj.square_image.demo;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
public class SquareImageView extends ImageView {
//________auto created constructor
public SquareImageView(Context context) {
super(context);
}
//________auto created constructor
public SquareImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
//________auto created constructor
public SquareImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//_______________pass same paramere value for width and geight for the SquareImageView.
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
} - Open your main_activity.xml file and update it-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#203F51B5"
android:padding="@dimen/activity_horizontal_margin"
tools:context="com.pankaj.square_image.demo.MainActivity">
<!--scroll view to provide the scrollable screen if contant is large-->
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<!--main child layout for scroll view-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<!--title for default image view-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Default Image"
android:textSize="20dp" />
<!--default image view-->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_horizontal_margin"
android:scaleType="fitXY"
android:src="@drawable/madhya_pradesh" />
<!--title for square image view-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_horizontal_margin"
android:text="Square Image"
android:textSize="20dp" />
<!--square image view-->
<com.pankaj.square_image.demo.SquareImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_horizontal_margin"
android:scaleType="fitXY"
android:src="@drawable/madhya_pradesh" />
</LinearLayout>
</ScrollView>
</LinearLayout> - All the application development process with squareImageView has completed, Now run the app and look the screen.
- Good bye, Thanks to read this blog.

