James Newton
James Newton

Reputation: 7092

Android: get current activity in an extended class

In an Android app, each time a new activity is loaded, I want to use text-to-speech to announce the name of the activity. How do I obtain the name of the current activity?

I am creating an application that is to be accessible to blind people. I have created an AccessibleActivity class which extends Activity; the other activity classes that I create extend that.

Here is a barebones version of the AccessibleActivity class:

package com.example;

import android.app.Activity;
import com.example.TTS;

public class AccessibleActivity extends Activity {

    TTS tts = new TTS(); // See below

    @Override
    public void onResume() {
        tts.resume(getApplicationContext());
        super.onResume();
    }

    @Override
    public void onPause() {
        tts.pause();
        super.onPause();
    }
}

Here is the TTS class used by the AccessibleActivity class:

package com.example;

import android.content.Context;
import android.speech.tts.TextToSpeech;
import java.util.Locale;

public class TTS {

    TextToSpeech ttobj;

    public void resume(Context context) {
        ttobj = new android.speech.tts.TextToSpeech(context,
                new android.speech.tts.TextToSpeech.OnInitListener() {

                    @Override
                    public void onInit(int status) {
                        if (status != android.speech.tts.TextToSpeech.ERROR) {
                            ttobj.setLanguage(Locale.UK);
// THIS IS WHERE I WANT TO USE THE NAME OF THE ACTIVITY, INSTEAD OF "Welcome"
                            speakText("Welcome");
                        }
                    }
                });           
    }

    public void pause() {
        if (ttobj != null) {
            ttobj.stop();
            ttobj.shutdown();
        }
    }

    public void speakText(String toSpeak) {
        ttobj.speak(toSpeak, android.speech.tts.TextToSpeech.QUEUE_FLUSH, null);
    }
}

For example, suppose this activity is loaded...

package com.example;

public class BrailleActivity extends AccessibleActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_braille);
    }
}

... and that the strings.xml file contains an entry like this:

<string name="activity_braille">Learn to type in Braille</string>

How can I get the identifier activity_braille at some point in or after the onResume() method in the AccessibleActivity class, so that the appropriate string can be used in the onInit() method of the TextToSpeech object?

Upvotes: 0

Views: 1335

Answers (1)

Sam Dozor
Sam Dozor

Reputation: 40734

Hard to tell what you're really asking here...but just to state the obvious you can get the String by calling context.getString(R.string.activity_braille). You can then pass that String to TTS. Likewise you could pass the integer value R.string.activity_braille to TTS and have TTS call getString().

Here's another variation, using the getTitle() method of an Activity. This would let you make your AccessiblityActivity generic, so that all of your Activities could inherit from it, and you would only have to customize the label for each Activity in your AndroidManifest.xml:

Give the Activity a title/label in your AndroidManifest.xml:

<activity android:name="com.example.BrailleActivity" android:label="@string/activity_braille"/>

and then just pass the Activity to the TTS object::

TTS yourTtsObject = new TTS();
yourTtsObject.onResume(this);

And then change TTS onResume to call getTitle():

public void resume(final Activity currentActivity) {
    ttobj = new android.speech.tts.TextToSpeech(currentActivity.getApplicationContext(),
            new android.speech.tts.TextToSpeech.OnInitListener() {

                @Override
                public void onInit(int status) {
                    if (status != android.speech.tts.TextToSpeech.ERROR) {
                        ttobj.setLanguage(Locale.UK);

                        speakText(currentActivity.getTitle());
                    }
                }
            });           
}

Upvotes: 1

Related Questions