0

I'm trying to change the font on my navigationdrawer, I'm using a custom xml file with the adapter so I can change successfully the color and background of each element within the ListView, nevertheless I can't change the Typeface, AndroidStudio is giving me a null object reference so, I think it hasn't found the textview. My code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    mDrawerListView = (ListView) inflater.inflate(
            R.layout.fragment_navigation_drawer, container, false);
    TextView lbl = (TextView) mDrawerListView.findViewById(R.id.section_label);
    Typeface tf = Typeface.createFromAsset(getActivity().getAssets(),"fonts/Jaapokki-Regular.otf");
    lbl.setTypeface(tf);
    mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    });
    mDrawerListView.setAdapter(new ArrayAdapter<String>(
            getActionBar().getThemedContext(),
            R.layout.fragment_main,
            R.id.section_label,
            new String[]{
                    getString(R.string.title_section1),
                    getString(R.string.title_section2),
                    getString(R.string.title_section3),
                    "Acerca de...",
            }));

    mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
    return mDrawerListView;
}

Thanks so much!

user1423168
  • 155
  • 1
  • 1
  • 13
  • I doubt android doesn't supports OpenType fonts. Try using TrueType fonts instead. http://stackoverflow.com/questions/1426244/use-external-fonts-in-android. Or try using a custom adapter and change the typeface from adapter's getview() method. – Srijith Aug 04 '15 at 09:49
  • Yes it is, I'm using it in another activity – user1423168 Aug 04 '15 at 09:53

1 Answers1

1

try with this

mDrawerListView.setAdapter(new ArrayAdapter<String>(getActionBar().getThemedContext(), R.layout.fragment_main,R.id.section_label, new String[]{
                getString(R.string.title_section1),
                getString(R.string.title_section2),
                getString(R.string.title_section3),
                "Acerca de...",
                }) {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = super.getView(position, convertView, parent);          
        TextView tv=(TextView)v.findViewById(R.id.section_label);
        tv.setTypeface(tf);             
        return v;
    }
});
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Ravi
  • 34,851
  • 21
  • 122
  • 183