Search code examples
androidandroid-layoutandroid-actionbartitlebar

settitle() is not working in android on button clicklistner


I use the setActionBarTitle(); to change the title of the action bar it work fine in onCreate() method but it did not work when i want to implement it on button click listener

   public class ScrollingActivity extends AppCompatActivity {
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scrolling);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        CollapsingToolbarLayout mToolbarlayout= (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
        setSupportActionBar(toolbar);

        Button newbutton= (Button) findViewById(R.id.changetitle);
        if (newbutton != null) {
            newbutton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(ScrollingActivity.this, "Hi", Toast.LENGTH_SHORT).show();
                    setActionBarTitle("HI");
                }
            });
        }
      }

   public void setActionBarTitle(String title) {
        //noinspection ConstantConditions
        getSupportActionBar().setTitle(title); //check not working

    }

 }

The xml which is used for the activity in which i want to change the title

    <?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"
        android:fitsSystemWindows="true"
        tools:context="appcom.taskremainder.calender.ScrollingActivity">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/app_bar_height"
            android:fitsSystemWindows="true"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

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

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

        <include layout="@layout/content_scrolling" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/fab_margin"
            android:src="@android:drawable/ic_menu_edit"
            app:layout_anchor="@id/app_bar"
            app:layout_anchorGravity="bottom|end" />

    </android.support.design.widget.CoordinatorLayout

>

Solution

  • Yes you are right that in onCreate() the Title of ActionBar/ToolBar changes , and if we call the method to change the Title on Button click it doesn't.

    The button is defined in the:

    <include layout="@layout/content_scrolling"/>
    

    I tried many different ways and stumbled upon this issue https://code.google.com/p/android/issues/detail?id=77763

    I have a work around if you would like to try (to change title)

    public class MainActivity extends AppCompatActivity {
    
    Toolbar toolbar;
    android.support.v7.app.ActionBar actionBar;
    CollapsingToolbarLayout mToolbarlayout;
    Button newButton;
    
    // On every click I will change the value of i
    static int i=1;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbarlayout= (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
        newButton= (Button) findViewById(R.id.changetitle);
    
        setSupportActionBar(toolbar);
        actionBar=getSupportActionBar();
    
        newButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //oldWay();
                newWay();
                }
        });
    }
    
    private void oldWay() {
        Toast.makeText(MainActivity.this, ""+actionBar.getTitle(), Toast.LENGTH_SHORT).show();
        actionBar.setTitle("Something Else");
        Toast.makeText(MainActivity.this/, ""+actionBar.getTitle(), Toast.LENGTH_SHORT).show();
    
        //Toast says that value has changed, but it doesn't on screen
    }
    
    private void newWay() {
        mToolbarlayout.setTitle("" + i++);
       }
    }
    

    Let me know if this helps or not.