First Android App
Create a Android Project
File > Android
Application Project
Android App Name
Eclipse Project Name
App Package Name Google play package is only
Running Your First App
Introduction directories and files in the Android
project
The first Android project architecture
http://developer.android.com/tools/projects/index.html
AndroidManifest.xml
<?xml	
  version="1.0"	
  encoding="utf-­‐8"?>	
  
<manifest	
  xmlns:android="http://schemas.android.com/apk/res/android"	
  
	
  	
  	
  	
  package="com.example.firstapp"	
  
	
  	
  	
  	
  android:versionCode="1"	
  
	
  	
  	
  	
  android:versionName="1.0"	
  >	
  
!
	
  	
  	
  	
  <uses-­‐sdk	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:minSdkVersion="8"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:targetSdkVersion="21"	
  />	
  
!
	
  	
  	
  	
  <application	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:allowBackup="true"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:icon="@drawable/ic_launcher"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:label="@string/app_name"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:theme="@style/AppTheme"	
  >	
  
	
  	
  	
  	
  	
  	
  	
  	
  <activity	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  android:name=".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>
AndroidManifest.xml
<?xml	
  version="1.0"	
  encoding="utf-­‐8"?>	
  
<manifest	
  xmlns:android="http://schemas.android.com/apk/res/android"	
  
	
  	
  	
  	
  package="com.example.firstapp"	
  
	
  	
  	
  	
  android:versionCode="1"	
  
	
  	
  	
  	
  android:versionName="1.0"	
  >	
  
!
	
  	
  	
  	
  <uses-­‐sdk	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:minSdkVersion="8"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:targetSdkVersion="21"	
  />	
  
!
	
  	
  	
  	
  <application	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:allowBackup="true"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:icon="@drawable/ic_launcher"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:label="@string/app_name"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:theme="@style/AppTheme"	
  >	
  
	
  	
  	
  	
  	
  	
  	
  	
  <activity	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  android:name=".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>
AndroidManifest.xml
<?xml	
  version="1.0"	
  encoding="utf-­‐8"?>	
  
<manifest	
  xmlns:android="http://schemas.android.com/apk/res/android"	
  
	
  	
  	
  	
  package="com.example.firstapp"	
  
	
  	
  	
  	
  android:versionCode="1"	
  
	
  	
  	
  	
  android:versionName="1.0"	
  >	
  
!
	
  	
  	
  	
  <uses-­‐sdk	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:minSdkVersion="8"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:targetSdkVersion="21"	
  />	
  
!
	
  	
  	
  	
  <application	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:allowBackup="true"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:icon="@drawable/ic_launcher"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:label="@string/app_name"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:theme="@style/AppTheme"	
  >	
  
	
  	
  	
  	
  	
  	
  	
  	
  <activity	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  android:name=".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>
AndroidManifest.xml
<?xml	
  version="1.0"	
  encoding="utf-­‐8"?>	
  
<manifest	
  xmlns:android="http://schemas.android.com/apk/res/android"	
  
	
  	
  	
  	
  package="com.example.firstapp"	
  
	
  	
  	
  	
  android:versionCode="1"	
  
	
  	
  	
  	
  android:versionName="1.0"	
  >	
  
!
	
  	
  	
  	
  <uses-­‐sdk	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:minSdkVersion="8"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:targetSdkVersion="21"	
  />	
  
!
	
  	
  	
  	
  <application	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:allowBackup="true"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:icon="@drawable/ic_launcher"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:label="@string/app_name"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:theme="@style/AppTheme"	
  >	
  
	
  	
  	
  	
  	
  	
  	
  	
  <activity	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  android:name=".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>
res/values/string.xml
<?xml	
  version="1.0"	
  encoding="utf-­‐8"?>	
  
<manifest	
  xmlns:android="http://schemas.android.com/apk/res/android"	
  
	
  	
  	
  	
  package="com.example.firstapp"	
  
	
  	
  	
  	
  android:versionCode="1"	
  
	
  	
  	
  	
  android:versionName="1.0"	
  >	
  
!
	
  	
  	
  	
  <uses-­‐sdk	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:minSdkVersion="8"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:targetSdkVersion="21"	
  />	
  
