Android ActionBar Application 1: ActionBar returns any page and top search bar

Source: Internet
Author: User

Android ActionBar Application 1: ActionBar returns any page and top search bar

 

 

Action Bar is a square area on the top of an app. It is not always displayed (such as when Theme is displayed in full screen). It is part of a window and is somewhat similar to the title Bar of a windows window, as shown in:

 

 

Brief Introduction to ActionBar:

 

As shown in, there are generally three buttons in the title bar:

1. It can be called the home area. Here is a button that identifies the app. This button is not activated by default, and the image display is consistent with the app icon by default.

2. It can be called the button area. Multiple title bar buttons displayed in the button Area

3. Click the "more" button to pull out the hidden title bar button, which is also known as overflow.

 

It must be clear that:

The ActionBar API was first added to Android 3.0 (API Level 11), but we can also use it in Android 2.1 or later versions, you only need to add the support package, that is, v4 support library and v7 support library.

 

Generally, when writing an application for compatibility purpose, the ActionBar APIs in the compatibility package are used instead of the built-in APIs, therefore, the subsequent sections are based on the content in V4 and V7 packages by default.

 

In the import to use the ActionBar in the compatibility package, our MainActivity or other XxxActivity cannot inherit from the Activity. Instead, we need to replace it with ActionBarActivity, and then in the list file, change to the AppCompat topic or its subthemes. For details about the topic, see the ActionBar topic color:

 

Get the ActionBar object:

 

public class MainActivity extends ActionBarActivity

Then, you can get the ActionBar object through the following code:

 

 

ActionBar mActionBar = getSupportActionBar();

 

Then, you can call the following two sentences to display or hide the entire actionBar:

 

mActionBar.hide();mActionBar.show();

 

Activate home:However, it is useless to get this object, and you still cannot click it. Further Processing is required. It is similar to the enable setting of the Button. setEnable:

 

 

MActionBar. setHomeButtonEnabled (true); mActionBar. setDisplayShowHomeEnabled (true); // The actionBar icon can respond to the Click Event mActionBar. setDisplayHomeAsUpEnabled (true); // This sentence is mainly used to return results later.

Set the button area:

In the preceding button area, you need to use an xml file indicating the menu, which is generally stored under res/menu/, and then override the onCreateOptionsMenu () method to set it, the res/menu/main_activity_actions.xml file is as follows:

 

 

 


 

As shown in the preceding figure, a custom property showAsAction is used to indicate the display mode of a button. The commonly used property is ifRoom (indicating that a space is displayed ), withText (with a name, but not necessarily displayed, the text is usually displayed under the "more" button), never (never directly displayed on the ActionBar, this means that the button will always be displayed on the "more" button drop-down menu). Note that the "more" button may not be displayed, if there is not enough space or a button with the never attribute, it is displayed; otherwise, it is not displayed. Stackoverflow has studied how to make more always show, interested can see: http://stackoverflow.com/questions/20444596/how-to-force-action-bar-overflow-icon-to-show

In addition, the above xml also specifies the button id and image and title attributes.

 

Then, you can rewriteonCreateOptionsMenu()To display these icons:

 

@Overridepublic boolean onCreateOptionsMenu(Menu menu) {    // Inflate the menu items for use in the action bar    MenuInflater inflater = getMenuInflater();    inflater.inflate(R.menu.main_activity_actions, menu);    return super.onCreateOptionsMenu(menu);}

 

 

Process click events:

The onOptionsItemSelected () method can be rewritten to achieve the goal, but note that for the ActionBar icon, that is, the app icon, its id is android. r. id. home.

 

@Overridepublic boolean onOptionsItemSelected(MenuItem item) {int itemId = item.getItemId();switch (itemId) {case android.R.id.home:Toast.makeText(this, home, 0).show();break;case R.id.action_search:Toast.makeText(this, search, 0).show();break;default:Toast.makeText(this, itemId= + itemId, 1).show();break;}return super.onOptionsItemSelected(item);}

 

 

After completing the preceding sections, you can get an interface similar to the following.




The ActionBar function is used to return the previous Activity or any Activity:

 

When writing an App, you often want to use this function to return the initial/homepage Activity from any activity. This function can be easily completed by using the ActionBar button.

 

There are two implementation methods: xml configuration and java code.

 

You need to set an attribute mentioned above in either of the two methods:

 

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_details);    ActionBar actionBar = getSupportActionBar();    actionBar.setDisplayHomeAsUpEnabled(true);    ...}

 

