Android is a mobile operating system developed by Google, based on the Linux kernel and intended primarily for touchscreen mobile devices such as smartphones and tablets. Android’s user interface is mainly based on direct manipulation using touch screens along with a virtual keyboard for text inputs. Variants of Android are also used on notebooks, game consoles, digital cameras, and other electronics.
How to build an Android app?
Creating an android application in Android Studio:
Step 1: In Android Studio, create a new project:
Click on New Project
The create New Project screen appears.
Step 2: Fill out the fields on the screen, and click Next.
Application Name is the app name that appears to users.
For this project,i use “My First App.”
Company domain provides a qualifier that will be appended to the package name.
Android Studio will remember this qualifier for each new project you create.
Package name is the fully qualified name for the project.Remember that your package name must be unique across all packages installed on the Android system. You can Edit this value independently from the application name or the company domain.
Project location is the directory on your system that holds the project files.
Step 3: Under select the form factors your app will run on, check the box for Phone and Tablet.
Step 4: Now select the Android version.
Step 5: Leave all of the other options (TV, Wear, and Glass) unchecked and click Next.
Activities
Step 6: Under Add an activity to , select Blank Activity and click Next.
Step 7: Under Customize the Activity, change the Activity Name to MyActivity. The Layout Name changes to activity_my, and the Title to MyActivity. The Menu Resource Name is menu_my.
Step 8: Click the Finish button to create the project.
Your Android project is now a basic “Hello World” app that contains some default files.
Running your application.
In Android Studio, to run your app on the emulator you need to first create an Android Virtual Device(AVD). An AVD is a device configuration for the Android emulator that allows you to model a specific device.
Creating AVD
Step 1:Launch the Android Virtual Device Manager.Select Tools > Android > AVD Manager, or click the AVD Manager icon in the toolbar. The AVD Manager screen appears.
Step 2:On the AVD Manager main screen, click Create Virtual Device.
Step 3:In the Select Hardware window, select a device configuration, such as Nexus 6, then click Next.
Step 4:Select the desired system version for the AVD and click Next.
Step 5:Verify the configuration settings, then click Finish.
Run the app from Android Studio
In Android Studio, select your project and click Run from the toolbar.
In the Choose Device window, click the Launch emulator radio button.
From the Android virtual device pull-down menu, select the emulator you created, and click OK.
Building a simple user interface
Create a linear layout
In the Preview pane, click the Hide icon to close the Preview pane.
In Android Studio, from the res/layout directory, open the content_my.xml file.
Delete the element.
Change the element to .
Add the android:orientation attribute and set it to “horizontal”.
Remove the android:padding attributes and the tools:context attribute.
Add a text field.
In the content_my.xml file, within the element, define an element with the id attribute set to @+id/edit_message.
Define the layout_width and layout_height attributes as wrap_content.
Define a hint attribute as a string object named edit_message
Add resource string
By default, your Android project includes a string resource file at res/values/strings.xml. Here, you’ll add a new string named “edit_message” and set the value to “Enter a message.”
In Android Studio, from the res/values directory, open strings.xml.
Add a line for a string named “edit_message” with the value, “Enter a message”.
Add a line for a string named “button_send” with the value, “Send”.
Add a button
In Android Studio, from the res/layout directory, edit the content_my.xml file
This layout is applied by the default Activity class that the SDK tools generated when you created the project.
In Android Studio, from the toolbar, click Run .
Another activity
Respond to send button
In Android Studio, from the res/layout directory, edit the content_my.xml file.
Add the android:onClick attribute.
The android:onClick attribute’s value, “sendMessage”, is the name of a method in your activity that the system calls when the user clicks the button.
In the java/com.mycompany.myfirstapp directory, open the MyActivity.java file.
Within the MyActivity class, add the sendMessage() method.
Build an Intent
In MyActivity.java, inside the sendMessage() method, create an Intent to start an activity called DisplayMessageActivity
java/com.mycompany.myfirstapp/MyActivity.java
Intents
An Intent is an object that provides run time binding between separate components . The Intent represents an app’s “intent to do something.”
Android Studio indicates that you must import the Intent class.
At the top of the file, import the Intent class:
java/com.mycompany.myfirstapp/MyActivity.java
Inside the sendMessage() method, use findViewById() to get the EditText element.
java/com.mycompany.myfirstapp/MyActivity.java
At the top of the file, import the EditText class.
Assign the text to a local message variable, and use the putExtra() method to add its text value to the intent.
At the top of the MyActivity class, add the EXTRA_MESSAGE definition as follows:
java/com.mycompany.myfirstapp/MyActivity.java
In the sendMessage() method, to finish the intent, call the startActivity()method, passing it the Intent object created.
The system receives this call and starts an instance of the Activity specified by the Intent. Now you need to create the DisplayMessageActivity class for this to work.