2

I used CollapsingToolbarLayout and try to set dynamically Toolbar Title using retrofit but it is not working. just look at my code :-

Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    final ApiInterface api = ApiClient.getApiService();
    Call<GetAttributesResponse> call = api.getAttributes(27, attributId);

    call.enqueue(new Callback<GetAttributesResponse>() {
        @SuppressLint("SetTextI18n")
        @Override
        public void onResponse(@NonNull Call<GetAttributesResponse> call, @NonNull Response<GetAttributesResponse> response) {
            if (response.code() == 200) {
                if (response.body() != null) {
                        titleName = response.body().getData().getAttributeName();
                        getSupportActionBar().setTitle(titleName);
                        toolbar.setTitle(titleName);
                    }
                }

        }

My Xml Code Structure:-

<android.support.design.widget.CoordinatorLayout 
<android.support.design.widget.AppBarLayout
    <android.support.design.widget.CollapsingToolbarLayout

         <ImageView
         </ImageView>
         <android.support.v7.widget.Toolbar
         </android.support.v7.widget.Toolbar>
    </android.support.design.widget.CollapsingToolbarLayout>

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

Hitech P
  • 103
  • 8

1 Answers1

0

try this :

collapsingToolbarLayout.setTitleEnabled(false);
toolbar.setTitle("My Title");

when you call setTitleEnabled(false);, the title appears in the toolbar

alireza easazade
  • 3,324
  • 4
  • 27
  • 35