0
votes

I am unable to set FontFace in a list view.

In buttons or textviews, I am able to set font like

Typeface typeface=Typeface.createFromAsset(getAssets(),"fonts/Raleway-Medium.ttf"); button.setTypeface(typeface);

How can I do it for the String appearing in ListView as well?

XML

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:id="@+id/linearLayout"
        android:elevation="10dp">
        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </ListView>
    </LinearLayout>

Java

 ListView listView= (ListView) findViewById(R.id.list);


    // Defined Array values to show in ListView
    String[] values = new String[] { "string 1", "string 2", ..... }
    };



    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, values);


    // Assign adapter to ListView
    listView.setAdapter(adapter);



    // ListView Item Click Listener
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            // ListView Clicked item index
            // ListView Clicked item value
            String  itemValue    = (String) listView.getItemAtPosition(position);
            gp.setphrase(itemValue);
            Intent i = new Intent(PhraseListView.this,record.class);
            startActivity(i);
        }});}

:(

2
Here's a hint. The TextView you wish to assign your Typeface to exists within android.R.layout.simple_list_item_1.Mapsy
Got it, Trying! :)Urvika G
Awesome! It might be helpful to know that internally, Android uses android.R.id.text1 as the resourceId for the TextView.Mapsy
in oncreate final Typeface typeface2 = Typeface.createFromAsset(getAssets(), "fonts/Raleway-Medium.ttf"); after setting adapter TextView text1= (TextView)this.findViewById(android.R.id.text1); text1.setTypeface(typeface2); dint work the problem is of this or view in this. could you help pleaseUrvika G
you cannot modify the textview which will be used by the adapter for view creation , although can create a layout , add font file to textview , mention the textview id while adapter creation and donePavneet_Singh

2 Answers

1
votes

You can create a customize item.xml layout with fonts in XML which has backward compatibility till API 14

and use ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects) constructor

0
votes

My personal recommendation is don't. Use a solution like Calligraphy to handle custom face fonts. https://github.com/chrisjenx/Calligraphy