Add Library
compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bhaumik.listviewwithletters.MainActivity">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
item_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image_view"
android:layout_width="50dp"
android:layout_height="50dp"
/>
<TextView
android:id="@+id/text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="70"
android:gravity="center"
android:text="Hello"
android:textStyle="bold" />
</LinearLayout>
MainActivity.java
package com.example.bhaumik.listviewwithletters;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView listView;
ArrayList<String>list = new ArrayList<>();
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list_view);
list.add("Apple");
list.add("Banana");
list.add("Cheery");
list.add("Damson");
list.add("Elderberry");
list.add("Feijoa");
list.add("Grape");
list.add("Grapefruit");
list.add("Honeyberry");
list.add("Jackfruit");
list.add("Kiwifruit");
list.add("Lemon");
list.add("Lime");
list.add("Mango");
list.add("Papaya");
list.add("Pineapple");
adapter = new ListAdapter(MainActivity.this,R.layout.item_list,list);
listView.setAdapter(adapter);
}
}
ListAdapter.java
package com.example.bhaumik.listviewwithletters;
import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.amulyakhare.textdrawable.TextDrawable;
import com.amulyakhare.textdrawable.util.ColorGenerator;
import java.util.List;
/**
* Created by Bhaumik on 9/6/2017.
*/
public class ListAdapter extends ArrayAdapter<String>{
Context context;
List<String> list;
public ListAdapter(@NonNull Context context, @LayoutRes int resource, List<String> list) {
super(context, resource,list);
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Nullable
@Override
public String getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder viewHolder;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null){
convertView = inflater.inflate(R.layout.item_list,parent,false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textView.setText(getItem(position));
String letters = String.valueOf(getItem(position).charAt(0));
ColorGenerator colorGenerator = ColorGenerator.MATERIAL;
int color = colorGenerator.getColor(getItem(position));
TextDrawable textDrawable = TextDrawable.builder()
.buildRound(letters,color);
viewHolder.imageView.setImageDrawable(textDrawable);
return convertView;
}
public class ViewHolder{
ImageView imageView;
TextView textView;
public ViewHolder(View iteView) {
imageView = iteView.findViewById(R.id.image_view);
textView = iteView.findViewById(R.id.text_view);
}
}
}
Output
0 comments:
Post a Comment