0

I'm trying to write an app that uses TextToSpeech to speak a brief message without interfering with what is displayed on the screen.

MainActivity.kt reads as follows:

package com.example.nearbyplaces

import android.os.Bundle
import android.speech.tts.TextToSpeech
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import java.util.*

class MainActivity : AppCompatActivity(), TextToSpeech.OnInitListener {

    private var mTTS: TextToSpeech? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mTTS = TextToSpeech(this,this)
    }

    override fun onInit(status: Int) {
        if (status == TextToSpeech.SUCCESS) {
            val result = mTTS!!.setLanguage(Locale.ENGLISH)
            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS","The Language specified is not supported!")
            } else {
                speak("Nearby places")
            }
        } else {
            Log.e("TTS", "Initilization Failed!")
        }
    }

    private fun speak(mText: String) {
        mTTS!!.speak(mText, TextToSpeech.QUEUE_FLUSH, null, "")
    }

    override fun onDestroy() {
        if (mTTS != null) {
            mTTS!!.stop()
            mTTS!!.shutdown()
        }
        super.onDestroy()
    }
}

and AndroidManifest.xml is

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.nearbyplaces">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

With the default blank layout the app works, but displays the default Hello World view (with Title Bar) on top of what was there previously. If I remove the line setContentView(R.layout.activity_main) and safe delete the activity_main.xml, a blank view with Title Bar is still displayed.

Following advice on a separate thread I tried inserting android:theme="@android:style/Theme.Translucent.NoTitleBar" in the Manifest. This worked OK with an app that did nothing but display a Toast, but in the app with TextToSpeech code I get the error "You need to use a Theme.AppCompat theme (or descendant) with this activity.". LogCat reports:

2021-02-28 16:37:40.378 17048-17048/? I/le.nearbyplace: Late-enabling -Xcheck:jni
2021-02-28 16:37:40.411 17048-17048/? E/le.nearbyplace: Unknown bits set in runtime_flags: 0x8000
2021-02-28 16:37:41.240 17048-17048/com.example.nearbyplaces I/TextToSpeech: Sucessfully bound to com.google.android.tts
2021-02-28 16:37:41.304 17048-17048/com.example.nearbyplaces D/AndroidRuntime: Shutting down VM
2021-02-28 16:37:41.306 17048-17048/com.example.nearbyplaces E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.nearbyplaces, PID: 17048
    java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:843)
        at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:806)
        at androidx.appcompat.app.AppCompatDelegateImpl.onPostCreate(AppCompatDelegateImpl.java:527)
        at androidx.appcompat.app.AppCompatActivity.onPostCreate(AppCompatActivity.java:127)
        at android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1402)
        at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3528)
        at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
        at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2183)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:241)
        at android.app.ActivityThread.main(ActivityThread.java:7604)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)
2021-02-28 16:37:41.358 17048-17048/com.example.nearbyplaces I/Process: Sending signal. PID: 17048 SIG: 9

Is there a "Theme.AppCompat theme (or descendant)" with NoTitleBar, or any other way of preventing the blank view with Title Bar appearing?

prepbgg
  • 3,564
  • 10
  • 39
  • 51
  • It may help if you include what you're trying to do exactly and why, and how you expect a working version of this to behave. You may find there is a totally different approach/solution to the problem. – Nerdy Bunz Mar 02 '21 at 04:31
  • Thanks, Nerdy. I'm thinking about a different approach and shall come back with more specific questions if I get stuck. – prepbgg Mar 02 '21 at 16:32
  • After trying some alternative approaches, I've now done what Nerdy Bunz recommended and asked a new question describing what I'm trying to achieve, in case someone can suggest a better approach/solution. So far no responses: https://stackoverflow.com/questions/66615429/hands-free-app-to-display-information-while-navigating-with-google-maps – prepbgg Mar 14 '21 at 11:41

1 Answers1

0

you can hide and show action bar programmatically:

 actionBar?.hide()
 actionBar?.show()

or choose a theme without action bar:

 <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">

and for the Hello World view just remove it from activity_main.xml.

Sami Shorman
  • 149
  • 1
  • 11
  • Thanks for your suggestions. I tried actionBar?.hide() but still got the "You need to use a Theme.AppCompat theme" error. styles.xml already includes – prepbgg Feb 28 '21 at 19:07
  • – Sami Shorman Feb 28 '21 at 19:12
  • OK. The ActionBar has disappeared, but the screen goes dark grey.. – prepbgg Feb 28 '21 at 20:31
  • – Sami Shorman Feb 28 '21 at 20:45
  • I've added the dependency you recommended to build.gradle (Module:app), then amended styles.xml to include just the Theme.Test line that you recommended and in AndroidManifest.xml I've changed the theme line to android:theme="@style/Theme.Test">. However, I'm afraid that hasn't worked. The view is replaced by a blank white screen. Thanks for all your help. I'm going to abandon these attempts and accept that the app wants to display a view, so I'll print the spoken text in the view then wait for the speech to finish before closing the app. Thanks again. – prepbgg Mar 01 '21 at 17:01