简体   繁体   中英

How to set custom font in ListView and AlertDialog?

I created ListView, When i click list item it will open AlertDialog Message with NegativeButton. These are worked Perfectly. Now, i want to set custom font to listview item and AlertDialog's title, message and NegativeButton's fonts. Even i tried custom font library but not getting expected output. Here am attaching my tried code below. Please can anyone tell me what's going wrong?

public class NewActivity extends AppCompatActivity {

ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new);

    Calligrapher calligrapher = new Calligrapher(this);
    calligrapher.setFont(this, "Acme-Regular.ttf", true);

    listView = findViewById(R.id.list1);
    final ArrayList<String> concept = new ArrayList<String>();
    concept.add("Who is the captain of Indian Cricket Team?");
    concept.add("When we are celebrating Teacher's Day?");
    concept.add("When we are celebrating Women's Day?");

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
android.R.layout.simple_list_item_1, concept);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @SuppressLint("ResourceType")
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            AlertDialog.Builder adb = new AlertDialog.Builder(NewActivity.this);
            switch (i)
            {

                case 0:
                    adb.setTitle(concept.get(i));
                    adb.setMessage("Virat Kholi");
                    adb.setNegativeButton("Close", null);
                    adb.show();
                    break;

                case 1:
                    adb.setTitle(concept.get(i));
                    adb.setMessage("September 5");
                    adb.setNegativeButton("Close", null);
                    adb.show();
                    break;

                case 2:
                    adb.setTitle(concept.get(i));
                    adb.setMessage("March 8");
                    Typeface customTypeOne = Typeface.createFromAsset(getAssets(), "Acme-Regular.ttf");
                    adb.setTypeface(customTypeOne);
                    adb.setNegativeButton("Close", null);
                    adb.show();
                    break;
   }
  }
 });
 }
}

xml file

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NewActivity">

<ListView
    android:id="@+id/list1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp"
    android:fontFamily="assets/Acme-Regular.ttf"
    tools:ignore="MissingConstraints" />


</android.support.constraint.ConstraintLayout>

Replace

 android:fontFamily="assets/Acme-Regular.ttf"

https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml

 android:fontFamily="@font/Acme-Regular.ttf"

You can set font using Typeface Programatically

    Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/font name.ttf");

     textview.setTypeface(custom_font);

there are two ways to change textview font family

First in xml File:

by default android studio fonts are saved in font folder in res . if you want to use these font please add this line app:fontFamily="@font/alegreya_bold"

Second in java or pragmatically in code

 TextView textView= view.findViewById(R.id.textview);
    textView.setTextColor(Color.BLACK);
    textView.setTextSize(13);
    Typeface avnier_roman = Typeface.createFromAsset(context.getAssets(), "AvenirLTStd-Roman.otf");
    textView.setTypeface(avnier_roman); 

sir you just correctly use the naming convention of your custom fonts name. because android doesn't allow the asset/ drawable in capital words.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM