1.プロジェクトの作成
(1)AndroidStudioでプロジェクトを作成
Android開発環境構築を参照
(2)プロジェクトフォルダ
Projectツールウインドウで下記など、フォルダの見え方を変更できる
![](https://region-sys.com/wp-content/uploads/2021/08/image-181.png)
・Androidビュー:開発の必要なファイルのみを表示
![](https://region-sys.com/wp-content/uploads/2021/08/image-182.png)
・Projectビュー:エクスプローラーに表示されるファイルに近い表示
![](https://region-sys.com/wp-content/uploads/2021/08/image-183.png)
(3)Androidビューのフォルダの内容
・manifests
アプリの実行に必要な設定を記述する「AndroidManifests.xml」ファイルが格納されている。
・java
Androidアプリの開発では、アプリの画面構成を.xmlファイルに、処理を.javaファイルに記述する。このうち、処理をする.javaファイルを格納する。
・res
上記のアプリの画面構成の.xmlファイルなどを格納する。
resフォルダ内のサブフォルダとして、
drawble:画像を格納
layout:画面構成に関わる.xmlファイルを格納
mipmap:アプリのアイコンを格納
values:アプリで表示する固定文字列(strings.xml)、画面のスタイル(styles.xml)、色構成(colors.xml)の.xmlファイルを格納する。
2.Androidアプリの開発手順
Androidアプリはレイアウトファイル(.xmlファイル)とアクティビティファイル(.javaファイル)のペアで成り立っている。
①プロジェクトの作成
②strings.xmlファイルに表示文字列を記述
③レイアウトXMLファイルに画面構成を記述
④アクティビティなどの.javaファイルに処理を記述
⑤アプリを起動して、AVD(Android Virtual Device)などで動作確認する
の順で作成する。
3.ビューの作成
ビューの作成には、レイアウトとビューを使う。
・レイアウト:画面部品の配置を決める
・ビュー:画面部品そのもの
(1)レイアウト
主に使うレイアウトとして、
:画面部品を縦、横方向に並べて配置
:画面部品を相対的に配置
(2)ビュー部品
:文字列の表示
:テキストボックス
:ボタン
:ラジオボタン
:チェックボックス
:ドロップダウンリスト
:リスト表示
:スライダー
:レート値を表現
:ON/OFFを表現できるスイッチ (3)画面部品で使う主な属性
①android:id
アクティビティ内の画面部品を使うときのidを設定する
android:id=”@+id/(名前)”
②android:text
画面部品が表示されるとき、strings.xmlファイルに記述する文字列と紐づける名前を設定する
android:id=”@string/(名前)”
③android:layout_width/height
部品の幅と高さを設定する
④android:layout_margin/padding
部品の外側の余白と内側の余白を設定する
4.レイアウトの実装
(1)テキストビューとテキストボックス
![](https://region-sys.com/wp-content/uploads/2021/08/image-175.png)
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?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="#A1A9BA" android:orientation="vertical"> <TextView android:id="@+id/tvLabelInput" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:layout_marginTop="5dp" android:background="#ffffff" android:text="@string/tv_msg" android:textSize="25sp"/> <EditText android:id="@+id/etInput" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="25dp" android:layout_marginTop="5dp" android:background="#ffffff" android:inputType="text"/> </LinearLayout> |
strings.xml
1 2 3 4 |
<resources> <string name="app_name">画面部品サンプル</string> <string name="tv_msg">お名前を入力してください。</string> </resources> |
(2)チェックボタン
![](https://region-sys.com/wp-content/uploads/2021/08/image-176.png)
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?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="#A1A9BA" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#df7401" android:orientation="horizontal"> <CheckBox android:id="@+id/cbDrink" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="25dp" android:background="#ffffff" android:text="@string/cb_apple"/> <CheckBox android:id="@+id/cbFood" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ffffff" android:text="@string/cb_orange"/> </LinearLayout> </LinearLayout> |
strings.xml
1 2 3 4 5 6 |
<resources> <string name="app_name">画面部品サンプル</string> <string name="cb_apple">apple</string> <string name="cb_orange">orange</string> </resources> |
(3)ラジオボタン
![](https://region-sys.com/wp-content/uploads/2021/08/image-177.png)
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<?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="#A1A9BA" android:orientation="vertical"> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:background="#df7401" android:orientation="horizontal" android:paddingBottom="10dp" android:paddingTop="10dp"> <RadioButton android:id="@+id/rbMale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="25dp" android:layout_marginRight="25dp" android:background="#ffffff" android:text="@string/rb_male"/> <RadioButton android:id="@+id/rbFemale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ffffff" android:text="@string/rb_female"/> </RadioGroup> </LinearLayout> |
strings.xml
1 2 3 4 5 |
<resources> <string name="app_name">画面部品サンプル</string> <string name="rb_male">男</string> <string name="rb_female">女</string> </resources> |
(4)ドロップダウンリスト
![](https://region-sys.com/wp-content/uploads/2021/08/image-178.png)
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?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="#A1A9BA" android:orientation="vertical"> <Spinner android:id="@+id/spCurryList" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:entries="@array/sp_list" android:paddingBottom="5dp" android:paddingTop="5dp"/> <Button android:id="@+id/btSave" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/bt_save"/> </LinearLayout> |
strings.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<resources> <string name="app_name">画面部品サンプル</string> <string-array name="sp_list"> <item>算数</item> <item>理科</item> <item>国語</item> <item>社会</item> <item>英語</item> <item>図工</item> <item>家庭</item> </string-array> <string name="bt_save">保存</string> </resources> |
(5)リストビュー
![](https://region-sys.com/wp-content/uploads/2021/08/image-179.png)
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?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="#A1A9BA" android:orientation="vertical"> <ListView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#ffffff" android:entries="@array/lv_colorlist"/> </LinearLayout> |
strings.xml
1 2 3 4 5 6 7 8 9 10 11 12 |
<resources> <string name="app_name">画面部品サンプル</string> <string-array name="lv_colorlist"> <item>ホワイト</item> <item>レッド</item> <item>ホワイト</item> <item>イエロー</item> <item>グリーン</item> <item>ブルー</item> <item>ブラック</item> </string-array> </resources> |