Am extending a FragmentActivity
in a class which serves as my base activity where my other activities extend from.
My issue is when I extend my other activities from my base activity, I loose toolbar functionality. How can I add this to my base activity so that my activities can inherit the toolbar?
Any pointers?
Asked
Active
Viewed 1.2k times
5

codeFreak
- 353
- 1
- 3
- 15
-
do you mean actionBar ? – Ahmad Sanie May 10 '15 at 06:09
-
2No. I know how to add action bar. I mean toolbar. – codeFreak May 10 '15 at 06:24
2 Answers
3
You should extend your Activity from AppCompatActivity as this includes support for Fragments and the Toolbar
public class MainActivity extends AppCompatActivity { ...
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FragmentTransaction ft = getSupportFragmentManager.beginTransaction();
....
....

pcodex
- 1,812
- 15
- 16
2
In case fragments should have custom view of ToolBar
you can implement ToolBar
for each fragment separately.
add ToolBar
into fragment_layout
:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"/>
find it in fragment :
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
//set toolbar appearance
toolbar.setBackground(R.color.material_blue_grey_800);
//for crate home button
ActionBarActivity activity = (ActionBarActivity) getActivity();
activity.setSupportActionBar(toolbar);
activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
menu listener could be created two ways:
1.override onOptionsItemSelected
in your fragment:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case android.R.id.home:
getActivity().onBackPressed();
}
return super.onOptionsItemSelected(item);
}
2.set listener for toolbar when create it in onCreateView
():
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
return false;
}
});

sfmirtalebi
- 370
- 7
- 16

Nischal Hada
- 3,230
- 3
- 27
- 57
-
You mean I should extend Fragments instead of extending FragmentActivity for my base activity?? – codeFreak May 10 '15 at 06:23
-
-
It won't work because fragments require an activity to be attached for it to work. FragmentActivity where used pre- honeycomb. Honeycomb above require an activity – codeFreak May 10 '15 at 06:43
-
Better yet, create a Toolbar layouts folder, and use
tag to include it everywhere you use it to minimize code usage. This assumes you have the same toolbar in every fragment / activity – Lucas Crawford May 10 '15 at 07:05 -
-
-
ActionBarActivity is deprecated and in FragmentActivity there is no getActivity method – DoruChidean Nov 18 '15 at 15:48
-
Please ignore this, it's not correct. as of 2019. You have to use AppCompactActivity. – MobileEvangelist Sep 01 '19 at 05:08