Java – Change the collapsed toolbar title after a Retrofit call

Change the collapsed toolbar title after a Retrofit call… here is a solution to the problem.

Change the collapsed toolbar title after a Retrofit call

I’m trying to modify the toolbar title based on the Retrofit2 response, but nothing changes.

getSupportActionBar().setTitle("here work");

final Call<Process> getProcess = WiimApi.getService(serverAddress).getProcess(id);

getProcess.enqueue(new Callback<Process>() {
    @Override
    public void onResponse(Call<Process> call, Response<Process> response) {
        mProcess = response.body();

getSupportActionBar().setTitle(mProcess.getName());  this not work
    }

@Override
    public void onFailure(Call<Process> call, Throwable t) {
        // ... 
    }
});

mProcess.getName() is a string from a JSON file. I also tested with hardcoded strings (preventing mProcess.getName() from wrong values) but it didn’t work.
Working just before the call works like a charm.

Is there a way to update the toolbar title after getting the callback?

Update

Thanks for your help. Now I found the real problem: collapsing the toolbar.
I made a blank app to reproduce the error and the code works, but when I run the same code with the collapsed toolbar… Bingo! The complete code is as follows:

Main Activity .java:

package com.example.retrofittitle;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import retrofit2. Call;
import retrofit2. Callback;
import retrofit2. Response;

public class MainActivity extends AppCompatActivity {

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

getData();
    }

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

if (id == R.id.action_settings) {
            return true;
        }

return super.onOptionsItemSelected(item);
    }

public void getData() {
        getSupportActionBar().setTitle("Here work ...");

 https://raw.githubusercontent.com/LearnWebCode/json-example/master/
        final Call<Pet> getPet = MyApi.getService("https://raw.githubusercontent.com/LearnWebCode/json-example/master/").getPet();

getPet.enqueue(new Callback<Pet>() {
            @Override
            public void onResponse(Call<Pet> call, Response<Pet> response) {
                Pet pet = response.body();

getSupportActionBar().setTitle(pet.getName());  Here not work
                TextView text = findViewById(R.id.log);
                text.setText("Done");
            }

@Override
            public void onFailure(Call<Pet> call, Throwable t) {
                getSupportActionBar().setTitle("Failure Title");
            }
        });

}
}

MyApi.java

package com.example.retrofittitle;

import retrofit2. Call;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;

public class MyApi {
    public interface ApiService {
        @GET("pet-of-the-day.json")
        Call<Pet> getPet();
    }

public static ApiService getService(String url) {
        retrofit2. Retrofit retrofit = new retrofit2. Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

return retrofit.create(ApiService.class);
    }
}

Pet .java

package com.example.retrofittitle;

public class Pet {
    private String name;
    private String species;
    private Integer age;
    private String photo;

public String getName() {
        return name;
    }

public void setName(String name) {
        this.name = name;
    }

public String getSpecies() {
        return species;
    }

public void setSpecies(String species) {
        this.species = species;
    }

public Integer getAge() {
        return age;
    }

public void setAge(Integer age) {
        this.age = age;
    }

public String getPhoto() {
        return photo;
    }

public void setPhoto(String photo) {
        this.photo = photo;
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=". MainActivity">

<TextView
        android:id="@+id/log"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="Requesting..."
        android:layout_gravity="center" />

<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:theme="@style/AppTheme.AppBarOverlay">

<android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleGravity="top"
            app:expandedTitleMarginTop="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

<android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_collapseMode="pin" />

</android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

Solution

After discovering the real problem, I searched here and found a solution:
https://github.com/henrytao-me/smooth-app-bar-layout/issues/32

Set the CollapsingToolbarLayout widget ID (collapsing_toolbar) and Activity code:

CollapsingToolbarLayout mCollapsingToolbarLayout = findViewById(R.id.collapsing_toolbar);
mCollapsingToolbarLayout.setTitle(pet.getName());

Now renamed 😀

Solution

I’ve tried getSupportActionBar().setTitle("hello") in the transform callback; It works fine for me.

Your problem is that retrofit is called every time onFailure is called. Therefore, getSupportActionBar().setTitle("hello") will not be called.

You can also try getSupportActionBar().setTitle("hello") in onFailure.

Related Problems and Solutions