In this section, we are going to understand actual programming with the Android framework. To do that, you need Android Studio and I assume you already installed Android SDK and are done with the set-up of environment variables.
Let us start by creating an application in Android Studio:
- The first step you need to do is to Start a new Android Studio Project that will ask you for some details about the project like the application name, package information, and location.
- After providing all these details, it will ask you to declare the supported android OS version.
- Next, it will ask you to specify the layout of your android application.
- At last, you are going to use the development tool to write the application.
A development window will open in Android studio where you can write your application code. First, we are going to edit the main activity file Activity.java with the code given below:
package com.example.helloworld;
import android.support.v9.app.AppCompatActivity;
import android.os.Bundle;
public class Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Now we are going to declare the components of the application in its manifest.xml file. The default manifest.xml file will look like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.greatlearning.firstapp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
We also need to add strings of text that we want to show in our application. The default strings.xml file in our application will look like this:
<resources>
<string name="app_name">HelloWorldApp</string>
<string name="hello_world">Hello World!!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">Activity</string>
</resources>
Next, we are going to edit the layout file named activity_main.xml in our project. The following code we are going to use in this file:
<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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
tools:context=".Activity" />
</RelativeLayout>
This way, we are done with all configurations for our first android application. Now you can run the app and see how it works.