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?