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