Forumi


Povratak   PC Ekspert Forum > Računala > Software > Web dizajn, programiranje i ostalo
Ime
Lozinka

Odgovori
 
Uređivanje
Staro 07.04.2023., 19:44   #1
chief_wolfinjo
Guest
 
Postovi: n/a
Kako promjeniti font u Android widgetu - Android studio

bok,

naime početnik sam, i već sam sve živo probao ali jednostavno font se ne želi
promjeniti...

napravio sam jednostavan widget koji prikazuje točno vrijeme

i sad želim koristiti custom font

ali u Android studio skroz desno gdje je virtualni uređaj

tamo ne želi prikazati novi font

dok u layout pod design piše da font family: @font/pixel


ovo je code:

widget.xml:
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>
fonts.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
    <font
        android:fontStyle="normal"
        android:fontWeight="400"
        android:font="@font/pixel" />
</font-family>
molim pomoć

hvala
  Reply With Quote
Staro 08.04.2023., 11:04   #2
chief_wolfinjo
Guest
 
Postovi: n/a
probao sam par metoda i sa java ali nula bodova:
Code:
package com.example.sexyclockwidget;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Handler;
import android.widget.RemoteViews;
import androidx.core.content.ContextCompat;
import java.text.SimpleDateFormat;
import java.util.Date;


public class SexyClockWidget extends AppWidgetProvider {

    private Context context;

    private static final String TIME_FORMAT = "HH:mm:ss";
    private static final String SHARED_PREFS_FILE = "com.example.sexyclockwidget";
    private static final String PREF_TIME = "time";

    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        public void run() {
            updateClock(context); // pass the context parameter here
            handler.postDelayed(this, 1000);
        }
    };

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

        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(Context context) {
        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());

            // Set custom font for TextView
            Typeface font = Typeface.createFromAsset(context.getAssets(), "font/pixel.ttf");
            Paint paint = new Paint();
            paint.setTypeface(font);
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(ContextCompat.getColor(context, R.color.purple_200));
            paint.setTextSize(context.getResources().getDimension(R.dimen.text_size));

            // Draw text on bitmap and set it to ImageView
            Bitmap myBitmap = Bitmap.createBitmap((int) paint.measureText(currentTime), (int) paint.getTextSize(), Bitmap.Config.ARGB_8888);
            Canvas myCanvas = new Canvas(myBitmap);
            myCanvas.drawText(currentTime, 0, (int) paint.getTextSize(), paint);
            views.setImageViewBitmap(R.id.textView, myBitmap);

         appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }
}
  Reply With Quote
Oglasni prostor
Oglas
 
Oglas
Odgovori


Uređivanje

Pravila postanja
Vi ne možete otvarati nove teme
Vi ne možete pisati odgovore
Vi ne možete uploadati priloge
Vi ne možete uređivati svoje poruke

BB code je Uključeno
Smajlići su Uključeno
[IMG] kod je Uključeno
HTML je Isključeno

Idi na