5.2 別のレイアウトを利用するアプリケーション
[Application Name] MakingNewLayoutResource
[Company Domain] intro.f1.co.jp
[Activity Name] MakingNewLayoutResourceActivity
文字列リソースの修正
[フォルダ] res/values
[修正] strings.xml
<resources> <string name="app_name">MakingNewLayoutResource</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="text1">神田</string> <string name="text2">IT</string> <string name="text3">スクール</string> </resources>
画面レイアウトファイルの修正
[フォルダ] res/layout
[修正] new_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#FFFFFF" android:text="@string/text1" android:textColor="#000000" android:textSize="30sp" android:id="@+id/textView1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/text2" android:textColor="#FF0000" android:textSize="30sp" android:id="@+id/textView2" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#FFFFFF" android:text="@string/text3" android:textColor="#000000" android:textSize="30sp" android:id="@+id/textView3" /> </LinearLayout>
アクティビティファイルの修正
[フォルダ] java/jp.co.f1.intro.makingnewlayoutresource
[修正] MakingNewLayoutResourceActivity
package jp.co.f1.intro.makingnewlayoutresource; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; public class MakingNewLayoutResourceActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.new_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.menu_making_new_layout_resource, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }

5.3.1 リニアレイアウトを用いたアプリケーション
[Application Name] LayoutUsingLinearLayout
[Company Domain] intro.f1.co.jp
[Activity] LayoutUsingLinearLayoutActivity
文字列リソースの修正
[フォルダ] res/values
[修正] strings.xml
<resources> <string name="app_name">LayoutUsingLinearLayout</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="row1">1行目</string> <string name="row2">2行目</string> <string name="row3">3行目</string> <string name="row4">4行目</string> <string name="col1">1列目</string> <string name="col2">2列目</string> <string name="col3">3列目</string> <string name="col4">4列目</string> </resources>
画面レイアウトファイルの修正
[フォルダ] res/layout
[修正] activity_layout_using_linear_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".LayoutUsingLinearLayoutActivity"> <LinearLayout android:id="@+id/linearLayout1" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff0000" android:text="@string/row1" android:textColor="#000000" android:textSize="28sp" android:id="@+id/textView1" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#00ff00" android:text="@string/row2" android:textColor="#000000" android:textSize="28sp" android:id="@+id/textView2" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#0000ff" android:text="@string/row3" android:textColor="#000000" android:textSize="28sp" android:id="@+id/textView3" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#00ffff" android:text="@string/row4" android:textColor="#000000" android:textSize="28sp" android:id="@+id/textView4" /> </LinearLayout> <LinearLayout android:id="@+id/linearLayout2" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:background="#222222" android:text="@string/col1" android:textColor="#ffffff" android:textSize="28sp" android:id="@+id/textView5" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:background="#222222" android:text="@string/col2" android:textColor="#ffffff" android:textSize="28sp" android:id="@+id/textView6" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:background="#444444" android:text="@string/col3" android:textColor="#ffffff" android:textSize="28sp" android:id="@+id/textView7" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:background="#888888" android:text="@string/col4" android:textColor="#ffffff" android:textSize="28sp" android:id="@+id/textView8" /> </LinearLayout> </LinearLayout>

5.4.1 大きさの違うボタンを配置した時のアプリケーション
[Application Name] LayoutButtonDifferentSize
[Company Domain] intro.f1.co.jp
[Activity] LayoutButtonDifferentSizeActivity
文字列リソースの修正
[フォルダ] res/values
[修正] strings.xml
<resources> <string name="app_name">LayoutButtonDifferentSize</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="wrap_content">wrap_content</string> <string name="match_parent">match_parent</string> </resources>
画面レイアウトファイルの修正
[フォルダ] res/layout
[修正] activity_layout_button_different_size.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".LayoutButtonDifferentSizeActivity"> <LinearLayout android:id="@+id/linearLayout1" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FFCCFF"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/wrap_content" android:id="@+id/button1" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/match_parent" android:id="@+id/button2" /> </LinearLayout> <LinearLayout android:id="@+id/linearLayout2" android:orientation="vertical" android:layout_width="200dp" android:layout_height="wrap_content" android:background="#339900"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/wrap_content" android:id="@+id/button3" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/match_parent" android:id="@+id/button4" /> </LinearLayout> </LinearLayout>

