Android Basics: Toolbar Usage

Android Basics: Toolbar Usage

Toolbar is introduced in Android 5.0 to replace the ActionBar control. It can be highly customized and flexible to use. The official Toolbar must be used in systems above 5.0. If you need to use it in a lower version, you need to use the Toolbar in the support v7 package.

Next is the introduction of Toolbar on the official website:

  1. Toolbar
  2. extends ViewGroup
  3. java.lang.Object
  4. ↳ android. view . View  
  5. android.view.ViewGroup
  6. ↳ android.support.v7.widget.Toolbar

Prerequisites

To use ToolBar, you need to hide the ActionBar of the activity. So how to hide it? There are three methods. The following are introduced respectively

Method 1:

Modify in the res/values/styles.xml file

  1. <style name = "AppTheme.Base" parent= "Theme.AppCompat" >
  2. <item name = "windowActionBar" > false </item>
  3. <item name = "android:windowNoTitle" > true </item>
  4. </style>

You can also use the theme without ActionBar directly

  1. <style name = "AppTheme" parent= "Theme.AppCompat.Light.NoActionBar" >

Method 2:

Modify the theme of the specified activity in the manifest file as follows:

  1. <activity android: name = "MainActivity"  
  2. android:theme= "@style/Theme.AppCompat.Light.NoActionBar" >
  3. </activity>

Method 3:

Cancel it directly in the code. Before setContentView.

  1. requestWindowFeature(Window.FEATURE_NO_TITLE);
  2. setContentView(R.layout.activity_main);
  3. //supportRequestWindowFeature(Window.FEATURE_NO_TITLE); in AppCompatActivity

How to use

Add the following code in the layout file. The position is not fixed.

  1. <android.support.v7.widget.Toolbar
  2. android:id= "@+id/toolbar"  
  3. android:layout_width= "match_parent"  
  4. android:layout_height= "?android:attr/actionBarSize"  
  5. android:background= "?attr/colorPrimaryDark"  
  6. app:title= "Title"  
  7. app:titleTextColor= "#222222"  
  8. app:logo= "@mipmap/ic_launcher"  
  9. app:subtitle= "subtitle" ></android.support.v7.widget.Toolbar>

title is used to set the title. subtitle is used to set the subtitle. titleTextColor is used to set the title font color. background is used to set the background color. The effect is as follows:

These values ​​can also be set dynamically in Java code.

  1. mToolbar.setTitle( "JavaTitle" );
  2. mToolbar.setSubtitle( "JavaSubTitle" );
  3. mToolbar.setLogo(R.mipmap.ic_launcher);
  4. mToolbar.setNavigationIcon(android.R.drawable.ic_input_delete);
  5. mToolbar.setOverflowIcon(ContextCompat.getDrawable(this, android.R.drawable.ic_menu_more));
  6. /setActionBar(mToolbar); //in activity
  7. setSupportActionBar(mToolbar); //In AppCompatActivity

Add Menu

To add a menu, we first need to have a menu. Here I choose to create main.xml in res/menu/ to define the menu file. The code is as follows

  1. <?xml version= "1.0" encoding= "utf-8" ?>
  2. <menu xmlns:android= "http://schemas.android.com/apk/res/android"  
  3. xmlns:app= "http://schemas.android.com/apk/res-auto" >
  4. <item
  5. android:id= "@+id/add"  
  6. android:icon= "@android:drawable/ic_menu_add"  
  7. android:title= "Add"  
  8. app:showAsAction= "never|withText" />
  9. <item
  10. android:id= "@+id/delete"  
  11. android:icon= "@android:drawable/ic_menu_delete"  
  12. android:title= "Delete"  
  13. app:showAsAction= "never|withText" />
  14. <item
  15. android:id= "@+id/edit"  
  16. android:icon= "@android:drawable/ic_menu_edit"  
  17. android:title= "Edit"  
  18. app:showAsAction= "never|withText" />
  19. <item
  20. android:id= "@+id/email"  
  21. android:icon= "@android:drawable/sym_action_email"  
  22. android:title= "Email"  
  23. app:showAsAction= "never|withText" />
  24.  
  25. </menu>

Below is the code in java file.

  1. @Override
  2. public boolean onCreateOptionsMenu(Menu menu) {
  3. getMenuInflater().inflate(R.menu.main, menu);
  4. return   true ;
  5. }
  6.  
  7. @Override
  8. public boolean onOptionsItemSelected(MenuItem item) {
  9. switch (item.getItemId()) {
  10. case android.R.id.home:
  11. Toast.makeText(MainActivity.this, "You clicked on NavigationIcon" , Toast.LENGTH_SHORT).show();
  12. break;
  13. case R.id. add :
  14. Toast.makeText(MainActivity.this, "Add" , Toast.LENGTH_SHORT).show();
  15. break;
  16. case R.id. delete :
  17. Toast.makeText(MainActivity.this, "Delete" , Toast.LENGTH_SHORT).show();
  18. break;
  19. case R.id.edit:
  20. Toast.makeText(MainActivity.this, "Edit" , Toast.LENGTH_SHORT).show();
  21. break;
  22. case R.id.email:
  23. Toast.makeText(MainActivity.this, "Email" , Toast.LENGTH_SHORT).show();
  24. break;
  25. }
  26. return   true ;
  27. }

Display Icon in Menu

After writing this, we will find that the hidden menu does not display the icon, so how do we set it:

Just re-do the method: The activity here is AppCompatActivity

  1. @Override
  2. protected boolean onPrepareOptionsPanel( View   view , Menu menu) {
  3. if (menu != null ) {
  4. if (menu.getClass() == MenuBuilder.class) {
  5. try {
  6. Method m = menu.getClass().getDeclaredMethod( "setOptionalIconsVisible" , Boolean.TYPE);
  7. m.setAccessible( true );
  8. m.invoke(menu, true );
  9. } catch (Exception e) {
  10. Log.i( "tag" , "onPrepareOptionsPanel: " +
  11. getClass().getSimpleName() +
  12. "onMenuOpened...unable to set icons for overflow menu"  
  13. + e);
  14. }
  15. }
  16. }
  17. return super.onPrepareOptionsPanel( view , menu);
  18. }

The effect is as follows:

<<:  Android development SMS verification code example

>>:  How do IT staff manage user application experience in a complex environment?

Recommend

Tik Tok marketing promotion, how can one video gain 480,000 followers in 2 days?

Faced with the trend of Douyin, the actual situat...

How do mini programs split? Tips for attracting 1,000+ new customers a day!

Recently, a friend asked me how to achieve explos...

In-depth analysis of KOL operations from 0 to 1!

What I talk about most with friends recently is K...

Why is there a hole in a mint? Was it to cut corners?

This article was reviewed by Pa Li Ze, chief phys...

ARKit & OpenGL ES - ARKit principle and implementation

Principle If you want to learn more about OpenGL ...

Let’s talk about SwiftUI layout protocol

Introduction One of the best new features added t...