Use Android Studio to create your first project

Source: Internet
Author: User

1. Create a HelloWorld project

The first program written in any programming language will undoubtedly be Hello world, a tradition that has been passed down since the the 1970s and has become a timeless classic in the programming world, so use Android studio to create your first app today.
First run Android studio and create a new project in the popup shortcut window.

Click "Star a new Android Studio project" above, and the "Create New Project" dialog box will appear. Where application name is the name of the app, which is displayed on the phone when the app is installed with the phone. Here we fill in Hello World (usually with no spaces in future project names). As shown

Next click on "Next", will pop up "Add A activity to Mobile" Dialogue window, here we choose "Empty activity", as for other activities (hereafter known as activity) will be introduced later.

Click Next to fill in the activity name, where activity name is named HelloWorld activity.

Click Finish and an Android project is created.

2. Run the HelloWorld project
2.1 Use Android Studio to run with the emulator.

2.2 Use a third-party emulator.

Here I am using the Genymotion Simulator, personally feel very useful. The same can be associated with Android studio via plugins.
As for how to download the Genymotion simulator and associate it with Android Studio, these are all platforms built, as a developer should first learn how to sharpen their tools, Chinese proverb工欲善其事 Its prerequisite , cough, pull away! But you really have to build a platform on your own.


In Genymotion, I have downloaded a simulator, the Android 4.1 version of Samsung Note 2.
First , click "Star" in the above to open it, like this (desktop background I changed).

then , click "Run" in Android Studio and select the emulator you just opened in the pop-up window.

Click "OK" all right, wait a minute (depending on the speed of the computer) you will see the Hello World project that you just created in the emulator, and Android Studio has helped us to create all the other code.

2, the analysis under the creation of the project directory.

2.1, Manifest
This is actually the Androidmanifest.xml file, which is the entire Android project configuration file, defined in the program four components (activity– activity, service– service, broadcastreceiver– broadcast receiver, Content provider– contents Provider). The following code is androidmanifest.xml.

<?xml version= "1.0" encoding= "Utf-8"?><manifest package ="Com.example.chencong.helloworld"xmlns:android= "Http://schemas.android.com/apk/res/android">              <applicationandroid:allowbackup="true"android:icon="@mipmap/ Ic_launcher "android:label=" @string/app_name "android:supportsrtl=" True "android:theme=" @style/apptheme ">                                                <activity android:name=". Helloworldactivity ">            <intent-filter>                <action android:name="Android.intent.action.MAIN"/>                <category android:name="Android.intent.category.LAUNCHER"/>            </intent-filter>        </activity>    </Application></manifest>

In the app on the phone desktop, the user clicks on the program, and the First activity (page) that is seen is the main activity of the program, and the main activity needs to be declared in Androidmanifest.xml.

<activity  android:name  = ". Helloworldactivity ";  <intent-filter  &                gt;  <action  android:name  = "Android.intent.action.MAIN" />  < ; category  android:name  = "Android.intent.category.LAUNCHER"        />  </intent-filter ;  </activity ;   

If the activity is not the primary activity, then the configuration in Androidmanifest.xml should omit the contents of the Intent-filter tag. Because the action inside is to declare that the activity is the main event.

2.2. Java
Here is where the Java code is placed, its meaning is the same as in the Java project in Eclipse, SRC is the same, open will find that we have just created the Helloworldactivity file is inside.

package  Com.example.chencong.helloworld; import  android.support.v7.app.AppCompatActivity; import  android.os.Bundle; public  class  helloworldactivity  extends  appcompatactivity  {  @Override  protected   void  oncreate  (Bundle savedinstancestate) {super        . OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_hello_world); }}

Here, the life cycle of the activity is inserted, and each activity starts with the OnCreate () method.

Learn more about the activity lifecycle and see this blog post. Activity life cycle
The first thing we need to know is that any activity in the project should rewrite the activity's OnCreate () method (higher version is appcompatactivity). The code is as follows:

publicclass HelloWorldActivity extends AppCompatActivity {    @Override    protectedvoidonCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);    }}

As you can see, the OnCreate () method is simply called the OnCreate () method of the parent class, which is, of course, just the default, in which we can add our own logic.

Example: Time Click event. (using display Intent)

 Public  class firstactivity extends appcompatactivity {    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);/* Do not display the title bar in the activity, be sure to do it before Setcontentview () */Setcontentview (r.layout.first_layout);        Button button1= (button) Findviewbyid (r.id.button_1); Button1.setonclicklistener (NewView.onclicklistener () {@Override             Public void OnClick(View v) {Intent intent=NewIntent (firstactivity. This, Secondactivity.class);            StartActivity (Intent);    }        }); }

But we found that there was actually a line of code.

 setContentView(R.layout.activity_hello_world);

Referring to the layout file in your code, call R.layout.activity_hello_world to see the ID of the Activity_hello_world.xml, and then pass the value to the Setcontentview () method.

2.3, Res
The file in this directory is a bit more, simple point is that in your project to use the picture, layout, string and other resources to be stored in this directory. Of course there are many subdirectories in this directory.

2.3.1, Mipmap

Just for the application icon, but also many of the same name, but the resolution is different, the system can be based on different resolutions to optimize the use of different resolutions of the icon.

2.3.2, drawable
Other icon Resources

2.3.3, Values
String

In the project created above, the runtime displays Hello World, whose string is stored in the String.xml file

<resources>    <string name="app_name">Hello World</string></resources>

2.3.4, Layout
Layout file

Here is the code for Activity_hello_world.xml:

<?xml version= "1.0" encoding= "Utf-8"?><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.chencong.helloworld.HelloWorldActivity">    <TextViewandroid:layout_width="Wrap_content"android:layout_height ="Wrap_content"android:text="Hello world!" />                        </relativelayout>

This is just a layout file, but the android:text= "Hello World" in the TextView tag above is not the string that is displayed when the program runs, really in the String.xml file, which is already explained in the values above.

Use Android Studio to create your first project

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.