How to change the title of an Activity?

Asked

Viewed 2,211 times

0

I have an Activity and I need to change its title programmatically, I found a way but it doesn’t work for me, check it out:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_relatorio);

    Window w = getWindow();
    w.setTitle("Relatório de Vendas");
}

I tried to use api 21 and 22 and the title keeps showing the name of the application and the text of setTitle() is not displayed. How this change can be made?

  • I do via Manifest, but the funny thing is that in the last app I did, there is Activvity that appears the title and others not, and everything is right in XML.

2 answers

4

Just one more way

You can try to switch via xml as well.

On Androidmanifest.xml

android:label="Relatório de vendas"

Or, with the string reference

android:label="@strings/Relatorio_de_vendas"

For example:

<activity
        android:name=".Splash"
        android:label="@string/splash_activity_title" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

3


The method to change the title is setTitle in fact, but of Activity and not of Window

Once your class extends Activity just call directly:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_relatorio);

    setTitle("Relatório de Vendas");
} 

Documentation for the method setTitle of Activity

Browser other questions tagged

You are not signed in. Login or sign up in order to post.