How to change the title of an activity?

0

I have an activity and I need to change its title programmatically, I found a way but it does not work for me, see:

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 in api 21 and 22 and the title continues to show the application name and the setTitle() text is not displayed. How can this change be made?

    
asked by anonymous 16.03.2018 / 03:42

2 answers

2

The method for changing the title is setTitle de facto, but Activity and not Window

Since 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 method setTitle of Activity

    
16.03.2018 / 03:52
2

Just one more shape

You can try to change via xml as well.

In 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>
    
16.03.2018 / 03:54