Custom Listview With Rounded Circle Images

Introduction 

   In couple of previous blog i was explained about custom listview with images,that all images positioned in normal rectangular shape,now i am going to explain custom list view with rounded circle images

demo

Using the code

activity_main.xml:
<RelativeLayout 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"
tools:context=".MainActivity" >

<ListView
android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>


list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp">

<TextView
android:id="@+id/title"
android:textColor="#000"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/imageview"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ic_launcher"
android:layout_marginRight="10dp"
android:contentDescription="@string/app_name"/>

</LinearLayout>


MainActivity.java:
package com.example.list;

import java.util.ArrayList;
import java.util.Arrays;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends ListActivity 
{
 private String[] listview_names =  {"India","Bangladesh""China","Indonesia" };
 static Context mcontext;

 private static int[] listview_images = 
 {
 R.drawable.india,R.drawable.bangladesh,R.drawable.china,
 R.drawable.indonesia};

 private ListView lv;
 private static ArrayList<String> array_sort;
 private static ArrayList<Integer> image_sort;
 @Override
 protected void onCreate(Bundle savedInstanceState) 
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  lv = (ListView) findViewById(android.R.id.list);
  array_sort=new ArrayList<String> (Arrays.asList(listview_names));
  image_sort=new ArrayList<Integer>();
  for (int index = 0; index < listview_images.length; index++)
  {
   image_sort.add(listview_images[index]);
  }
  setListAdapter(new bsAdapter(this));

  lv.setOnItemClickListener(new OnItemClickListener() {

  public void onItemClick(AdapterView<?> arg0,
  View arg1, int position, long arg3)
  {
   Toast.makeText(getApplicationContext(), array_sort.get(position),
   Toast.LENGTH_SHORT).show();
  }
 });
}
public static class bsAdapter extends BaseAdapter
{
 Activity cntx;
 public bsAdapter(Activity context)
 {
 // TODO Auto-generated constructor stub
 this.cntx=context;
}

public int getCount()
{
 // TODO Auto-generated method stub
 return array_sort.size();
}

public Object getItem(int position)
{
 // TODO Auto-generated method stub
 return array_sort.get(position);
}

public long getItemId(int position)
{
 // TODO Auto-generated method stub
 return array_sort.size();
}

public View getView(final int position, View convertView, ViewGroup parent)
{
 View row=null;
 LayoutInflater inflater=cntx.getLayoutInflater();
 row=inflater.inflate(R.layout.list_itemnull);
 TextView tv = (TextView) row.findViewById(R.id.title);
 ImageView im = (ImageView) row.findViewById(R.id.imageview);
 tv.setText(array_sort.get(position));

 im.setImageBitmap(getRoundedShape(decodeFile(cntxlistview_images[pos ition]),200));

 return row;
}

public static Bitmap decodeFile(Context context,int resId) {
try {
// decode image size
mcontext=context;
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeResource(mcontext.getResources(), resId, o);
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 200;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true)
{
 if (width_tmp / 2 < REQUIRED_SIZE
 || height_tmp / 2 < REQUIRED_SIZE)
 break;
 width_tmp /= 2;
 height_tmp /= 2;
 scale++;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeResource(mcontext.getResources(), resId, o2);
catch (Exception e) {
}
return null;
}
}
public static Bitmap getRoundedShape(Bitmap scaleBitmapImage,int width) {
 // TODO Auto-generated method stub
 int targetWidth = width;
 int targetHeight = width;
 Bitmap targetBitmap = Bitmap.createBitmap(targetWidth,
 targetHeight,Bitmap.Config.ARGB_8888);

 Canvas canvas = new Canvas(targetBitmap);
 Path path = new Path();
 path.addCircle(((float) targetWidth - 1) / 2,
 ((float) targetHeight - 1) / 2,
 (Math.min(((float) targetWidth),
 ((float) targetHeight)) / 2),
 Path.Direction.CCW);
 canvas.clipPath(path);
 Bitmap sourceBitmap = scaleBitmapImage;
 canvas.drawBitmap(sourceBitmap,
 new Rect(0, 0, sourceBitmap.getWidth(),
 sourceBitmap.getHeight()),
 new Rect(0, 0, targetWidth,
 targetHeight), null);
 return targetBitmap;
 }
}


                                                            
Previous
Next Post »

21 comments

Write comments
Anonymous
AUTHOR
February 17, 2015 at 2:39 PM delete

