3

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

Salek
  • 449
  • 1
  • 10
  • 19
LorenzoBerti
  • 6,704
  • 8
  • 47
  • 89
  • 3
    Does this answer your question? [Use Toolbar across all activities (Android)](https://stackoverflow.com/questions/28661981/use-toolbar-across-all-activities-android) – Bouh Sep 08 '20 at 00:00

4 Answers4

14

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);
    }
}
Richard
  • 560
  • 1
  • 8
  • 17
  • Thank you!! fantastic! it Work! – LorenzoBerti Jul 18 '16 at 12:38
  • 1
    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 – Andrea_86 Apr 05 '19 at 10:44
  • this is really a bad design from OOP point of view – anshulkatta Sep 21 '21 at 15:01
1

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.

Android Surya
  • 544
  • 3
  • 17
1

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.

Mr Schak
  • 21
  • 4
0

You can do like this:

class BaseActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);          
    }

    public void initToolbar(int toolbarId)
    {
        Toolbar myToolbar = (Toolbar) findViewById(toolbarId);
        setSupportActionBar(myToolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        assert myToolbar != null;
        myToolbar.setLogo(R.mipmap.logo_big);
    }
}

class YourActivity extends BaseActivity
{
     @Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(your activity layout);

          initToolbar(R.id.yourToolbarId);
     }
}
FarshidABZ
  • 3,860
  • 4
  • 32
  • 63