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.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 
     30 /**
     31  * This demo shows how various action bar display option flags can be combined and their effects.
     32  */
     33 public class ActionBarDisplayOptions extends Activity
     34         implements View.OnClickListener, ActionBar.TabListener {
     35     private View mCustomView;
     36 
     37     @Override
     38     protected void onCreate(Bundle savedInstanceState) {
     39         super.onCreate(savedInstanceState);
     40         setContentView(R.layout.action_bar_display_options);
     41 
     42         findViewById(R.id.toggle_home_as_up).setOnClickListener(this);
     43         findViewById(R.id.toggle_show_home).setOnClickListener(this);
     44         findViewById(R.id.toggle_use_logo).setOnClickListener(this);
     45         findViewById(R.id.toggle_show_title).setOnClickListener(this);
     46         findViewById(R.id.toggle_show_custom).setOnClickListener(this);
     47         findViewById(R.id.toggle_navigation).setOnClickListener(this);
     48         findViewById(R.id.cycle_custom_gravity).setOnClickListener(this);
     49 
     50         mCustomView = getLayoutInflater().inflate(R.layout.action_bar_display_options_custom, null);
     51         // Configure several action bar elements that will be toggled by display options.
     52         final ActionBar bar = getActionBar();
     53         bar.setCustomView(mCustomView,
     54                 new ActionBar.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
     55 
     56         bar.addTab(bar.newTab().setText("Tab 1").setTabListener(this));
     57         bar.addTab(bar.newTab().setText("Tab 2").setTabListener(this));
     58         bar.addTab(bar.newTab().setText("Tab 3").setTabListener(this));
     59     }
     60 
     61     @Override
     62     public boolean onCreateOptionsMenu(Menu menu) {
     63         getMenuInflater().inflate(R.menu.display_options_actions, menu);
     64         return true;
     65     }
     66 
     67     public void onClick(View v) {
     68         final ActionBar bar = getActionBar();
     69         int flags = 0;
     70         switch (v.getId()) {
     71             case R.id.toggle_home_as_up:
     72                 flags = ActionBar.DISPLAY_HOME_AS_UP;
     73                 break;
     74             case R.id.toggle_show_home:
     75                 flags = ActionBar.DISPLAY_SHOW_HOME;
     76                 break;
     77             case R.id.toggle_use_logo:
     78                 flags = ActionBar.DISPLAY_USE_LOGO;
     79                 break;
     80             case R.id.toggle_show_title:
     81                 flags = ActionBar.DISPLAY_SHOW_TITLE;
     82                 break;
     83             case R.id.toggle_show_custom:
     84                 flags = ActionBar.DISPLAY_SHOW_CUSTOM;
     85                 break;
     86 
     87             case R.id.toggle_navigation:
     88                 bar.setNavigationMode(
     89                         bar.getNavigationMode() == ActionBar.NAVIGATION_MODE_STANDARD
     90                                 ? ActionBar.NAVIGATION_MODE_TABS
     91                                 : ActionBar.NAVIGATION_MODE_STANDARD);
     92                 return;
     93             case R.id.cycle_custom_gravity:
     94                 ActionBar.LayoutParams lp = (ActionBar.LayoutParams) mCustomView.getLayoutParams();
     95                 int newGravity = 0;
     96                 switch (lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
     97                     case Gravity.LEFT:
     98                         newGravity = Gravity.CENTER_HORIZONTAL;
     99                         break;
    100                     case Gravity.CENTER_HORIZONTAL:
    101                         newGravity = Gravity.RIGHT;
    102                         break;
    103                     case Gravity.RIGHT:
    104                         newGravity = Gravity.LEFT;
    105                         break;
    106                 }
    107                 lp.gravity = lp.gravity & ~Gravity.HORIZONTAL_GRAVITY_MASK | newGravity;
    108                 bar.setCustomView(mCustomView, lp);
    109                 return;
    110         }
    111 
    112         int change = bar.getDisplayOptions() ^ flags;
    113         bar.setDisplayOptions(change, flags);
    114     }
    115 
    116     public void onTabSelected(Tab tab, FragmentTransaction ft) {
    117     }
    118 
    119     public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    120     }
    121 
    122     public void onTabReselected(Tab tab, FragmentTransaction ft) {
    123     }
    124 }
    125