4

I am trying to implement fragment using android 4.0. I have added the three items in list fragment in my DummyContent.java file

static {
    // Add 3 sample items.
    addItem(new DummyItem("1", "Videos"));
    addItem(new DummyItem("2", "Images"));
    addItem(new DummyItem("3", "Story"));
}

These Videos,Images,Story are appearing on left side of fragment on click of each it item shows details information on detail fragment in right hand side.

I want to change the font of these list views but problem is list fragment uses systems textview as below

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // TODO: replace with a real list adapter.
    setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(getActivity(),
            android.R.layout.simple_list_item_activated_1,
            android.R.id.text1, DummyContent.ITEMS));
}

Which is not allowing me to apply Typeface to android.R.id.text1

Please help me

Hemantwagh07
  • 1,516
  • 1
  • 15
  • 27

2 Answers2

8

One way to is to extend ArrayAdapter and override the getView method to get a hold of the TextView:

public class MyArrayAdapter<T> extends ArrayAdapter<T> {

    public MyArrayAdapter(Context context, List<T> items) {

        super(context, android.R.layout.simple_list_item_activated_1, android.R.id.text1, items);
    }

    public MyArrayAdapter(Context context, T[] items) {

        super(context, android.R.layout.simple_list_item_activated_1, android.R.id.text1, items);
    }

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

        View view = super.getView(position, convertView, parent);
        TextView textView = (TextView) view.findViewById(android.R.id.text1);
        textView.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "font/segoeuil.tff"));

        return view;
    }
}

You would then set your adapter like so:

setListAdapter(new MyArrayAdapter<DummyContent.DummyItem>(getActivity(), DummyContent.ITEMS);
Jason Robinson
  • 31,005
  • 19
  • 77
  • 131
  • Thanks for your answer. I tried this solution but the only problem is it throws `java.lang.RuntimeException: native typeface cannot be made` on line `textView.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "font/segoeuil.tff")); ` If i comment this line then it works without typeface.I am not able to find out the problem – Hemantwagh07 Jan 07 '13 at 07:21
  • Make sure your font file actually exists at `assets/font/segoeuil.tff` and that you don't have any typos – Jason Robinson Jan 07 '13 at 07:30
  • Yes.I checked that,file is exists at location `assets/font/segoeuil.tff`.Actually by using the same font i am able to change font of Detail fragment.Only it is not working for list fragment – Hemantwagh07 Jan 07 '13 at 07:33
  • are you sure your font is in `font` folder and not `fonts`? – Jason Robinson Jan 07 '13 at 07:39
  • If the above is not the case, copy and paste your `setTypeface` line from your Detail fragment. It has to be a simple typo somewhere. If it works in the Detail fragment, then use the exact same code you use there. – Jason Robinson Jan 07 '13 at 07:41
  • Thank you Jason, It is working for me but i have made some changes in code like I merge the both the solutions,I was having a custom.xml for text view,I used it for TypeFace so now i have code like `TextView textView = (TextView) view.findViewById(R.id.cust_view); //textView.setTypeface(Typeface.createFromAsset(ctx.getAssets(), "font/segoeuil.tff")); Typeface tf = Typeface.createFromAsset(ctx.getAssets(), "font/segoeuil.ttf"); textView.setTypeface(tf);` also I have changed the super constructor call `super(context, R.layout.custom,items);` – Hemantwagh07 Jan 07 '13 at 07:45
  • @JasonRobinson thank you, perfect code snippet! Maybe anybody can use this codesnippet for font change. Instead of hard coding the font style everywhere you just have to instantiate a class once and pass the UI element to the included method. And you only need to change one line to change the fonts again. – Alex Cio Jan 11 '13 at 16:23
1

You can use your own custom layout instead of predefined layouts Create an xml file like this

<?xml version="1.0" encoding="utf-8"?>
say your layout name be custom.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
 android:id="@+id/cust_view"  
    android:layout_width="match_parent" 
    android:textSize="12dp"
    android:padding="6dp"
    android:layout_height="wrap_content" /> 

you can give your own font to this layout. and than set the adapter like this

setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(getActivity(),
            R.layout.custom,
            android.R.id.text1, DummyContent.ITEMS));

I hope this will help you

Pragnani
  • 20,075
  • 6
  • 49
  • 74
  • I tried this solution but there is a problem `R.layout.custom.xml` dosen't work with .xml so i have removed .xml and added as `R.layout.custom` so it dose not show any error. But it crashes the application – Hemantwagh07 Jan 07 '13 at 06:19
  • Hey sorry we should not include .xml its just a typing mistake...What exactly the exception you are getting please mention that and also no need of third parameter setListAdapter(new ArrayAdapter(getActivity(), R.layout.custom, DummyContent.ITEMS)); – Pragnani Jan 07 '13 at 06:28
  • Yes Now it is changing the font size as per `custom.xml` But when i am trying to set Typeface as `TextView tv = ((TextView) findViewById(R.id.cust_view)); Typeface tf = Typeface .createFromAsset(getAssets(), "font/segoeuil.ttf"); tv.setTypeface(tf); setListAdapter(new ArrayAdapter(getActivity(), R.layout.custom, DummyContent.ITEMS));` then it shows an error for `findViewById` and `getAssets` – Hemantwagh07 Jan 07 '13 at 06:45