Where can I define XML for a toolbar widget in Android 5.0?

Ok, now I am looking through a few StackOverflow posts, but I'm still confused about where this xml is for my toolbar.

<android.support.v7.widget.Toolbar android:id="@+id/my_awesome_toolbar" android:layout_height="wrap_content" android:layout_width="match_parent" android:background="@styles/colorPrimary" /> 

/layout/activity_main.xml this happen in my /layout/activity_main.xml ?

+7
android android-layout android-5.0-lollipop android-toolbar
source share
2 answers

The toolbar is a generalization of action bars for use in application layouts, now there are two practices for answering your question:

Bad practice:

Bad practice is to define a toolbar in each layout.

Standard practice:

Standard practice is to identify the layout and indicate it in the base activity. You just need to include this toolbar layout in any layout you want (using <include> ), and extend a certain base activity depending on your activity.

This standard practice will help you maintain a single code base for the toolbar and save time from defining the toolbar every time.

Example: Google I / O 2014 Android App

toolbar_actionbar_with_headerbar.xml

 <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:iosched="http://schemas.android.com/apk/res-auto" style="@style/HeaderBar" iosched:theme="@style/ActionBarThemeOverlay" iosched:popupTheme="@style/ActionBarPopupThemeOverlay" android:id="@+id/toolbar_actionbar" iosched:titleTextAppearance="@style/ActionBar.TitleText" iosched:contentInsetStart="?actionBarInsetStart" android:layout_width="match_parent" android:layout_height="?actionBarSize" /> 

This toolbar layout is referenced in the options action, as shown below:

activity_settings.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".ui.SettingsActivity"> <include layout="@layout/toolbar_actionbar_with_headerbar" /> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout> 
+21
source share

For me, I usually do ToolbarActivity. Then, if you want your activity to have a toolbar, you just need YourActivity extends ToolbarActivity .

 public class ToolbarActivity extends AppCompatActivity { @Override public void setContentView(int layoutResID) { super.setContentView(R.layout.activity_toolbar); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); LayoutInflater inflater = LayoutInflater.from(this); View contentView = inflater.inflate(layoutResID, null); LinearLayout layout = (LinearLayout) findViewById(R.id.layout); layout.addView(contentView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); } } 

XML:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/layout" tools:context=".ToolbarActivity" > <android.support.v7.widget.Toolbar android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" android:layout_height="wrap_content" android:layout_width="match_parent" android:minHeight="?attr/actionBarSize" android:background="?attr/colorPrimary" android:id="@+id/toolbar" /> </LinearLayout> 
+1
source share

All Articles