0

I have set up a working custom list view array adapter code is almost similar to the one showed here (without the cache part)

now how do I change the font of all the items to something like roboto

edit i tried this

added private Typeface textFont; before oncreate();

 TextView yourTextView = (TextView) listAdapter.getView(0, null, null);
      TypefacetextFont=Typeface.createFromAsset(getApplicationContext().getAssets(),"RobotoBoldCondensed.ttf");
     yourTextView.setTypeface(textFont);
user1979237
  • 11
  • 2
  • 7
  • http://stackoverflow.com/questions/9327053/using-custom-font-in-android-textview-using-xml – dilix Jan 16 '13 at 11:22

4 Answers4

1

Create a folder in the root of your project called assets/fonts/ then paste the TTF font file (in this case roboto.ttf).

Then use that from your adapter's getview() method like this:

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

      /* create a new view of my layout and inflate it in the row */
      convertView = ( RelativeLayout ) inflater.inflate( resource, null );

      /* Extract the city's object to show */
      City city = getItem( position );

      /* Take the TextView from layout and set the city's name */
      TextView txtName = (TextView) convertView.findViewById(R.id.cityName);
      txtName.setText(city.getName());

      /* Take the TextView from layout and set the city's wiki link */
      TextView txtWiki = (TextView) convertView.findViewById(R.id.cityLinkWiki);
      txtWiki.setText(city.getUrlWiki());

      Typeface face=Typeface.createFromAsset(getAssets(),"fonts/roboto.ttf");

      txtName.setTypeface(face);
      txtWiki.setTypeface(face);

      return convertView;
}

EDIT :

Change this line,

TypefacetextFont=Typeface.createFromAsset(getApplicationContext().getAssets(),"RobotoBoldCondensed.ttf");

with,

textFont=Typeface.createFromAsset(getApplicationContext().getAssets(),"RobotoBoldCondensed.ttf");
Pratik Sharma
  • 13,307
  • 5
  • 27
  • 37
  • my listadapter is setup differently.. TextView yourTextView = (TextView) listAdapter.getView(0, null, null); Typeface textFont = Typeface.createFromAsset(getApplicationContext().getAssets(),"Roboto-BoldCondensed.ttf"); yourTextView.setTypeface(textFont); – user1979237 Jan 16 '13 at 11:47
0

in xml:

android:typeface

or in java:

setTypeface
0

Using Typeface you can change the font of your text, keep desire font ttf file in your assets folder, access and set to your desire view, just like below:

TextView txt = (TextView) findViewById(R.id.custom_font);  
Typeface font = Typeface.createFromAsset(getAssets(), "roboto.ttf");  
txt.setTypeface(font);

For more help check Quick Tip: Customize Android Fonts

RobinHood
  • 10,897
  • 4
  • 48
  • 97
0

Copy your font in to your assest folder and put this code inside of your custom array adapter

    TextView yourTextView = (TextView)findViewById(R.id.yourid);
    Typeface textFont = Typeface.createFromAsset(context.getAssets(),"YourFont.ttf");
    yourTextView.setTypeface(textFont);

It should work.

EDIT

private Typeface textFont; Declare

@Override
public void onCreate(){

textFont = Typeface.createFromAsset(context.getAssets(),"YourFont.ttf"); }

in OnCreate() or OnStart()

and just use your custom font in your getView()

yourTextView.setTypeface(textFont);
vinothp
  • 9,939
  • 19
  • 61
  • 103
  • have you initialised the `TypeFace` in `onCreate`. In Your code `TextView yourTextView = (TextView) listAdapter.getView(0, null, null);` `Typeface textFont=Typeface.createFromAsset(getApplicationContext().getAssets(),"RobotoBoldCondensed.ttf"); yourTextView.setTypeface(textFont);` remove this line `Typeface textFont=Typeface.createFromAsset(getApplicationContext().getAssets(),"RobotoBoldCondensed.ttf");` and put it in onCreate. – vinothp Jan 16 '13 at 11:57
  • sorry. what do you mean. And also post your error log. It will help to find the exact problem. – vinothp Jan 16 '13 at 12:26