Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2010 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 package com.example.android.supportv7.app;
     17 
     18 import android.os.Bundle;
     19 import android.support.v4.app.FragmentTransaction;
     20 import android.support.v7.app.ActionBar;
     21 import android.support.v7.app.ActionBar.Tab;
     22 import android.support.v7.app.ActionBarActivity;
     23 import android.view.Gravity;
     24 import android.view.Menu;
     25 import android.view.View;
     26 import android.view.ViewGroup.LayoutParams;
     27 
     28 import com.example.android.supportv7.R;
     29 
     30 /**
     31  * This demo shows how various action bar display option flags can be combined and their effects.
     32  */
     33 public class ActionBarDisplayOptions extends ActionBarActivity
     34         implements View.OnClickListener, ActionBar.TabListener {
     35     private View mCustomView;
     36     private ActionBar.LayoutParams mCustomViewLayoutParams;
     37 
     38     @Override
     39     protected void onCreate(Bundle savedInstanceState) {
     40         super.onCreate(savedInstanceState);
     41         setContentView(R.layout.action_bar_display_options);
     42 
     43         findViewById(R.id.toggle_home_as_up).setOnClickListener(this);
     44         findViewById(R.id.toggle_show_home).setOnClickListener(this);
     45         findViewById(R.id.toggle_use_logo).setOnClickListener(this);
     46         findViewById(R.id.toggle_show_title).setOnClickListener(this);
     47         findViewById(R.id.toggle_show_custom).setOnClickListener(this);
     48         findViewById(R.id.toggle_navigation).setOnClickListener(this);
     49         findViewById(R.id.cycle_custom_gravity).setOnClickListener(this);
     50         findViewById(R.id.toggle_visibility).setOnClickListener(this);
     51 
     52         // Configure several action bar elements that will be toggled by display options.
     53         mCustomView = getLayoutInflater().inflate(R.layout.action_bar_display_options_custom, null);
     54         mCustomViewLayoutParams = new ActionBar.LayoutParams(
     55                 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
     56 
     57         final ActionBar bar = getSupportActionBar();
     58         bar.setCustomView(mCustomView, mCustomViewLayoutParams);
     59         bar.addTab(bar.newTab().setText("Tab 1").setTabListener(this));
     60         bar.addTab(bar.newTab().setText("Tab 2").setTabListener(this));
     61         bar.addTab(bar.newTab().setText("Tab 3").setTabListener(this));
     62     }
     63 
     64     @Override
     65     public boolean onCreateOptionsMenu(Menu menu) {
     66         getMenuInflater().inflate(R.menu.display_options_actions, menu);
     67         return true;
     68     }
     69 
     70     @Override
     71     public boolean onSupportNavigateUp() {
     72         finish();
     73         return true;
     74     }
     75 
     76     @Override
     77     public void onClick(View v) {
     78         final ActionBar bar = getSupportActionBar();
     79         int flags = 0;
     80         switch (v.getId()) {
     81             case R.id.toggle_home_as_up:
     82                 flags = ActionBar.DISPLAY_HOME_AS_UP;
     83                 break;
     84             case R.id.toggle_show_home:
     85                 flags = ActionBar.DISPLAY_SHOW_HOME;
     86                 break;
     87             case R.id.toggle_use_logo:
     88                 flags = ActionBar.DISPLAY_USE_LOGO;
     89                 break;
     90             case R.id.toggle_show_title:
     91                 flags = ActionBar.DISPLAY_SHOW_TITLE;
     92                 break;
     93             case R.id.toggle_show_custom:
     94                 flags = ActionBar.DISPLAY_SHOW_CUSTOM;
     95                 break;
     96             case R.id.toggle_navigation:
     97                 bar.setNavigationMode(
     98                         bar.getNavigationMode() == ActionBar.NAVIGATION_MODE_STANDARD
     99                                 ? ActionBar.NAVIGATION_MODE_TABS
    100                                 : ActionBar.NAVIGATION_MODE_STANDARD);
    101                 return;
    102             case R.id.cycle_custom_gravity: {
    103                 ActionBar.LayoutParams lp = mCustomViewLayoutParams;
    104                 int newGravity = 0;
    105                 switch (lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
    106                     case Gravity.LEFT:
    107                         newGravity = Gravity.CENTER_HORIZONTAL;
    108                         break;
    109                     case Gravity.CENTER_HORIZONTAL:
    110                         newGravity = Gravity.RIGHT;
    111                         break;
    112                     case Gravity.RIGHT:
    113                         newGravity = Gravity.LEFT;
    114                         break;
    115                 }
    116                 lp.gravity = lp.gravity & ~Gravity.HORIZONTAL_GRAVITY_MASK | newGravity;
    117                 bar.setCustomView(mCustomView, lp);
    118                 return;
    119             }
    120             case R.id.toggle_visibility:
    121                 if (bar.isShowing()) {
    122                     bar.hide();
    123                 } else {
    124                     bar.show();
    125                 }
    126                 return;
    127         }
    128 
    129         int change = bar.getDisplayOptions() ^ flags;
    130         bar.setDisplayOptions(change, flags);
    131     }
    132 
    133     @Override
    134     public void onTabSelected(Tab tab, FragmentTransaction ft) {
    135     }
    136 
    137     @Override
    138     public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    139     }
    140 
    141     @Override
    142     public void onTabReselected(Tab tab, FragmentTransaction ft) {
    143     }
    144 }
    145