Skip to content Skip to sidebar Skip to footer

How To Change Font Type And Color In This Listview

I have two string arrays: array1 and array2. How can I change the font type? How can I change the color for some element of the array (for example the first and the fifth element)?

Solution 1:

It is convenient to make custom adapter for that. Like this:--

privateclassMyListAdapterextendsBaseAdapter {

    List<Map<String, String>> data ;
    LayoutInflater inflater;

    publicMyListAdapter(Context context,List<Map<String, String>> data ) {
        // TODO Auto-generated constructor stubthis.data  = data;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Overridepublic int getCount() {
        // TODO Auto-generated method stubreturn mDisplayedValues.size();
    }

    @OverridepublicObjectgetItem(int arg0) {
        // TODO Auto-generated method stubreturnnull;
    }

    @Overridepublic long getItemId(int position) {
        // TODO Auto-generated method stubreturn0;
    }

    @OverridepublicViewgetView(final int position, View convertView,
            ViewGroup parent) {
        // TODO Auto-generated method stubView v = convertView;
        ViewHolder holder;
        if (v == null) {
            v = inflater.inflate(R.layout.<your_list_row>, null);
            holder = newViewHolder();
            holder.text1= (TextView) v
                    .findViewById(R.id.text1);
            holder.text2 = (TextView) v.findViewById(R.id.text2);

            v.setTag(holder);
        } else
            holder = (ViewHolder) v.getTag();
        holder.text1.setText("mytext1");
        holder.text2.setText("mytext2");
                    holder.text1.setTypeFace(--YourTypeface(font)---);

        return v;
    }

    classViewHolder {
        TextView text1;
        TextView text2;

    }
}

Solution 2:

Create a CustomAdapter and in that you have the getView() so there if you want to change the listview font use this :

For changing font :

finalTypefacefont= Typeface.createFromAsset(assetManager, "price.TTF");
    listview1.setTypeface(font);

& Color:

    listview1.setTextColor(Color.BLACK);

Post a Comment for "How To Change Font Type And Color In This Listview"