Hello, everyone, last time we talked about Java multithreaded programming example, this time we say the example is Android Hello World. Stop gossiping and return to the truth. Let’s Talk Android!

Look guys, in the previous chapter, we introduced the basic knowledge of Java, with these basic knowledge can introduce Android, so from this time, we officially start to introduce the knowledge of Android. The nearly 30 sessions are devoted to Java related knowledge, and some viewers can’t wait to see when Android will be introduced. Now we don’t have to wait, we’re going to introduce Android right now, right now!

Let’s start with a simple example: Hello World. Hopefully, this example will give you a basic understanding of Android development. The following are the specific operation steps for your reference:

  • 1. To build the Android development environment, you only need to install JDK and Android Studio(hereafter referred to as AST), and choose the latest version of the installation (my development environment is JDK 1.8 and AST 2.2.3);Copy the code
  • 2. Use AST to create a new project called BlogApp001 and fill in the package name. Use default Settings for other steps.Copy the code
  • 3. The AST creates an empty layout by default and adds a TextView to the layout to display the text;Copy the code
  • 4. Manually add a file control and a button control to the layout to display text;Copy the code
  • 5. Compile and run the program in the simulator environment, which needs to be created by the AVD tool of the AST;Copy the code

The following is a screenshot of the results of the program, please refer to:

You can see three Hello World! The first one is created by the AST by default, and the remaining two are the ones we added in Step 4, with different colors to distinguish them. Whether the system defaults or we add them, they are all controls, and they are all in the same layout. In Android development, we call a layout an Activity, and it is the carrier of what we see in an application. Android provides XML files to control its display and Java files to control its logic. Let’s demonstrate the code for XML files and Java files in this program:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/text_view"
            android:textColor="@color/colorRed"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:textStyle="bold"
            android:textColor="@color/colorGreen"
            android:textAllCaps="false"/>
    </LinearLayout>
Copy the code

We have omitted the outermost code and only demonstrated the core code. You can see from the above code that two TextView controls and a Button control are located inside the LinearLayout control. Combined with the results of the program, we can understand that these three controls are used to display the Hello World text, as for the text color is controlled by the control properties, we will not introduce the details, we can see through the code. With the display in place, let’s take a look at the logical handling of the layout file.

Here is the code in the Java file:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView tv = (TextView)findViewById(R.id.text_view);
        tv.setText("Hello World!"); }}Copy the code

As you can see from the above code, the Activity handles the related code logic through Java classes. In this class, it inherits the AppCompatActivity parent and overrides the onCreate() method of that parent to put everything there. The main thing is setContentView(R.layout.activity_main); This line of code associates the Activity’s layout file with the code logic. To be more straightforward, it loads controls and layouts from XML files into the Activity so that we can manipulate individual controls from XML in Java programs. For example, in our code we change the text in the TextView control to :Hello orld! . Of course, all actions on the control should come after this line of code. Otherwise, how can we manipulate the control before it has been loaded? This is not a clever housewife without rice?

Let’s make a final conclusion, folks: Developing an Android application is exactly the same as developing a Hello World application, and the main thing we do is design the layout and deal with the logic behind the layout. Layout design involves many controls, we have to master the use of each control to understand how to better use them. For example, using TextView to display text content in our application is uncontroversial, but using Button to display text content is a bit nonsensical. Of course, we’re just using it to compare to the default text in this example, nothing else.

Android offers many controls, and we won’t be able to cover all of them in one example, but don’t worry, we’ll cover more controls in a later chapter. Once you’ve designed the layout, you need to deal with the logic behind the layout, which is more complex than just designing it. Similarly, we will introduce this in the following chapter.

Hello World is an Android version of Hello World. If you want to know more examples, let’s listen to the next time.