Stein van Broekhoven
Stein van Broekhoven

Reputation: 347

android change listview font and color

I've found loads of different ways of accomplishing this but I'm not sure what's the best for my scenario.

This is my Java code for the listview:

ListView lv;
lv = (ListView) findViewById(R.id.favList);

This is the xml code for the list:

<ListView
        android:id="@+id/favList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="40dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginTop="20dp"
        android:background="@android:color/transparent"
        android:cacheColorHint="@android:color/transparent"
        android:listSelector="@android:color/transparent" >
    </ListView>

For a text view I would add:

final Typeface fontList = Typeface.createFromAsset(assets, "optima-extra-black.ttf");
lv.setTypeface(fontList);

But this doesn't work for listviews. How do I change my font in this case?

Oke I'm almost there... I need to access my assets but I can't from within my custom adapter. I tried using final AssetManager assets = this.getAssets(); but that won't get me any further..

How to tackle this?

    class Myadapter extends BaseAdapter {

    LayoutInflater lif;
    ImageView sideArrow;
    TextView tv;


    public Myadapter(Context ctx) {
        lif = (LayoutInflater) ctx
                .getSystemService(LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {

        return favarets.size();
    }

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

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

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

        View vi = convertView;
        if (convertView == null)
            vi = lif.inflate(R.layout.inflate, null);
        sideArrow = (ImageView) vi.findViewById(R.id.imageViewsidemark);

        tv = (TextView) vi.findViewById(R.id.textFav);
        tv.setText(favarets.get(position));
        final AssetManager assets = this.getAssets();
        final Typeface tvFont = Typeface.createFromAsset(assets, "OPTIMA.TTF");
        tv.setTypeface(tvFont);

        tv.setTextColor(Color.BLACK);

        return vi;

Upvotes: 5

Views: 32784

Answers (6)

Ehsan Mohammadi
Ehsan Mohammadi

Reputation: 44

create custom Text view layout
Step 1:
create new layout for example listview_custom
Step 2:
clear all code in listview_custom
Step 3:
Insert the following code

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
<!-- change background color -->    
android:background="@color/white"
    android:id="@android:id/text1"
<--!change text color-->
    android:textColor="@color/black"
<--!insert font--> 
    android:fontFamily="@font/iransansweb"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:minHeight="?android:attr/listPreferredItemHeightSmall" />

And set listview Adapter very easy

ArrayAdapter ad=new ArrayAdapter(MainActivity.this,R.layout.listview_custom,list);
listview.setAdapter(ad)

Upvotes: 0

Aniket Rawat
Aniket Rawat

Reputation: 99

Simply, instead of using the inbuild xml file in the SKD

`ArrayAdapter ad=new ArrayAdapter(GuestActivity.this,android.R.layout.simple_list_item_1,list);`

Make your own xml layout file like this-

`<TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="@style/YourStyle"
        android:gravity="center_vertical"
        android:paddingStart="?android:attr/listPreferredItemPaddingStart"
        android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
        android:minHeight="?android:attr/listPreferredItemHeightSmall" />`

and use that in the adapter.

Sample Image:

Formatted List Item

Upvotes: 1

Stein van Broekhoven
Stein van Broekhoven

Reputation: 347

I found out the solution :D

    public class Myadapter extends BaseAdapter {

    AssetManager assetManager = getAssets(); 

    LayoutInflater lif;
    ImageView sideArrow;
    TextView tv;


    public Myadapter(Context ctx) {
        lif = (LayoutInflater) ctx.getSystemService(LAYOUT_INFLATER_SERVICE);


    }

    @Override
    public int getCount() {

        return favarets.size();
    }

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

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


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

        View vi = convertView;
        if (convertView == null)
            vi = lif.inflate(R.layout.inflate, null);
        sideArrow = (ImageView) vi.findViewById(R.id.imageViewsidemark);


        tv = (TextView) vi.findViewById(R.id.textFav);
        tv.setText(favarets.get(position));

        final Typeface tvFont = Typeface.createFromAsset(assetManager, "OPTIMA.TTF");
        tv.setTypeface(tvFont);
        tv.setTextColor(Color.BLACK);

        return vi;
    }

}

Upvotes: 6

beetstra
beetstra

Reputation: 7952

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).

Upvotes: 6

DropAndTrap
DropAndTrap

Reputation: 1630

Just set it to the TextView which you are inflating set the typeface in adapter or in the xml layout.

Upvotes: 0

Ahmed Ekri
Ahmed Ekri

Reputation: 4651

you need to create a custom adapter.

check this answer

and then have a custom xml too.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/textView"
    android:textSize="20px" android:paddingTop="10dip" android:paddingBottom="10dip"/>
</LinearLayout>

then set the custom adapter to your listview.

listAdapter = new CustomListAdapter(YourActivity.this , R.layout.custom_list , mList);
mListView.setAdapter(listAdapter);

Upvotes: 1

Related Questions