how-to-make-square-view-android

Hello guys, this is #TeamRadicallabs, this is our first blog post about android development. In this blog we tell you how to make SquareView in android view. Sometimes we see that many apps show images or videos in

GridView

with equal height and width. This looks very pretty. But most of the android developers uses equal measure of hard coded height and width to get SquareView. This is bad idea. This leads to misbehaving in image rendering in

GridView

. To overcome this situation we tell you a layout view hack.

Create a class

SquareView.java

which

extands RelativeLayout

and

class SquareView extends RelativeLayout{

@override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)

public class SquareView extends RelativeLayout {

public SquareView(Context context) {
super(context);
}

public SquareView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public SquareView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@TargetApi(Build.VERSION_CODES.<em>LOLLIPOP</em>)
public SquareView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec//This help to acheive squareview);
}
}

and in

super.onMeasure(int, int);

set

widthMeasureSpec

in second parameter. This will lead to height equals to width. Insert all the views inside this

SquareView

in

layout.xml

.

For more tips please visit our blog page https://www.radicallabs.com/blog

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>