0

I'm trying to use font-awesome icons and having some problem. It worked from MainActivity but when I use from Fragment its not showing up the icons.

This is my HomeFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

  View home = inflater.inflate(R.layout.fragment_home, container, false);
  View feed_row = inflater.inflate(R.layout.feed_row, container, false);

  like_button = (Button) feed_row.findViewById(R.id.like_button);
  Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "fonts/fontawesome-webfont.ttf");
  like_button.setTypeface(tf);

  mHomeFeed = (RecyclerView) home.findViewById(R.id.home_feed);
  mLayoutManager = new LinearLayoutManager(this.getActivity());
  mHomeFeed.setHasFixedSize(true);
  mHomeFeed.setLayoutManager(mLayoutManager);

This is feed_row.xml:

<Button
   android:id="@+id/like_button"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/icon_heart"
   android:background="@color/noBackground"/>

Strings.xml

<string name="icon_heart">&#xf004;</string>
<string name="icon_comment">&#xf0e5;</string>

What should I do now? It had no error but icons looked like below:

this

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
kuloo
  • 11
  • 1

2 Answers2

0

Instead using:

Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), 
                                       "fonts/fontawesome-webfont.ttf");

which has memory issues because constantly creating a new typeface for each icon (as in the answer comment), you can use Calligraphy. Where after you have configured your app with the library, you only need to add fontPath attribute:

So you have a more and flexible code, where the font is only in the XML:

<Button
   android:id="@+id/like_button"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/icon_heart"
   android:background="@color/noBackground"
   fontPath="fonts/fontawesome-webfont.ttf"/>
Community
  • 1
  • 1
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
0

You can do this by making a class and extends it from Button class like this

public class AwesomeFontButton extends Button {

    public AvenirBookEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public AvenirBookEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AvenirBookEditText(Context context) {
        super(context);
        init();
    }

    private void init() {
        if (!isInEditMode()) {
            Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
                    "fontawesome-webfont.ttf"); //add your font path here
            setTypeface(tf);
        }
    }

}

and use it in xml like this:

<YourFilePath.AwesomeFontButton 
   android:id="@+id/like_button"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/icon_heart"
   android:background="@color/noBackground"/>

use it in xml wherever you need

AbhayBohra
  • 2,047
  • 24
  • 36