Java Code Examples for android.graphics.Typeface#createFromAsset()

The following examples show how to use android.graphics.Typeface#createFromAsset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NewMainActivity.java    From Cybernet-VPN with GNU General Public License v3.0 6 votes vote down vote up
protected void setupToolbar() {
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
    Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/JosefinSans.ttf");
    toolbarTitle.setTypeface(myTypeface);


    toolbar.setNavigationIcon(R.drawable.ic_menu_white);


    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mDrawer.toggleMenu();
        }
    });
}
 
Example 2
Source File: MainActivity.java    From Moment with GNU General Public License v3.0 6 votes vote down vote up
private void applyFontsToTitle(Toolbar toolbar) {
    int childCount = toolbar.getChildCount();
    for (int i = 0; i < childCount; i++) {
        View child = toolbar.getChildAt(i);
        if (child instanceof TextView) {
            TextView tv = (TextView) child;
            tv.setTextSize(getResources().getDimensionPixelSize(R.dimen.font_text_size));
            Typeface titleFont = Typeface.
                    createFromAsset(getAssets(), "fonts/AlexBrush-Regular.ttf");
            if (tv.getText().equals(toolbar.getTitle())) {
                tv.setTypeface(titleFont);
                break;
            }
        }
    }
}
 
Example 3
Source File: BookPageFactory.java    From eBook with Apache License 2.0 6 votes vote down vote up
private void getFontFromAssets() {
    mTypefaceList.add(Typeface.DEFAULT);

    String[] fontNameList = null;
    AssetManager assetManager = mContext.getAssets();
    try {
        fontNameList = assetManager.list(FontPopup.FONTS);
    } catch (IOException e) {
        e.printStackTrace();
    }

    for (int i = 0; i < fontNameList.length; i++) {

        String fontPath = FontPopup.FONTS + "/" + fontNameList[i];
        Typeface typeface = Typeface.createFromAsset(assetManager, fontPath);//根据路径得到Typeface
        mTypefaceList.add(typeface);
    }

}
 
Example 4
Source File: TypefacesUtil.java    From buddycloud-android with Apache License 2.0 6 votes vote down vote up
public static Typeface get(Context c, String assetPath) {
	synchronized (cache) {
		if (!cache.containsKey(assetPath)) {
			try {
				Typeface t = Typeface.createFromAsset(c.getAssets(),
						assetPath);
				cache.put(assetPath, t);
			} catch (Exception e) {
				Logger.error(TAG, "Could not get typeface '" + assetPath
						+ "' because " + e.getMessage());
				return null;
			}
		}
		return cache.get(assetPath);
	}
}
 
Example 5
Source File: BaseFragmentActivityTest.java    From edx-app-android with Apache License 2.0 6 votes vote down vote up
/**
 * Generic method for asserting title setup
 *
 * @param activity The activity instance
 * @param title    The expected title
 */
protected void assertTitle(BaseFragmentActivity activity, CharSequence title) {
    ActionBar bar = activity.getSupportActionBar();
    assumeNotNull(bar);
    assumeNotNull(title);
    Typeface type = Typeface.createFromAsset(
            activity.getAssets(), "fonts/OpenSans-Semibold.ttf");
    int titleId = activity.getResources().getIdentifier(
            "action_bar_title", "id", "android");
    TextView titleTextView = (TextView) activity.findViewById(titleId);
    assumeNotNull(titleTextView);
    assertThat(titleTextView).hasCurrentTextColor(
            activity.getResources().getColor(R.color.edx_white));
    assertEquals(type, titleTextView.getTypeface());
    assertEquals(bar.getTitle(), title);
}
 
Example 6
Source File: LyricsTextFactory.java    From QuickLyric with GNU General Public License v3.0 6 votes vote down vote up
public static Typeface get(String name, Context context) {
    Typeface tf = fontCache.get(name);
    if (tf == null) {
        try {
            if (name.contains("dyslexic"))
                tf = Typeface.createFromAsset(context.getAssets(), "fonts/opendyslexic.otf");
            else if (name.equals("bold"))
                tf = Typeface.create("sans-serif", Typeface.BOLD);
            else if (name.equals("light"))
                tf = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Light.ttf");
            else if (name.equals("medium"))
                tf = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Medium.ttf");
            else
                tf = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular.ttf");
        } catch (Exception e) {
            return null;
        }
        fontCache.put(name, tf);
    }
    return tf;
}
 
