8 Replies - 1004 Views - Last Post: 03 October 2012 - 04:57 AM

#1 kabuto178  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 130
  • Joined: 29-January 11

question on using custom fonts

Posted 29 September 2012 - 10:40 AM

Is there a way to give listview list items custom fonts? Either XML or via code.
Is This A Good Question/Topic? 0
  • +

Replies To: question on using custom fonts

#2 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: question on using custom fonts

Posted 29 September 2012 - 11:48 AM

This is a simple example... create a folder in the root of your project
called assets/fonts/ then paste the TTF font file (in this case Verdana.ttf).
Then, if you want to apply that font to, say a TextView, do the following:
public class FontSampler extends Activity {
 @Override
 public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  setContentView(R.layout.main);

  TextView tv=(TextView)findViewById(R.id.custom);
  Typeface face=Typeface.createFromAsset(getAssets(),
                     "fonts/Verdana.ttf");

  tv.setTypeface(face);
 }
}

Was This Post Helpful? 1
  • +
  • -

#3 kabuto178  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 130
  • Joined: 29-January 11

Re: question on using custom fonts

Posted 30 September 2012 - 12:36 AM

Thanks for your reply, but I was wondering specifically for a ListView.
Was This Post Helpful? 0
  • +
  • -

#4 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: question on using custom fonts

Posted 30 September 2012 - 06:05 AM

Then i guess that you have a class that extends ArrayAdapter<> as the adapter.
The ListView items is a LinearLayout then a TextView, and if you make a custom item you can list use the
getView void in the extended ArrayAdapter<> and do almost the same as in my last post.
Was This Post Helpful? 0
  • +
  • -

#5 kabuto178  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 130
  • Joined: 29-January 11

Re: question on using custom fonts

Posted 30 September 2012 - 09:14 AM

Should the ListView be parameters of the ArrayAdapter?
Was This Post Helpful? 0
  • +
  • -

#6 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: question on using custom fonts

Posted 30 September 2012 - 11:08 PM

No. The ArrayAdapter<> is a custom class that extends ArrayAdapter<> that you'll have to make.
It handles the function that you request.
The most basic way to use a ListView it to make a list, and then do something like:
** = new ArrayAdapter<String>(Context, item_Layout_as_int,your_list); 


But that will not work in this case. I'll give you a sample of what you need later.
Was This Post Helpful? 0
  • +
  • -

#7 kabuto178  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 130
  • Joined: 29-January 11

Re: question on using custom fonts

Posted 01 October 2012 - 12:13 AM

Thanks again for the reply. Looking forward for this
Was This Post Helpful? 0
  • +
  • -

#8 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: question on using custom fonts

Posted 01 October 2012 - 08:40 PM

This is a sample, it will not work if you just copy and paste it!
You'll need to create a custom item layout. You do this the same way to make any layout.
Create a XML and add the elements you want in your list. In this sample I just use a TextView

Then the Adapter will look something like this:
class CustomAdapter extends ArrayAdapter<String> {

	private LayoutInflater inflator = null;
	private List<String> strings;
	private static class ViewHolder {
		public TextView title;
		
	}
    public CustomAdapter(Context context, int resource, List<String> strings) {
            super(context, resource, strings);
            this.strings= strings;
            inflator = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    	ViewHolder holder;
    	
    	if(convertView == null){
    		convertView = inflator.inflate(R.layout.list_item, null);
    		TextView TitleText = (TextView) convertView.findViewById(R.id.title);
    		holder = new ViewHolder();
    		holder.title = TitleText;
    		convertView.setTag(holder);
    		
    	} else {
    		holder = (ViewHolder) convertView.getTag();
    	}
    	String text = strings.get(position);
    	holder.title.setText(text );
    	
    	
    	return convertView;
    	
    }
 
}


And then you use this type of adapter just like a regular one:
.. = new CustomAdapter(getBaseContext(), R.layout.YOU_ITEM_LAYOUT, strings);

Was This Post Helpful? 0
  • +
  • -

#9 kabuto178  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 130
  • Joined: 29-January 11

Re: question on using custom fonts

Posted 03 October 2012 - 04:57 AM

Ok, I shall take a look into this on the weekend and post my progress results. Thanks again
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1