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 */ 16package com.example.android.supportv7.app; 17 18import android.os.Bundle; 19import android.support.v4.app.FragmentTransaction; 20import android.support.v7.app.ActionBar; 21import android.support.v7.app.ActionBar.Tab; 22import android.support.v7.app.AppCompatActivity; 23import android.view.Gravity; 24import android.view.Menu; 25import android.view.View; 26import android.view.ViewGroup.LayoutParams; 27import android.widget.ArrayAdapter; 28 29import com.example.android.supportv7.R; 30 31/** 32 * This demo shows how various action bar display option flags can be combined and their effects. 33 */ 34public class ActionBarDisplayOptions extends AppCompatActivity 35 implements View.OnClickListener, ActionBar.TabListener { 36 private View mCustomView; 37 private ActionBar.LayoutParams mCustomViewLayoutParams; 38 39 @Override 40 protected void onCreate(Bundle savedInstanceState) { 41 super.onCreate(savedInstanceState); 42 setContentView(R.layout.action_bar_display_options); 43 44 findViewById(R.id.toggle_home_as_up).setOnClickListener(this); 45 findViewById(R.id.toggle_show_home).setOnClickListener(this); 46 findViewById(R.id.toggle_use_logo).setOnClickListener(this); 47 findViewById(R.id.toggle_show_title).setOnClickListener(this); 48 findViewById(R.id.toggle_show_custom).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 60 final ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(bar.getThemedContext(), 61 R.layout.support_simple_spinner_dropdown_item, 62 new String[] { "Item 1", "Item 2", "Item 3" }); 63 64 bar.setLogo(R.drawable.ic_media_play); 65 } 66 67 @Override 68 public boolean onCreateOptionsMenu(Menu menu) { 69 getMenuInflater().inflate(R.menu.display_options_actions, menu); 70 return true; 71 } 72 73 @Override 74 public boolean onSupportNavigateUp() { 75 finish(); 76 return true; 77 } 78 79 @Override 80 public void onClick(View v) { 81 final ActionBar bar = getSupportActionBar(); 82 int flags = 0; 83 switch (v.getId()) { 84 case R.id.toggle_home_as_up: 85 flags = ActionBar.DISPLAY_HOME_AS_UP; 86 break; 87 case R.id.toggle_show_home: 88 flags = ActionBar.DISPLAY_SHOW_HOME; 89 break; 90 case R.id.toggle_use_logo: 91 flags = ActionBar.DISPLAY_USE_LOGO; 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 @Override 131 public void onTabSelected(Tab tab, FragmentTransaction ft) { 132 } 133 134 @Override 135 public void onTabUnselected(Tab tab, FragmentTransaction ft) { 136 } 137 138 @Override 139 public void onTabReselected(Tab tab, FragmentTransaction ft) { 140 } 141} 142