Example 7
Source File: TypefaceManager.java    From Android-Font-Library with The Unlicense 5 votes vote down vote up
/**
 * Returns the typeface identified by the given font name and style. The font must be a name
 * defined in any nameset in the font xml file. The style must be one of the constants defined
 * by {@link Typeface}.
 *
 * @param name The font name
 * @param style The style
 * @return The Typeface
 */
public Typeface getTypeface(String name, int style) {
	Font font = mFonts.get(name.toLowerCase());
	String file = font.styles.get(toInternalStyle(style));
	synchronized (mCache) {
		if (!mCache.containsKey(file)) {
			Log.i(TAG, String.format("Inflating font %s (style %d) with file %s",
					name, style, file));
			Typeface t = Typeface.createFromAsset(mContext.getAssets(),
					String.format("fonts/%s", file));
			mCache.put(file, t);
		}
	}
	return mCache.get(font.styles.get(toInternalStyle(style)));
}
 
Example 8
Source File: RadarWatchFace.java    From radar-watch-face with MIT License 5 votes vote down vote up
private void initializeHourTextPaint() {
    Resources resources = RadarWatchFace.this.getResources();
    Typeface radarTextTypeface = Typeface.createFromAsset(getAssets(), "fonts/NexaLight.ttf");
    float hourTextSize = 20;

    hourTextPaint = new Paint();
    hourTextPaint.setColor(ContextCompat.getColor(RadarWatchFace.this, R.color.tick_color));
    hourTextPaint.setStrokeWidth(resources.getDimension(R.dimen.radar_hand_stroke));
    hourTextPaint.setAntiAlias(true);
    hourTextPaint.setTextAlign(Paint.Align.LEFT);
    hourTextPaint.setTextSize(hourTextSize);
    hourTextPaint.setTypeface(radarTextTypeface);
}
 
Example 9
Source File: RadarWatchFace.java    From radar-watch-face with MIT License 5 votes vote down vote up
private void initializeRadarTextPaint() {
    Resources resources = RadarWatchFace.this.getResources();
    Typeface radarTextTypeface = Typeface.createFromAsset(getAssets(), "fonts/NexaLight.ttf");
    float radarTextSize = 60;

    radarTextPaint = new Paint();
    radarTextPaint.setColor(ContextCompat.getColor(RadarWatchFace.this, R.color.radar_text_color));
    radarTextPaint.setStrokeWidth(resources.getDimension(R.dimen.radar_hand_stroke));
    radarTextPaint.setAntiAlias(true);
    radarTextPaint.setTextAlign(Paint.Align.LEFT);
    radarTextPaint.setTextSize(radarTextSize);
    radarTextPaint.setTypeface(radarTextTypeface);
}
 
Example 10
Source File: TypefaceHelp.java    From X-Alarm with GNU Affero General Public License v3.0 5 votes vote down vote up
public static Typeface get(Context context, String name) {
    synchronized (CACHE) {
        if (!CACHE.containsKey(name)) {
            Typeface t = Typeface.createFromAsset(context.getAssets(), name);
            CACHE.put(name, t);
        }
        return CACHE.get(name);
    }
}
 
Example 11
Source File: MaterialSpinner.java    From XERUNG with Apache License 2.0 4 votes vote down vote up
private void initAttributes(Context context, AttributeSet attrs) {

        TypedArray a = context.obtainStyledAttributes(new int[]{R.attr.colorControlNormal, R.attr.colorAccent});
        int defaultBaseColor = a.getColor(0, 0);
        int defaultHighlightColor = a.getColor(1, 0);
        int defaultErrorColor = context.getResources().getColor(R.color.error_color);

        a.recycle();

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MaterialSpinner);
        baseColor = array.getColor(R.styleable.MaterialSpinner_ms_baseColor, defaultBaseColor);
        highlightColor = array.getColor(R.styleable.MaterialSpinner_ms_highlightColor, defaultHighlightColor);
        errorColor = array.getColor(R.styleable.MaterialSpinner_ms_errorColor, defaultErrorColor);
        disabledColor = context.getResources().getColor(R.color.disabled_color);
        error = array.getString(R.styleable.MaterialSpinner_ms_error);
        hint = array.getString(R.styleable.MaterialSpinner_ms_hint);
        floatingLabelText = array.getString(R.styleable.MaterialSpinner_ms_floatingLabelText);
        floatingLabelColor = array.getColor(R.styleable.MaterialSpinner_ms_floatingLabelColor, baseColor);
        multiline = array.getBoolean(R.styleable.MaterialSpinner_ms_multiline, true);
        minNbErrorLines = array.getInt(R.styleable.MaterialSpinner_ms_nbErrorLines, 1);
        alignLabels = array.getBoolean(R.styleable.MaterialSpinner_ms_alignLabels, true);
        thickness = array.getDimension(R.styleable.MaterialSpinner_ms_thickness, 1);
        thicknessError = array.getDimension(R.styleable.MaterialSpinner_ms_thickness_error, 2);
        arrowColor = array.getColor(R.styleable.MaterialSpinner_ms_arrowColor, baseColor);
        arrowSize = array.getDimension(R.styleable.MaterialSpinner_ms_arrowSize, dpToPx(DEFAULT_ARROW_WIDTH_DP));

        String typefacePath = array.getString(R.styleable.MaterialSpinner_ms_typeface);
        if (typefacePath != null) {
            typeface = Typeface.createFromAsset(getContext().getAssets(), typefacePath);
        }

        array.recycle();

        floatingLabelPercent = 0f;
        errorLabelPosX = 0;
        isSelected = false;
        floatingLabelVisible = false;
        lastPosition = -1;
        currentNbErrorLines = minNbErrorLines;

    }
 
