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 android.os.Bundle; 20 import android.view.Gravity; 21 import android.view.Menu; 22 import android.view.View; 23 import android.view.ViewGroup.LayoutParams; 24 25 import androidx.appcompat.app.ActionBar; 26 import androidx.appcompat.app.AppCompatActivity; 27 import androidx.appcompat.widget.Toolbar; 28 29 import 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 * when used on a Toolbar-provided Action Bar 34 */ 35 public class ToolbarDisplayOptions extends AppCompatActivity 36 implements View.OnClickListener { 37 38 private View mCustomView; 39 private ActionBar.LayoutParams mCustomViewLayoutParams; 40 41 @Override onCreate(Bundle savedInstanceState)42 protected void onCreate(Bundle savedInstanceState) { 43 super.onCreate(savedInstanceState); 44 setContentView(R.layout.toolbar_display_options); 45 46 Toolbar toolbar = findViewById(R.id.toolbar); 47 setSupportActionBar(toolbar); 48 49 findViewById(R.id.toggle_home_as_up).setOnClickListener(this); 50 findViewById(R.id.toggle_show_home).setOnClickListener(this); 51 findViewById(R.id.toggle_use_logo).setOnClickListener(this); 52 findViewById(R.id.toggle_show_title).setOnClickListener(this); 53 findViewById(R.id.toggle_show_custom).setOnClickListener(this); 54 findViewById(R.id.cycle_custom_gravity).setOnClickListener(this); 55 findViewById(R.id.toggle_visibility).setOnClickListener(this); 56 57 // Configure several action bar elements that will be toggled by display options. 58 mCustomView = getLayoutInflater().inflate(R.layout.action_bar_display_options_custom, null); 59 mCustomViewLayoutParams = new ActionBar.LayoutParams( 60 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 61 } 62 63 @Override onCreateOptionsMenu(Menu menu)64 public boolean onCreateOptionsMenu(Menu menu) { 65 getMenuInflater().inflate(R.menu.display_options_actions, menu); 66 return true; 67 } 68 69 @Override onSupportNavigateUp()70 public boolean onSupportNavigateUp() { 71 finish(); 72 return true; 73 } 74 75 @Override onClick(View v)76 public void onClick(View v) { 77 final ActionBar bar = getSupportActionBar(); 78 int flags = 0; 79 switch (v.getId()) { 80 case R.id.toggle_home_as_up: 81 flags = ActionBar.DISPLAY_HOME_AS_UP; 82 break; 83 case R.id.toggle_show_home: 84 flags = ActionBar.DISPLAY_SHOW_HOME; 85 break; 86 case R.id.toggle_use_logo: 87 flags = ActionBar.DISPLAY_USE_LOGO; 88 getSupportActionBar().setLogo(R.drawable.ic_media_play); 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.cycle_custom_gravity: { 97 ActionBar.LayoutParams lp = mCustomViewLayoutParams; 98 int newGravity = 0; 99 switch (lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK) { 100 case Gravity.LEFT: 101 newGravity = Gravity.CENTER_HORIZONTAL; 102 break; 103 case Gravity.CENTER_HORIZONTAL: 104 newGravity = Gravity.RIGHT; 105 break; 106 case Gravity.RIGHT: 107 newGravity = Gravity.LEFT; 108 break; 109 } 110 lp.gravity = lp.gravity & ~Gravity.HORIZONTAL_GRAVITY_MASK | newGravity; 111 bar.setCustomView(mCustomView, lp); 112 return; 113 } 114 case R.id.toggle_visibility: 115 if (bar.isShowing()) { 116 bar.hide(); 117 } else { 118 bar.show(); 119 } 120 return; 121 } 122 123 int change = bar.getDisplayOptions() ^ flags; 124 bar.setDisplayOptions(change, flags); 125 } 126 } 127