Method 1: return the configuration method of the xml configuration file:

 

For example, if two activities: A and B, and B want to return A, set android in meta-data mode at the position of listing file B. support. PARENT_ACTIVITY is the full class name of A, as shown in the following code.

 

 

    ...    
             ...        
             
         
     
 
 

Then, click the home Key of the ActionBar to return to ActivityA directly from ActivityB.

 

 

 

Method 2: overridegetSupportParentActivityIntent()AndonCreateSupportNavigateUpTaskStack()Method.

In fact, these two methods do not have to be overwritten. getSupportParentActivityIntent () is used to jump to the internal activity of the app, and onCreateSupportNavigateUpTaskStack () is used to jump to the activity of other apps, view http://developer.android.com/guide/topics/ui/actionbar.html#ActionViewCollapsing

 

GetSupportParentActivityIntent () returns an Intent to jump to. Therefore, you only need to return an Intent object pointing to the Activity you want to jump.

 

Relatively speaking, the code method is more flexible.

 

It is worth noting that the above two methods can only be applied to jump between activities. To apply them to Fragment, you need to useonSupportNavigateUp()And popBackStack () methods.

 

 

Android search bar:

 

First, you need to add a custom attribute to the corresponding item in the xml file of the preceding menu, such as the item in which action_search is located to add a SearchView for this button:

 

  


 

 

Then you need to create the SearchView in the onCreateOptionsMenu method:

 

@Overridepublic boolean onCreateOptionsMenu(Menu menu) {    getMenuInflater().inflate(R.menu.main_activity_actions, menu);    MenuItem searchItem = menu.findItem(R.id.action_search);    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);    // Configure the search info and add any event listeners    ...    return super.onCreateOptionsMenu(menu);}


 

After you click the search button, the corresponding search bar is displayed:

 

 

But now it's not that good... If no search prompt text is displayed, and no search function is available, perform the following two steps:

 

Search Prompt text (hint ):

 

Here we need to write a searchable xml file to the res/xml/folder:

Res/xml/searchable. xml

 

 

  
  

 

 

 


The above is a simple searchable. In fact, there are many searchable attributes, but most of them cannot be used unless you need Voice Search or other additional features.

 

Next, you need to set this searchable. xml is associated with the activity that we want to display the search results in the list file. meta-data is also used here. Note that the location here is the activity where the search results are to be displayed, not necessarily the current activity:

 

                    
                              
              
          
  
  
       ...
  

Here, android. app. searchable indicates the search attributes and hint prompts. It cannot be changed. @ xml/searchable indicates the res/xml/searchable. xml file.

 

 

Then, we also need to hook the search input box to the corresponding search activity by default, and also use meta-data. However, we need to configure it in the application or other activity to be searched, here, we will uninstall the application as an example:

 

        
   
        ...
   

 

 

After configuration, we will receive the Intent object in the onCreate method of the activity to display the search result, and then judge whether the Intent object needs to be searched for: Intent. ACTION_SEARCH

 

@Overridepublic void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.search);    // Get the intent, verify the action and get the query    Intent intent = getIntent();    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {      String query = intent.getStringExtra(SearchManager.QUERY);      doSearching(query);    }}


 

 

Private void doSearching (String query) {System. out. println (time-consuming operations for searching ....);}

After the preceding operations, you can create a simple search structure. Then, in doSearching, you can perform real search operations, query databases, or search networks...

 

 

Put the return function and search together, and the results are as follows:

 

 

 

 

Related Article

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.