Example 12
Source File: FontFactory.java    From tilt-game-android with MIT License 4 votes vote down vote up
public static StrokeFont createStrokeFromAsset(final FontManager pFontManager, final TextureManager pTextureManager, final int pTextureWidth, final int pTextureHeight, final PixelFormat pPixelFormat, final TextureOptions pTextureOptions, final AssetManager pAssetManager, final String pAssetPath, final float pSize, final boolean pAntiAlias, final int pColor, final float pStrokeWidth, final int pStrokeColor) {
	return new StrokeFont(pFontManager, new EmptyTexture(pTextureManager, pTextureWidth, pTextureHeight, pPixelFormat, pTextureOptions), Typeface.createFromAsset(pAssetManager, FontFactory.sAssetBasePath + pAssetPath), pSize, pAntiAlias, pColor, pStrokeWidth, pStrokeColor);
}
 
Example 13
Source File: Main.java    From Cook-It-Android-XML-Template with MIT License 4 votes vote down vote up
private void applyFontToMenuItem(MenuItem mi) {
    Typeface font = Typeface.createFromAsset(getAssets(), "fonts/SourceSansPro-Semibold.otf");
    SpannableString mNewTitle = new SpannableString(mi.getTitle());
    mNewTitle.setSpan(new CustomTypefaceSpan("" , font), 0 , mNewTitle.length(),  Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    mi.setTitle(mNewTitle);
}
 
Example 14
Source File: BoldTextView.java    From Instagram-Profile-Downloader with MIT License 4 votes vote down vote up
private void init() {
    Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
            GlobalConstant.BOLD_FONT);
    setTypeface(tf);
}
 
Example 15
Source File: FontFactory.java    From tilt-game-android with MIT License 4 votes vote down vote up
public static StrokeFont createStrokeFromAsset(final FontManager pFontManager, final ITexture pTexture, final AssetManager pAssetManager, final String pAssetPath, final float pSize, final boolean pAntiAlias, final int pColor, final float pStrokeWidth, final int pStrokeColor) {
	return new StrokeFont(pFontManager, pTexture, Typeface.createFromAsset(pAssetManager, FontFactory.sAssetBasePath + pAssetPath), pSize, pAntiAlias, pColor, pStrokeWidth, pStrokeColor);
}
 
Example 16
Source File: PostAdapter.java    From Hews with MIT License 4 votes vote down vote up
public void updatePostPrefs() {
    mFont = Typeface.createFromAsset(mContext.getAssets(),
        SharedPrefsManager.getPostFont(prefs) + ".ttf");
    mTextSize = SharedPrefsManager.getPostFontSize(prefs);
    mLineHeight = SharedPrefsManager.getPostLineHeight(prefs);
}
 
Example 17
Source File: FontUtil.java    From FimiX8-RE with MIT License 4 votes vote down vote up
public static Typeface getTf(AssetManager mgr) {
    if (lantingTF == null) {
        lantingTF = Typeface.createFromAsset(mgr, "lanting.TTF");
    }
    return lantingTF;
}
 