!
	
  	
  	
  	
  <application	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:allowBackup="true"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:icon="@drawable/ic_launcher"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:label="@string/app_name"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:theme="@style/AppTheme"	
  >	
  
	
  	
  	
  	
  	
  	
  	
  	
  <activity	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  android:name=".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>
res/values/string.xml
<?xml	
  version="1.0"	
  encoding="utf-­‐8"?>	
  
<manifest	
  xmlns:android="http://schemas.android.com/apk/res/android"	
  
	
  	
  	
  	
  package="com.example.firstapp"	
  
	
  	
  	
  	
  android:versionCode="1"	
  
	
  	
  	
  	
  android:versionName="1.0"	
  >	
  
!
	
  	
  	
  	
  <uses-­‐sdk	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:minSdkVersion="8"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:targetSdkVersion="21"	
  />	
  
!
	
  	
  	
  	
  <application	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:allowBackup="true"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:icon="@drawable/ic_launcher"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:label="@string/app_name"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:theme="@style/AppTheme"	
  >	
  
	
  	
  	
  	
  	
  	
  	
  	
  <activity	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  android:name=".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>
<?xml	
  version="1.0"	
  encoding="utf-­‐8"?>	
  
<resources>	
  
!
	
  	
  	
  	
  <string	
  name="app_name">FirstApp</string>	
  
	
  	
  	
  	
  <string	
  name="hello_world">Hello	
  world!</string>	
  
	
  	
  	
  	
  <string	
  name="action_settings">Settings</string>	
  
!
</resources>
res/values/string.xml
<?xml	
  version="1.0"	
  encoding="utf-­‐8"?>	
  
<manifest	
  xmlns:android="http://schemas.android.com/apk/res/android"	
  
	
  	
  	
  	
  package="com.example.firstapp"	
  
	
  	
  	
  	
  android:versionCode="1"	
  
	
  	
  	
  	
  android:versionName="1.0"	
  >	
  
!
	
  	
  	
  	
  <uses-­‐sdk	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:minSdkVersion="8"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:targetSdkVersion="21"	
  />	
  
!
	
  	
  	
  	
  <application	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:allowBackup="true"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:icon="@drawable/ic_launcher"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:label="@string/app_name"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:theme="@style/AppTheme"	
  >	
  
	
  	
  	
  	
  	
  	
  	
  	
  <activity	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  android:name=".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>
<?xml	
  version="1.0"	
  encoding="utf-­‐8"?>	
  
<resources>	
  
!
	
  	
  	
  	
  <string	
  name="app_name">FirstApp</string>	
  
	
  	
  	
  	
  <string	
  name="hello_world">Hello	
  world!</string>	
  
	
  	
  	
  	
  <string	
  name="action_settings">Settings</string>	
  
!
</resources>
AndroidManifest.xml
<?xml	
  version="1.0"	
  encoding="utf-­‐8"?>	
  
<manifest	
  xmlns:android="http://schemas.android.com/apk/res/android"	
  
	
  	
  	
  	
  package="com.example.firstapp"	
  
	
  	
  	
  	
  android:versionCode="1"	
  
	
  	
  	
  	
  android:versionName="1.0"	
  >	
  
!
	
  	
  	
  	
  <uses-­‐sdk	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:minSdkVersion="8"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:targetSdkVersion="21"	
  />	
  
!
	
  	
  	
  	
  <application	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:allowBackup="true"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:icon="@drawable/ic_launcher"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:label="@string/app_name"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:theme="@style/AppTheme"	
  >	
  
	
  	
  	
  	
  	
  	
  	
  	
  <activity	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  android:name=".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>
src/{your package name}/
MainActivity.java
public	
  class	
  MainActivity	
  extends	
  ActionBarActivity	
  {	
  
!
	
   @Override	
  
	
   protected	
  void	
  onCreate(Bundle	
  savedInstanceState)	
  {	
  
	
   	
   super.onCreate(savedInstanceState);	
  
	
   	
   setContentView(R.layout.activity_main);	
  
	
   }	
  
!
	
   @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;	
  
	
   }	
  
