- How to Create Custom AppBar/ActionBar/ToolBar in Android Studio | Java
- Gradle dependency
- Set the styles
- Design our AppBar/ActionBar/ToolBar
- Interfaces
- Annotation Types
- Gradle dependencies
- Androidx artifact mapping:
- Androidx class mapping:
- Overview
- Summary
- Constructors
- Methods
- Parameters:
- Returns:
- Parameters:
- Returns:
- Parameters:
- Returns:
- Parameters:
- Returns:
- Parameters:
- Returns:
- Parameters:
- Parameters:
- Returns:
- Returns:
- Returns:
- Parameters:
- Returns:
- Parameters:
- Parameters:
- Returns:
- Returns:
- Returns:
- Parameters:
- Parameters:
- Returns:
- Parameters:
- Parameters:
- Parameters:
- Parameters:
- Parameters:
- Parameters:
- Returns:
- Parameters:
- Parameters:
- Parameters:
- Parameters:
- Returns:
- Parameters:
- Returns:
- Parameters:
- Parameters:
- Returns:
- Parameters:
- Parameters:
- Returns:
- Parameters:
- Returns:
- Parameters:
- Parameters:
- Parameters:
- Returns:
- Returns:
- Parameters:
- Returns:
- Returns:
- Returns:
- Parameters:
- Returns:
- Parameters:
- Returns:
- Returns:
- Returns:
- Returns:
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.
Источник
Interfaces
Annotation Types
Gradle dependencies
compile group: ‘androidx.appcompat’, name: ‘appcompat’, version: ‘1.3.0-alpha02’
- groupId: androidx.appcompat
- artifactId: appcompat
- version: 1.3.0-alpha02
Artifact androidx.appcompat:appcompat:1.3.0-alpha02 it located at Google repository (https://maven.google.com/)
Androidx artifact mapping:
Androidx class mapping:
Overview
A standard toolbar for use within application content.
A Toolbar is a generalization of action bars for use within application layouts. While an action bar is traditionally part of an opaque window decor controlled by the framework, a Toolbar may be placed at any arbitrary level of nesting within a view hierarchy. An application may choose to designate a Toolbar as the action bar for an Activity using the setSupportActionBar() method.
Toolbar supports a more focused feature set than ActionBar. From start to end, a toolbar may contain a combination of the following optional elements:
- A navigation button. This may be an Up arrow, navigation menu toggle, close, collapse, done or another glyph of the app’s choosing. This button should always be used to access other navigational destinations within the container of the Toolbar and its signified content or otherwise leave the current context signified by the Toolbar. The navigation button is vertically aligned within the Toolbar’s minimum height, if set.
- A branded logo image. This may extend to the height of the bar and can be arbitrarily wide.
- A title and subtitle. The title should be a signpost for the Toolbar’s current position in the navigation hierarchy and the content contained there. The subtitle, if present should indicate any extended information about the current content. If an app uses a logo image it should strongly consider omitting a title and subtitle.
- One or more custom views. The application may add arbitrary child views to the Toolbar. They will appear at this position within the layout. If a child view’s Toolbar.LayoutParams indicates a value of the view will attempt to center within the available space remaining in the Toolbar after all other elements have been measured.
- An action menu. The menu of actions will pin to the end of the Toolbar offering a few frequent, important or typical actions along with an optional overflow menu for additional actions. Action buttons are vertically aligned within the Toolbar’s minimum height, if set.
In modern Android UIs developers should lean more on a visually distinct color scheme for toolbars than on their application icon. The use of application icon plus title as a standard layout is discouraged on API 21 devices and newer.
Summary
Constructors | |
---|---|
public | Toolbar(Context context) |
Methods | |
---|---|
public boolean | canShowOverflowMenu() |
protected boolean | checkLayoutParams(ViewGroup.LayoutParams p) |
public void | collapseActionView() Collapse a currently expanded action view. |
public void | dismissPopupMenus() Dismiss all currently showing popup menus, including overflow or submenus. |
protected Toolbar.LayoutParams | generateDefaultLayoutParams() |
public Toolbar.LayoutParams | generateLayoutParams(AttributeSet attrs) |
protected Toolbar.LayoutParams | generateLayoutParams(ViewGroup.LayoutParams p) |
public java.lang.CharSequence | getCollapseContentDescription() Retrieve the currently configured content description for the collapse button view. |
public Drawable | getCollapseIcon() Return the current drawable used as the collapse icon. |
public int | getContentInsetEnd() Gets the ending content inset for this toolbar. |
public int | getContentInsetEndWithActions() Gets the end content inset to use when action buttons are present. |
public int | getContentInsetLeft() Gets the left content inset for this toolbar. |
public int | getContentInsetRight() Gets the right content inset for this toolbar. |
public int | getContentInsetStart() Gets the starting content inset for this toolbar. |
public int | getContentInsetStartWithNavigation() Gets the start content inset to use when a navigation button is present. |
public int | getCurrentContentInsetEnd() Gets the content inset that will be used on the ending side of the bar in the current toolbar configuration. |
public int | getCurrentContentInsetLeft() Gets the content inset that will be used on the left side of the bar in the current toolbar configuration. |
public int | getCurrentContentInsetRight() Gets the content inset that will be used on the right side of the bar in the current toolbar configuration. |
public int | getCurrentContentInsetStart() Gets the content inset that will be used on the starting side of the bar in the current toolbar configuration. |
public Drawable | getLogo() Return the current logo drawable. |
public java.lang.CharSequence | getLogoDescription() Return the description of the toolbar’s logo. |
public Menu | getMenu() Return the Menu shown in the toolbar. |
public java.lang.CharSequence | getNavigationContentDescription() Retrieve the currently configured content description for the navigation button view. |
public Drawable | getNavigationIcon() Return the current drawable used as the navigation icon. |
public Drawable | getOverflowIcon() Return the current drawable used as the overflow icon. |
public int | getPopupTheme() |
public java.lang.CharSequence | getSubtitle() Return the subtitle of this toolbar. |
public java.lang.CharSequence | getTitle() Returns the title of this toolbar. |
public int | getTitleMarginBottom() |
public int | getTitleMarginEnd() |
public int | getTitleMarginStart() |
public int | getTitleMarginTop() |
public boolean | hasExpandedActionView() Check whether this Toolbar is currently hosting an expanded action view. |
public boolean | hideOverflowMenu() Hide the overflow items from the associated menu. |
public void | inflateMenu(int resId) Inflate a menu resource into this toolbar. |
public boolean | isOverflowMenuShowing() Check whether the overflow menu is currently showing. |
public boolean | isOverflowMenuShowPending() |
public boolean | isTitleTruncated() |
protected void | onDetachedFromWindow() |
public boolean | onHoverEvent(MotionEvent ev) |
protected void | onLayout(boolean changed, int l, int t, int r, int b) |
protected void | onMeasure(int widthMeasureSpec, int heightMeasureSpec) |
protected void | onRestoreInstanceState(Parcelable state) |
public void | onRtlPropertiesChanged(int layoutDirection) |
protected Parcelable | onSaveInstanceState() |
public boolean | onTouchEvent(MotionEvent ev) |
public void | setCollapseContentDescription(java.lang.CharSequence description) Set a content description for the collapse button if one is present. |
public void | setCollapseContentDescription(int resId) Set a content description for the collapse button if one is present. |
public void | setCollapseIcon(Drawable icon) Set the icon to use for the toolbar’s collapse button. |
public void | setCollapseIcon(int resId) Set the icon to use for the toolbar’s collapse button. |
public void | setCollapsible(boolean collapsible) Force the toolbar to collapse to zero-height during measurement if it could be considered «empty» (no visible elements with nonzero measured size) |
public void | setContentInsetEndWithActions(int insetEndWithActions) Sets the start content inset to use when action buttons are present. |
public void | setContentInsetsAbsolute(int contentInsetLeft, int contentInsetRight) Sets the content insets for this toolbar. |
public void | setContentInsetsRelative(int contentInsetStart, int contentInsetEnd) Sets the content insets for this toolbar relative to layout direction. |
public void | setContentInsetStartWithNavigation(int insetStartWithNavigation) Sets the start content inset to use when a navigation button is present. |
public void | setLogo(Drawable drawable) Set a logo drawable. |
public void | setLogo(int resId) Set a logo drawable from a resource id. |
public void | setLogoDescription(java.lang.CharSequence description) Set a description of the toolbar’s logo. |
public void | setLogoDescription(int resId) Set a description of the toolbar’s logo. |
public void | setMenu(MenuBuilder menu, androidx.appcompat.widget.ActionMenuPresenter outerPresenter) |
public void | setMenuCallbacks(MenuPresenter.Callback pcb, MenuBuilder.Callback mcb) Must be called before the menu is accessed |
public void | setNavigationContentDescription(java.lang.CharSequence description) Set a content description for the navigation button if one is present. |
public void | setNavigationContentDescription(int resId) Set a content description for the navigation button if one is present. |
public void | setNavigationIcon(Drawable icon) Set the icon to use for the toolbar’s navigation button. |
public void | setNavigationIcon(int resId) Set the icon to use for the toolbar’s navigation button. |
public void | setNavigationOnClickListener(OnClickListener listener) Set a listener to respond to navigation events. |
public void | setOnMenuItemClickListener(Toolbar.OnMenuItemClickListener listener) Set a listener to respond to menu item click events. |
public void | setOverflowIcon(Drawable icon) Set the icon to use for the overflow button. |
public void | setPopupTheme(int resId) Specifies the theme to use when inflating popup menus. |
public void | setSubtitle(java.lang.CharSequence subtitle) Set the subtitle of this toolbar. |
public void | setSubtitle(int resId) Set the subtitle of this toolbar. |
public void | setSubtitleTextAppearance(Context context, int resId) Sets the text color, size, style, hint color, and highlight color from the specified TextAppearance resource. |
public void | setSubtitleTextColor(ColorStateList color) Sets the text color of the subtitle, if present. |
public void | setSubtitleTextColor(int color) Sets the text color of the subtitle, if present. |
public void | setTitle(java.lang.CharSequence title) Set the title of this toolbar. |
public void | setTitle(int resId) Set the title of this toolbar. |
public void | setTitleMargin(int start, int top, int end, int bottom) Sets the title margin. |
public void | setTitleMarginBottom(int margin) Sets the bottom title margin in pixels. |
public void | setTitleMarginEnd(int margin) Sets the ending title margin in pixels. |
public void | setTitleMarginStart(int margin) Sets the starting title margin in pixels. |
public void | setTitleMarginTop(int margin) Sets the top title margin in pixels. |
public void | setTitleTextAppearance(Context context, int resId) Sets the text color, size, style, hint color, and highlight color from the specified TextAppearance resource. |
public void | setTitleTextColor(ColorStateList color) Sets the text color of the title, if present. |
public void | setTitleTextColor(int color) Sets the text color of the title, if present. |
public boolean | showOverflowMenu() Show the overflow items from the associated menu. |
from java.lang.Object | clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructors
Methods
Specifies the theme to use when inflating popup menus. By default, uses the same theme as the toolbar itself.
Parameters:
resId: theme used to inflate popup menus
Returns:
resource identifier of the theme used to inflate popup menus, or 0 if menus are inflated against the toolbar theme
Sets the title margin.
Parameters:
start: the starting title margin in pixels
top: the top title margin in pixels
end: the ending title margin in pixels
bottom: the bottom title margin in pixels
Returns:
the starting title margin in pixels
See also:
Sets the starting title margin in pixels.
Parameters:
margin: the starting title margin in pixels
See also:
Returns:
the top title margin in pixels
See also:
Sets the top title margin in pixels.
Parameters:
margin: the top title margin in pixels
See also:
Returns:
the ending title margin in pixels
See also:
Sets the ending title margin in pixels.
Parameters:
margin: the ending title margin in pixels
See also:
Returns:
the bottom title margin in pixels
See also:
Sets the bottom title margin in pixels.
Parameters:
margin: the bottom title margin in pixels
See also:
Set a logo drawable from a resource id.
This drawable should generally take the place of title text. The logo cannot be clicked. Apps using a logo should also supply a description using Toolbar.setLogoDescription(int).
Parameters:
resId: ID of a drawable resource
Check whether the overflow menu is currently showing. This may not reflect a pending show operation in progress.
Returns:
true if the overflow menu is currently showing
Show the overflow items from the associated menu.
Returns:
true if the menu was able to be shown, false otherwise
Hide the overflow items from the associated menu.
Returns:
true if the menu was able to be hidden, false otherwise
Dismiss all currently showing popup menus, including overflow or submenus.
Set a logo drawable.
This drawable should generally take the place of title text. The logo cannot be clicked. Apps using a logo should also supply a description using Toolbar.setLogoDescription(int).
Parameters:
drawable: Drawable to use as a logo
Return the current logo drawable.
Returns:
The current logo drawable
Set a description of the toolbar’s logo.
This description will be used for accessibility or other similar descriptions of the UI.
Parameters:
resId: String resource id
Set a description of the toolbar’s logo.
This description will be used for accessibility or other similar descriptions of the UI.
Parameters:
description: Description to set
Return the description of the toolbar’s logo.
Returns:
A description of the logo
Check whether this Toolbar is currently hosting an expanded action view.
An action view may be expanded either directly from the MenuItem it belongs to or by user action. If the Toolbar has an expanded action view it can be collapsed using the Toolbar.collapseActionView() method.
Returns:
true if the Toolbar has an expanded action view
Collapse a currently expanded action view. If this Toolbar does not have an expanded action view this method has no effect.
An action view may be expanded either directly from the MenuItem it belongs to or by user action.
Returns the title of this toolbar.
Returns:
The current title.
Set the title of this toolbar.
A title should be used as the anchor for a section of content. It should describe or name the content being viewed.
Parameters:
resId: Resource ID of a string to set as the title
Set the title of this toolbar.
A title should be used as the anchor for a section of content. It should describe or name the content being viewed.
Parameters:
title: Title to set
Return the subtitle of this toolbar.
Returns:
The current subtitle
Set the subtitle of this toolbar.
Subtitles should express extended information about the current content.
Parameters:
resId: String resource ID
Set the subtitle of this toolbar.
Subtitles should express extended information about the current content.
Parameters:
subtitle: Subtitle to set
Sets the text color, size, style, hint color, and highlight color from the specified TextAppearance resource.
Sets the text color, size, style, hint color, and highlight color from the specified TextAppearance resource.
Sets the text color of the title, if present.
Parameters:
color: The new text color in 0xAARRGGBB format
Sets the text color of the title, if present.
Parameters:
color: The new text color
Sets the text color of the subtitle, if present.
Parameters:
color: The new text color in 0xAARRGGBB format
Sets the text color of the subtitle, if present.
Parameters:
color: The new text color
Retrieve the currently configured content description for the navigation button view. This will be used to describe the navigation action to users through mechanisms such as screen readers or tooltips.
Returns:
The navigation button’s content description
Set a content description for the navigation button if one is present. The content description will be read via screen readers or other accessibility systems to explain the action of the navigation button.
Parameters:
resId: Resource ID of a content description string to set, or 0 to clear the description
Set a content description for the navigation button if one is present. The content description will be read via screen readers or other accessibility systems to explain the action of the navigation button.
Parameters:
description: Content description to set, or null to clear the content description
Set the icon to use for the toolbar’s navigation button.
The navigation button appears at the start of the toolbar if present. Setting an icon will make the navigation button visible.
If you use a navigation icon you should also set a description for its action using Toolbar.setNavigationContentDescription(int). This is used for accessibility and tooltips.
Parameters:
resId: Resource ID of a drawable to set
Set the icon to use for the toolbar’s navigation button.
The navigation button appears at the start of the toolbar if present. Setting an icon will make the navigation button visible.
If you use a navigation icon you should also set a description for its action using Toolbar.setNavigationContentDescription(int). This is used for accessibility and tooltips.
Parameters:
icon: Drawable to set, may be null to clear the icon
Return the current drawable used as the navigation icon.
Returns:
The navigation icon drawable
Set a listener to respond to navigation events.
This listener will be called whenever the user clicks the navigation button at the start of the toolbar. An icon must be set for the navigation button to appear.
Parameters:
listener: Listener to set
See also: Toolbar
Retrieve the currently configured content description for the collapse button view. This will be used to describe the collapse action to users through mechanisms such as screen readers or tooltips.
Returns:
The collapse button’s content description
Set a content description for the collapse button if one is present. The content description will be read via screen readers or other accessibility systems to explain the action of the collapse button.
Parameters:
resId: Resource ID of a content description string to set, or 0 to clear the description
Set a content description for the collapse button if one is present. The content description will be read via screen readers or other accessibility systems to explain the action of the navigation button.
Parameters:
description: Content description to set, or null to clear the content description
Return the current drawable used as the collapse icon.
Returns:
The collapse icon drawable
Set the icon to use for the toolbar’s collapse button.
The collapse button appears at the start of the toolbar when an action view is present .
Parameters:
resId: Resource ID of a drawable to set
Set the icon to use for the toolbar’s collapse button.
The collapse button appears at the start of the toolbar when an action view is present .
Parameters:
icon: Drawable to set, may be null to use the default icon
Return the Menu shown in the toolbar.
Applications that wish to populate the toolbar’s menu can do so from here. To use an XML menu resource, use Toolbar.inflateMenu(int).
Returns:
The toolbar’s Menu
Set the icon to use for the overflow button.
Parameters:
icon: Drawable to set, may be null to clear the icon
Return the current drawable used as the overflow icon.
Returns:
The overflow icon drawable
Inflate a menu resource into this toolbar.
Inflate an XML menu resource into this toolbar. Existing items in the menu will not be modified or removed.
Parameters:
resId: ID of a menu resource to inflate
Set a listener to respond to menu item click events.
This listener will be invoked whenever a user selects a menu item from the action buttons presented at the end of the toolbar or the associated overflow.
Parameters:
listener: Listener to set
Sets the content insets for this toolbar relative to layout direction.
The content inset affects the valid area for Toolbar content other than the navigation button and menu. Insets define the minimum margin for these components and can be used to effectively align Toolbar content along well-known gridlines.
Parameters:
contentInsetStart: Content inset for the toolbar starting edge
contentInsetEnd: Content inset for the toolbar ending edge
Gets the starting content inset for this toolbar.
The content inset affects the valid area for Toolbar content other than the navigation button and menu. Insets define the minimum margin for these components and can be used to effectively align Toolbar content along well-known gridlines.
Returns:
The starting content inset for this toolbar
Gets the ending content inset for this toolbar.
The content inset affects the valid area for Toolbar content other than the navigation button and menu. Insets define the minimum margin for these components and can be used to effectively align Toolbar content along well-known gridlines.
Returns:
The ending content inset for this toolbar
Sets the content insets for this toolbar.
The content inset affects the valid area for Toolbar content other than the navigation button and menu. Insets define the minimum margin for these components and can be used to effectively align Toolbar content along well-known gridlines.
Parameters:
contentInsetLeft: Content inset for the toolbar’s left edge
contentInsetRight: Content inset for the toolbar’s right edge
Gets the left content inset for this toolbar.
The content inset affects the valid area for Toolbar content other than the navigation button and menu. Insets define the minimum margin for these components and can be used to effectively align Toolbar content along well-known gridlines.
Returns:
The left content inset for this toolbar
Gets the right content inset for this toolbar.
The content inset affects the valid area for Toolbar content other than the navigation button and menu. Insets define the minimum margin for these components and can be used to effectively align Toolbar content along well-known gridlines.
Returns:
The right content inset for this toolbar
Gets the start content inset to use when a navigation button is present.
Different content insets are often called for when additional buttons are present in the toolbar, as well as at different toolbar sizes. The larger value of Toolbar.getContentInsetStart() and this value will be used during layout.
Returns:
the start content inset used when a navigation icon has been set in pixels
See also:
Sets the start content inset to use when a navigation button is present.
Different content insets are often called for when additional buttons are present in the toolbar, as well as at different toolbar sizes. The larger value of Toolbar.getContentInsetStart() and this value will be used during layout.
Parameters:
insetStartWithNavigation: the inset to use when a navigation icon has been set in pixels
See also:
Gets the end content inset to use when action buttons are present.
Different content insets are often called for when additional buttons are present in the toolbar, as well as at different toolbar sizes. The larger value of Toolbar.getContentInsetEnd() and this value will be used during layout.
Returns:
the end content inset used when a menu has been set in pixels
See also:
Sets the start content inset to use when action buttons are present.
Different content insets are often called for when additional buttons are present in the toolbar, as well as at different toolbar sizes. The larger value of Toolbar.getContentInsetEnd() and this value will be used during layout.
Parameters:
insetEndWithActions: the inset to use when a menu has been set in pixels
See also:
Gets the content inset that will be used on the starting side of the bar in the current toolbar configuration.
Returns:
the current content inset start in pixels
Gets the content inset that will be used on the ending side of the bar in the current toolbar configuration.
Returns:
the current content inset end in pixels
Gets the content inset that will be used on the left side of the bar in the current toolbar configuration.
Returns:
the current content inset left in pixels
Gets the content inset that will be used on the right side of the bar in the current toolbar configuration.
Returns:
the current content inset right in pixels
Force the toolbar to collapse to zero-height during measurement if it could be considered «empty» (no visible elements with nonzero measured size)
Источник