Monthly Archives: September 2018

Use AndroidX in a project

Use AndroidX in a new project
gradle.properties file:

android.useAndroidX=true
android.enableJetifier=true

Migrate an existing project using Android Studio

select Refactor > Migrate to AndroidX from the menu bar

References
https://developer.android.com/topic/libraries/support-library/androidx-overview?utm_source=android-studio&utm_medium=studio-assistant#migration
https://developer.android.com/topic/libraries/support-library/refactor#migrate

Asset font is not loaded in android webView

@font-face { font-family: 'Persian'; src: url('fonts/b_yekan.ttf'); }
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<style type="text/css">
    @font-face { font-family: 'B Homa'; src: url('fonts/BHOMA.TTF');}
    @font-face { font-family: 'B Lotus'; src: url('fonts/BLOTUS.TTF');}
    @font-face { font-family: 'B Lotus Bold'; src: url('fonts/BLOTUSBD.TTF');}
</style>
</head>
<body>
    <p style="font-family:'B Homa';font-size:20px;">B Homa: مخصص</p>
    <p style="font-family:'B Lotus';font-size:20px;">B Lotus: مخصص</p>
    <p style="font-family:'B Lotus Bold';font-size:20px;">B Lotus Bold: مخصص</p>
</body>
</html>

References

https://stackoverflow.com/questions/25077094/asset-font-is-not-loaded-in-android-webview

Change status bar text color when primaryDark is white

you can use API 23 specific stuff, you can add the following to your AppTheme styles.xml:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <!-- Status bar stuff. -->
    <item name="android:statusBarColor">@color/colorPrimaryDark</item>
    <item name="android:windowLightStatusBar">true</item> 
</style>

References

https://stackoverflow.com/questions/38382283/change-status-bar-text-color-when-primarydark-is-white

Change font on TabLayout using Calligraphy

private void initTabs() {
        TabPagerAdapter adapter = new TabPagerAdapter(getSupportFragmentManager());
        pager.setAdapter(adapter);
        tabLayout.setupWithViewPager(pager);
        //tabLayout it doesn't respond to style attribute 'tabTextAppearance' so we have to use customview
        for (int i = 0; i < adapter.getCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(R.layout.tab_text);
                tab.setText(adapter.getPageTitle(i));
            }
        }
    }

res/layout/tab_text.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- needs this container to work with the Tablayout -->
<FrameLayout 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="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center">

 <TextView
        android:id="@android:id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:gravity="center"
        android:maxLines="2"
        app:fontPath="**your custom font here**"
        tools:ignore="MissingPrefix"
        tools:text="User Profile" />
</FrameLayout>

References

https://github.com/chrisjenx/Calligraphy/issues/180