how to add typeface and select a font from assets folder to customise list activity titles

200 views Asked by At
public class MainActivity extends ListActivity {
/**
 * This class describes an individual sample (the sample title, and the activity class that
 * demonstrates this sample).
 */
  private class Sample {
    private CharSequence title;
    private Class<? extends Activity> activityClass;

    public Sample(int titleResId, Class<? extends Activity> activityClass) {
        this.activityClass = activityClass;
        this.title = getResources().getString(titleResId);
    }

    @Override
    public String toString() {
        return title.toString();
    }
  }

/**
 * The collection of all samples in the app. This gets instantiated in {@link
 * #onCreate(android.os.Bundle)} because the {@link Sample} constructor needs access to {@link
 * android.content.res.Resources}.
 */
 private static Sample[] mSamples;

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Instantiate the list of samples.
    mSamples = new Sample[]{
            new Sample(R.string.title_story_one, StoryOneActivity.class),
            new Sample(R.string.title_story_two, StoryTwoActivity.class),
            new Sample(R.string.title_story_three, StoryThreeActivity.class),
            new Sample(R.string.title_story_four, StoryFourActivity.class),
            new Sample(R.string.title_story_five, StoryFiveActivity.class),
            new Sample(R.string.title_story_six, StorySixActivity.class),
            new Sample(R.string.title_story_seven, StorySevenActivity.class),
            new Sample(R.string.title_story_eight, StoryEightActivity.class),
            new Sample(R.string.title_story_nine, StoryNineActivity.class),
            new Sample(R.string.title_story_ten, StoryTenActivity.class),
            new Sample(R.string.title_story_eleven, StoryElevenActivity.class),
            new Sample(R.string.title_story_twelve, StoryTwelveeActivity.class),
            new Sample(R.string.title_story_thirteen, StoryThirteenActivity.class),
            new Sample(R.string.title_story_fourteen, StoryFourteenActivity.class),
            new Sample(R.string.title_story_fifteen, StoryFifteenActivity.class),
            new Sample(R.string.title_story_sixteen, StorySixteenActivity.class),
            new Sample(R.string.title_story_seventeen, StorySeventeenActivity.class),
            new Sample(R.string.title_story_eighteen, StoryEighteenActivity.class),
            new Sample(R.string.title_story_nineteen, StoryNineteenActivity.class),
            new Sample(R.string.title_story_twenty, StoryTwentyActivity.class),
            new Sample(R.string.title_story_twentyone, StoryTwentyOneActivity.class),
            new Sample(R.string.title_story_twentytwo, StoryTwentyTwoActivity.class),
            new Sample(R.string.title_story_twentythree, StoryTwentyThreeActivity.class),
            new Sample(R.string.title_story_twentyfour, StoryTwentyFourActivity.class),
            new Sample(R.string.title_story_twentyfive, StoryTwentyFiveActivity.class),
            new Sample(R.string.title_story_twentysix, StoryTwentySixActivity.class),
            new Sample(R.string.title_story_twentyseven, StoryTwentySevenActivity.class),
            new Sample(R.string.title_story_twentyeight, StoryTwentyEightActivity.class),
    };

    setListAdapter(new ArrayAdapter<Sample>(this,
            android.R.layout.simple_list_item_1,
            android.R.id.text1,
            mSamples));
 }

 @Override
 protected void onListItemClick(ListView listView, View view, int position, long id) {
    // Launch the sample associated with this list position.
    startActivity(new Intent(MainActivity.this, mSamples[position].activityClass));
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.main, menu);
    return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_about:
        Intent intent = new Intent(MainActivity.this, AboutActivity.class);
        startActivity(intent);
        return true;
                    }


            return super.onOptionsItemSelected(item);
     }
}
1

There are 1 answers

11
Alexey A. On

The inconvenience with custom fonts is that it should be manually set it to every TextView. Please have a look at example below of one approach in your case. Of course font asset should be loaded once in static field to perform better.

setListAdapter(new ArrayAdapter<Sample>(this,
            android.R.layout.simple_list_item_1,
            android.R.id.text1,
            mSamples) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            View view = super.getView(position, convertView, parent);
            TextView textView = (TextView) view.findViewById(R.id.text1);
            textView.setTypeface(Typeface.createFromAsset(getAssets(), "myfont.ttf"));
            return view;
        }
    });