I have extended Fragment Activity in a class where I also need to have a custom toolbar. So,I added the code to get the toolbar but the setSupportActionBar(toolbar) did not work.Then, I added AppCompatActivity.getActivity() to cast but that did not work as well.
Here is the code -
public class main_fragment extends FragmentActivity implements FragmentDrawer.FragmentDrawerListener {
private Toolbar toolbar;
private FragmentDrawer drawerFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_fragment);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
drawerFragment.setDrawerListener(this);
}
AppCompatActivity extends FragmentActivity. The first thing you have to change is
extends FragmentActivity
with
extends AppCompatActivity
getActivity() is a method of Fragment. Activity has not that method, so you don't need to call it to use setSupportActionBar and getSupportActionBar
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With