Example 18
Source File: ForecastBottomSheetDialogFragment.java    From good-weather with GNU General Public License v3.0 4 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_forecast_bottom_sheet, parent, false);

    String speedScale = Utils.getSpeedScale(getActivity());
    String percentSign = getActivity().getString(R.string.percent_sign);
    String pressureMeasurement = getActivity().getString(R.string.pressure_measurement);
    String mmLabel = getString(R.string.millimetre_label);

    Float temperatureMorning = mWeather.getTemperatureMorning();
    Float temperatureDay = mWeather.getTemperatureDay();
    Float temperatureEvening = mWeather.getTemperatureEvening();
    Float temperatureNight = mWeather.getTemperatureNight();

    String description = mWeather.getDescription();
    String temperatureMorningStr = getActivity().getString(R.string.temperature_with_degree,
                                                           String.format(Locale.getDefault(),
                                                                         "%.0f",
                                                                         temperatureMorning));
    String temperatureDayStr = getActivity().getString(R.string.temperature_with_degree,
                                                       String.format(Locale.getDefault(),
                                                                     "%.0f",
                                                                     temperatureDay));
    String temperatureEveningStr = getActivity().getString(R.string.temperature_with_degree,
                                                           String.format(Locale.getDefault(),
                                                                         "%.0f",
                                                                         temperatureEvening));
    String temperatureNightStr = getActivity().getString(R.string.temperature_with_degree,
                                                         String.format(Locale.getDefault(),
                                                                       "%.0f",
                                                                       temperatureNight));
    String wind = getActivity().getString(R.string.wind_label, mWeather.getWindSpeed(), speedScale);
    String windDegree = mWeather.getWindDegree();
    String windDirection = Utils.windDegreeToDirections(getActivity(),
                                                        Double.parseDouble(windDegree));
    String rain = getString(R.string.rain_label, mWeather.getRain(), mmLabel);
    String snow = getString(R.string.snow_label, mWeather.getSnow(), mmLabel);
    String pressure = getActivity().getString(R.string.pressure_label, mWeather.getPressure(), pressureMeasurement);
    String humidity = getActivity().getString(R.string.humidity_label, mWeather.getHumidity(), percentSign);

    TextView descriptionView = (TextView) v.findViewById(R.id.forecast_description);
    TextView windView = (TextView) v.findViewById(R.id.forecast_wind);
    TextView rainView = (TextView) v.findViewById(R.id.forecast_rain);
    TextView snowView = (TextView) v.findViewById(R.id.forecast_snow);
    TextView humidityView = (TextView) v.findViewById(R.id.forecast_humidity);
    TextView pressureView = (TextView) v.findViewById(R.id.forecast_pressure);

    TextView temperatureMorningView = (TextView) v.findViewById(
            R.id.forecast_morning_temperature);
    TextView temperatureDayView = (TextView) v.findViewById(
            R.id.forecast_day_temperature);
    TextView temperatureEveningView = (TextView) v.findViewById(
            R.id.forecast_evening_temperature);
    TextView temperatureNightView = (TextView) v.findViewById(
            R.id.forecast_night_temperature);
    Typeface typeface = Typeface.createFromAsset(getActivity().getAssets(),
                                                 "fonts/weathericons-regular-webfont.ttf");

    descriptionView.setText(description);
    windView.setTypeface(typeface);
    windView.setText(wind + " " + windDirection);
    rainView.setText(rain);
    snowView.setText(snow);
    humidityView.setText(humidity);
    pressureView.setText(pressure);
    if (temperatureMorning > 0) {
        temperatureMorningStr = "+" + temperatureMorningStr;
    }
    if (temperatureDay > 0) {
        temperatureDayStr = "+" + temperatureDayStr;
    }
    if (temperatureEvening > 0) {
        temperatureEveningStr = "+" + temperatureEveningStr;
    }
    if (temperatureNight > 0) {
        temperatureNightStr = "+" + temperatureNightStr;
    }
    temperatureMorningView.setText(temperatureMorningStr);
    temperatureDayView.setText(temperatureDayStr);
    temperatureEveningView.setText(temperatureEveningStr);
    temperatureNightView.setText(temperatureNightStr);

    return v;
}
 
Example 19
Source File: CustomTypeface.java    From custom-typeface with Apache License 2.0 2 votes vote down vote up
/**
 * This is a shortcut to let {@code CustomTypeface} create directly a {@link Typeface}
 * for you. This will create the Typeface from a file located in the assets directory.
 * For more information see the {@link #registerTypeface(String, Typeface)} method.
 *
 * @param typefaceName a name that will identify this {@code Typeface}
 * @param assets       a instance of {@link AssetManager}
 * @param filePath     a path to a TTF file located inside the assets folder
 *
 * @see #registerTypeface(String, Typeface)
 */
public void registerTypeface(String typefaceName, AssetManager assets, String filePath) {
    Typeface typeface = Typeface.createFromAsset(assets, filePath);
    mTypefaces.put(typefaceName, typeface);
}