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