1

I want to change font type of textview when i click the respective item in menu subitem. I have font options Kadwa, Vollkorn and Default which is droid sans. I have been able to show toast on click. I am a newbie to android studio and below is my code so far and I have attached a screenshot below. No idea what to do next. Thanks for your help. Image

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement

    if (id == R.id.item2) {
        Toast.makeText(this, "Night mode", Toast.LENGTH_LONG).show();
        return true;
    }

    if (id == R.id.item3) {
        Toast.makeText(this, "Fonts", Toast.LENGTH_LONG).show();
        return true;
    }

    if (id == R.id.font1) {

        Toast.makeText(this, "Font 1", Toast.LENGTH_LONG).show();
        return true;
    }

    if (id == R.id.font2) {
        Toast.makeText(this, "Font 2", Toast.LENGTH_LONG).show();
        return true;
    }

    if (id == R.id.font3) {
        Toast.makeText(this, "Font 3", Toast.LENGTH_LONG).show();
        return true;
    }


    return super.onOptionsItemSelected(item);
}

Menu xml

  <?xml version="1.0" encoding="utf-8"?>
  <menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto">

<item android:title="@string/settings"
    app:showAsAction="always">

    <menu>

        <item android:id="@+id/item2"
            android:title="@string/night_mode"
            android:icon="@drawable/ic_night"
            app:showAsAction="withText" />

        <item android:id="@+id/item3"
            android:title="@string/font"
            app:showAsAction="never" >

            <menu>

                <item android:id="@+id/font1"
                    android:title="Kadwa"/>

                <item android:id="@+id/font2"
                    android:title="Vollkorn"/>

                <item android:id="@+id/font3"
                    android:title="Default"/>

            </menu>

        </item>


    </menu>


</item>
  • create one method and change font inside it. check [here](https://stackoverflow.com/a/9820211/7783718) for how to change font. – Abhay Koradiya Dec 12 '18 at 09:57
  • Attach these fonts, then https://google.com/search?q=android+change+font+textview – Geno Chen Dec 12 '18 at 09:59
  • 1
    Possible duplicate of [How to change the font on the TextView?](https://stackoverflow.com/questions/2888508/how-to-change-the-font-on-the-textview) – GianhTran Dec 12 '18 at 10:10

2 Answers2

1
//create font using font from asset

Typeface font = Typeface.createFromAsset(
getContext().getAssets(), 
"fonts/androidnation.ttf");

textview.setTypeface(font);
0

Use setTypeFace method on TextView.

you can get font/typeface by:

ResourcesCompat.getFont(this, R.font.fontnamehere)
Rahul Khurana
  • 8,577
  • 7
  • 33
  • 60