0

Why can't I change the font style for TextView in Android Studio ?

I really tried everything what i know, and everytime the font is the same.

here is the code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:src="@drawable/my_image" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:fontFamily="@font/pixel"
        android:gravity="center"
        android:textColor="#FF0000"
        android:textSize="48sp" />

</RelativeLayout>

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="300dp"
    android:minHeight="80dp"
    android:updatePeriodMillis="60000"
    android:initialLayout="@layout/sexy_clock_widget"
    android:previewImage="@drawable/my_preview_image"
    android:widgetCategory="home_screen|keyguard">
</appwidget-provider>

package com.example.sexyclockwidget;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.os.Handler;
import android.widget.RemoteViews;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SexyClockWidget extends AppWidgetProvider {

    private static final String TIME_FORMAT = "HH:mm:ss";

    private Context context;
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        public void run() {
            updateClock();
            handler.postDelayed(this, 1000); // ponovno pokrenite ovaj runnable nakon 1 sekunde
        }
    };

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        this.context = context;

        for (int appWidgetId : appWidgetIds) {
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.sexy_clock_widget);

            // Postavi sliku na ImageView
            views.setImageViewResource(R.id.imageView, R.drawable.my_image);

            // Pokreni runnable koji će osvježavati vrijeme
            handler.postDelayed(runnable, 1000);

            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }

    @Override
    public void onDisabled(Context context) {
        super.onDisabled(context);
        // Zaustavi runnable kada je zadnji widget uklonjen
        handler.removeCallbacks(runnable);
    }

    private void updateClock() {
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, SexyClockWidget.class));

        for (int appWidgetId : appWidgetIds) {
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.sexy_clock_widget);

            // Postavi trenutno vrijeme na TextView
            SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT);
            String currentTime = sdf.format(new Date());
            views.setTextViewText(R.id.textView, currentTime);

            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }
}

I hope you can tell me what i can do. I can normally change font size, color, etc...

but font stlye is not working for some reason. I even tried the standard fonts available

Thank you

KiluSs
  • 421
  • 4
  • 13
mike9999
  • 47
  • 3
  • Maybe something wrong with your font file. It is usually in otf or ttf – KiluSs Apr 07 '23 at 07:01
  • 1
    You can't use custom fonts in an App Widget. For a little while after they'd introduced font resources, it was possible, but only because they'd inadvertently overlooked it. It's been fixed now, though. – Mike M. Apr 07 '23 at 13:48
  • a widget that shows the exact time, I need to make a widget. but if I want to use a custom font, then what is my alternative? – mike9999 Apr 08 '23 at 04:22
  • With the regular `RemoteViews` stuff, I think about the only option is to draw the text with your custom font to a `Bitmap`, and show that in an `ImageView` instead. There's a basic example in [this answer](https://stackoverflow.com/a/4411060). Apart from that, you might be able to do it in [Glance](https://developer.android.com/reference/kotlin/androidx/glance/package-summary) somehow, but that's a completely different setup that's more like Compose than Views, plus I have no idea how it would/could be done there, as I've not used it yet. – Mike M. Apr 08 '23 at 04:32

1 Answers1

0

Make sure that the font file you are using is a valid TrueType or OpenType font file with .ttf or .otf extension. You can try using a different font file to see if that resolves the issue.

If you are using a standard font such as 'sans-serif', 'serif', or 'monospace', then it's possible that the font is not being loaded properly.

In that case, you can try setting the font programmatically instead of using android:fontFamily attribute in XML:

TextView textView = views.findViewById(R.id.textView);
Typeface typeface = Typeface.create("sans-serif", Typeface.NORMAL);
textView.setTypeface(typeface);