Set toolbar for all Activities

10,550

Solution 1

You could create a base activity that runs the common code and have all other activities inherit from it:

// the base class
public abstract class BaseActivity extends AppCompatActivity
{
    protected final void onCreate(Bundle savedInstanceState, int layoutId)
    {
        super.onCreate(savedInstanceState);
        setContentView(layoutId);

        Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(myToolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        assert myToolbar != null;
        myToolbar.setLogo(R.mipmap.logo_big);

        TextView usernameField = (TextView) findViewById(R.id.username);
        try {
            usernameField.setText(User.getInstance().getUsername());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

// inheriting activity
public class SomeActivity extends BaseActivity
{
    protected final void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState, R.layout.some_layout);
    }
}

Solution 2

keep your code in one CustomActivity.

Where every required in your application extends CustomActivity .

Example:

CustomActivity extends Activity{
// your toolbar code
}

In your all activities extends CustomActivity.

Solution 3

Create a new activity called CustomActivity and a corresponding layout called activity_custom.xml :

<android.support.v4.widget.DrawerLayout
android:id="@+id/activity_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="@color/background_material_dark"
        />
    <FrameLayout
        android:id="@+id/activity_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
<android.support.design.widget.NavigationView
    android:id="@+id/navigationView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:menu="@menu/menu_base"/>

Open BaseActivity.java and make sure to remove setContentView(R.layout.activity_base) from the onCreate() method. and override the setContentView() method with your own implementation:

@Override
public void setContentView(int layoutResID) {
    DrawerLayout fullView = (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_base, null);
    FrameLayout activityContainer = (FrameLayout) fullView.findViewById(R.id.activity_content);
    getLayoutInflater().inflate(layoutResID, activityContainer, true);
    super.setContentView(fullView);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    setTitle("Activity Title");
}

In your all activities extends CustomActivity.

Share:
10,550

Related videos on Youtube

LorenzoBerti
Author by

LorenzoBerti

Updated on June 04, 2022

Comments

  • LorenzoBerti
    LorenzoBerti almost 2 years

    In my app I would like to make a unique toolbar for all activities except for mainActivity. I have written this code for set Title and logo, but in toolbar I also have username logged. So I have written in my dashboard activity this code:

    Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(myToolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        assert myToolbar != null;
        myToolbar.setLogo(R.mipmap.logo_big);
    
    TextView usernameField = (TextView) findViewById(R.id.username);
        try {
            usernameField.setText(User.getInstance().getUsername());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    

    And I made a layout that can be included in all xml files. But How can I reuse this code in all my activities without copy and paste?

    Is it wrong to make a singleton? or a utility class?

    Thanks

  • LorenzoBerti
    LorenzoBerti almost 8 years
    Thank you!! fantastic! it Work!
  • Joe Okatch
    Joe Okatch over 5 years
    could you paste and example child activity especially the onCreate()
  • Andrea_86
    Andrea_86 about 5 years
    I'll tried this, but the super.onCreate() in the SomeActivity referer directly to te onCreate of AppCompatActivity and not of one n my BaseActivity, so the call to method with 2 parameter isn't recognized to a super call to onCreate and give me error
  • anshulkatta
    anshulkatta over 2 years
    this is really a bad design from OOP point of view