Android-RobotoTextView

repository·master·Indexed 21 days ago

https://github.com/johnkil/android-robototextview

A library providing native support for Roboto, Roboto Condensed, Roboto Slab, and Roboto Mono fonts for Android TextViews and their subclasses. Compatible with API 14+, it includes the RobotoTextView widget, RobotoTypefaceSpan for spannable strings, and a RobotoInflater for standard TextViews. Note: This project is deprecated in favor of native font implementations introduced in Android O (API 26).

Tokens
2.5K
Snippets
8
Records
9
Agent score
23%

What's inside Android-RobotoTextView

  1. Remove unused fonts from build to reduce APK size

    master

    If you do not need all the supported fonts (Roboto, Roboto Condensed, Roboto Slab, Roboto Mono), you can add a Gradle task to remove specific font files from the merged assets during the build process. For example, to remove RobotoSlab fonts:

    android.applicationVariants.all{ variant ->
        variant.mergeAssets.doLast {
            File fonts = file("$variant.mergeAssets.outputDir/fonts")
            if (fonts.exists()) {
                for (File file : fonts.listFiles()) {
                    if (file.getName().contains("RobotoSlab")) {
                        println("delete " + file.getName() + " font")
                        file.delete()
                    }
                }
            }
        }
    }
  2. Install the RobotoTextView Gradle Plugin

    master

    To use the plugin, first add the dependency to your project's buildscript block using the Sonatype snapshots repository. Then, apply the plugin to your specific module.

    Note: The version used in this example is 3.0.0-SNAPSHOT. Ensure you check for the latest available version if applicable.

    buildscript {
        repositories {
            maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
        }
        dependencies {
            classpath 'com.github.johnkil.android-robototextview:robototextview-gradle-plugin:3.0.0-SNAPSHOT'
        }
    }
    
    // Apply in your module
    apply plugin: 'com.devspark.robototextview.gradle-plugin'
  3. Install Android-RobotoTextView via Gradle or Maven

    master

    Add the dependency to your project using one of the following methods. The library is compatible from API 14 (Android 4.0).

    ### Gradle
    ```groovy
    compile 'com.github.johnkil.android-robototextview:robototextview:4.0.0'

    Maven

    <dependency>
        <groupId>com.github.johnkil.android-robototextview</groupId>
        <artifactId>robototextview</artifactId>
        <version>4.0.0</version>
        <type>aar</type>
    </dependency>
  4. Apply Roboto fonts to standard TextViews using RobotoInflater

    master

    If you want to use Roboto fonts on standard TextView widgets (instead of RobotoTextView), you must attach the RobotoInflater in your Activity's onCreate() method. Once attached, you can use the app:robotoTypeface attribute in your XML for standard TextView components.

    Note: IDEs like Android Studio may flag app:robotoTypeface on a standard TextView as an error. You can suppress this by adding tools:ignore="MissingPrefix" to the view and ensuring the xmlns:tools="http://schemas.android.com/tools" namespace is declared.

    // 1. Attach inflater in Activity
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        RobotoInflater.attach(this);
        super.onCreate(savedInstanceState);
    }
    
    // 2. Use in XML
    <TextView
        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"
        app:robotoTypeface="roboto_light_italic"
        tools:ignore="MissingPrefix"/>
  5. Use RobotoTextView in XML Layouts

    master

    You can use com.devspark.robototextview.widget.RobotoTextView in your XML layouts. You have two ways to specify the typeface:

    1. Using app:robotoTypeface: A single parameter for a specific typeface preset.
    2. Using individual parameters: Combine app:robotoFontFamily, app:robotoTextWeight, and app:robotoTextStyle for more granular control.
    <!-- Option 1: Using robotoTypeface -->
    <com.devspark.robototextview.widget.RobotoTextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:robotoTypeface="roboto_light_italic"/>
    
    <!-- Option 2: Using individual parameters -->
    <com.devspark.robototextview.widget.RobotoTextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:robotoFontFamily="roboto"
        app:robotoTextWeight="light"
        app:robotoTextStyle="italic"/>
  6. Configure font inclusion and exclusion

    master

    The plugin allows you to manage which Roboto fonts are kept in your project to reduce size. You can either specify a list of fonts to remove using exclude or a list of fonts to keep using include.

    Important: Do not use include and exclude in the same configuration block. If both are present, exclude will be ignored.

    // To remove specific fonts
    robototextview {
        exclude 'RobotoSlab', 'RobotoMono'
    }
    
    // OR to keep only specific fonts
    robototextview {
        include 'Roboto', 'RobotoCondensed'
    }
  7. Configure RobotoTextView in Java code

    master

    To set the typeface programmatically on a RobotoTextView instance, use the RobotoTypefaces.setUpTypeface() method. You can either pass a single typeface constant or specify the family, weight, and style separately.

    <!-- Using a single typeface constant -->
    RobotoTextView textView = new RobotoTextView(context);
    RobotoTypefaces.setUpTypeface(
            textView, 
            RobotoTypefaces.TYPEFACE_ROBOTO_LIGHT_ITALIC);
    
    <!-- Using individual parameters -->
    RobotoTextView textView = new RobotoTextView(context);
    RobotoTypefaces.setUpTypeface(
            textView, 
            RobotoTypefaces.FONT_FAMILY_ROBOTO,
            RobotoTypefaces.TEXT_WEIGHT_LIGHT,
            RobotoTypefaces.TEXT_STYLE_ITALIC);
  8. Use RobotoTypefaceSpan for Spannable strings

    master

    To apply Roboto fonts to specific parts of a string using Spans, use RobotoTypefaceSpan. You can initialize it with a single typeface constant or with individual font parameters.

    <!-- Using a single typeface constant -->
    RobotoTypefaceSpan span = new RobotoTypefaceSpan(
            context, 
            RobotoTypefaces.TYPEFACE_ROBOTO_LIGHT_ITALIC);
    Spannable spannable = new SpannableString("text");
    spannable.setSpan(span, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    
    <!-- Using individual parameters -->
    RobotoTypefaceSpan span = new RobotoTypefaceSpan(
            context,
            RobotoTypefaces.FONT_FAMILY_ROBOTO,
            RobotoTypefaces.TEXT_WEIGHT_LIGHT,
            RobotoTypefaces.TEXT_STYLE_ITALIC);
    Spannable spannable = new SpannableString("text");
    spannable.setSpan(span, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);