How to use a font icon (font-awesome) in an XML selector

Is it possible to use the font icon in the selector instead of the extruded one?

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/menu_home_press" android:state_pressed="true"></item> <item android:drawable="@drawable/menu_home"></item> </selector> 
+5
source share
3 answers

I changed the color of the text in the selector instead of drawable. His work is wonderful.

Create a class MyTextView that extends TextView

 public class MyTextView extends TextView { public MyTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context); } public MyTextView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public MyTextView(Context context) { super(context); init(context); } private void init(Context context) { Typeface tf = Typeface.createFromAsset(context.getAssets(), "fontawesome-webfont.ttf"); setTypeface(tf); } } 

Create a text_color_selector.xml selector

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="#ff0000" android:state_pressed="true" /> <!-- pressed --> <item android:color="#ff0000" android:state_focused="true" /> <!-- focused --> <item android:color="#000000" /> <!-- default --> </selector> 

And then use it in your layout

  <com.example.mohsin.myapplication.MyTextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="50sp" android:clickable="true" android:textColor="@drawable/text_color_selector" android:text="\uF242"> </com.example.mohsin.myapplication.MyTextView> 
+4
source

You can use font icons as follows:

1 - Copy the font-awesome font file into your resource directory

2 - Found symbol objects for the icons I wanted using this page

3 - Create an entry in the strings.xml file for each icon. For instance:

 <string name="icon_eg">&#xf13d;</string> 

4 - Download the font into the onCreate method and set it for the appropriate views:

 Typeface font = Typeface.createFromAsset( getAssets(), "fontawesome-webfont.ttf" ); ... Button button = (Button)findViewById( R.id.like ); button.setTypeface(font); 

Do not forget to indicate the line in sight.

 <Button android:id="@+id/my_btn" style="?android:attr/buttonStyleSmall" ... android:text="@string/icon_eg" /> 

Check this link for more information.

you cannot use it as a selector. but you can dynamically change the icons.

+1
source

The easiest way!

Since Android Support Library 26 and Android Studio 3.0 you can use the native font support in XML.

https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html

  • Create a font directory in the res directory
  • Copy font file to font directory
  • Create a new font resource file near the font file

     <?xml version="1.0" encoding="utf-8"?> <font-family xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <font android:font="@font/font_awesome_font" android:fontStyle="normal" android:fontWeight="400" app:font="@font/font_awesome_font" app:fontStyle="normal" app:fontWeight="400" /> </font-family> 

    Use android:font for API 26 and app:font for support API since 14.

  • Now you can use fontFamily in a TextView :

     <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:fontFamily="@font/font_awesome" android:text="\uF0da" /> 
  • To insert font-awesome char enter UTF code with \u

NOTE. Android Studio design preview does not display font-awesome char, but it works correctly in the application.

+1
source

Source: https://habr.com/ru/post/1242542/


All Articles