Set toolbar for all Activities

android android-toolbar

7437 просмотра

4 ответа

2001 Репутация автора

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 Источник Размещён: 18.07.2016 11:07

Ответы (4)


1 плюс

480 Репутация автора

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 Размещён: 18.07.2016 11:11

12 плюса

520 Репутация автора

Решение

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 Размещён: 18.07.2016 11:13

0 плюса

1611 Репутация автора

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 Размещён: 18.07.2016 11:15

0 плюса

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 Размещён: 27.03.2018 02:29
Вопросы из категории :
32x32