0

I am new to programming in android and I am struggling with the following problem. I am using a Custom ListView to display a Store name and their Logo. I want to change the standard textview font for a custom font.

The method I use for a normal textview is not working. Can you please Help me?

The following code is my ListView Adapter.

package com.mallspecials.shoppingmallspecialsau;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ShowTheStoreAdapter extends BaseAdapter {


       // Declare Variables
    Context context;
    String[] thestore;

    int[] logo;
    LayoutInflater inflater;

    public ShowTheStoreAdapter(Context context, String[] thestore,  int[] logo) {
        this.context = context;
        this.thestore = thestore;
        this.logo = logo;

    }

    private AssetManager getAssets() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int getCount() {
        return thestore.length;
    }

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

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

    public View getView(int position, View convertView, ViewGroup parent) {

 //Set Snell Roundhand Font for the StoreName
    Typeface font = Typeface.createFromAsset(getAssets(), "Snell Roundhand Bold Script.ttf");

        // Declare Variables
        TextView StoreName;
        ImageView imglogo;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.listview_stores, parent, false);

            //Set the StoreName Text
            StoreName = (TextView) itemView.findViewById(R.id.thestore);
            StoreName.setTextColor(Color.RED);
            StoreName.setShadowLayer(3, -2, -5,(Color.YELLOW));
            StoreName.setTypeface(font);
            StoreName.setText(thestore[position]);

        // Locate the ImageView in listview_stores.xml
        imglogo = (ImageView) itemView.findViewById(R.id.storelogo);
        // Capture position and set to the ImageView
        imglogo.setImageResource(logo[position]);

        return itemView;
    }


}

Below is the code for my adapter View implementation.

list = (ListView) findViewById(R.id.listview);
adapter = new ShowTheStoreAdapter(this, thestore, storelogo);
list.setAdapter(adapter);       

2 Answers2

1

Create Subview for your ListView with the help of BaseAdapter. Add custom textview in that SubView.

https://github.com/johnkil/Android-RobotoTextView

0

The list view itself isn't responsible for drawing the items, it uses an adapter to create the list items. This adapter creates a view to display the list item when required. To change the font used to display the list item, you have to change the adapter to return a view with the new font. This can be done in the Adapter.getView method. If you are currently using a standard Adapter implementation, you may need to subclass it (or completely replace it).

Check this: How to change color and font on ListView

Community
  • 1
  • 1
Prasanth Louis
  • 4,658
  • 2
  • 34
  • 47
  • When you say the Adapter implementation, do you mean when i try to set the items from the main activity? I have added the code from my main activity to call the custom adapter. – Leigh Harvey Sep 07 '14 at 01:40
  • I also see the example you showed me uses an array adapter. – Leigh Harvey Sep 07 '14 at 01:40