Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.supportv7.app;
     18 
     19 import com.example.android.supportv7.R;
     20 
     21 import android.os.Bundle;
     22 import android.support.v4.app.FragmentTransaction;
     23 import android.support.v7.app.ActionBar;
     24 import android.support.v7.app.ActionBar.Tab;
     25 import android.support.v7.app.ActionBarActivity;
     26 import android.support.v7.widget.Toolbar;
     27 import android.view.Gravity;
     28 import android.view.Menu;
     29 import android.view.View;
     30 import android.view.ViewGroup.LayoutParams;
     31 import android.widget.ArrayAdapter;
     32 import android.widget.Toast;
     33 
     34 /**
     35  * This demo shows how various action bar display option flags can be combined and their effects
     36  * when used on a Toolbar-provided Action Bar
     37  */
     38 public class ToolbarDisplayOptions extends ActionBarActivity
     39         implements View.OnClickListener {
     40 
     41     private View mCustomView;
     42     private ActionBar.LayoutParams mCustomViewLayoutParams;
     43 
     44     @Override
     45     protected void onCreate(Bundle savedInstanceState) {
     46         super.onCreate(savedInstanceState);
     47         setContentView(R.layout.toolbar_display_options);
     48 
     49         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
     50         setSupportActionBar(toolbar);
     51 
     52         findViewById(R.id.toggle_home_as_up).setOnClickListener(this);
     53         findViewById(R.id.toggle_show_home).setOnClickListener(this);
     54         findViewById(R.id.toggle_use_logo).setOnClickListener(this);
     55         findViewById(R.id.toggle_show_title).setOnClickListener(this);
     56         findViewById(R.id.toggle_show_custom).setOnClickListener(this);
     57         findViewById(R.id.cycle_custom_gravity).setOnClickListener(this);
     58         findViewById(R.id.toggle_visibility).setOnClickListener(this);
     59 
     60         // Configure several action bar elements that will be toggled by display options.
     61         mCustomView = getLayoutInflater().inflate(R.layout.action_bar_display_options_custom, null);
     62         mCustomViewLayoutParams = new ActionBar.LayoutParams(
     63                 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
     64     }
     65 
     66     @Override
     67     public boolean onCreateOptionsMenu(Menu menu) {
     68         getMenuInflater().inflate(R.menu.display_options_actions, menu);
     69         return true;
     70     }
     71 
     72     @Override
     73     public boolean onSupportNavigateUp() {
     74         finish();
     75         return true;
     76     }
     77 
     78     @Override
     79     public void onClick(View v) {
     80         final ActionBar bar = getSupportActionBar();
     81         int flags = 0;
     82         switch (v.getId()) {
     83             case R.id.toggle_home_as_up:
     84                 flags = ActionBar.DISPLAY_HOME_AS_UP;
     85                 break;
     86             case R.id.toggle_show_home:
     87                 flags = ActionBar.DISPLAY_SHOW_HOME;
     88                 break;
     89             case R.id.toggle_use_logo:
     90                 flags = ActionBar.DISPLAY_USE_LOGO;
     91                 getSupportActionBar().setLogo(R.drawable.ic_media_play);
     92                 break;
     93             case R.id.toggle_show_title:
     94                 flags = ActionBar.DISPLAY_SHOW_TITLE;
     95                 break;
     96             case R.id.toggle_show_custom:
     97                 flags = ActionBar.DISPLAY_SHOW_CUSTOM;
     98                 break;
     99             case R.id.cycle_custom_gravity: {
    100                 ActionBar.LayoutParams lp = mCustomViewLayoutParams;
    101                 int newGravity = 0;
    102                 switch (lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
    103                     case Gravity.LEFT:
    104                         newGravity = Gravity.CENTER_HORIZONTAL;
    105                         break;
    106                     case Gravity.CENTER_HORIZONTAL:
    107                         newGravity = Gravity.RIGHT;
    108                         break;
    109                     case Gravity.RIGHT:
    110                         newGravity = Gravity.LEFT;
    111                         break;
    112                 }
    113                 lp.gravity = lp.gravity & ~Gravity.HORIZONTAL_GRAVITY_MASK | newGravity;
    114                 bar.setCustomView(mCustomView, lp);
    115                 return;
    116             }
    117             case R.id.toggle_visibility:
    118                 if (bar.isShowing()) {
    119                     bar.hide();
    120                 } else {
    121                     bar.show();
    122                 }
    123                 return;
    124         }
    125 
    126         int change = bar.getDisplayOptions() ^ flags;
    127         bar.setDisplayOptions(change, flags);
    128     }
    129 }
    130