Srujan Barai Srujan Barai - 4 years ago 93
Java Question

Android code to make imageView round not working

I am trying to make my

ImageView
round. I have written the following code to make it appear round but somehow it is still showing square
ImageView
. [Using picasso to fetch image]

Java code:

ImageView iv = (ImageView) addLinkDialog.findViewById(R.id.group_icon_jsoup);
Picasso.with(getBaseContext()).load(GroupImageUrl).into(iv);
iv.setBackgroundResource(R.drawable.icon_img);


ImageView code:

<ImageView
android:id="@+id/group_icon_jsoup"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_gravity="center"
android:layout_margin="8dp"
android:background="@drawable/icon_img" />


@drawable/icon_img.xml:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/circle"/>
</layer-list>


@drawable/circle.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="1.9"
android:useLevel="false" >
<solid android:color="@android:color/transparent" />

<stroke
android:width="10dp"
android:color="@android:color/white" />
</shape>

Answer Source

Major problem will be when you use Picasso to set image again to set to imageView view bounds not to the its background that you create.

If you programmatically set a one it will override your background!

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <shape android:shape="oval">
            <solid android:color="@color/colorPrimary"/>
        </shape>
    </item>
</selector>

You can set this as background of your view.Then try to use view.setBackgroundResource(R.drawable.icon_img); . you will notice the change!

You can go through Add a background image to shape in xml Android

Mask ImageView with round corner background

to check the various ways people tried out here!

But with Picasso you can do this directly with out other 3rd parties.

  final ImageView imageView = (ImageView) findViewById(R.id.group_icon_jsoup);
    Picasso.with(YourActivity.this).load("http://i.imgur.com/DvpvklR.png")
            .resize(100, 100)
            .into(imageView, new Callback() {
                @Override
                public void onSuccess() {
                    Bitmap imageBitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
                    RoundedBitmapDrawable imageDrawable = RoundedBitmapDrawableFactory.create(getResources(), imageBitmap);
                    imageDrawable.setCircular(true);
                    imageDrawable.setCornerRadius(Math.max(imageBitmap.getWidth(), imageBitmap.getHeight()) / 2.0f);
                    imageView.setImageDrawable(imageDrawable);
                }
                @Override
                public void onError() {
                    imageView.setImageResource(R.drawable.amanda);
                }
            });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download