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.supportv7.app;
     17 
     18 import android.os.Bundle;
     19 import android.view.Gravity;
     20 import android.view.Menu;
     21 import android.view.View;
     22 import android.view.ViewGroup.LayoutParams;
     23 import android.widget.ArrayAdapter;
     24 
     25 import androidx.appcompat.app.ActionBar;
     26 import androidx.appcompat.app.ActionBar.Tab;
     27 import androidx.appcompat.app.AppCompatActivity;
     28 import androidx.fragment.app.FragmentTransaction;
     29 
     30 import com.example.android.supportv7.R;
     31 
     32 /**
     33  * This demo shows how various action bar display option flags can be combined and their effects.
     34  */
     35 public class ActionBarDisplayOptions extends AppCompatActivity
     36         implements View.OnClickListener, ActionBar.TabListener {
     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.action_bar_display_options);
     44 
     45         findViewById(R.id.toggle_home_as_up).setOnClickListener(this);
     46         findViewById(R.id.toggle_show_home).setOnClickListener(this);
     47         findViewById(R.id.toggle_use_logo).setOnClickListener(this);
     48         findViewById(R.id.toggle_show_title).setOnClickListener(this);
     49         findViewById(R.id.toggle_show_custom).setOnClickListener(this);
     50         findViewById(R.id.cycle_custom_gravity).setOnClickListener(this);
     51         findViewById(R.id.toggle_visibility).setOnClickListener(this);
     52 
     53         // Configure several action bar elements that will be toggled by display options.
     54         mCustomView = getLayoutInflater().inflate(R.layout.action_bar_display_options_custom, null);
     55         mCustomViewLayoutParams = new ActionBar.LayoutParams(
     56                 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
     57 
     58         final ActionBar bar = getSupportActionBar();
     59         bar.setCustomView(mCustomView, mCustomViewLayoutParams);
     60 
     61         final ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(bar.getThemedContext(),
     62                 R.layout.support_simple_spinner_dropdown_item,
     63                 new String[] { "Item 1", "Item 2", "Item 3" });
     64 
     65         bar.setLogo(R.drawable.ic_media_play);
     66     }
     67 
     68     @Override
     69     public boolean onCreateOptionsMenu(Menu menu) {
     70         getMenuInflater().inflate(R.menu.display_options_actions, menu);
     71         return true;
     72     }
     73 
     74     @Override
     75     public boolean onSupportNavigateUp() {
     76         finish();
     77         return true;
     78     }
     79 
     80     @Override
     81     public void onClick(View v) {
     82         final ActionBar bar = getSupportActionBar();
     83         int flags = 0;
     84         switch (v.getId()) {
     85             case R.id.toggle_home_as_up:
     86                 flags = ActionBar.DISPLAY_HOME_AS_UP;
     87                 break;
     88             case R.id.toggle_show_home:
     89                 flags = ActionBar.DISPLAY_SHOW_HOME;
     90                 break;
     91             case R.id.toggle_use_logo:
     92                 flags = ActionBar.DISPLAY_USE_LOGO;
     93                 break;
     94             case R.id.toggle_show_title:
     95                 flags = ActionBar.DISPLAY_SHOW_TITLE;
     96                 break;
     97             case R.id.toggle_show_custom:
     98                 flags = ActionBar.DISPLAY_SHOW_CUSTOM;
     99                 break;
    100             case R.id.cycle_custom_gravity: {
    101                 ActionBar.LayoutParams lp = mCustomViewLayoutParams;
    102                 int newGravity = 0;
    103                 switch (lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
    104                     case Gravity.LEFT:
    105                         newGravity = Gravity.CENTER_HORIZONTAL;
    106                         break;
    107                     case Gravity.CENTER_HORIZONTAL:
    108                         newGravity = Gravity.RIGHT;
    109                         break;
    110                     case Gravity.RIGHT:
    111                         newGravity = Gravity.LEFT;
    112                         break;
    113                 }
    114                 lp.gravity = lp.gravity & ~Gravity.HORIZONTAL_GRAVITY_MASK | newGravity;
    115                 bar.setCustomView(mCustomView, lp);
    116                 return;
    117             }
    118             case R.id.toggle_visibility:
    119                 if (bar.isShowing()) {
    120                     bar.hide();
    121                 } else {
    122                     bar.show();
    123                 }
    124                 return;
    125         }
    126 
    127         int change = bar.getDisplayOptions() ^ flags;
    128         bar.setDisplayOptions(change, flags);
    129     }
    130 
    131     @Override
    132     public void onTabSelected(Tab tab, FragmentTransaction ft) {
    133     }
    134 
    135     @Override
    136     public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    137     }
    138 
    139     @Override
    140     public void onTabReselected(Tab tab, FragmentTransaction ft) {
    141     }
    142 }
    143