Collective

Press the “launch MarkActivity” Button to create a MarkActivity and call its onCreate method. Watch this method do three simple animations: fade in, fade out, and zoom out. Then press one of the other nine Buttons to launch an Activity in another app.

The onButtonClick method of class MainActivity shows how to make an Intent that launches another Activity that belongs to the same app. We also saw how to do this in class on June 27, 2015 in the startStarWars method of the MainActivity of this app. (Read • To start an activity and • Explicit intents.) The onAsaClick method of class MarkActivity shows how to make an Intent that launches an Activity that belongs to a different app. The first parameter of the ComponentName constructor seems redundant. But it is needed when the name of the class does not begin with the name of the package:

        ComponentName componentName = new ComponentName(
                "com.google.android.apps.maps",        //name of package
                "com.google.android.maps.MapsActivity" //name of class
        );

The action bar of the MarkActivity has a left arrow leading to the MainActivity because we said that the MainActivity was the hierarchical parent of the MarkActivity when we created the MarkActivity. See Navigation with Back and Up and Navigating Up with the App Icon.

Source code in Jun27.zip

  1. MainActivity.java. Class MainActivity belongs to package edu.nyu.scps.mark.jun27.
  2. activity_main.xml is displayed by the onCreate method of class MainActivity.
  3. MarkActivity.java. Class MarkActivity belongs to package edu.nyu.scps.mark.jun27.
  4. activity_mark.xml is displayed by the onCreate method of class MarkActivity.
  5. strings.xml. The string resource app_name is the label that appears under the app’s icon in the launcher page. I changed it from Jun27 to Jun27 Mark because it would be confusing to have ten apps with the same label. I also added a new string resource named buttonText.
  6. logo.png (1280 × 773 pixels)
  7. AndroidManifest.xml contains two <activity> elements. I added the <intent-filter> element by hand to the <activity> element for MarkActivity.
  8. build.gradle (Module: app)

Create the project

In Android Studio, pull down
File → New → New Project…
Create New Project
New Project
Android Studio
Configure your new project
Application name: Jun27
Company Domain: mark.scps.nyu.edu (or whatever your first name is)
Package name: edu.nyu.scps.mark.jun27
etc.

The project already has a MainActivity. To create another subclass of class AppCompatActivity, select the edu.nyu.scps.yourname.jun27 folder in the Android Studio project view, and pull down
File → New → Activity → Blank Activity
New Android Activity
Customize the Activity
Creates a new blank activity with an action bar.
Activity Name: MarkActivity (or whatever your first name is)
Hierarchical parent: edu.nyu.scps.mark.jun27.MainActivity (or whatever your first name is)
Finish
This will create the files MarkActivity.java and activity_mark.xml, and will create an <activity> element for MarkActivity in AndroidManifest.xml. In MarkActivity.java, change ActionBarActivity to AppCompatActivity.

Copy logo.png into the app’s app/res/drawable folder as we copied the photo in America.

Things to try

  1. Replace the nine onClick methods in MarkActivity with the following method. Make the nine Buttons call the new method.
        public void onClick(View view) {
            Button button = (Button)view;
    	CharSequence charSequence = button.getText();
            String upperName = charSequence.toString();
            String lowerName = upperName.substring(0, 1).toLowerCase() + upperName.substring(1);
            Intent intent = new Intent();
    
            ComponentName componentName = new ComponentName(
                    "edu.nyu.scps." + lowerName + ".jun27",                          //name of package
                    "edu.nyu.scps." + lowerName + ".jun27." + upperName + "Activity" //name of class
            );
    
            intent.setComponent(componentName);
    
            try {
                startActivity(intent);
            } catch (ActivityNotFoundException activityNotFoundException) {
                Toast toast = Toast.makeText(this, activityNotFoundException.toString(), Toast.LENGTH_LONG);
                toast.show();
            }
        }