5
I’ve seen the own documentation of android I’ve done all the steps of other forums and still could not use Toolbar in my screen layout.
How I should proceed step by step regarding the implementation and use in my toobar?
5
I’ve seen the own documentation of android I’ve done all the steps of other forums and still could not use Toolbar in my screen layout.
How I should proceed step by step regarding the implementation and use in my toobar?
16
To use the Toolbar
, you have two options:
android.support.v7.widget.Toolbar
in their layouts;android.widget.Toolbar
.To begin, unfortunately, the Toolbar
need to be in all layouts. For theme attribute settings and positioning. Instantiate Toolbar
and adding it manually to the layout can be tricky to have to configure its position and add all attributes to style it.
Remembering that in this answer, it is necessary to import the library of appcompat v7 21
, or adding the jar if it’s in Eclipse, or importing the dependency if it’s in Android Studio. And like appcompat
depends on the support library v4
, she needs to be added too.
As I am using Android Studio, I use these two dependencies:
compile 'com.android.support:appcompat-v7:21.+'
compile 'com.android.support:support-v4:21.+'
Heed, if you’re using the Toolbar as a substitute for Action Bar, you need to use a theme without the ActionBar
. For this use the themes with ending NoActionBar
, so much of appcompat
as in version 21. One of the themes I use appcompat
for that purpose is the Theme.AppCompat.Light.NoActionBar
.
An example of layout using Toolbar
is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include android:id="@+id/toolbar" layout="@layout/toolbar_support" />
<FrameLayout
android:id="@+id/am_frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
In that case, I used the include
for reasons of reuse (DRY), assuming that the Toolbar
will always have the same attributes in all layouts it makes no sense to repeat everywhere.
Its position can vary the will in the layout, and this is one of the advantages of using the Toolbar
.
Definition of Toolbar
in the include
:
<?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_height="@dimen/ab_height"
android:layout_width="match_parent"
android:minHeight="@dimen/ab_height"
android:background="?attr/colorPrimary"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
If you’re using android.widget.Toolbar
, the definition would be:
<android.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_height="@dimen/ab_height"
android:layout_width="match_parent"
android:minHeight="@dimen/ab_height"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.Material.ActionBar"
android:popupTheme="@style/ThemeOverlay.Material.Light" />
In the Activity
, some things need to be done.
If you want to use the Toolbar
as a substitute for the ActionBar
, just use the method setSupportActionBar
(appcompat) or setActionBar
(Android 21) after the setContentView
. If you’re using the appcompat
, it is paramount that your Activity
be a subclass of ActionBarActivity
.
Example:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Dando a responsabilidade de Action Bar para o Toolbar
Toolbar tlb = findViewById(R.id.toolbar);
setSupportActionBar(tlb);
// ou
//setActionBar(tlb);
// Apos isso, podera configurar sua Action Bar normalmente
// Por exemplo:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
To use setSupportActionBar
or setActionBar
, it is necessary to use a theme that does not have Action Bar
, or you will have problems. Because you are adding a Action Bar
to a Window
that already has one. For this use one of the themes with suffix *.NoActionBar
(e. g: Theme.AppCompat.NoActionBar
, Theme.Material.NoActionBar
and etc...)
Regarding the construction of menus, using setSupportActionBar
or setActionBar
, the others callbacks (onCreateOptionsMenu
, onOptionsItemSelected
and etc...) continue to be called as well. So no changes need to be made to the code.
If you’re not setting the Toolbar
as ActionBar
, it is necessary to use the method Toolbar.inflateMenu
to add menu items to Toolbar
.
If you use one of the themes without Action Bar (e. g: Theme.AppCompat.Light.NoActionBar
), and not set the Toolbar
using the setSupportActionBar
or setActionBar
, will have problems with NullPointerException
in the calls of getSupportActionBar
or getActionBar
.
If you’re using Action Mode with the Toolbar
at the top of the screen, Toolbar
is not superimposed by the bar of the Action Mode , as in the Action Bar
standard. The bar of Action Mode is above the Toolbar
. To avoid this problem I recommend that you hide the Toolbar
during the Action Mode.
An example would be:
@Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
MenuInflater inflater = actionMode.getMenuInflater();
inflater.inflate(R.menu.custom_deck_cab, menu);
Toolbar tlb = findViewById(R.id.toolbar);
tlb.setVisibility(View.GONE);
return true;
}
@Override
public void onDestroyActionMode(ActionMode actionMode) {
Toolbar tlb = findViewById(R.id.toolbar);
tlb.setVisibility(View.VISIBLE);
}
In this new version, there was a depreciation of the methods related to the navigation modes of the Action Bar
(e. g: setNavigationMode
, setSelectedNavigationItem
and setListNavigationCallbacks
), logo if you use tabbed or list navigation (NAVIGATION_MODE_LIST
or NAVIGATION_MODE_TABS
), should migrate to the Toolbar
.
This migration can be done by adding View's
at the Toolbar
, since he is a ViewGroup
. For tabbed navigation, you can add a PagerTabStrip
, or similar, in the Toolbar
. If you are using list browsing, you can add a Spinner
at the Toolbar
, and achieve the same effect.
It’s almost working out anyway.. only an error in Activity saying that you need the following code: requestWindowFeature(Window.FEATURE_ACTION_BAR); and I added it before setContentView(R.layout.activity_main); after the following error: requestFeature() must be called before Adding content, I searched a lot in the foruns and did not find solution.
Hmm, strange this mistake, these were all the steps I took to migrate from Action Bar
to the Toolbar
and did not give any error. Where this error occurs? could put the whole message and the location? I do not recommend calling the requestWindowFeature(Window.FEATURE_ACTION_BAR)
because it will add the ActionBar
and is not necessary.
ok I just called the requestWindowFeature(Window.FEATURE_ACTION_BAR) had given a previous error, I had q add... but I will take and show the error in my post.
About using Action Mode, Toolbar can also be overwritten using <item name="windowActionModeOverlay">true</item>
in the app theme instead of being done programmatically. Link: http://stackoverflow.com/questions/26443403/toolbar-and-contextual-actionbar-with-appcompat-v7
@Luídne, Hmmm, I didn’t know that. Very nice to know this attribute.
They complicate things sometimes if before it was superimposed because now it is not. Alas that anger I spent hours thinking I was doing something wrong because at https://developer.android.com/ does not inform this.
Browser other questions tagged android android-layout
You are not signed in. Login or sign up in order to post.
Unfortunately I cannot say exactly the error, because they have not yet released the source code of the latest version of
appcompat
. Kick that is the theme or is usingsetSupportActionBar
before thesetContentView
. Could you check these points?– Wakim
in the theme issue I had to add in the manifest.xml the android line:Theme="@style/Theme.Appcompat" because before it was only android:Theme="@style/Theme"
– Pedro Rangel
and I am following the scratch of your code, so setSupportActionBar is after setContentView.
– Pedro Rangel
Okay, change the subject to
@style/Theme.AppCompat.NoActionBar
. With this theme, your app will not haveAction Bar
, and theToolbar
will play the role ofAction Bar
in place.– Wakim
it worked thanks even it was only that then. You know very well the android hope one day to reach the same understanding.
– Pedro Rangel
I don’t think I made that very clear. I love the Android platform, I’m always trying to keep up with new technologies. A tip I give is to try to understand what is behind, seeing the source code whenever you have a strange error or to understand how it works.
– Wakim
@Wakim Thank you so much for your tips.
– Pedro Rangel