- How set a title for Activity with ToolBar?
- Searching of the problem
- Possible solutions
- How to Create Custom AppBar/ActionBar/ToolBar in Android Studio | Java
- Gradle dependency
- Set the styles
- Design our AppBar/ActionBar/ToolBar
- Android set title toolbar
- Define Design Support Library:
- Elements of Toolbar In Android:
- Basic Toolbar XML code:
- Setting Toolbar as An ActionBar:
- Attributes of Toolbar In Android:
- Important Methods of Toolbar:
- Toolbar Example 1 In Android Studio:
- Toolbar Example 2 In Android Studio:
How set a title for Activity with ToolBar?
Sep 15, 2019 · 3 min read
In last version of Android appeared the component ToolBar instead of old ActionBar, that may be adds as bar to activity. I do not want to describe its capabilities, sure most developers use it with AppBarLayout and CollapsingToolbarLayout. In this short article i want to talk about not so clear way of adding title to ToolBar, which will be the title of the screen
Firstly let’s create small application “ ToolBarTest” with ToolBar, apply theme “ AppTheme” which has “ Theme.AppCompat.Light.NoActionBar” as parent, because we have ToolBar
Next let’s create the layout with ToolBar inside CoordinatorLayout:
And set it for MainActivity screen:
T h e method Activity::setSupportActionBar plug in our component T oolBar to screen instead of usual ActionBar and set its name “ MainActivity”. (Worth noting that the component is obtained from layout using kotlinx.android.synthetic). Now let’s launch the app:
It’s strange, but setting of title via method ToolBar::setTitle does not work, and we see name of App “ ToolBarTest” which was used in Manifest, instead of the name of the Activity as title. Ok, let’s see what is not correct.
Searching of the problem
Let’s look at the order of method invokings in onCreate():
- First we invoke the method setSupportActionBar to use the ToolBar as the ActionBar of the screen. There is the class ToolbarActionBaris created under hood, which is ActionBar for our Activity:
Let’s notice second parameter, which return the value of the title from Activity from the private field mTitle. At the moment, the value of this field is already initialized by the application name — “ToolBarTest” . This initialization was get from the private method performLaunchActivity of class ActivityThread, in the method loadLabel :
This method return value from attribute “label” of the AndroidManifest, that is name of the our application. So, in the first step the name of the screen is the name of the app = “ToolBarTest ”
2. Next step in MainActivity is the setting of the title “MainActivity” to Toolbar. I was sure it right solving, and the screen has correct name now. But no…
3. In according to a lifecycle of an activity the method Activity::onPostCreate() is followed after method Activity::onStart(). This method calls the onTitleChanged() method in the Activity superclass::
This method changes the ToolBar name to the value from Activity::getTitle(), which as we noted early, returns the value from AndroidManifest. So, at the end of the Activity initialization, we see the title of the screen again as name of app = “ToolBarTest”.
Possible solutions
Let’s find ways to solve this problem
- Set label for MainActivity in AndroidManifest.xml. As we saw before, the Activity use title for screen which was set in Manifest, so we may override this attribute for our Activity to the necessary value:
So you shouldn’t set title in MainActivity. The disadvantage of this way is you can’t change it on runtime.
- Set title for Activity programmatically:
On this way you may change title dynamically, for example from intent.
- Get ActionBar from Activity, and set title for it:
- Set title for ToolBar in overrided method onPostCreate. As we notice before, the title is changed on method onPostCreate in superclass. So we can revert title in overridden method
Thanks for reading this article! I hope it will be useful for you. Write about your suggestions or my mistakes about this issue.
Источник
How to Create Custom AppBar/ActionBar/ToolBar in Android Studio | Java
Today in this tutorial, we’re going to see how to create a custom AppBar/ActionBar/ToolBar in android.
I also already created a tutorial on this topic, but here I will demonstrate it with different examples.
Gradle dependency
for circle image view:
Set the styles
now go to your styles.xml file and set the theme to NoAction Bar so that we can create our own toolbar.
Design our AppBar/ActionBar/ToolBar
now go to your main XML file and create a ToolBar and set the width and height, make sure min-height should be actionBar size.
this will give you just a toolbar without any field. Look at the below image.
for the logo, we’re using a circular image view. Already add the dependency. Put it inside the toolbar and do the adjustment.
here we set our title to null, so in the main java file assign our toolbar and do the below change
the result will be:
we can also set the logo by using app:logo=”your logo here” in your toolbar
the result will be:
for the menu, create a menu android resource folder and create a menu resource file ( /res/menu/custom_menu.xml)
import that menu in our main java file by using these code of lines
for adding a title simply add a textView inside the toolbar.
Источник
Android set title toolbar
In Android Toolbar is similar to an ActionBar(now called as App Bars). Toolbar is a Viewgroup that can be placed at anywhere in the Layout. We can easily replace an ActionBar with Toolbar.
Toolbar was introduced in Material Design in API level 21 (Android 5.0 i.e Lollipop). Material Design brings lot of new features in Android that changed a lot the visual design patterns regarding the designing of modern Android applications.
An Action bar is traditionally a part of an Activity opaque window decor controlled by the framework but a Toolbar may be placed at any level of nesting within a view hierarchy. Toolbar provides more feature than ActionBar. A Toolbar may contain a combination of elements from start to end.
Important Note: Toolbar’s are more flexible than ActionBar. We can easily modify its color, size and position. We can also add labels, logos, navigation icons and other views in it. In Material Design Android has updated the AppCompat support libraries so that we can use Toolbar’s in our devices running API Level 7 and up. In AppCompat, Toolbar is implemented in the android.support.v7.widget.Toolbar class.
Table Of Contents
Define Design Support Library:
To use Toolbar you need to add design support library in build.gradle file.
Gradle Scripts > build.gradle (Module:App) -> inside dependencies
Elements of Toolbar In Android:
In Android Toolbar has more features than ActionBar and we can easily replace a ActionBar with Toolbar. In Toolbar from start to end it may contain a combination of Elements. Below we describe each and every element of Toolbar.
Navigation Button: It may be a Navigation menu toggle, up arrow, close, done, collapse or any other glyph of the app’s choosing.
- Brand Logo Image: It may extend to the height of the toolbar and can be arbitrarily wide.
- Title and SubTitle: A title should be a signpost for the current position of Toolbar’s navigation hierarchy and the content contained there. Subtitle represents any extended information about the current content. If an app uses a logo then it should strongly consider omitting a title and subtitle.
- One or More Custom Views: An Application may add arbitrary child views to the Toolbar. If child view’s Toolbar.LayoutParams indicates CENTER_HORIZONTAL Gravity then view will attempt to center within the available space remaining in the Toolbar after all other element’s have been measured.
- Action Menu: The menu of Actions will pin to the end of the Toolbar offering a few important, typical or frequent actions along with an optional overflow menu for additional actions. Action buttons are aligned vertically within the Toolbar’s minimum height if we set.
Basic Toolbar XML code:
Setting Toolbar as An ActionBar:
We can easily replace ActionBar with Toolbar by using setSupportActionBar() method. Here is the code of replacing ActionBar with Toolbar.
Attributes of Toolbar In Android:
Now let’s we discuss some common attributes of a Toolbar that helps us to configure it in our layout (xml).
- id: This attribute is used to set the id for the Toolbar. Id is used to uniquely identify a Toolbar.
Below we set the id of the Toolbar.
logo: This attribute is used to set as the drawable logo that appears at the starting side of the Toolbar means just after the navigation button. We can also do this programmatically by using setLogo() method.Below we set the logo for the Toolbar.
logoDescription: This attribute is used to set the content description string to describe the appearance of the associated logo image. We can also do this programmatically by using setLogoDescription() method.
Below we set the description for the displayed logo.
navigationIcon: This attribute is used to set the Icon drawable for the navigation button that located at the start of the toolbar. We can also do this programmatically by using setNavigationIcon() method.Below we set the icon for the navigation button.
navigationContentDescription: This attribute is used to set the text for the description of navigation button. We can also do this programmatically by using setNavigationContentDescription() method.
Below we set the content description for the displayed icon of navigation button.
title: This attribute is used to set title for the Toolbar. We can also do this programmatically by using setTitle() method.
Below we set the title for the Toolbar.
titleTextColor: This attribute is used to set the color for the title text. We can also do this programmatically by using setTitleTextColor() method.Below we set the red color for the dispalyed title text.
subtitle: This attribute is used to set the sub title for the Toolbar. We can also do this programmatically by using setSubtitle() method.
Below we set the sub title for the Toolbar.
Important Methods of Toolbar:
Let’s we discuss some important methods of Toolbar that may be called in order to add Action icons manage the Toolbar.
Important Note: To understand below functions of Toolbar properly please do read 2nd example in this article.
- setLogo(int resId): This method is used to add a logo drawable from a resource id. Below we set a logo drawable in our Toolbar.
- setLogo(Drawable drawable): This method is also used to add a logo drawable in our Toolbar. In this method we set a drawable object. Below we set a drawable logo in our Toolbar.
- getLogo(): This method is used to get the logo icon from Toolbar. This method returns the Drawable logo which we set using setLogo() method.
Below we firstly set the drawable logo and then get the same from Toolbar.
setLogoDescription(CharSequence description): This method is used to set a description for the drawable logo of Toolbar.
Below we set the description for Toolbar’s logo.
setLogoDescription(int resId): This method is also used to set the description for drawable logo of the Toolbar. This method set the description from string file.
Below we set the description for Toolbar’s logo.
Below we set a navigation icon in the Toolbar from resource id.
Below we set the title for the Toolbar.
setTitle(CharSequence title): This method is also used to set the title for the Toolbar.
Below we set the title for the Toolbar.
setSubtitle(int resId): This method is used to set the sub Title for the Toolbar. In this method we set the sub title form string file.
Below we set the sub title for the Toolbar.
setSubtitle(CharSequence subtitle): This method is also used to set the sub title for the Toolbar.
Below we set the sub title for the Toolbar.
Below we set the description for Navigation Button.
Below we set the red color for the displayed text of title.
setSubtitleTextColor(int color): This method is used to set the text color for the displayed sub title.
Below we set the red color for the displayed sub text of title.
Toolbar Example 1 In Android Studio:
Below is the first example of Toolbar in which we create a Toolbar and replace it with ActionBar. In this examle we add action icons in Toobar and on click of navigation Button of Toolbar we open a Navigation Drawer. In our main layout we use Drawer Layout and Navigation View. Drawer Layout is the root layout in which we include a Toolbar and also define a FrameLayout and a Navigation View. In Navigation View we set the items from menu file and FrameLayout is used to replace the Fragments on the click of menu items. Whenever a user click on menu item, Fragment is replaced according to menu item’s id and a toast message with menu item title is displayed on the screen. We also create three Fragments that should be replaced on click of menu items
Step 1: Create a new project and name it ToolbarExample.
Step 2: Open Gradle Scripts > build.gradle and add Design support library dependency. If you are on the latest version of Android Studio you don’t need to add a compiled dependency for Appcombat v7 21 library if not then please make sure you add the line below in your gradel build dependencies.
apply plugin: ‘com.android.application’
Step 3: ActionBars are replaced with Toolbar so we can still use ActionBar but in our example we are going to make a Toolbar so go to your style.xml file and change the theme to “Theme.AppCompat.Light.NoActionBar” This theme helps us get rid of the ActionBar so we can make a Toolbar.
Step 4: Now let’s talk about the color scheme for our application. Open your color.xml file from values folder. In this XML file colorPrimary is the primary color for the app and colorPrimaryDark color is the color of the status bar. We can easily change our colors from this file.
Step 5: Open res -> layout -> activity_main.xml (or) main.xml and add following code:
In this step we define a DrawerLayout that is the parent layout in which we include a toolbar.xml file and also define a FrameLayout and a Navigation View. In Navigation View we set the items from menu file named “nav_items” and FrameLayout is used to replace the Fragments on the click of menu items.
Step 6: Open res -> menu -> nav_items.xml and add following code. Here we define the menu items.
Step 7: Now create a xml layouts by right clicking on res/layout -> New -> Layout Resource File and name it toolbar.xml
In this file we set the background color, navigation button and tilte for the Toolbar.
Step 8: Open src -> package -> MainActivity.java
In this step we open the MainActivity and add the code for initiates the views(DrawerLayout, Toolbar,NavigationView and other views). In this we replace the ActionBar with our ToolBar by using setSupportActionBar() method and then implment setNavigationOnClickListener event and add the code to open the drawer on click of navigation button. After that we implement setNavigationItemSelectedListener event on NavigationView so that we can replace the Fragments according to menu item’s id and a toast message with menu item’s title is displayed on the screen. In this I have added comments in code to help you to understand the code easily so make you read the comments.
Step 9: Now we need 3 fragments and 3 xml layouts. So create three fragments by right click on your package folder and create classes and name them as FirstFragment, SecondFragment and ThirdFragment and add the following code respectively.
Step 10: Now create 3 xml layouts by right clicking on res/layout -> New -> Layout Resource File and name them as fragment_first, fragment_ second and fragment_third and add the following code in respective files.
Here we will design the basic simple UI by using TextView in all xml’s.
Output:
Now run the App and you will see Menu icon and “AbhiAndroid” text written at the top which is created using Toolbar. Now click on menu and three items inside it will be shown. Now click on any of them and corresponding to it layout will open up.
Toolbar Example 2 In Android Studio:
Below is the simple example of Toolbar in which we create a Toolbar and replace it with our ActionBar. In this example we set Logo, title and menu for Toolbar. In our main layout we create a Toolbar and in Activity get the reference of Toolbar and set the title and logo on it. In this we use Activity’s overrided method onCreateOptionsMenu to set the menu items from menu file and onOptionsItemSelected to set click listeners on menu item’s. On click of menu item the title of menu is displayed on the screen with the help of Toast.
Step 1: Create a new project and name it SimpleToolbarExample.
Step 2: Open Gradle Scripts > If you are on the latest version of Android Studio you don’t need to add a compiled dependency for Appcombat v7 21 library if not then please make sure you add the line below in your gradel build dependencies.
Step 3: ActionBars are replaced with Toolbar so we can still use ActionBar but in our example we are going to make a Toolbar so go to your style.xml file and change the theme to “Theme.AppCompat.Light.NoActionBar” This theme helps us get rid of the ActionBar so we can make a Toolbar.
Step 4: Now let’s talk about the color scheme for our application. Open your color.xml file from values folder. In this XML file colorPrimary is the primary color for the app and colorPrimaryDark color is the color of the status bar. We can easily change our colors from this file.
Step 5: Open res -> layout -> activity_main.xml (or) main.xml and add following code:
In this step we define a Toolbar in our main XML file and set it’s background color and theme.
Step 6: Open res -> menu -> menu_main.xml and add following code:
In this step we create 3 menu items that will be displayed on Toolbar.
Step 7: Open src -> package -> MainActivity.java
In this step we open the MainActivity and add the code for initiates the Toolbar.. In this we replace the ActionBar with our ToolBar by using setSupportActionBar() method and also set the tiltle and logo for the Toolbar. After that we implement Activity’s overrided method onCreateOptionsMenu to set the menu items from menu file and onOptionsItemSelected to set click listeners on menu item’s. On click of menu item the tiltle of menu is displayed on the screen with the help of Toast. In this I have added comments in code to help you to understand the code easily so make sure you read the comments.
Output:
Now run the App and you will see Toolbar at the top. Click on it and message created using Toast will be displayed.
Источник