1

How can i change Textview inside Toolbar when Fragments are changed? My toolbar is in a BaseActivity, so i could use in all activities.

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

                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/toolbar_title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginBottom="4dp"
                        android:layout_marginLeft="20dp"
                        ndroid:layout_marginRight="attr/actionBarSize"
                        android:layout_marginTop="4dp"
                        android:textColor="@color/colorAccent2"
                        android:textSize="19sp"
                        tools:ignore="HardcodedText" />


                </FrameLayout>
            </android.support.v7.widget.Toolbar>
Jean Vitor
  • 893
  • 1
  • 18
  • 24
Alimov Shohrukh
  • 65
  • 1
  • 5
  • 17

4 Answers4

4

You could easily change title using this line:

((MyAppCompatBaseActivity)getActivity()).getSupportActionBar().setTitle("Hello World!");

Edit:

I just saw that you have a TextView inside your Toolbar. So to set the title do this in your BaseActivity:

public class BaseActivity extends AppCompatActivity {

    TextView textViewTitle;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // Other Code
        textViewTitle = (TextView) findViewById(R.id.toolbar_title);
    }

    public void setTitle(String title) {
        textViewTitle.setText(title);
    }

}

Then in your Fragment just do this:

((BaseActivity)getActivity()).setTitle("Hello World!");
Eric B.
  • 4,622
  • 2
  • 18
  • 33
0

Simply you can do this inside your onCreateView() method.

getActivity().setTitle("Your Title");

EDIT 1:

For making title in center you need to make your layout like below.

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:paddingLeft="0dp"
            android:paddingRight="0dp"
            android:layout_width="match_parent"
            app:contentInsetStart="0dp"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary">

            <TextView
                    android:id="@+id/toolbar_title"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="LOGO"
                    android:textColor="@color/colorAccent"
                    android:textSize="32sp"
                    android:textStyle="bold"
                    tools:ignore="HardcodedText" />

    </android.support.v7.widget.Toolbar>
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
0

Make textView and toolbar as public in your BaseActivity. Now use the following code in your fragment.

((BaseActivity)getActivity()).yourTextView.setText("HOME");
Sergi Juanola
  • 6,531
  • 8
  • 56
  • 93
0

Just Add to Your Code

((MainActivity) getActivity()).getSupportActionBar().setTitle("Yeeee");