
上QQ阅读APP看书,第一时间看更新
How to do it...
For this first example, we'll guide you through creating a new project. Android Studio provides a Quick Start wizard, which makes the process extremely easy. Follow these steps to get started:
- Launch Android Studio, which brings up the Welcome to Android Studio dialog:
- Click on the Start a new Android Studio project option.
- Enter an application name; for this example, we used DeclareAnActivity. Click on Next:
- In the Target Android Devices dialog, you can leave the Phone and Tablet checkbox selected with the default API 21: Android 5.0 (Lollipop) selection for the minimum SDK (for this example, it really doesn't matter which API level you choose, as activities have existed since API level 1). Click on Next:
- In the Add an Activity to Mobile dialog, select the Empty Activity option. Click on Next:
- In the Configure Activity dialog, you can leave the defaults as provided, but note that the default activity name is MainActivity. Click on Finish:
After finishing the wizard, Android Studio will create the project files. For this recipe, the two files that we will examine are MainActivity.java (which corresponds to the activity name mentioned in step 6) and AndroidManifest.xml.
If you take a look at the MainActivity.java file, you will realize that it's pretty basic. This is because we chose the Empty Activity option (in step 5). Now, look at the AndroidManifest.xml file. This is where we actually declare the activity. Within the <application> element is the <activity> element:
<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>
When viewing this xml in Android Studio, you may notice that the label element shows the actual text ( DeclareAnActivity in this case) as defined in the strings.xml resource file.