简体   繁体   中英

How can i set Toolbar title using Retrofit android?

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>

try this :

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM