Menggunakan setelan teks sistem

Android TV menyediakan setelan yang memungkinkan pengguna menyetel preferensi teks secara terpusat untuk menciptakan pengalaman yang kohesif di seluruh aplikasi media.

Setelan ini memungkinkan pengguna mengaktifkan teks, memilih bahasa pilihan, dan menentukan gaya teks kustom berdasarkan kebutuhan mereka. Pengguna juga dapat menentukan apakah mereka lebih menyukai teks yang disederhanakan dengan tingkat bacaan sekitar kelas tiga SD, yang dikenal sebagai "Easy Reader".

Panduan ini menunjukkan cara mendapatkan dan menerapkan setelan teks yang disediakan sistem ke teks di aplikasi Anda.

Temukan opsi teks, termasuk pratinjau gaya teks yang dipilih, di bagian Setelan > Aksesibilitas > Teks:

Menu setelan teks di Android TV.
Gambar 1. Halaman setelan teks.

Mendapatkan CaptioningManager

Dari aktivitas, dapatkan layanan CaptioningManager dari Context:

CaptioningManager captioningManager = (CaptioningManager) context.getSystemService(Context.CAPTIONING_SERVICE);

Menangani perubahan setelan teks

Tangani perubahan setelan teks dengan menerapkan class CaptioningChangeListener:

if (captioningManager != null) {
  // Define a class to store the CaptionStyle details.
  CurrentCaptionStyle currentCaptionStyle = new CurrentCaptionStyle();
  // Define the listeners.
  captioningManager.addCaptioningChangeListener(new CaptioningChangeListener() {

    @Override
    public void onEnabledChanged(boolean enabled) {
      super.onEnabledChanged(enabled);
      Log.d(TAG, "onEnabledChanged");
      currentCaptionStyle.isEnabled = enabled;
    }

    @Override
    public void onLocaleChanged(@Nullable Locale locale) {
      super.onLocaleChanged(locale);
      Log.d(TAG, "onLocaleChanged");
      currentCaptionStyle.locale = locale;
      if (locale == null) {
        currentCaptionStyle.isEasyReaderEnabled = false;
      } else {
        currentCaptionStyle.isEasyReaderEnabled = locale.getVariant().contains("simple");
      }
    }

    @Override
    public void onFontScaleChanged(float fontScale) {
      super.onFontScaleChanged(fontScale);
      Log.d(TAG, "onFontScaleChanged");
      currentCaptionStyle.fontScale = fontScale;
    }

    @Override
    public void onUserStyleChanged(@NonNull CaptionStyle userStyle) {
      super.onUserStyleChanged(userStyle);
      Log.d(TAG, "onUserStyleChanged");
      currentCaptionStyle.hasBackgroundColor = userStyle.hasBackgroundColor();
      currentCaptionStyle.backgroundColor = userStyle.backgroundColor;
      currentCaptionStyle.backgroundOpacity = userStyle.backgroundColor >>> 24;
      currentCaptionStyle.hasForegroundColor = userStyle.hasForegroundColor();
      currentCaptionStyle.foregroundColor = userStyle.foregroundColor;
      currentCaptionStyle.foregroundOpacity = userStyle.foregroundColor >>> 24;
      currentCaptionStyle.hasWindowColor = userStyle.hasWindowColor();
      currentCaptionStyle.windowColor = userStyle.windowColor;
      currentCaptionStyle.windowOpacity = userStyle.windowColor >>> 24;
      currentCaptionStyle.hasEdgeColor = userStyle.hasEdgeColor();
      currentCaptionStyle.edgeColor = userStyle.edgeColor;
      currentCaptionStyle.hasEdgeType = userStyle.hasEdgeType();
      currentCaptionStyle.edgeType = userStyle.edgeType;
      currentCaptionStyle.typeFace = userStyle.getTypeface();
    }

  });
}

Atau, panggil metode getUserStyle secara langsung:

CaptionStyle systemCaptionStyle = captioningManager.getUserStyle();