Do I need to create one toolbar for each action in an Android app?

I have this doubt regarding the new toolbar in Android.

Do I need to create one toolbar for each action in my application or is there a best practice for creating one toolbar for all actions?

I am trying to create a Singleton by inflating a layout and looking for a view identifier to create a toolbar and return the same instance for all actions, but this does not work.

Can someone help me ?: S

+7
android toolbar
source share
3 answers

Toolbar is just a view, and you have to add it to each Activity in which you want to show it.

One way is to simply put it in a separate layout file and include it in the Activity layout.

toolbar.xml

 <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" > </android.support.v7.widget.Toolbar> 

Now in your activity layout where you want to add it, just include the following:

 <include android:id="@+id/toolbar" layout="@layout/toolbar"/> 
+24
source share

You can also expand your AppCompatActivity activity class instead of Activity

+1
source share
 <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" > </android.support.v7.widget.Toolbar> 
+1
source share

All Articles