5.5.1 テーブルレイアウトを使用したアプリケーション
[Application Name] LayoutUsingTableLayout
[Company Domain] intro.f1.co.jp
[Activity] LayoutUsingTableLayoutActivity
文字列リソースの修正
[フォルダ] res/values
[修正] strings.xml
<resources> <string name="app_name">LayoutUsingTableLayout</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="login">ログイン</string> <string name="user">ユーザーネーム</string> <string name="password">パスワード</string> </resources>
画面レイアウトファイルの修正
[フォルダ] res/layout
[修正] activity_layout_using_table_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".LayoutUsingTableLayoutActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="@string/login" android:textSize="20sp" /> <TableLayout android:id="@+id/tableLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:shrinkColumns="0" android:stretchColumns="1"> <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/user" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="text" /> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/password" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPassword" /> </TableRow> <TableRow android:id="@+id/tableRow3" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_span="2" android:text="@string/login" /> </TableRow> </TableLayout> </LinearLayout>

5.6.1 リレーティブレイアウトを使用したアプリケーション
[Application Name]LayoutUsingRelativeLayout
[Company Domain] intro.f1.co.jp
[Activity] LayoutUsingRelativeLayouteActivity
文字列リソースの修正
[フォルダ] res/values
[修正] strings.xml
<resources> <string name="app_name">LayoutUsingRelativeLayout</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="doing">今どうしてる?</string> <string name="length">140文字以内</string> <string name="clear">クリア</string> <string name="tweet">ツイート</string> </resources>
画面レイアウトファイルの修正
[フォルダ] res/layout
[修正] activity_layout_using_relative_layout.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" tools:context=".LayoutUsingRelativeLayoutActivity"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="@string/doing" /> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView1" android:hint="@string/length" android:inputType="text" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/editText1" android:text="@string/tweet" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/editText1" android:layout_toLeftOf="@+id/button1" android:text="@string/clear" /> </RelativeLayout>

5.7 <実習> 簡単アンケートアプリケーション
[Application Name] EnqueteInput
[Company Domain] intro.f1.co.jp
[Activity] EnqueteInputActivity
スピナーリソースの修正
[フォルダ] res/values
[修正] arrays.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="blood"> <item>A型</item> <item>B型</item> <item>AB型</item> <item>O型</item> </string-array> </resources>
文字列リソースの修正
[フォルダ] res/values
[修正] strings.xml
<resources> <string name="app_name">EnqueteInput</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="name">名前</string> <string name="blood">血液型</string> <string name="blood_prompt">血液型を選択</string> <string name="sex">性別</string> <string name="man">男性</string> <string name="woman">女性</string> <string name="hobby">趣味</string> <string name="travel">旅行</string> <string name="sports">スポーツ</string> <string name="outdoor">アウトドア</string> <string name="shopping">ショッピング</string> <string name="button">送信</string> </resources>
画面レイアウトファイルの修正
[フォルダ] res/layout
[修正] activity_enquete_input.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" tools:context=".EnqueteInputActivity" > <TableLayout android:id="@+id/table1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:shrinkColumns="0" android:stretchColumns="1"> <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/name" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="text" android:lines="1" /> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/sex" /> <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="@string/man" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/woman" /> </RadioGroup> </TableRow> <TableRow android:id="@+id/tableRow3" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/blood" /> <Spinner android:id="@+id/spinner1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:entries="@array/blood" android:prompt="@string/blood_prompt" android:spinnerMode="dialog" /> </TableRow> <TableRow android:id="@+id/tableRow4" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hobby" /> <TableLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <TableRow android:id="@+id/tableRow5" android:layout_width="wrap_content" android:layout_height="wrap_content" > <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/travel" /> <CheckBox android:id="@+id/checkBox2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/sports" /> </TableRow> <TableRow android:id="@+id/tableRow6" android:layout_width="wrap_content" android:layout_height="wrap_content" > <CheckBox android:id="@+id/checkBox3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/outdoor" /> <CheckBox android:id="@+id/checkBox4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/shopping" /> </TableRow> </TableLayout> </TableRow> </TableLayout> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/table1" android:text="@string/button" /> </RelativeLayout>
