アプリ制作実習 〜アクションバーアイコンをいじってみた〜

こんばんは、haneoです。

今回は製作中のアプリにアクションバーのアイコンをつけてみたので紹介します。

まずアクションバーってどんなの?と言うと

画面の上部についているこれです。
androidのバージョン3.0からつけられるようになったのですが、
今までアプリの持つ機能はメニューボタンを押して何が出来るのか探していたので
その画面で何が出来るか、アイコンで何となくわかるというのは便利ですね。

実装はonCreateOptionsMenu()というメソッドで
メニューのxmlファイルを呼んで、項目を作り
onOptionsItemSelected()というメソッドでそれぞれのボタンに機能を作って行く感じです
メニュー項目生成

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.album, menu);
		return true;
	}

画面を移動する機能をつけます。

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case android.R.id.home:
	            Intent intent = new Intent(this, HomeActivity.class);
	            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	            startActivity(intent);
	            return true;
	        case R.id.menu_edit:
	        	Intent intentE = new Intent(this, AlbumEditActivity.class);
	        	startActivity(intentE);
	        	return true;
	        case R.id.menu_add_new:
	        	Intent intentA = new Intent(this, PhotoNewActivity.class);
	        	startActivity(intentA);
	        	return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}

アイコンは結構種類があり、自分で作って足すことも可能なので直感的に
機能を表すアイコンを選びましょう。
好きな白黒画像からオリジナルのアイコンを作ってくれるツールもあるみたいですよ。