How to add or change font in Android application

How to add or change font in Android application

Fonts are the book covers of the digital age: no matter how good the content might be, an ugly font is enough reason to turn away forever. Indeed, an ugly font is more than just ugly. It makes reading into a chore that proves exhausting for the eyes. On the other hand, a well-designed font can completely change the atmosphere and mood of a website or app.

That’s why finding the right font for your phone could be the difference between a wonderful Android experience and an Android that you despise using.

STEPS are…

  1. add to gradle  compile ‘uk.co.chrisjenx:calligraphy:2.2.0’
  2. Add your dezire font like this

 

dezire font

 

3 . Now create a class like This

 

public class MrGunjanApplication extends Application{
    @Override
    public void onCreate() {
        super.onCreate();
        CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                .setDefaultFontPath("fonts/lato_light.ttf")
                .setFontAttrId(R.attr.fontPath)
                .build()
        );
    }
}

4. Add this to manifest

<application
    android:name=".MrGunjanApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".activities.SplashActivity"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>.......

5. Add this in your activity

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}

Like

public class AboutUs extends AppCompatActivity {

   @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
    }
    Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about_us);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.arrow_left);
        toolbar.setSubtitle("About Us");
        Intent intent = getIntent();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            this.onBackPressed();
            return false;
        }
        return super.onOptionsItemSelected(item);

    }
}

5. Now Check you app font changed.

 




Categories: Android

Tags: ,,,

Leave A Reply

Your email address will not be published.

error: Alltutorial.in content is protected !!