I do not even know how I ended up here, but I thought this post was good.
I do not know who you are but definitely you're going to a famous blogger
if you are not already ;) Cheers!

Feel free to visit my site ... christian louboutin cheap

Reply
avatar
Madson Gr.
AUTHOR
May 15, 2015 at 9:43 PM delete

How do I handle "listview_images[pos ition]),200" if I don´t use array? I get images by json/url and it is List

Reply
avatar
Kalidoss
AUTHOR
May 19, 2015 at 3:13 PM delete

Hi madson,

How do you get and store json/url values.

Reply
avatar
Unknown
AUTHOR
December 10, 2015 at 11:54 AM delete

thx sir, i always wanted to make a custom list wid the rounded images as i m newbe....

Reply
avatar
Unknown
AUTHOR
June 25, 2017 at 2:58 PM delete

This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.
android development course fees in chennai | android app development training in chennai|Android Training institute in chennai with placement | Best Android Training in velachery

Reply
avatar
Mounika
AUTHOR
September 1, 2018 at 2:47 PM delete

I would really like to read some personal experiences like the way, you've explained through the above article. I'm glad for your achievements and would probably like to see much more in the near future. Thanks for share.
Click here:
Microsoft azure training in sollinganallur
Click here:
Microsoft azure training in btm

Reply
avatar
Mounika
AUTHOR
September 1, 2018 at 3:10 PM delete

I would like to thank you for your nicely written post, its informative and your writing style encouraged me to read it till end. Thanks
Click here:
angularjs4 Training in Chennai
Click here:
angularjs5 Training in Chennai

Reply
avatar
sai
AUTHOR
September 1, 2018 at 4:13 PM delete

I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post I would like to read this
Click here:
angularjs training in sholinganallur
Click here:
angularjs training in btm
Click here:
angularjs training in rajajinagar
Click here:
angularjs training in marathahalli

Reply
avatar
pooja
AUTHOR
September 10, 2018 at 10:50 AM delete

Thanks for the good words! Really appreciated. Great post. I’ve been commenting a lot on a few blogs recently, but I hadn’t thought about my approach until you brought it up. 

Blueprism training in annanagar

Blueprism training in velachery

Blueprism training in marathahalli


AWS Training in chennai

AWS Training in bangalore

Reply
avatar
August 26, 2019 at 6:42 PM delete

Your info is really amazing with impressive content..Excellent blog with informative concept. Really I feel happy to see this useful blog, Thanks for sharing such a nice blog..
If you are looking for any Big data Hadoop Related information please visit our website hadoop classes in pune page!

Reply
avatar
easylearn
AUTHOR
February 11, 2020 at 11:19 AM delete

Its great post.Very easy to understand,got lot of information.Thanks for posting,keep updating.IT managers suggest to take Hadoop Admin Training in Bangalore because its best suited for them who have basic linux experience.

Reply
avatar
March 9, 2020 at 11:43 PM delete

Its very amazing an helpful data..
Thanks for sharing with us,
We are again come on your website,
Thanks and good day,
Please visit our site,
buylogo

Reply
avatar
ganesh
AUTHOR
October 27, 2020 at 6:11 PM delete

I like it and help me to development very well. Thank you for this brief explanation and very nice information. Well, got a good knowledge.
Angular js Training in Chennai

Angular js Training in Velachery

Angular js Training in Tambaram

Angular js Training in Porur

Angular js Training in Omr
Angular js Training in Annanagar

Reply
avatar
harini
AUTHOR
October 30, 2020 at 5:38 PM delete

Wonderful Blog!! Indeed it is very interesting learn all the new things around. I have been wanting to improve my skills and this blog really helps! Indeed it is an amazing blog. loved it. Appreciate all your work
Selenium Training in Chennai

Selenium Training in Velachery

Selenium Training in Tambaram

Selenium Training in Porur

Selenium Training in Omr

Selenium Training in Annanagar

Reply
avatar
Anonymous
AUTHOR
November 6, 2020 at 5:02 PM delete

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
tally training in chennai

hadoop training in chennai

sap training in chennai

oracle training in chennai

angular js training in chennai

Reply
avatar
lakshmik7410
AUTHOR
December 13, 2020 at 11:29 PM delete

This was a very informative article, indeed loved to read and clear my doubts. Keep us posted a lot more blogs. Also check out our blog pages too.

data science training in chennai

ccna training in chennai

iot training in chennai

cyber security training in chennai

ethical hacking training in chennai

Reply
avatar