I need help in the custom Font and TextView

I'm new in android working on an app that gives the user info about all font's of Google. For that, I need to make an app with a TextView Something like this

On click of the TextView, the font will change With text.

I'm thinking about using onclicklistener

3 answers

  • answered 2018-09-24 03:42 Tung Duong

    Have 2 way to archive this

    1st way

    public class FontCache {
    
        private static HashMap<String, Typeface> fontCache = new HashMap<>();
    
        public static Typeface getTypeface(String fontname, Context context) {
            Typeface typeface = fontCache.get(fontname);
    
            if (typeface == null) {
                try {
                    typeface = Typeface.createFromAsset(context.getAssets(), fontname);
                } catch (Exception e) {
                    return null;
                }
    
                fontCache.put(fontname, typeface);
            }
    
            return typeface;
        }
    }
    

    This caches the fonts while minimizing the number of accesses to the assets. Now, since we've a method to access our custom font, let's implement a class, which extends TextView.

    Extending TextView Next, we'll create a new Java class, which extends TextView. This allows us to use that class in all XML views. It inherits all functionality and properties of a regular TextView; but adds our custom font.

    Once again, we're taking a peek at the source code of our eat foody project. The code might look complex for a second, but is straight-forward:

    public class EatFoodyTextView extends TextView {
    
        public EatFoodyTextView(Context context) {
            super(context);
    
            applyCustomFont(context);
        }
    
        public EatFoodyTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
    
            applyCustomFont(context);
        }
    
        public EatFoodyTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
    
            applyCustomFont(context);
        }
    
        private void applyCustomFont(Context context) {
            Typeface customFont = FontCache.getTypeface("SourceSansPro-Regular.ttf", context);
            setTypeface(customFont);
        }
    }
    

    The first three methods are just constructors, which we override to call a single method applyCustomFont(). That method is the important piece of the puzzle. It simply gets the (hopefully cached) font from our FontCache class. Lastly, we've to call setTypeface() with the font and we're almost done. In case you're wondering, we can call the setTypeface() directly (and not on a TextView object), since we're extending the TextView class.

    Using the Class You might wonder, if so much preparation is worth the effort. In this section you'll see that it is indeed. Because all you've left to do is use the class in an XML view and it automatically has your custom font. There is no Java code necessary!

    <RelativeLayout  
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <com.futurestudio.foody.views.EatFoodyTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/eat_foody_green_dark"
            android:textSize="20sp"
            android:text="Future Studio Blog"
            android:layout_marginBottom="24dp"/>
    
    </RelativeLayout>  
    

    As you can see, you can continue to use all niceties (e.g. textSize, textColor) of TextView. Now, just replace all elements with the class we just created, for example and you applied your custom font everywhere! (Ref: https://futurestud.io/tutorials/custom-fonts-on-android-extending-textview)

    2nd way Follow Google guide support from API 26 (Android 8) https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml

    Make change between textview to change font

    <RelativeLayout  
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/textview_normal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/eat_foody_green_dark"
            android:textSize="20sp"
            android:text="Future Studio Blog"
            android:layout_marginBottom="24dp"/>
        <com.futurestudio.foody.views.EatFoodyTextView
            android:id="@+id/textview_custom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/eat_foody_green_dark"
            android:textSize="20sp"
            android:text="Future Studio Blog"
            android:visibility="gone"
            android:layout_marginBottom="24dp"/>
    
    </RelativeLayout> 
    

    attention at android:visibility="gone" in Activity you use this code to toggle between 2 TextViews

        final TextView normalTextView = findViewById(R.id.textview_normal);
        final TextView customTextView = findViewById(R.id.textview_custom);
    
        normalTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                normalTextView.setVisibility(View.GONE);
                customTextView.setVisibility(View.VISIBLE);
            }
        });
    
        customTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                normalTextView.setVisibility(View.VISIBLE);
                customTextView.setVisibility(View.GONE);
            }
        });
    

  • answered 2018-09-24 04:10 Vishal Chhodwani

    You can implement your own custom font with TextView, EditText, Button etc.. by using android attributes.

    How to

    -Here are some steps to use:

    1.Create attribute file (res->values->attrs.xml)

    <?xml version="1.0" encoding="UTF-8"?>
    <resources>
        <declare-styleable name="TextElement">
            <attr name="font" format="string"/>
            <attr name="underline" format="boolean"/>
        </declare-styleable>
    </resources>
    

    2.Create Custom TextView class (anywhere in java folder) 3. Use attributes inside your layout file 4. and just run your code.

    Here is the full example of your question, you can go through this exmaple:

    Full Demonstration

  • answered 2018-09-24 06:28 Mohamad Rostami

    you can put "your_font.ttf" file in asset folder then import it with

    Typeface custom_font_1 = Typeface.createFromAsset(getAssets(),  "your_font.ttf");
    

    then assign it to your showCaseTextView with this

       showCaseTextView.setTypeFace(custom_font_1);
    

    then in your onClickListener of showCaseTextView to change your specifiedTextView font do like this

       specifiedTextView.setTypeFace(custom_font_1);
    

    and repeat it for other fonts.