!
	
   @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();	
  
	
   	
   if	
  (id	
  ==	
  R.id.action_settings)	
  {	
  
	
   	
   	
   return	
  true;	
  
	
   	
   }	
  
	
   	
   return	
  super.onOptionsItemSelected(item);	
  
	
   }	
  
}
src/{your package name}/
MainActivity.java
public	
  class	
  MainActivity	
  extends	
  ActionBarActivity	
  {	
  
!
	
   @Override	
  
	
   protected	
  void	
  onCreate(Bundle	
  savedInstanceState)	
  {	
  
	
   	
   super.onCreate(savedInstanceState);	
  
	
   	
   setContentView(R.layout.activity_main);	
  
	
   }	
  
!
	
   @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;	
  
	
   }	
  
!
	
   @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();	
  
	
   	
   if	
  (id	
  ==	
  R.id.action_settings)	
  {	
  
	
   	
   	
   return	
  true;	
  
	
   	
   }	
  
	
   	
   return	
  super.onOptionsItemSelected(item);	
  
	
   }	
  
}
res/layout/activity_main.xml
public	
  class	
  MainActivity	
  extends	
  ActionBarActivity	
  {	
  
!
	
   @Override	
  
	
   protected	
  void	
  onCreate(Bundle	
  savedInstanceState)	
  {	
  
	
   	
   super.onCreate(savedInstanceState);	
  
	
   	
   setContentView(R.layout.activity_main);	
  
	
   }	
  
!
	
   @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;	
  
	
   }	
  
!
	
   @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();	
  
	
   	
   if	
  (id	
  ==	
  R.id.action_settings)	
  {	
  
	
   	
   	
   return	
  true;	
  
	
   	
   }	
  
	
   	
   return	
  super.onOptionsItemSelected(item);	
  
	
   }	
  
}
<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="com.example.firstapp.MainActivity"	
  >	
  
!
	
  	
  	
  	
  <TextView	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:layout_width="wrap_content"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:layout_height="wrap_content"	
  
	
  	
  	
  	
  	
  	
  	
  	
  android:text="@string/hello_world"	
  />	
  
!
</RelativeLayout>
res gen src bin jni libs assets
layout
layout-land = 橫向

layout-port = 直向
drawable
xlarge = 960dp x 720dp

large = 640dp x 480dp

normal = 470dp x 320dp

small = 426dp x 320dp
menu Menu item
value
color.xml

string.xml

style.xml
res gen src bin jni libs assets
BuildConfig.java
/**	
  Automatically	
  generated	
  file.	
  DO	
  NOT	
  MODIFY	
  */	
  
package	
  com.example.firstapp;	
  
!
public	
  final	
  class	
  BuildConfig	
  {	
  
	
  	
  	
  	
  public	
  final	
  static	
  boolean	
  DEBUG	
  =	
  true;	
  
}
res gen src bin jni libs assets
R.java
AUTO-­‐GENERATED	
  FILE.	
  	
  DO	
  NOT	
  MODIFY.	
  
package	
  com.example.firstapp;	
  
!
public	
  final	
  class	
  R	
  {	
  
	
  	
  	
  	
  public	
  static	
  final	
  class	
  layout	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  public	
  static	
  final	
  int	
  activity_main=0x7f040000;	
  
	
  	
  	
  	
  }	
  
	
   	
  …………	
  
}
res gen src bin jni libs assets
package	
  com.example.firstapp;	
  
!
import	
  android.app.Activity;	
  
import	
  android.os.Bundle;	
  
!
public	
  class	
  MainActivity	
  extends	
  Activity	
  {	
  
!
	
   @Override	
  
	
   protected	
  void	
  onCreate(Bundle	
  savedInstanceState)	
  {	
  
	
   	
   super.onCreate(savedInstanceState);	
  
	
   	
   setContentView(R.layout.activity_main);	
  
	
   }	
  
!
}
res gen src bin jni libs assets
res gen src bin jni libs assets
res gen src bin jni libs assets
Third party library
res gen src bin jni libs assets
This is empty. You can use it to store raw asset files. Files
that you save here are compiled into an .apk file as-is,
and the original filename is preserved. You can navigate
this directory in the same way as a typical file system
using URIs and read files as a stream of bytes using the
AssetManager. For example, this is a good location for
textures and game data.
res gen src bin jni libs assets
Android Project Builder
res gen src bin jni libs assets

