Search code examples
javaandroidlistviewandroid-fragmentscustom-adapter

custom font in customAdapter


I use custom adapter for my listView, and I want to make the textView with custom font. the listView is in fragment: todolist_fragment.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"
android:orientation="vertical"
>

<ListView
    android:id="@+id/checkable_list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

todolistfragment.java

public class toDoListFragment extends Fragment {

ListView checkableList;
Button showSelectedButton;

List<itemsModel> itemsList;

public toDoListFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_todolist, container, false);

    checkableList = rootView.findViewById(R.id.checkable_list);

    itemsList = new ArrayList<>();
    itemsList.add(new itemsModel(false, "item1"));
    itemsList.add(new itemsModel(false, "item2"));
    itemsList.add(new itemsModel(false, "item3"));
    itemsList.add(new itemsModel(false, "item4"));
    itemsList.add(new itemsModel(false, "item5"));
    itemsList.add(new itemsModel(false, "item6"));
    itemsList.add(new itemsModel(false, "item7"));

    final customList adapter = new customList(getActivity(), itemsList);

    checkableList.setAdapter(adapter);

    checkableList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            itemsModel model = itemsList.get(i);

            if(model.isSelected())
                model.setSelected(false);
            else
                model.setSelected(true);

            itemsList.set(i, model);

            adapter.updateRecords(itemsList);
        }
    });


    return rootView;
}

}

so I created customList.java:

public class customList extends BaseAdapter {

Activity activity;
List<itemsModel> itemsList;
LayoutInflater inflater;

public customList(Activity activity) {
    this.activity = activity;
}

public customList(Activity activity, List<itemsModel> itemsList) {
    this.activity = activity;
    this.itemsList = itemsList;

    inflater = activity.getLayoutInflater();
}

@Override
public int getCount() {
    return itemsList.size();
}

@Override
public Object getItem(int i) {
    return i;
}

@Override
public long getItemId(int i) {
    return i;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    ViewHolder holder = null;
    if(view == null){
        view = inflater.inflate(R.layout.list_view_item_row, viewGroup, false);

        holder = new ViewHolder();

        holder.itemText = view.findViewById(R.id.itemName);
        holder.checkBox = view.findViewById(R.id.checkboxIcon);

        view.setTag(holder);
    }else
        holder = (ViewHolder) view.getTag();

    itemsModel model = itemsList.get(i);

    holder.itemText.setText(model.getItemsList());

    if(model.isSelected())
        holder.checkBox.setBackgroundResource(R.drawable.checked);

    else
        holder.checkBox.setBackgroundResource(R.drawable.check);

    return view;
}

public void updateRecords(List<itemsModel> itemsList){
    this.itemsList = itemsList;

    notifyDataSetChanged();
}

class ViewHolder{
    TextView itemText;
    ImageView checkBox;
}

}

and the layout "list_view_item_row.xml" is the layout with the imageview Checkbox and the textview items:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:padding="5dp" >

<ImageView
    android:id="@+id/checkboxIcon"
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginRight="10dp"
    android:layout_marginEnd="10dp"
    android:layout_centerVertical="true"
    android:background="@drawable/check"/>


<TextView
    android:id="@+id/itemName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/checkboxIcon"
    android:layout_toStartOf="@+id/checkboxIcon"
    android:padding="10dp"
    android:text="Item Name"
    android:textColor="@android:color/black"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    />

enter image description here

I set the custom font manually... like this:

TextView textView;
   textView = rootView.findViewById(R.id.text1); 
   Typeface customFontBold = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Assistant-Bold.ttf");
   textView.setTypeface(customFontBold);

where I can set the custom font? Thank you.


Solution

  • I used akhilesh0707 answer, but I changed it.

     public customList(Activity activity, List<itemsModel> itemsList) {
        this.activity = activity;
        this.itemsList = itemsList;
        inflater = activity.getLayoutInflater();
        customFontBold = Typeface.createFromAsset(activity.getApplication().getAssets(), "fonts/Assistant-Bold.ttf");
    }
    

    Thanks akhilesh0707.