Add search and share Windows to the title bar via Actionbar in Android

Source: Internet
Author: User
Tags custom name

After Android3.0, Google has made a series of changes to UI navigation design, and one of the most useful new features is the introduction of Actionbar, which replaces the title bar before 3.0 and provides a richer navigation effect. The main purpose of Actionbar is to:

1. Provides a private space to identify the application's logo and the user's location.

2. Provide a consistent navigation and visual experience across different applications.

3. Highlight activity's key operations (such as search, create, share, and so on) and provide users with quick access within a predictable approach.

Today we will simply comb how to add "search" and "share" to the title via Actionbar, such as:

The Action window is a visual widget displayed in the action Bar as an alternative to the action item button. For example, if you have an optional menu item for searching, you can use the Searchview class to replace the search button on the action bar.

To a menu item in a resource declares an action window where you can specify either a layout resource or a visual artifact class to use, using the Android:actionlayout property and the Android:actionviewclass property.

Like the Action window, the action provider (defined by the Actionprovider Class) replaces an action item with a custom layout, which also requires control over all of these project behaviors. When you declare an action item to a menu item in the Action bar, it does not only need a custom layout to control the appearance of this menu item, but also handles its default event when it is displayed in the overflow menu. It can provide a submenu, whether in the action bar or in the overflow menu. For example, Actionprovider's extended class Shareactionprovider, which facilitates the sharing of operations by displaying a valid list of shared targets in the action bar. Unlike an action item that uses a traditional invocation of the Action_send type Intent object, you can declare a Shareactionprovider object to handle an action item. This operation provider retains a drop-down list of applications with the intent object that handles Action_send, so that the menu item is displayed in the overflow menu. Therefore, when you use an operation provider like this, you do not have to deal with user events about this menu item. To declare an action provider for an action item, you define the Android:actionproviderclass attribute in the corresponding <item> element in the menu resource, and the provider uses the full class name.

First step: Create a menu resource file

Create a menu resource file, set the search and share item feature components.

(1) Create a new menu folder,

(2) Create a Serch_share_menu.xml file under the Menu Resource folder (custom name)

(3) Serch_share_menu.xml component Deployment Code:

1<?xml version= "1.0" encoding= "Utf-8"?>2<menu xmlns:android= "Http://schemas.android.com/apk/res/android"3xmlns:app= "Http://schemas.android.com/apk/res-auto" >4<Item5Android:id= "@+id/search"6App:actionviewclass= "Android.support.v7.widget.SearchView"7android:icon= "@android:d rawable/ic_menu_search"8android:orderincategory= "100"9android:title= "Search"Tenapp:showasaction= "Always|collapseactionview"/> One<Item AAndroid:id= "@+id/share" -android:icon= "@android:d rawable/ic_menu_share" -android:orderincategory= "200" theAndroid:title= "Sharing" -App:actionproviderclass= "Android.support.v7.widget.ShareActionProvider" -app:showasaction= "Ifroom"/> -</menu>
The second step: Java features implementation code Mainactivity.java:
1 Importandroid.content.Intent;2 ImportAndroid.support.v4.view.MenuItemCompat;3 Importandroid.support.v7.app.AppCompatActivity;4 ImportAndroid.os.Bundle;5 ImportAndroid.support.v7.widget.SearchView;6 ImportAndroid.support.v7.widget.ShareActionProvider;7 Importandroid.view.Gravity;8 ImportAndroid.view.Menu;9 ImportAndroid.view.MenuItem;Ten ImportAndroid.widget.Toast; One /** A * Created by Panchengjia on 2016/12/23. -  */ -  the  Public classMainactivityextendsappcompatactivity { - Searchview Searchview; - @Override -     protected voidonCreate (Bundle savedinstancestate) { +         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); +     } A @Override at      Public BooleanOncreateoptionsmenu (Menu menu) { -         //Use the menu filler to get the menu resource file under menu - getmenuinflater (). Inflate (R.menu.search_share_menu,menu); -         //Get the Search menu component -MenuItem MenuItem =Menu.finditem (r.id.search); -Searchview =(Searchview) Menuitemcompat.getactionview (menuItem); in         //set the events for the search -Searchview.setonquerytextlistener (NewSearchview.onquerytextlistener () { to @Override +              Public Booleanonquerytextsubmit (String query) { -Toast t = Toast.maketext (mainactivity. This, query, toast.length_short); theT.setgravity (gravity.top,0,0); * t.show (); $                 return false;Panax Notoginseng             } - @Override the              Public BooleanOnquerytextchange (String newtext) { +                 return false; A             } the         }); +         //Get the Shared menu sub-component -MenuItem Shareitem =Menu.finditem (r.id.share); $Shareactionprovider Shareactionprovider =(Shareactionprovider) Menuitemcompat.getactionprovider (shareitem); $         //get all apps with sharing features via setshareintent call Getdefaultintent () - shareactionprovider.setshareintent (Getdefaultintent ()); -         return Super. Oncreateoptionsmenu (menu); the     } -     //settings can call all apps in your phone that can share picturesWuyi     PrivateIntent getdefaultintent () { theIntent Intent =NewIntent (); - intent.setaction (intent.action_send); Wu         //the types here can be set on demand -Intent.settype ("image/*"); About         returnIntent; $     } -}


While these features can be achieved with actionbar, the effects of each Android version and custom ROM are mixed, resulting in a serious fragmentation problem, so Google released a series of material design-style controls at the 2015 IO Conference. Where toolbar is the control that replaces the Actionbar. We will also briefly describe the simple usage of toolbar in the following.

Add search and share Windows to the title bar via Actionbar in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.