7. 開発環境
本トレーニングでは下記の開発環境で行う
ソフトウェア
バージョン
統合開発環境
Eclipse IDE with built-in ADT
Java SDK
JDK 1.6.X
Android SDK
Android 4.2.2(API 17)
Android Plug-in
Android Development Tools (ADT) Ver.21.1.0
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
7
23. Android 3.x for Users
タブレット向けに一からデザインされた新たなUI
カスタマイズ可能なホーム画面
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
23
24. Android 3.x for Users
Quick Controls menu in the Browser
より快適な操作が可能になる
Home screen widgets can now be resized
カスタマイズをより柔軟に行える
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
24
30. Android 4.0 for Users
Google+のハングアウトと連携することが
可能
パノラマカメラの標準
搭載
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
30
31. Android 4.0 for Users
通信データの使用量の確認と上限設定を
アプリ毎に設定可能
NFCによるデータ通信をサポートします。
URLを転送したりすることが可能
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
31
33. 概要
New Developer Features(開発者向け新機能)
Visual Styles(ユーザインタフェースの改善)
UI Pattern Best Practice(UI プログラミング ベストプラクティス
)
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
33
34. New Developer Features(1)
多くの機能は3.xから使用
Honeycomb APIのハンドセット対応
Action Bar
System Bar
Fragment
Loader
PopupMenu
Drag and Drop
Copy and Paste
USB Host and Accessory
Richer Notifications
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
34
35. New Developer Features(2)
System Bar
タブレットでの呼び方
Navigation Bar
ハンドセットでの呼び方
タブレットでは時計やバッテリー残
量などが表示されているためナビ
ゲーション以外の機能があるので名
前を使い分けている
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
35
36. New Developer Features(3)
Recent Apps
Redesigned UI widgets
Widgetを重ねたり、グリッド表
示したり、サイズ変更にも対応
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
36
37. New Developer Features(4)
Rich Notifications
UIにボタンを持つことができる
ユーザが受信したメッセージを常に呼び出すことができ音楽の再生、ア
プリケーションからのリアルタイムのアップデートを確認できる
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
37
43. Resource Qualifiers
従来のqualifiers
xlarge (960dp x 720dp)
large (640dp x 480dp)
normal (470dp x 320dp)
small (426dp x 320dp)
※これだけだと携帯電話とタブレットを切り分けられないというケースもでてきた
新qualifiers
Smallest Width
Available width/height
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
43
48. UI Patterns Best Practices
UI Pattern
Honeycomb以降に追加されたUI機能の実装におけるベストプラクティス
Action Bar
Multi-Pane Layouts
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
48
49. Action Bar : Parts(1)
Action Bar
ICSからは小さい画面でも快適に使えるように改良
3つの要素で構成
App Icon
View Details
Action Items
Action Mode
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
49
50. Action Bar : Parts(2)
App Icon
アプリケーションのアイコンを表示する領域
アプリケーションのホームアクティビティに遷移するように実装する
アプリが階層構造の場合は上に行くように実装することも可
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
50
51. Action Bar : Parts(3)
View Details
シンプルなテキストを置く
タブやDropDownメニュー、検索ボックス等を配置する
Navigation機能
View Detailsを使ってアプリのどの階層にいるのかを表現する
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
51
52. Action Bar : Parts(4)
Action Items
従来のメニューと同等の機能として実装する
どの機能を持っているのかをボタンの形などで表現可能
2.x まではオプションメニューとして、3.x からはハードキーの代わりに
使用する
新プロパティの追加
android:showAsAction="ifRoom"
※詳細は後述
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
52
53. Action Bar : New Functions
Action Bar
3つの新機能
Stacked Action Bar
Split Action Bar
Action Provider
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
53
54. Action Bar : Design(1)
Stacked Action Bar
タブを表示するスペースが無いときは2段目に移動
デフォルトの挙動
プログラムの改修は不要
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
54
55. Action Bar : Design(2)
Split Action Bar
場所がない場合は画面の下部にでてくる
android:uiOptions="splitActionBarWhenNarrow"
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
55
56. Action Bar : Menu(1)
Action Provider
MenuItemの処理を共通化する
onOptionsItemSelectedを実装する必要がない
1ファイル当たりのステップ数が短くなる
Action Provider
ActivityA
ActivityB
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
56
57. Action Bar : Menu(2)
Action Provider
実装方法
ActionProviderのサブクラスを作成する
メニューの表示は以下の2つの方法がある
• menuリソースにandroid:actionProviderClassプロパティを追加
• onCreateOptionsMenuメソッドのオーバライド
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
57
58. public class MyActionProvider extends ActionProvider {
Context context;
public MyActionProvider(Context context) {
super(context);
this.context = context;
}
@Override
public View onCreateActionView() {
return null;
}
@Override
public boolean onPerformDefaultAction() {
Toast.makeText(context, "onPerformDefaultAction", Toast.LENGTH_SHORT).show();
return super.onPerformDefaultAction();
}
}
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/myprovider"
android:actionProviderClass="com.example.actionprov.MyActionProvider"
android:showAsAction="ifRoom"
android:title="Prov">
</item>
</menu>
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
59. Action Bar : Menu(3)
Action Provider
長所
処理の共通化により管理がしやすい
Activityにロジックを記述する必要がない
短所
ICS以降のため、下位互換できない
Activityで定義しているメソッド、変数にアクセス出来ない
MenuItem単位でActionProviderクラスが必要になるためファイルの総数は増
加する
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
59
60. Action Bar : ActionMode
ActionMode
item選択時はActionModeになることができる。
テキスト選択やコピー、ドラッグアンドドロップなどに用いる
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
60
61. UI Patterns : Multi-Pane Layout
Maluti-Pane Layout
画面の狭い端末では複数画面に分けて表示し、画面の広い端末では画面
を分割して表示する
Fragmentを使用して画面サイズに応じてマルチペインとシングルペイン
を切り替えが可能
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
61
75. Multi-Pane Layout : Implementation(
9)
FragmentTransaction
FragmentManager#beginTransactionメソッドで取得する
ActivitiyにFragmentを追加、除去、交換、表示、非表示などの操作を行
うことができる
commitメソッドを使って操作を完了する
beginTransactionからcommitの間にFragmentの追加、除去、交換などの
一連の処理が1つのトランザクションとして実行される
Fragmentの操作するための一連の処理
トランザクション
begin
hid
e
remov
e
ad
d
etc...
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
comm
it
75
76. Multi-Pane Layout :
Implementation(10)
FragmentTransactionの主なメソッド
メソッド
説明
add(int containerViewId
,Fragment fragment, String tag)
Fragmentを追加する
remove(Fragment fragment)
Fragment を削除
replace(Fragment fragment, String
tag)
container にすでに追加されている
Fragment を置き換える
show(Fragment fragment)
hidden 状態になっている Fragment
(の View)を表示する
hide(Fragment fragment)
存在している Fragment を隠す
(hidden 状態にする)
引数
int : FragmentのコンテナID(省略可)
Fragment :追加するFramgnet
String :Fragment設定するTag
使わない場合はnullを指定(省略可)
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
76
85. Multi-Pane Layout実習(3)
手順
1. Fragmentの準備
FirstFragmentの作成
SecondFragmentの作成
2. レイアウトリソースの準備
/res/layout/fragment_first.xml
/res/layout/fragment_second.xml
/res/layout-sw600dp/activity_main.xml
3. Fragmentの処理の実装
FirstFragmentの修正
SecondFragment.javaの修正
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
85
86. Multi-Pane Layout実習(4)
1. Fragmentの準備
FirstFragmentの作成
SecondFragmentの作成
※まだ処理は実装しない
package com.example.multipanesample;
import android.app.Fragment;
public class FirstFragment extends Fragment {
}
package com.example.multipanesample;
import android.app.Fragment;
public class SecondFragment extends Fragment {
}
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
86
87. Multi-Pane Layout実習(5)
2. レイアウトリソースの準備
/res/layout/fragment_first.xml
View
Property
階層構造
LinearLayout
<LinearLayout>
<TextView />
<TextView />
</LinearLayout>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
android:gravity="center"
android:orientation="vertical”
TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FirstFragment"
android:textAppearance="?android:attr/textAppearanceLarge”
TextView
android:id="@+id/text_no"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content"
style="@android:drawable/list_selector_background"
android:text="1"
android:clickable="true"
android:textSize="200dp"
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
87
88. Multi-Pane Layout実習(6)
2. レイアウトリソースの準備
/res/layout/fragment_second.xml
View
Property
階層構造
LinearLayout
<LinearLayout>
<TextView />
<TextView />
</LinearLayout>
android:layout_width="match_parent"
android:gravity="center"
android:background="#00ff00"
android:layout_height="match_parent"
android:orientation="vertical"
TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SecoundFragment"
android:textAppearance="?android:attr/textAppearanceLarge”
TextView
android:id="@+id/text_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:textSize="200dp"
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
88
89. Multi-Pane Layout実習(7)
2. レイアウトリソースの準備
/res/layout-sw600dp/activity_main.xml
View
階層構造
<LinearLayout>
<fragment />
<FrameLayout />
</LinearLayout>
Property
LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
fragment
android:id="@+id/fragment1"
android:name="com.example.multipanesample.FirstFragme
nt"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
FrameLayout
android:id="@+id/second_fragment_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
89
90. Multi-Pane Layout実習(8)
2. Fragmentの処理の実装
FirstFragmentの修正
• onCreateViewメソッドをオーバライドする
• レイアウトファイルとしてfragment_first.xmlを指定する
public class FirstFragment extends Fragment {
private static final String TAG = "FirstFragment";
TextView textView;
TextView textNo;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_first, null);
textView = (TextView) v.findViewById(R.id.textView1);
textView.setText(TAG);
・
・
・
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
90
91. Multi-Pane Layout実習(9)
3. Fragmentの作成
FirstFragmentの修正(続き)
• 遷移ロジックの実装
• No表示用のTextViewにonClickイベントを追加する
• onClickイベントにSecondFragmentの起動処理を追加する
FirstFragment.java
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
〜略〜
textNo = (TextView) v.findViewById(R.id.text_no);
textNo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
SecondFragment fragment = new SecondFragment();
transaction.add(R.id.second_fragment_container, fragment);
transaction.commit();
}
・・
・2
});
return v;
}
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
91
92. Multi-Pane Layout実習(10)
3. Fragmentの作成
SecondFragmentの修正
• onCreateViewメソッドをオーバライドする
• レイアウトファイルとしてfragment_second.xmlを指定する
public class SecondFragment extends Fragment {
private static final String TAG = "SecondFragment";
private TextView textView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.v(TAG, new Throwable().getStackTrace()[0].getMethodName());
View v = inflater.inflate(R.layout.fragment_second, container, false);
textView = (TextView) v.findViewById(R.id.textView1);
textView.setText(TAG);
return v;
}
}
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
92
95. Multi-Pane Layout実習[補足]
実習 [補足 1]
ハンドセット対応にする
ハンドセット:2画面
タブレット:Multi-Pane
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
95
99. ステップアップ手順(1)
5つのStepでアプリケーションを完成させる
Step 1 Action Bar
Action Barの作成
Action Barのカスタマイズ
Action Itemsの作成
Step 4 Fragmentの連携
FragmentとFragmentの連携
FragmentとActivityの連携
Step 5 その他の機能拡張
Step 2 Fragment
ContentFragmentの追加
System Bar, Action Barの表示
制御
ActionMode対応
Step 3 ListFragment
ListFragmentを使ってタイト
ル一覧を表示する
ListFragmentをカスタマイズ
する
Fragmentの表示・非表示
DialogFragment
Notification
Configuration対応
テーマの動的切り替え
※テーマの動的切り替えはタブレット新機
能を使わないため本トレーニングでは省略
する
各自持ち帰り課題とし資料を参考に仕上げ
ること
This material is licensed under the Creative Commons License BY-NC-SA 4.0.
99
100. ステップアップ手順(2)
初期状態
Step 1 Action Bar
Step 3 ListFragment
Step 4 Fragmentの連携
Step 2 Fragment
Step 5 その他の新機能
This material is licensed under the Creative Commons License BY-NC-SA 4.0.
100
111. Action Barの構成要素
Action Barの構成
App Icon
アプリケーションのタイトルやアイコンを表示する
クリックすることが可能
• クリック後はホームアクティビティに戻るようにするのが一般的
View Details
アプリケーションのナビゲーションを担当する
• タブナビゲーション
• ドロップダウンナビゲーション
SearchViewなどの検索バーを表示することも可能
Action Items
従来のオプションメニュー相当
App Icon
View Details
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
Action Items
111
112. Action Barを表示する
Action Barの使い方
Action Barの表示
Honeycombからはデフォルトで追加される。
API Lvを11以上に設定すれば、プログラムの修正やリソースの追加・変更は
不要
AndroidManifest.xml
<uses-sdk android:minSdkVersion=”11" />
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
112
113. Action Barを操作する
Action Barの使い方
Action Barの取得
プログラムからAction Barを操作するにはActivity#getActionBarメソッドを使
う
取得したActionBarオブジェクトからAction Barのデザインを変更することが
できる
ActionBarを取得する
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActionBar actionBar = getActionBar();
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
113
114. App Icon
Action Bar : App Icon
App Icon領域でhomeアイコン、タイトルの表示設定する
android:icon=“@drawable/ic_luncher”
android:logo=“@drawable/logo”
homeアイコン:App Icon領域に表示されるアプリケーションアイコン
iconリソースを使用
homeアイコンにロゴを指定
非表示に設定
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
114
115. App Iconのカスタマイズ(1)
Action Barのカスタマイズ:App Icon
ActionBar#setDisplayOptionsで引数にAction Barの表示モードを定数で
指定する
各設定値は|でつなげることができる
設定値
説明
サンプル(ApiDemo>Action Bar>Display
Options)
指定なし
ロゴとタイトルが表示さ
れる
DISPLAY_HOME_AS_U
P
UP矢印”<”が左側に表示
する
DISPLAY_SHOW_CUST
OM
カスタムビューを使用す
る
DISPLAY_SHOW_HOME
homeアイコンを表示す
る
DISPLAY_SHOW_TITLE
タイトルを表示する
DISPLAY_USE_LOGO
homeアイコンにロゴを
This
使用する material is licensed under the Creative
Commons License BY-NC-SA 4.0.
115
116. App Iconのカスタマイズ(2)
Action Barのカスタマイズ:App Icon
setDisplayOptionsの設定例 1
homeアイコンの表示
タイトルの表示
UP矢印の表示
ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME
| ActionBar.DISPLAY_SHOW_TITLE
| ActionBar.DISPLAY_HOME_AS_UP);
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
116
117. App Iconのカスタマイズ(3)
Action Barのカスタマイズ:App Icon
setDisplayOptionsの設定例 2
ロゴの表示
• android:logoにリソースIDを指定する
• homeアイコンと合わせて表示することはできない
タイトルの表示
UP矢印の表示
画像リソース(ic_home.png, apidemo_androidlogo.png)
AndroidManifest.xml
<activity
android:name=".ActionBarExampleActivity"
android:icon="@drawable/ic_home"
android:label="@string/app_name"
This material is licensed under the Creative
android:logo="@drawable/apidemo_androidlogo" >
Commons License BY-NC-SA 4.0.
117
118. App Iconのカスタマイズ(4)
setDisplayOptionsの設定例 2 続き
Activityソース
※DisplayOptionを設定しなくてもロゴは優先されるが、UP矢印も表示するため
以下の様になる
ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(
ActionBar.DISPLAY_SHOW_TITLE
| ActionBar.DISPLAY_SHOW_HOME ← HOME表示フラグが必要
| ActionBar.DISPLAY_USE_LOGO
| ActionBar.DISPLAY_HOME_AS_UP);
実行結果
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
118
119. App Iconのカスタマイズ(5)
setDisplayOptionsの設定例 2 (別の方法)
デフォルトの設定をそのままでUP矢印のフラグだけONにする
引数2つのActionBar#setDisplayOptions(int options, int mask)を使用する
• int options: 設定フラグ
• int mask: 変更対象のフラグ
Activityソース
ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP ,
ActionBar.DISPLAY_HOME_AS_UP);
実行結果
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
119
120. Action Barの作成:App Icon(
1)
実習 1
homeアイコンにロゴを表示する
アプリ一覧のアイコンを変更する
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
120
121. Action Barの作成:App Icon(
2)
手順
1. AndroidManifest.xmlの修正
アプリアイコンとロゴの設定
2. MainActivityの修正
Action Barの表示設定
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
121
122. Action Barの作成:App Icon(
3)
1. AndroidManifest.xmlの修正
applicationタグの設定を以下のように変更する
プロパティ
値
android:icon
@drawable/icon
android:logo
画像
@drawable/logo
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:logo="@drawable/logo" >
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
122
123. Action Barの作成:App Icon(
4)
2. MainActivityの修正
Action Barの表示設定
• onCreateメソッドにAction BarのIcon設定処理を実装する
– homeアイコンにロゴを設定する
– タイトルの非表示
• setDisplayOptions引数に以下の値を設定する
– DISPLAY_SHOW_HOME
– DISPLAY_USE_LOGO
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Create ActionBar
ActionBar bar = getActionBar();
bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME
| ActionBar.DISPLAY_USE_LOGO);
}
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
123
139. Action Barの作成:Action Items(
1)
実習 3
Action BarにAction Itemsを表示する
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
139
140. Action Barの作成:Action Items(
2)
手順
1. menuリソースの作成
/res/menu/main_menu.xml
2. MainActivityの修正
Activityに以下のメソッドをオーバライドする
• onCreateOptionsMenu
• onOptionsItemSelected
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
140
141. Action Barの作成:Action Items(
3)
1. menuリソースの作成
/res/menu/main_menu.xml
• <item>タグに以下の要素を設定
表示名称
プロパティ
Camera
android:id="@+id/camera"
android:icon="@android:drawable/ic_men
u_camera"
android:showAsAction="ifRoom"
android:title="Camera”
Toggle Titles
android:id="@+id/toggleTitles"
android:icon="@drawable/ic_menu_toggle
_holo_dark"
android:showAsAction="ifRoom|withText"
android:title="Toggle Titles"
Day/Night
android:id="@+id/toggleTheme"
android:showAsAction="never"
android:title="Day/Night"
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
141
142. Action Barの作成:Action Items(
4)
1. menuリソースの作成(続き)
表示名称
プロパティ
Show a dialog
android:id="@+id/showDialog"
android:showAsAction="never"
android:title="Show a dialog"
Show a basic nitification
android:id="@+id/showStandardNotificatio
n"
android:showAsAction="never"
android:title="Show a basic nitification”
Show a custom
notification
android:id="@+id/showCustomNotification
"
android:showAsAction="never"
android:title="Show a custom notification"
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
142
143. Action Barの作成:Action Items(
5)
2. MainActivityの修正
Activityに以下のメソッドをオーバライドする
• onCreateOptionsMenu
• onOptionsItemSelected
※onOptionItemSelectedは空実装し、アイテム選択時の処理は行わない
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
143
144. Action Barの作成:Action Items(
確認)
確認
Action BarにActiomItemsが表示されていること
カメラはアイコンだけ表示されていること
ToggleTitlesにアイコンとタイトルが表示されていること
その他のアイテムがオーバフローメニューに収まっていること
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
144
152. Content Fragmentを作成する(
5)
2. Fragmentの作成
イメージを表示するためのContentFragmentを作成する
onCreateViewのオーバライド
• レイアウトにcontent_welcome.xmlに指定する
• ImageViewに表示するための画像を設定
– ※ここでは一時的な表示画像として「big_droid」を指定する。今後
の実習で設定画像の動的変化に対応する
public class ContentFragment extends Fragment {
private View contentView;
// The bitmap currently used by ImageView
private Bitmap bitmap = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
contentView = inflater.inflate(R.layout.content_welcome, null);
final ImageView imageView = (ImageView) contentView.findViewById(R.id.image);
// TODO change image resource. this operation will be deleted later.
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.big_droid);
imageView.setImageBitmap(bitmap);
return contentView;
}
}
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
152
153. Content Fragmentを作成する(
6)
3. main.xmlの修正
以下のように修正する
• デフォルトで記述されているTextViewは削除する
main.xmlの構成
• ルート要素:LinearLayout
• LinearLayoutの子要素:fragment
階層構造
<LinearLayout>
<fragment />
</LinearLayout>
View
properties
LinearLayout
(修正)
android:id="@+id/flags"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
fragment
(追加)
android:id="@+id/frag_content"
android:name="com.example.android.shcgallery.Conte
ntFragment"
android:layout_width="match_parent"
android:layout_height="match_parent”
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
153
156. System Bar, Action Barの制御
System Bar, Action Barの制御
System BarやAction Barの表示状態を制御することができる
System Barの表示状態を同期して、Action Barの表示状態を制御するこ
とも可能
非表示状態のSystem Bar, Action Bar
This material is licensed under the Creative Commons License BY-NC-SA 4.0.
156
157. System Barの制御(1)
System Barの表示状態を制御する
View#setSystemUiVisibilityメソッドを使って、System Barの表示状態を
制御する
setSystemUiVisibilityの引数にSystem Barの定義済の表示状態定数を使い
、表示、非表示を設定する
メソッド
戻り
値
メソッド
説明
void
setSystemUiVisibility (int
visibility)
System Barの表示状態を制御す
る
引数
•
int visibility 表示定数
引数に指定する定数
定数
説明
STATUS_BAR_VISIBLE
表示
STATUS_BAR_HIDDEN
非表示
This material is licensed under the Creative Commons License BY-NC-SA 4.0.
157
158. System Barの制御(2)
表示状態定数Tips
API Level 14より、STATUS_BAR_VISIBLE、STATUS_BAR_HIDDENは
非推奨となっています。
API Level 14以降では以下の定数を使用します
STATUS_BAR_VISIBLE → SYSTEM_UI_FLAG_VISIBLE
STATUS_BAR_HIDDEN → SYSTEM_UI_FLAG_LOW_PROFILE
※本トレーニングでどちらの値を使用するかは講師の指示に従ってください
定数
説明
STATUS_BAR_VISIBLE
表示(API Level 14より非推
奨)
STATUS_BAR_HIDDEN
非表示(API Level 14より非推
奨)
SYSTEM_UI_FLAG_VISIBLE
表示
SYSTEM_UI_FLAG_LOW_PR
OFILE
非表示(アイコンが”・”にな
る)
This material is licensed under the Creative Commons License BY-NC-SA 4.0.
158
159. System Barの制御(3)
サンプル
表示
// System Barの表示
contentView.setSystemUiVisibility(View.STATUS_BAR_VISIBLE);
// System Barの表示(API Lv14以降)
contentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
非表示
// System Barの非表示
contentView.setSystemUiVisibility(View.STATUS_BAR_HIDDEN);
// System Barの表示(API Lv14以降)
contentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
This material is licensed under the Creative Commons License BY-NC-SA 4.0.
159
160. System Bar, Action Barの同期(1)
System Bar表示状態と同期する方法
System Barは表示状態の変更イベントを受け取ることにより、System
Barの表示状態に合わせて処理を同期させることができる
変更イベントは以下のAPIを使用する
setOnSystemUiVisibilityChangeListener
• 変更通知先を設定する
• 引数:OnSystemUiVisibilityChangeListener
OnSystemUiVisibilityChangeListener
• 変更通知を受け取るためのリスナ
イベント発
生
System Barの
表示状態が変
更
イベント通知
画面表示
同期処理
This material is licensed under the Creative Commons License BY-NC-SA 4.0.
160
161. System Bar, Action Barの同期(2)
setOnSystemUiVisibilityChangeListener
戻り
値
メソッド
説明
void
setOnSystemUiVisibilityChangeListener
(View.OnSystemUiVisibilityChangeListen
er l)
System Barの可視性が変更されたときの
コールバックの通知先を指定する
引数
• OnSystemUiVisibilityChangeListener
コールバックの通知先
OnSystemUiVisibilityChangeListener
戻り
値
メソッド
説明
void
onSystemUiVisibilityChange(int visibility)
System Barの可視性が変更されたときに呼
び出される
引数
• int visibility
表示状態定数
This material is licensed under the Creative Commons License BY-NC-SA 4.0.
161
162. System Bar, Action Barの同期(3)
setSystemUiVisibilityのサンプル
contentView.setOnSystemUiVisibilityChangeListener(new OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
if (visibility == View.STATUS_BAR_VISIBLE) {
Toast.makeText(getActivity(), "SystemBar Visible", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), "SystemBar Hidden", Toast.LENGTH_SHORT).show();
}
}
});
This material is licensed under the Creative Commons License BY-NC-SA 4.0.
162
164. System Bar, Action Barの制御
(1)
実習 2
イメージクリック時にSystem BarとAction Barの表示状態を切り替える
クリック
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
164
165. System Bar, Action Barの制御
(2)
手順
ContentFragmentの修正
onCreateViewの修正
• System Bar表示制御
• System Barの表示状態と同期してAction Barの表示状態を制御
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
165
166. System Bar, Action Barの制御
(3)
ContentFragmentの修正
System Bar表示制御
• イメージクリック時にSystem Barの表示、非表示を切り替える
contentView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (contentView.getSystemUiVisibility() == View.STATUS_BAR_VISIBLE) {
contentView.setSystemUiVisibility(View.STATUS_BAR_HIDDEN);
} else {
contentView.setSystemUiVisibility(View.STATUS_BAR_VISIBLE);
}
}
});
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
166
167. System Bar, Action Barの制御
(4)
ContentFragmentの修正
System Barの表示状態と同期してAction Barの表示状態を制御
• FragmentにsetOnSystemUiVisibilityChangeListenerをセット
• onSystemUiVisibilityChangeメソッド内でSystem Barの状態に応じて、
Action Barの表示、非表示を切り替える
final Activity activity = getActivity();
contentView.setOnSystemUiVisibilityChangeListener(new OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
ActionBar actionBar = activity.getActionBar();
if (actionBar != null) {
if (visibility == View.STATUS_BAR_VISIBLE) {
actionBar.show();
} else {
actionBar.hide();
}
}
}
});
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
167
168. System Bar, Action Barの制御(確
認)
確認
イメージクリック時にSystem BarとAction Barの表示状態が変わること
を確認
クリック
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
168
169. System Bar, Action Barの制御(解
答)
解答
別ドキュメント参照
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
169
179. Action Mode対応(2)
手順
1. stringリソースの作成
2. Action Mode用のメニューリソースを作成
3. ContentFragmentの修正
メンバ変数の追加
• ActionMode
ActionMode.Callbackインタフェースの実装クラスの定義
Action Mode開始のメソッドを呼び出す
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
179
180. Action Mode対応(3)
1. stringリソースの作成
Name
Value
photo_selection_cab_title
Photo selection
2. Action Mode用のメニューリソースを作成
/res/menu/photo_context_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/share"
android:icon="@android:drawable/ic_menu_share"
android:showAsAction="always|withText"
android:title="Share">
</item>
</menu>
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
180
181. Action Mode対応(4)
3. ContentFragmentの修正
メンバ変数の追加
• ActionMode
// Current action mode
private ActionMode currentActionMode;
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
181
182. Action Mode対応(5)
3. ContentFragmentの修正(続き)
ActionMode.Callbackインタフェース実装クラスの定義
• 内部クラスで定義しメンバ変数として確保する
• 下記メソッドのオーバライド
– onCreateActionMode
– タイトルの設定
– Menuの組み立て処理
– onActionItemClicked
– アイテム選択時にToastを表示する
– メッセージ:Share item selected.
– Action Modeの終了
– onDestroyActionMode
– contentViewの選択解除
– currentActionModeの参照を外す
– onPrepareActionMode
– falseを返す
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
182
183. Action Mode対応(6)
ActionMode.Callbackインタフェース実装クラス(ソース)
ActionMode.Callback actionModeCallback = new ActionMode.Callback() {
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
contentView.setSelected(false);
currentActionMode = null;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.setTitle(R.string.photo_selection_cab_title);
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.photo_context_menu, menu);
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.share:
Toast.makeText(getActivity(), "Share item selected.", Toast.LENGTH_SHORT).show();
mode.finish();
return true;
}
return false;
}
};
This material is licensed under the Creative
Commons License BY-NC-SA 4.0.
183
184. Action Mode対応(7)
3. ContentFragmentの修正(続き)
onCreateViewメソッド内でAction Mode開始のメソッドを呼び出す
ロングクリックイベントを検知するためのリスナをセットする
• OnLongClickListener
ロングクリックイベントでAction Modeに切り替える
• startActionModeメソッドの呼び出し
• 引数にActionMode.Callbackインタフェースを指定
• 戻り値を取得し、メンバ変数にセット
// When long-pressing a photo, activate the action mode for selection, showing the
// contextual Action Bar (CAB).
contentView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (currentActionMode != null) {
return false;
}
currentActionMode = getActivity().startActionMode(actionModeCallback);
contentView.setSelected(true);
return true;
}
This material is licensed under the Creative
});
Commons License BY-NC-SA 4.0.
184