4

I use custom fonts in my app so i want a custom font for Crouton. I 've tried to do it with setTextAppearance, it doesn't work.

<?xml version="1.0" encoding="utf-8"?>
<com.ecab.ui.custom.TextViewCustomFont
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res/com.crouton"
    android:id="@+id/crouton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/ban_confirmation"
    android:gravity="center"
    android:text="TEST"
    android:textColor="@android:color/white"
    custom:typeface="gothamBold" />

In Style class :

INFOCUSTOM = new Builder().setDuration(3000).setTextAppearance(R.id.crouton).build();

Then, I've tried to do it by changing setTypeface() with my font, it doesn't work.

In Crouton class :

private TextView initializeTextView(final Resources resources) {
TextView text = new TextView(this.activity);
    text.setId(TEXT_ID);
    text.setText(this.text);
    text.setTypeface(MyFonts.getGothamBookBold(this.activity));
    Log.d(Constants.D_TAG, "chaneg the typeFace");
    text.setGravity(this.style.gravity);
    // set the text color if set
    if (this.style.textColorResourceId != 0) {
      text.setTextColor(resources.getColor(this.style.textColorResourceId));
    }

    // Set the text size. If the user has set a text size and text
    // appearance, the text size in the text appearance
    // will override this.
    if (this.style.textSize != 0) {
      text.setTextSize(TypedValue.COMPLEX_UNIT_SP, this.style.textSize);
    }

    // Setup the shadow if requested
    if (this.style.textShadowColorResId != 0) {
      initializeTextViewShadow(resources, text);
    }

    // Set the text appearance
    if (this.style.textAppearanceResId != 0) {
      text.setTextAppearance(this.activity, this.style.textAppearanceResId);
    }
    return text;
  }

What can i do to have a custom Font ?

ps : library version ==> 1.7

mrroboaat
  • 5,602
  • 7
  • 37
  • 65
  • 1
    [This answer](http://stackoverflow.com/a/15223451/543711) might help. – iTurki Apr 20 '13 at 20:37
  • Did you try what the creator suggested here : http://stackoverflow.com/a/15223451/644669 – Zakaria Apr 20 '13 at 20:37
  • Yeah ! I know this issue but it doesn't work ... – mrroboaat Apr 21 '13 at 09:00
  • possible duplicate of [Using custom layouts for the crouton library](http://stackoverflow.com/questions/15223169/using-custom-layouts-for-the-crouton-library) – Ben Weiss Apr 22 '13 at 11:20
  • @aat How are you editing the Crouton.class file? It's locked in build/intermediates/exploded-aar/de-key..../crouton/1.8.5/classes.jar – ono May 21 '15 at 20:18
  • @ono Library is open source so you can modify sources and add it to your project. – mrroboaat May 24 '15 at 12:48

3 Answers3

2

Okay, I found the problem !

It works with the second solution by changing the Typeface. I had just forget to remove the

setTextAppearance(R.id.crouton)

in the Style class. So my custom style is like this :

INFOCUSTOM = new Builder().setDuration(3000).setBackgroundDrawable(R.drawable.ban_confirmation).setHeight(LayoutParams.WRAP_CONTENT)
          .build();

One problem resolves, another arrives :) ! With the background drawable, the text is not vertically center

mrroboaat
  • 5,602
  • 7
  • 37
  • 65
1

You can a custom Style that uses the resourceId of your text appearance via Style.Builder.setTextAppearance(...).

This takes a reference from your styles.xml and uses it within the internal TextView of the Crouton.

Then you can call Crouton.makeText or Crouton.showText with your custom Style.

Source

Community
  • 1
  • 1
Zakaria
  • 14,892
  • 22
  • 84
  • 125
0

How does MyFonts.getGothamBookBold() look like?

This however should work:

  private TextView initializeTextView(final Resources resources) {
    TextView text = new TextView(this.activity);
    text.setId(TEXT_ID);
    text.setText(this.text);
    Typeface myTypeFace = Typeface.createFromAsset(this.activity.getAssets(), "gothamBold.ttf"); 
    text.setTypeface(myTypeFace);
    text.setGravity(this.style.gravity);

    // set the text color if set
    if (this.style.textColorResourceId != 0) {
      text.setTextColor(resources.getColor(this.style.textColorResourceId));
    }
Ahmad
  • 69,608
  • 17
  • 111
  • 137
  • I have a class to manage my custom fonts like this : `public static Typeface getGothamBookBold(Context ctx) { return Typeface.createFromAsset(ctx.getAssets(), BASE + "Gotham-Bold.otf"); }` – mrroboaat Apr 21 '13 at 09:01
  • How are you editing the Crouton.class file? It's locked in build/intermediates/exploded-aar/de-key..../crouton/1.8.5/classes.jar – ono May 21 '15 at 20:19
  • @ono I'm not sure I understand your question. I'm not editing the Crouton source code. – Ahmad May 21 '15 at 20:26
  • @Ahmad aren't you editing the `initializeTextView` method in the Crouton.class? – ono May 21 '15 at 20:34
  • @ono Ah, sorry. I didn't look at the context of the question. I've answered this question two years ago and couldn't remember that the OP asked for a custom Version of Cruston. Basically you have to, instead of adding Crouton as a dependency through gradle, add it as a module and modify the source yourself. I just checked, and it seem like Crouton still does not support custom fonts, so that's one of the possible solutions. Probably the most straightforward one. – Ahmad May 21 '15 at 20:55