Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set toolbar on FragmentActivity?

I want to set toolbar on my activity which extends FragmentActivity. I know that for use setSuppoertActionBar(toolbar) method we extends AppCompatActivity instead of FragmentActivity but I override the onMenuItemSelected(int featureId, MenuItem item) method which is final in AppCompatActivity and final method cannot override. so I'm restricted to extends FragmentActivity.

Here is my code:

public class MainActivity extends FragmentActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);   -> error is here
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
       // Inflate the menu
       getMenuInflater().inflate(R.menu.menu_main, menu);
       return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()){
            case R.id.action_search:
                onSearchRequested();
                break;
        }
        return super.onMenuItemSelected(featureId, item);
    }

I saw many answers related to that question but everyone says extends AppCompatActivity instead of FragmentActivity but I want to set toolbar as well as override onMenuItemSelected(int featureId, MenuItem item) method.

what should I do, please help.

like image 226
Ekta Bhawsar Avatar asked Jan 19 '16 06:01

Ekta Bhawsar


People also ask

How to set toolbar in fragment android example?

if you are using custom toolbar or ActionBar and you want to get reference of your toolbar/action bar from Fragments then you need to first get instance of your Main Activity from Fragment's onCreateView Method like below. ImageView vRightBtn = activity. toolbar. findViewById(R.

Can a fragment have toolbar?

When using fragments, the app bar can be implemented as an ActionBar that is owned by the host activity or a toolbar within your fragment's layout. Ownership of the app bar varies depending on the needs of your app.

What is difference between fragment and FragmentActivity?

If you want to use Fragments in an app targeting a platform version prior to HoneyComb, you need to add the Support Package to your project and use the FragmentActivity to hold your Fragments . The FragmentActivity class has an API for dealing with Fragments , whereas the Activity class, prior to HoneyComb, doesn't.


2 Answers

This thing is good when you are using NavigationDrawer use this:-

 toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

then set Toolbar Title according to different fragment with different Titles in onNavigationItemSelected :-

@Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.

        sfm = getSupportFragmentManager();
        Fragment fragment = new Fragment();
        int id = item.getItemId();

        if (id == R.id.nav_id) {
            fragment = new YourFragment();
            toolbar.setTitle("SET TOOLBAR NAME");
        }else if (id == R.id.nav_id2) {
            fragment = new YourFragment();
            toolbar.setTitle("SET TOOLBAR NAME");
        } 

For single fragment, first customize your style.xml like this :-

<style name="YourStyleName" parent="Theme.AppCompat.Light.DarkActionBar">
// ToDo as you want to do or as per your requirement

</style>

then apply into your custom toolbar:-

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

  // ...........


</android.support.v7.widget.Toolbar>
like image 130
Abhinav Gupta Avatar answered Sep 22 '22 18:09

Abhinav Gupta


You can also create your own toolbar:

First set up the main theme to extend Theme.AppCompat.Light.NoActionBar

<style name="style_1" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->

</style>

Remember to apply the theme:

@Override
protected void onCreate(Bundle savedInstanceState) {
    this.setTheme(R.style.style_1);
    // ...
}

then in your Activity's xml you can set your own custom toolbar:

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

where @layout/my_toolbar may look like this:

<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="@dimen/toolbar_height"
    app:contentInsetStart="0dp"
    app:layout_collapseParallaxMultiplier="1.0">

    <!-- insert your views here -->
</android.support.v7.widget.Toolbar>
like image 44
Marko Pacak Avatar answered Sep 23 '22 18:09

Marko Pacak