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 import android.widget.AdapterView; 30 import android.widget.ArrayAdapter; 31 import android.widget.Spinner; 32 33 /** 34 * This demo shows how various action bar display option flags can be combined 35 * and their effects. 36 */ 37 public class ActionBarDisplayOptions extends Activity implements View.OnClickListener, 38 ActionBar.TabListener, Spinner.OnItemSelectedListener, ActionBar.OnNavigationListener { 39 private View mCustomView; 40 41 @Override onCreate(Bundle savedInstanceState)42 protected void onCreate(Bundle savedInstanceState) { 43 super.onCreate(savedInstanceState); 44 setContentView(R.layout.action_bar_display_options); 45 46 findViewById(R.id.toggle_home_as_up).setOnClickListener(this); 47 findViewById(R.id.toggle_show_home).setOnClickListener(this); 48 findViewById(R.id.toggle_use_logo).setOnClickListener(this); 49 findViewById(R.id.toggle_show_title).setOnClickListener(this); 50 findViewById(R.id.toggle_show_custom).setOnClickListener(this); 51 findViewById(R.id.cycle_custom_gravity).setOnClickListener(this); 52 findViewById(R.id.toggle_visibility).setOnClickListener(this); 53 findViewById(R.id.toggle_system_ui).setOnClickListener(this); 54 55 ((Spinner) findViewById(R.id.toggle_navigation)).setOnItemSelectedListener(this); 56 57 mCustomView = getLayoutInflater().inflate(R.layout.action_bar_display_options_custom, null); 58 // Configure several action bar elements that will be toggled by display options. 59 final ActionBar bar = getActionBar(); 60 bar.setCustomView(mCustomView, 61 new ActionBar.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 62 63 bar.addTab(bar.newTab().setText("Tab 1").setTabListener(this)); 64 bar.addTab(bar.newTab().setText("Tab 2").setTabListener(this)); 65 bar.addTab(bar.newTab().setText("Tab 3").setTabListener(this)); 66 67 final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 68 android.R.layout.simple_list_item_1); 69 adapter.add("Item 1"); 70 adapter.add("Item 2"); 71 adapter.add("Item 3"); 72 bar.setListNavigationCallbacks(adapter, this); 73 } 74 75 @Override onCreateOptionsMenu(Menu menu)76 public boolean onCreateOptionsMenu(Menu menu) { 77 getMenuInflater().inflate(R.menu.display_options_actions, menu); 78 return true; 79 } 80 81 @Override onClick(View v)82 public void onClick(View v) { 83 final ActionBar bar = getActionBar(); 84 int flags = 0; 85 switch (v.getId()) { 86 case R.id.toggle_home_as_up: 87 flags = ActionBar.DISPLAY_HOME_AS_UP; 88 break; 89 case R.id.toggle_show_home: 90 flags = ActionBar.DISPLAY_SHOW_HOME; 91 break; 92 case R.id.toggle_use_logo: 93 flags = ActionBar.DISPLAY_USE_LOGO; 94 break; 95 case R.id.toggle_show_title: 96 flags = ActionBar.DISPLAY_SHOW_TITLE; 97 break; 98 case R.id.toggle_show_custom: 99 flags = ActionBar.DISPLAY_SHOW_CUSTOM; 100 break; 101 case R.id.cycle_custom_gravity: 102 ActionBar.LayoutParams lp = (ActionBar.LayoutParams) mCustomView.getLayoutParams(); 103 int newGravity = 0; 104 switch (lp.gravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) { 105 case Gravity.START: 106 newGravity = Gravity.CENTER_HORIZONTAL; 107 break; 108 case Gravity.CENTER_HORIZONTAL: 109 newGravity = Gravity.END; 110 break; 111 case Gravity.END: 112 newGravity = Gravity.START; 113 break; 114 } 115 lp.gravity = lp.gravity & ~Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK | newGravity; 116 bar.setCustomView(mCustomView, lp); 117 return; 118 case R.id.toggle_visibility: 119 if (bar.isShowing()) { 120 bar.hide(); 121 } else { 122 bar.show(); 123 } 124 return; 125 case R.id.toggle_system_ui: 126 if ((getWindow().getDecorView().getSystemUiVisibility() 127 & View.SYSTEM_UI_FLAG_FULLSCREEN) != 0) { 128 getWindow().getDecorView().setSystemUiVisibility(0); 129 } else { 130 getWindow().getDecorView().setSystemUiVisibility( 131 View.SYSTEM_UI_FLAG_FULLSCREEN); 132 } 133 return; 134 } 135 136 int change = bar.getDisplayOptions() ^ flags; 137 bar.setDisplayOptions(change, flags); 138 } 139 140 @Override onTabSelected(Tab tab, FragmentTransaction ft)141 public void onTabSelected(Tab tab, FragmentTransaction ft) { 142 } 143 144 @Override onTabUnselected(Tab tab, FragmentTransaction ft)145 public void onTabUnselected(Tab tab, FragmentTransaction ft) { 146 } 147 148 @Override onTabReselected(Tab tab, FragmentTransaction ft)149 public void onTabReselected(Tab tab, FragmentTransaction ft) { 150 } 151 152 @Override onItemSelected(AdapterView<?> parent, View view, int position, long id)153 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 154 final ActionBar bar = getActionBar(); 155 switch (parent.getId()) { 156 case R.id.toggle_navigation: 157 final int mode; 158 switch (position) { 159 case 1: 160 mode = ActionBar.NAVIGATION_MODE_TABS; 161 break; 162 case 2: 163 mode = ActionBar.NAVIGATION_MODE_LIST; 164 break; 165 default: 166 mode = ActionBar.NAVIGATION_MODE_STANDARD; 167 } 168 bar.setNavigationMode(mode); 169 return; 170 } 171 } 172 173 @Override onNothingSelected(AdapterView<?> parent)174 public void onNothingSelected(AdapterView<?> parent) { 175 } 176 177 @Override onNavigationItemSelected(int itemPosition, long itemId)178 public boolean onNavigationItemSelected(int itemPosition, long itemId) { 179 return false; 180 } 181 } 182