This is the third day of my participation in the August More Text Challenge
The following tutorial walks you through the process of creating a basic Android application using Android Studio. It will help you take your first steps toward learning Android development.
The application will have a simple UI and a few lines of Java code so you can get started on Android development. With basic validation, you can certainly make it more feature-rich.
We’ll use Android Development Studio and explain step-by-step how to use it. The tutorial also uses images to determine what you need to do.
How to use Android Studio to create Android applications
Create Android applications in Studio
- Start Android Development Studio
- On the screen above, click “Start a New Android Studio Project”.
Name your application “MyFirstProject” and click Next.
Add the activity to the Android application
In this step, we will add an empty activity to our application. To do this, click the Next button on the screen shown below.
Now, select the Empty Activity template from the options, and click the Next button.
Click the finish button.
Update the Android application XML file
Now in the RES folder, go to the activity_main.xml file in the Layout folder and write the following code.
Remember, here we are creating our first project, so we will create a screen that displays the message “Hello World” and the “Click me” button.
In the body, TAB copies the following code.
The following.xml file is the layout file for MainActivity. You can create an interactive UI here.
The file has a TextView and a button that can be brought to the design screen by dragging and dropping from the left panel.
Exercise: Try to place various elements in the sample screen.
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" 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.soumyaagarwal.myfirstproject.MainActivity"><TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_centerInParent="true"
android:textColor="# 000000"
android:textSize="40dp"
android:textStyle="bold"
android:id="@+id/textView" />
<Button
android:text="Click Me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="61dp"
android:id="@+id/button" />
</RelativeLayout>
Copy the code
Update the mainactivity. Java file
In the Java folder, go to the mainActivity.java file in the package and write the following code.
Here the “ID” of the button is given, and then the Click Listener is set. After clicking this “Click me” button, we are presented with a toast (one that says “Button clicked! “).
package com.example.soumyaagarwal.myfirstproject;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivityextends AppCompatActivity {
Button clickme;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
clickme= (Button)findViewById(R.id.button);
clickme.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this."The Button is clicked !", Toast.LENGTH_LONG).show(); }}); }}Copy the code
Now we need to run our first project. Even though we had the Genymotion emulator installed, running the application on a real device was still quick. So, connect your Android device to your computer and click the Run option on the toolbar.
You will find the following dialog box.
Select the device you are connected to, and then click OK.
Now let the Gradle build complete. After that, you’ll see your first application launch on your Android device.
The following is a screenshot of the launch application.
When you CLICK the “CLICK ME” button, a prompt appears.
conclusion
Great! You’ve finished your first Android app. We hope you should now be able to add more features and functionality to this application.
If you have any questions about the above code, please leave a comment. Also, you should read the Android Studio tutorial below to see how to set it up properly.