ToolBar Title showing empty in Fragment

1.4k Views Asked by At

I am trying to add actionbar toolbar in my fragment. Toolbar is displaying but the title is showing empty.

enter image description here

XML code:

<android.support.v7.widget.Toolbar
                android:id="@+id/profile_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?android:attr/actionBarSize"
                android:minHeight="?android:attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

Here is my FragmentActivity:

public class ProfileFragment extends android.support.v4.app.Fragment {
    Toolbar toolbar;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_profile, container, false);
        toolbar = (Toolbar) getActivity().findViewById(R.id.profile_toolbar);
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Tamil");
        return view;
    }

}
4

There are 4 best solutions below

0
vanlooverenkoen On BEST ANSWER

It is best to set your toolbar in the activity itself. Otherwise you will always need to allocate extra memory everytime you change the fragment. If you set the toolbar in the parent activity you just can call:

getActivity().setTitle("title");

in the fragment. If you do it this way no extra memory will be allocated, and it is easier to maintain.

So:

Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.profile_toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);

wil go to the parent activity and will become:

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

if you want to set the title in a fragment or in the activity itself:

getActivity().setTitle("title");
0
Karthik Sridharan On
toolbar.setTitle("My title");

try this

0
sohan shetty On

TypeCaste to your ActivityName instead of AppCompatActivity like this

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_profile, container, false);
    toolbar = (Toolbar) getActivity().findViewById(R.id.profile_toolbar);
    ((YourFragmentActivity) getActivity()).setSupportActionBar(toolbar);
    ((YourFragmentActivity) getActivity()).getSupportActionBar().setTitle("Tamil");
    return view;
}
0
Nour On

As silly as this may sound, try to change the text color of your toolbar's title.

toolbar.setTitleTextColor(Color.parseColor("#ffffff"));