Viewを動的に切り替える
はじめに
ボタンを押すと画面のビューを別のものに切り替えたい場合があります。
そのときはgetLayoutInflater().inflate(int ResourceId, ViewGroup root)を使えばいいです。
// 変更したいレイアウトを取得する LinearLayout layout = (LinearLayout)findViewById(R.id.layout); // レイアウトのビューをすべて削除する layout.removeAllViews(); // レイアウトをR.layout.sampleに変更する getLayoutInflater().inflate(R.layout.sample, layout);
サンプルの作成
それではボタンを押すとその下の赤い部分が青に変更するプログラムを作成します。
起動したときは赤
クリックすると青に変更
切替元レイアウトを指定する
はじめにactivity_main.xmlに変更するレイアウトを作成し、idを設定する
<LinearLayout android:id="@+id/layout" android:background="#FF0000" ... ><!-- 赤色 -->
切替後のレイアウトを作成する
切替後のレイアウトは背景(background)が青のものを作成します
sample.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#0000FF" android:orientation="vertical" > </LinearLayout>
ボタンを押したときの処理を記述する
今回はonCreate()にボタンを押すとレイアウトが切り替えるような処理を記述します。
// ボタンを押したときにイベントを取得できるようにする Button button1 = (Button)findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { /* * 一部のビューだけを変更する */ // 変更したいレイアウトを取得する LinearLayout layout = (LinearLayout)findViewById(R.id.layout); // レイアウトのビューをすべて削除する layout.removeAllViews(); // レイアウトをR.layout.sampleに変更する getLayoutInflater().inflate(R.layout.sample, layout); } });
ソースコード
MainActivity.java
package com.example.blogsample; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setTitle("View切替のサンプル"); // ボタンを押したときにイベントを取得できるようにする Button button1 = (Button)findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { /* * 一部のビューだけを変更する */ // 変更したいレイアウトを取得する LinearLayout layout = (LinearLayout)findViewById(R.id.layout); // レイアウトのビューをすべて削除する layout.removeAllViews(); // レイアウトをR.layout.sampleに変更する getLayoutInflater().inflate(R.layout.sample, layout); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="Click!" /> <LinearLayout android:id="@+id/layout" android:background="#FF0000" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/button1" android:orientation="vertical" > </LinearLayout> </RelativeLayout>
sample.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#0000FF" android:orientation="vertical" > </LinearLayout>
リソースからDrawableを作成する方法
getResources()を使えばよい
Drawable drawableMap = getResources().getDrawable( android.R.drawable.ic_dialog_map );
カメラアプリ、setPreviewSize()でエラーが発生する件
Androidでカメラアプリのサンプルを作成したときに parameters.setPreviewSize(width, height)
でエラーが発生したので調べてみました。
773: Androidでカメラを使う(1)
によれば、setPreviewSize()が廃止になったので
getSupportedPreviewSizes()を使ってくださいとのことです。
@Override public void surfaceChanged(SurfaceHolder holder, int f, int w, int h) { Camera.Parameters p = camera.getParameters(); List<Size> previewSizes = camera.getParameters().getSupportedPreviewSizes(); Size size = previewSizes.get(0); p.setPreviewSize(size.width, size.height); camera.startPreview(); }
AndroidでGoogle Mapを表示するサンプル
今回でハマったのがGoogle Map APIがV2になっているので
ネット上にある日本語情報では古い情報で使えなかったことだ。
いまのAPIではキーコードをManifestファイルに書き込めはいい。
注意する点はアクセス権限を忘れずに記述すること
MainActivity.java
package com.example.googlemapsample02; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.view.Menu; public class MainActivity extends FragmentActivity { private GoogleMap mMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setUpMapIfNeeded(); } @Override protected void onResume() { super.onResume(); setUpMapIfNeeded(); } private void setUpMapIfNeeded() { if (mMap == null) { mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); } if (mMap != null) { setUpMap(); } } private void setUpMap() { mMap.addMarker(new MarkerOptions().position(new LatLng(0,0)).title("Marker")); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.SupportMapFragment"/>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.googlemapsample02" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> <!-- External storage for caching. --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <!-- My Location --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="あなたのAPIキー"/> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <activity android:name="com.example.googlemapsample02.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Google Service Libraryでのエラー解決方法
Google Mapを使ってアプリを作りたいからGoogle Service Libraryを使ってみようとしたら以下のエラーが発生した。
Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace.
ネットで原因を探ってみると以下のサイトがあった。
自堕落な技術者の日記 : Android SDK 19だかJSONだかAdMobだかzipalignだかでハマった話 - livedoor Blog(ブログ)
Androidのツールに問題がありそうなので、このサイト主はバージョンを戻しているけど、個人的にはバージョンを戻したくないので、アップデートしてみた。
Android SDK ManagerでAndroid Build-Tools を 19 から 19.0.3 にアップデートして
ADTを再起動したら見事ビルドが通った。
ストア以外から入手した野良apkをインストールする
ネットを徘徊していたら激怒ぷんぷん丸という言葉からゲーム化にまでしていたのでそこで見つけたapkファイルを動かすために、ストア以外でインストールする方法を調べてみました。
激おこぷんぷん丸iPhone版 | Who made him angry!?
1.「提供元不明のアプリ」のインストールを許可
[設定] -> [セキュリティ] -> [提供元不明のアプリ]にチェックをする
このチェックがないとストアなどで配布されるアプリ以外はインストールできないのでチェックを外します。
2. Google Drive経由でインストール
「提供元不明のアプリ」にチェックをすればインターネット上からもインストールできます。なのではじめはGMail経由でやろうとしたけど、apkファイルは思ったよりもファイルサイズが大きいのでGoogle Drive経由でインストールしました。
実際ゲームは動いたんですが、ソースコードがHSPからC++に変換されたソースコードのみでした。
HSPには詳しくないのでどうやってiOS版やAndroid版が動いているのかはもう少し調べる必要がありそうです。