Ch2 first app

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
    App Package NameGoogle play package is only
  • 11.
  • 15.
    Introduction directories andfiles in the Android project
  • 16.
    The first Androidproject architecture http://developer.android.com/tools/projects/index.html
  • 17.
    AndroidManifest.xml <?xml  version="1.0"  encoding="utf-­‐8"?>   <manifest  xmlns:android="http://schemas.android.com/apk/res/android"          package="com.example.firstapp"          android:versionCode="1"          android:versionName="1.0"  >   !        <uses-­‐sdk                  android:minSdkVersion="8"                  android:targetSdkVersion="21"  />   !        <application                  android:allowBackup="true"                  android:icon="@drawable/ic_launcher"                  android:label="@string/app_name"                  android:theme="@style/AppTheme"  >                  <activity                          android:name=".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>
  • 18.
    AndroidManifest.xml <?xml  version="1.0"  encoding="utf-­‐8"?>   <manifest  xmlns:android="http://schemas.android.com/apk/res/android"          package="com.example.firstapp"          android:versionCode="1"          android:versionName="1.0"  >   !        <uses-­‐sdk                  android:minSdkVersion="8"                  android:targetSdkVersion="21"  />   !        <application                  android:allowBackup="true"                  android:icon="@drawable/ic_launcher"                  android:label="@string/app_name"                  android:theme="@style/AppTheme"  >                  <activity                          android:name=".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>
  • 19.
    AndroidManifest.xml <?xml  version="1.0"  encoding="utf-­‐8"?>   <manifest  xmlns:android="http://schemas.android.com/apk/res/android"          package="com.example.firstapp"          android:versionCode="1"          android:versionName="1.0"  >   !        <uses-­‐sdk                  android:minSdkVersion="8"                  android:targetSdkVersion="21"  />   !        <application                  android:allowBackup="true"                  android:icon="@drawable/ic_launcher"                  android:label="@string/app_name"                  android:theme="@style/AppTheme"  >                  <activity                          android:name=".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>
  • 20.
    AndroidManifest.xml <?xml  version="1.0"  encoding="utf-­‐8"?>   <manifest  xmlns:android="http://schemas.android.com/apk/res/android"          package="com.example.firstapp"          android:versionCode="1"          android:versionName="1.0"  >   !        <uses-­‐sdk                  android:minSdkVersion="8"                  android:targetSdkVersion="21"  />   !        <application                  android:allowBackup="true"                  android:icon="@drawable/ic_launcher"                  android:label="@string/app_name"                  android:theme="@style/AppTheme"  >                  <activity                          android:name=".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>
  • 21.
    res/values/string.xml <?xml  version="1.0"  encoding="utf-­‐8"?>   <manifest  xmlns:android="http://schemas.android.com/apk/res/android"          package="com.example.firstapp"          android:versionCode="1"          android:versionName="1.0"  >   !        <uses-­‐sdk                  android:minSdkVersion="8"                  android:targetSdkVersion="21"  />   !        <application                  android:allowBackup="true"                  android:icon="@drawable/ic_launcher"                  android:label="@string/app_name"                  android:theme="@style/AppTheme"  >                  <activity                          android:name=".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>
  • 22.
    res/values/string.xml <?xml  version="1.0"  encoding="utf-­‐8"?>   <manifest  xmlns:android="http://schemas.android.com/apk/res/android"          package="com.example.firstapp"          android:versionCode="1"          android:versionName="1.0"  >   !        <uses-­‐sdk                  android:minSdkVersion="8"                  android:targetSdkVersion="21"  />   !        <application                  android:allowBackup="true"                  android:icon="@drawable/ic_launcher"                  android:label="@string/app_name"                  android:theme="@style/AppTheme"  >                  <activity                          android:name=".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> <?xml  version="1.0"  encoding="utf-­‐8"?>   <resources>   !        <string  name="app_name">FirstApp</string>          <string  name="hello_world">Hello  world!</string>          <string  name="action_settings">Settings</string>   ! </resources>
  • 23.
    res/values/string.xml <?xml  version="1.0"  encoding="utf-­‐8"?>   <manifest  xmlns:android="http://schemas.android.com/apk/res/android"          package="com.example.firstapp"          android:versionCode="1"          android:versionName="1.0"  >   !        <uses-­‐sdk                  android:minSdkVersion="8"                  android:targetSdkVersion="21"  />   !        <application                  android:allowBackup="true"                  android:icon="@drawable/ic_launcher"                  android:label="@string/app_name"                  android:theme="@style/AppTheme"  >                  <activity                          android:name=".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> <?xml  version="1.0"  encoding="utf-­‐8"?>   <resources>   !        <string  name="app_name">FirstApp</string>          <string  name="hello_world">Hello  world!</string>          <string  name="action_settings">Settings</string>   ! </resources>
  • 24.
    AndroidManifest.xml <?xml  version="1.0"  encoding="utf-­‐8"?>   <manifest  xmlns:android="http://schemas.android.com/apk/res/android"          package="com.example.firstapp"          android:versionCode="1"          android:versionName="1.0"  >   !        <uses-­‐sdk                  android:minSdkVersion="8"                  android:targetSdkVersion="21"  />   !        <application                  android:allowBackup="true"                  android:icon="@drawable/ic_launcher"                  android:label="@string/app_name"                  android:theme="@style/AppTheme"  >                  <activity                          android:name=".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>
  • 25.
    src/{your package name}/ MainActivity.java public  class  MainActivity  extends  ActionBarActivity  {   !   @Override     protected  void  onCreate(Bundle  savedInstanceState)  {       super.onCreate(savedInstanceState);       setContentView(R.layout.activity_main);     }   !   @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;     }   !   @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();       if  (id  ==  R.id.action_settings)  {         return  true;       }       return  super.onOptionsItemSelected(item);     }   }
  • 26.
    src/{your package name}/ MainActivity.java public  class  MainActivity  extends  ActionBarActivity  {   !   @Override     protected  void  onCreate(Bundle  savedInstanceState)  {       super.onCreate(savedInstanceState);       setContentView(R.layout.activity_main);     }   !   @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;     }   !   @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();       if  (id  ==  R.id.action_settings)  {         return  true;       }       return  super.onOptionsItemSelected(item);     }   }
  • 27.
    res/layout/activity_main.xml public  class  MainActivity  extends  ActionBarActivity  {   !   @Override     protected  void  onCreate(Bundle  savedInstanceState)  {       super.onCreate(savedInstanceState);       setContentView(R.layout.activity_main);     }   !   @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;     }   !   @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();       if  (id  ==  R.id.action_settings)  {         return  true;       }       return  super.onOptionsItemSelected(item);     }   } <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="com.example.firstapp.MainActivity"  >   !        <TextView                  android:layout_width="wrap_content"                  android:layout_height="wrap_content"                  android:text="@string/hello_world"  />   ! </RelativeLayout>
  • 28.
    res gen srcbin jni libs assets layout layout-land = 橫向 layout-port = 直向 drawable xlarge = 960dp x 720dp large = 640dp x 480dp normal = 470dp x 320dp small = 426dp x 320dp menu Menu item value color.xml string.xml style.xml
  • 29.
    res gen srcbin jni libs assets BuildConfig.java /**  Automatically  generated  file.  DO  NOT  MODIFY  */   package  com.example.firstapp;   ! public  final  class  BuildConfig  {          public  final  static  boolean  DEBUG  =  true;   }
  • 30.
    res gen srcbin jni libs assets R.java AUTO-­‐GENERATED  FILE.    DO  NOT  MODIFY.   package  com.example.firstapp;   ! public  final  class  R  {          public  static  final  class  layout  {                  public  static  final  int  activity_main=0x7f040000;          }      …………   }
  • 31.
    res gen srcbin jni libs assets package  com.example.firstapp;   ! import  android.app.Activity;   import  android.os.Bundle;   ! public  class  MainActivity  extends  Activity  {   !   @Override     protected  void  onCreate(Bundle  savedInstanceState)  {       super.onCreate(savedInstanceState);       setContentView(R.layout.activity_main);     }   ! }
  • 32.
    res gen srcbin jni libs assets
  • 33.
    res gen srcbin jni libs assets
  • 34.
    res gen srcbin jni libs assets Third party library
  • 35.
    res gen srcbin jni libs assets This is empty. You can use it to store raw asset files. Files that you save here are compiled into an .apk file as-is, and the original filename is preserved. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager. For example, this is a good location for textures and game data.
  • 36.
    res gen srcbin jni libs assets Android Project Builder
  • 37.
    res gen srcbin jni libs assets