CristianoWilson
CristianoWilson

Reputation: 143

Change font in my listView

So this listView of mine is giving me headaches. It is working perfectly and all, but I can't seem to change the font of the text in the lV.

This is my code:

public class MyActivity3 extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my3);
    Button m = (Button) findViewById(R.id.button3);
    tv = (TextView) findViewById(R.id.textViewcat);
    Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
    tv.setTypeface(typeface);


    String listArray[] = new String[] { "India", "England", "Canada",
            "New zealand", "South Africa", "Pakistan", "West indies" };
    int icon[] = new int[] { R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher };

    ListView listView = (ListView) findViewById(R.id.listView);
    List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();

    for (int i = 0; i <= listArray.length - 1; i++) {

        HashMap<String, String> hm = new HashMap<String, String>();
        hm.put("title", listArray[i]);
        hm.put("icon", Integer.toString(icon[i]));
        aList.add(hm);
    }

    String[] sfrm = { "title", "icon" };
    int[] sto = { R.id.title, R.id.list_image};

    SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,
            R.layout.row_layout, sfrm, sto);



    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view,
                                int position, long id) {

            switch (position) {

                case 0:
                    Toast.makeText(getApplicationContext(), "",
                            Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    Toast.makeText(getApplicationContext(), "",
                            Toast.LENGTH_SHORT).show();
                    break;
                case 2:
                    Toast.makeText(getApplicationContext(), "",
                            Toast.LENGTH_SHORT).show();
                    break;
                case 3:
                    Toast.makeText(getApplicationContext(), "",
                            Toast.LENGTH_SHORT).show();
                    break;
                case 4:
                    Toast.makeText(getApplicationContext(), "",
                            Toast.LENGTH_SHORT).show();
                    break;
                case 5:
                    Toast.makeText(getApplicationContext(), "",
                            Toast.LENGTH_SHORT).show();
                    break;
                case 6:
                    Toast.makeText(getApplicationContext(), "",
                            Toast.LENGTH_SHORT).show();
                    break;

            }

        }
    });
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.animation8, R.anim.animation7);
    }
}

row_layout.xml:

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


<LinearLayout
    android:id="@+id/thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="5dip"
    android:padding="3dip" >

    <ImageView
        android:id="@+id/list_image"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:src="@drawable/ic_launcher" />
</LinearLayout>



<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/thumbnail"
    android:text="Country name"
    android:textColor="#FFFFFF"
    android:textSize="30sp"
    android:typeface="sans" />

</RelativeLayout>

So that is my code, as you can see there is a textview with the id "title". How can I change the font of title? I'd really apreciate it if you answer the question based on my code, because I already tried all the other solutions on SO but with no succes

Upvotes: 0

Views: 131

Answers (4)

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

Try this way,hope this will help you to solve your problem.

public class MyActivity3 extends Activity {
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my3);
        Button m = (Button) findViewById(R.id.button3);
        tv = (TextView) findViewById(R.id.textViewcat);
        Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
        tv.setTypeface(typeface);


        String listArray[] = new String[] { "India", "England", "Canada",
                "New zealand", "South Africa", "Pakistan", "West indies" };
        int icon[] = new int[] { R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher };

        ListView listView = (ListView) findViewById(R.id.listView);
        ArrayList<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();

        for (int i = 0; i <= listArray.length - 1; i++) {

            HashMap<String, String> hm = new HashMap<String, String>();
            hm.put("title", listArray[i]);
            hm.put("icon", Integer.toString(icon[i]));
            aList.add(hm);
        }
//
//        String[] sfrm = { "title", "icon" };
//        int[] sto = { R.id.title, R.id.list_image};
//
//        SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,
//                R.layout.row_layout, sfrm, sto);


        listView.setAdapter(new CustomAdapter(this,aList));

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View view,
                                    int position, long id) {

                switch (position) {

                    case 0:
                        Toast.makeText(getApplicationContext(), "",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case 1:
                        Toast.makeText(getApplicationContext(), "",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case 2:
                        Toast.makeText(getApplicationContext(), "",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case 3:
                        Toast.makeText(getApplicationContext(), "",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case 4:
                        Toast.makeText(getApplicationContext(), "",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case 5:
                        Toast.makeText(getApplicationContext(), "",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case 6:
                        Toast.makeText(getApplicationContext(), "",
                                Toast.LENGTH_SHORT).show();
                        break;

                }

            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.animation8, R.anim.animation7);
    }

    class CustomAdapter extends BaseAdapter{
        private Context context;
        private ArrayList<HashMap<String,String>> data;
        public CustomAdapter(Context context,ArrayList<HashMap<String,String>> data){
            this.context=context;
            this.data=data;
        }
        @Override
        public int getCount() {
            return data.size();
        }

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if(convertView==null){
                viewHolder =new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.row_layout,null);
                viewHolder.list_image = (ImageView) convertView.findViewById(R.id.list_image);
                viewHolder.title = (TextView) convertView.findViewById(R.id.title);
                Typeface typeface = Typeface.createFromAsset(getAssets(), "yourcustomfontname.ttf");
                viewHolder.title.setTypeface(typeface);
                convertView.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.title.setText(data.get(position).get("title"));
            viewHolder.list_image.setImageResource(Integer.parseInt(data.get(position).get("icon")));
            return null;
        }

        class ViewHolder{
            ImageView list_image;
            TextView title;
        }
    }
}

Upvotes: 0

FR073N
FR073N

Reputation: 2091

You'll have to create a custom adapter, and in the getView method, put the font to your views :

public class ListAdapter extends ArrayAdapter<Item> {

public ListAdapter(Context context, int textViewResourceId) {
    super(context, textViewResourceId);
}

public ListAdapter(Context context, int resource, List<Item> items) {
    super(context, resource, items);
}

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

    View v = convertView;

    if (v == null) {

        LayoutInflater vi;
        vi = LayoutInflater.from(getContext());
        v = vi.inflate(R.layout.itemlistrow, null);

    }
    TextView tv = (TextView) v.findViewById(R.id.tv_name);
    tv.setTypeface(typeFace);
    ...
}

Upvotes: 0

Ram
Ram

Reputation: 114

Hi Cristiano,

I think i traced out your problem changing font.

At your code:
Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
tv.setTypeface(typeface);

Change like below and try out:
Typeface typeface = Typeface.createFromAsset(getAssets(), "customfont.ttf");
tv.setTypeface(typeface);

Please change the ttf file name to any name in smaller letters with no space in between letters and try out then it will work.

Upvotes: 1

dzsonni
dzsonni

Reputation: 193

Here is a detailed explanation of the issue.

http://javatechig.com/android/using-external-fonts-in-android-view

Basically for you, since it's in a listview I recommend that you create the custom textview. It's important to place you font files in the assets folder.

Saying that you tried all the other sources with no success is just wrong, as the above link appears like the second google search result and works perfectly.

Upvotes: 1

Related Questions