Java For Android - Building Your First Android App
Last Updated :
10 Oct, 2024
Android app development can play a major role in writing the app functionalities using Java programming. Every activity can be designed with Java programming. Android apps are developed using the Android Studio IDE, which provides the environment for Java development for Android programming.
Prerequisites
- Java Programming
- Android Studio in your System
- Emulator for testing App
- Basics of Android
- Basics of XML for UI Design
Key Terminologies
- Activity: Activity can represent the Android screens with a user interface; by default, create the MainActivity, which is the parent activity of the Android app.
- Layouts: This layer can represent the user interface structure using XML. By default, the activity_main layout can be created for the MainActivity screen of the Android app.
- Manifest File: This is the information file of the Android app, containing the essential information about the Android app.
- Resources: This folder contains the Android resource files, which include images, strings, and other assets used in the Android app.
- Intent: It can send messages that allow components to request functionality from others.
- Views: Views can be defined as the UI elements of the screen, such as buttons, text fields, images, etc.; they can be displayed in the activity layout.
Step by Step Implementation
We can develop the first simple Android app using Java. In this application, the user gives the details of their name and education. After that, clicking submit shows the user's details for another activity.
Step 1: If you want to build the Android app using Java. First, install Android Studio on your system.
Refer to this link to install android studio installation.
Step 2: Once you have installed Android Studio, open it and create your first new Android project. For a better understanding, refer to the below image.
Step 3: Once you fill in the details of the project, click on the finish button. After that, select the activity for use in your app. In our case, we can use an empty activity. Once you select the activity, click on Next. Refer to the below image.
Step 4: Once the project is completed, the file structure looks like the below image. Now we are working on creating the first android app using java programming
Step 5: Open activity_main.xml for UI design; it will be located in the layout folder of your project. In this file, we can design the edit texts and one button for submit.
Go to app > layouts > activity_main.xml and enter the below code.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10sp"
android:text="Enter Text: "
android:textSize="20sp" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10sp"
android:hint="Enter Text Here" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Here"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
Step 6: Main Acitivity file is the one shich
MainAcitivity.java
package com.gfg.myapplication;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button1);
EditText editText1 = findViewById(R.id.editText1);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
String text = editText1.getText().toString();
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
}
});
}
}
Step 7: Once runs(refer Android app run) your app is successful. After that, your app can be opened in the emulator. Finally, you can build your first Android app using Java. Your app looks like the below image.
Note: Once you enter the data, click on the submit button. After that, open another activity and display your entered data.1`
By following the procedure above, step-by-step, you can successfully build and run your first Android app using Java programming.
Output Video: