Android Studio creates the first app_android

Source: Internet
Author: User

This example introduces the detailed steps for Android studio to create the first app for your reference, as follows

1. Create HelloWorld Project

The first program written in any programming language will undoubtedly be Hello world, a tradition that has been handed down since the the 1970s and has become a timeless classic in the programming world, so today it's time to use Android Studio to create the first app (Hello World).
First run Android studio and create a new item in the pop-up shortcut window.

Click "Star a new Android Studio project" in the screenshot above, and the Create New Project dialog box pops up. Where application name represents the application name that will be displayed on the phone when the application is installed with the phone. Here we fill in Hello World (usually no spaces in future project names). As shown in the following figure

Next, clicking on "Next" will pop up the "Add a activities to Mobile" dialog window, where we choose "Empty activity", which will be introduced later in the event (hereafter known as the campaign).

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

Click Finish, an Android project is created.

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

2.2 Use a third party simulator.

Here I am using the Genymotion Simulator, personally feel very good. It can also be associated with Android studio through Plug-ins.
As for how to download the Genymotion simulator and associated with Android studio, these are platform building, as a developer should first learn how to sharpen their own tools, Chinese proverb工欲善其事 Its prerequisite , cough, pull away! But really have to build a platform of their own hands.


In Genymotion, I have already downloaded a simulator, the Android 4.1 version of Samsung note 2.
First , click "Star" in the screenshot above to open it, like this (the 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 is well, wait (depending on the speed of your computer) to see the Hello World project just created in the simulator, and Android Studio has helped us create all the other code.

3, the analysis of the creation of the project directory.

3.1, Manifest
This is actually a androidmanifest.xml file, this is the entire Android project configuration file, the four components defined in the program (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" >

  <application
    android: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>

On the Mobile desktop application, the user will start the program after clicking, and see the First Activity (page) is the main activity of the program, the main activity needs to be declared in the androidmanifest.xml.

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

If the activity is not a master activity, then the Androidmanifest.xml configuration should omit the contents of the Intent-filter label. Because the action in it is to declare that the activity is the main event.

3.2, Java
Here's where the Java code is, the meaning of which is the same as SRC in the Java project in eclipse, and it turns out that the helloworldactivity file we just created is in there.

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);
  }


The life cycle of the point activity is inserted here, each of which starts with the OnCreate () method.

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

public class Helloworldactivity extends Appcompatactivity {

  @Override
  protected void OnCreate (Bundle Savedinstancestate) {
    super.oncreate (savedinstancestate);
  }
}

As you can see, the OnCreate () method is simple, calling the OnCreate () method of the parent class, which is, of course, the default, where we can add our own logic.

For example: Time Click event. (Use 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 execute/
    Setcontentview (r.layout.first_layout) before Setcontentview ();
    Button button1= (button) Findviewbyid (r.id.button_1);
    Button1.setonclicklistener (New View.onclicklistener () {
      @Override public
      void OnClick (View v) {
        Intent Intent=new Intent (Firstactivity.this,secondactivity.class);
        StartActivity (intent);
      }
    );
  

But we found a line of code in there.

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

3.3, Res
The files in this directory are a bit more, simply put, the pictures, layouts, strings and so on that you use in your project should be stored in this directory. Of course there are many subdirectories under this directory.

3.3.1, Mipmap

Only used to store the application icon, but also more than the same name, but the resolution is different, the system can be optimized according to the resolution of different resolution

Icon.

3.3.2, drawable

Other icon Resources

3.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>

3.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 ">

  <textview
    android: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 a string that is displayed in the program's runtime, and is actually explained in the String.xml file in the above values.

That's all there is to it, and it helps to create your first project, Hello World, with Android studio.

Related Article

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.