In this class, we'll be learning about what is Activity and its life cycle. We'll see how we can use intents to launch activities. Apart from that, we'll take a glance over Android Manifest file, and we will also look into some basic UI components (Relative Layout, Linear Layout and GridView) to get you started.
(Figure source: developer.android.com Link)
The square rectangles in the above diagram represent callback methods you can implement to perform operations when the Activity moves between states. The colored ovals are major states the Activity can be in.
State | Description |
---|---|
Running/Resumed | Activity is visible and interacts with the user. |
Paused | Activity is still visible but partially obscured, instance is running but might be killed by the system. |
Stopped | Activity is not visible, instance is running but might be killed by the system. |
Destroyed | Activity has been terminated by the system of by a call to its finish() method. |
(Figure source: developer.android.com Link).
The above figure shows an Android application lifecycle depicting different Activity states.
Method | Purpose |
---|---|
onCreate() | Called when the activity is first created |
onStart() | Called when the activity becomes visible to the user |
onResume() | Called when the activity starts interacting with the user |
onPause() | Called when the current activity is being paused and the previous activity is being resumed |
onStop() | Called when the activity is no longer visible to the user |
onDestroy() | Called before the activity is destroyed by the system (either manually or by the system to conserve memory) |
onRestart() | Called when the activity has been stopped and is restarting again |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.imageviewer" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.imageviewer.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> <!-- ImageGrid Activity --> <activity android:name="com.example.imageviewer.ImageGridActivity" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" /> </application> </manifest>
(Figures in this section are drawn from www.androidhive.info)
Let's build a simple application which lets you:
So, in this app, we'll be having two screens:
<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=".MainActivity" > <!-- Application Title Text Label--> <TextView android:id="@+id/text_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="UW Image Viewer" android:textSize="22sp" android:textStyle="bold" /> <!-- Button to view the grid of UW Images --> <Button android:id="@+id/button_uwimages" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/text_title" android:layout_centerHorizontal="true" android:onClick="onButtonClick" android:text="UW Images" /> <!-- Button to open already installed Gallery App --> <Button android:id="@+id/button_gallery" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/button_uwimages" android:layout_centerHorizontal="true" android:onClick="onButtonClick" android:text="Open Gallery Application" /> <!-- Button to define some application preferences --> <Button android:id="@+id/button_settings" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/button_gallery" android:layout_centerHorizontal="true" android:onClick="onButtonClick" android:text="Settings" /> </RelativeLayout>
Things to notice:
/** * Callback method for clicks on the buttons in Main Screen. * @param v View clicked */ public void onButtonClick(View v) { switch(v.getId()) { case R.id.button_gallery: Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(galleryIntent, 0); break; default: } }
package com.example.imageviewer; import android.app.Activity; import android.os.Bundle; public class ImageGridActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_imagegrid); } }
<?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:orientation="vertical" > <!-- ImageGrid Screen Title --> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="UW Madison Images" android:textSize="20sp" /> <!-- Although not required in this case, but I just wanted to demonstrate its use --> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true" > <!-- Grid View showing image thumbnails --> <GridView android:id="@+id/grid_view" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dip" android:numColumns="2" android:stretchMode="columnWidth" android:verticalSpacing="20dp" android:horizontalSpacing="20dp" android:gravity="center" /> </ScrollView> </LinearLayout>
Things to notice:
/** * Callback method for clicks on the buttons in Main Screen. * @param v View clicked */ public void onButtonClick(View v) { switch(v.getId()) { case R.id.button_uwimages: // Create new intent Intent i = new Intent(MainActivity.this, ImageGridActivity.class); startActivity(i); break; case R.id.button_gallery: // Open pre-installed Gallery Application Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(galleryIntent, 0); break; case R.id.button_settings: // Lets just show toast for now. Toast.makeText(this, "Will add Preferences later!", Toast.LENGTH_LONG).show(); break; default: } }
Things to notice:
Now, after clicking the 'UWImages' Button, the app should crash. And, you should see the following error dialog:
<!-- ImageGrid Activity --> <activity android:name="com.example.imageviewer.ImageGridActivity" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />
package com.example.imageviewer; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; public class ImageAdapter extends BaseAdapter { private Context mContext; public ImageAdapter(Context c) // Constructor { mContext = c; } public Integer[] mThumbs = { R.drawable.pic0_thumb, R.drawable.pic1_thumb, R.drawable.pic2_thumb, R.drawable.pic3_thumb, R.drawable.pic4_thumb, R.drawable.pic5_thumb, R.drawable.pic6_thumb, R.drawable.pic7_thumb, R.drawable.pic8_thumb, R.drawable.pic9_thumb, R.drawable.pic10_thumb, R.drawable.pic11_thumb, R.drawable.pic12_thumb, R.drawable.pic13_thumb, R.drawable.pic14_thumb, R.drawable.pic15_thumb, R.drawable.pic16_thumb, R.drawable.pic17_thumb, R.drawable.pic18_thumb, R.drawable.pic19_thumb, R.drawable.pic20_thumb, R.drawable.pic21_thumb, R.drawable.pic22_thumb, R.drawable.pic23_thumb, R.drawable.pic24_thumb, R.drawable.pic25_thumb, R.drawable.pic26_thumb, R.drawable.pic27_thumb, R.drawable.pic28_thumb, R.drawable.pic29_thumb, R.drawable.pic30_thumb, R.drawable.pic31_thumb, R.drawable.pic32_thumb, R.drawable.pic33_thumb, R.drawable.pic34_thumb, R.drawable.pic35_thumb, R.drawable.pic36_thumb, R.drawable.pic37_thumb, R.drawable.pic38_thumb, R.drawable.pic39_thumb }; @Override public int getCount() { return mThumbs.length; } @Override public Object getItem(int position) { return mThumbs[position]; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) // Recycled View { imageView = new ImageView(mContext); imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); imageView.setLayoutParams(new GridView.LayoutParams(220, 220)); } else // Re-use the view { imageView = (ImageView) convertView; } imageView.setImageResource(mThumbs[position]); return imageView; } }
// Get GridView from xml GridView gridView = (GridView) findViewById(R.id.grid_view); // Set Adapter for GridView gridView.setAdapter(new ImageAdapter(this));