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:
Similar Reads
Android Java Projects - Build Your First Android App from Scratch
Are you eager to boost your Android development skills and create cool projects? Our blog post, "Android Java Projects," is here to help! Whether you're just starting your software development career or want to add good projects to your CV then, working on real projects is the best way to learn and
6 min read
Android | Running your first Android app
After successfully Setting up an Android project, all of the default files are created with default code in them. Let us look at this default code and files and try to run the default app created. The panel on the left side of the android studio window has all the files that the app includes. Under
3 min read
Android | Android Application File Structure
It is very important to know about the basics of Android Studio's file structure. In this article, some important files/folders, and their significance is explained for the easy understanding of the Android studio work environment. In the below image, several important files are marked: All of the f
4 min read
How to Build a Rick and Morty App in Android?
Rick and Morty is an American animated science fiction sitcom created by Justin Roiland and Dan Harmon. In this article, we will build an application that will display the name and image of all rick and Morty characters using this API. In order to build this application we will be using the Retrofit
5 min read
How to Build a Weather App in Android?
In this project, we will be building a weather application. This application will show the temperature of a location. To fetch weather information we will need an API. An API(Application Programming Interface) is a function that allows applications to interact and share data using various components
6 min read
How to Build a Sensor App in Android?
Android mobile phones have sensors, so we can perform various functions like Controlling screen brightness, Monitoring acceleration along a single axis, Motion detection, etc. In this article, we will be building an application that will determine the intensity of light in the room with the help of
5 min read
Build an Android App with HTML, CSS and JavaScript in Android Studio
Android is a mobile operating system based on a modified version of the Linux kernel and other open-source software, designed primarily for touchscreen mobile devices such as smartphones and tablets. Yes, you read it right in the title of this article. In this article, we are going to build an Andro
2 min read
How to Build a QR Code Android App using Firebase?
QR (Quick Response) code is a type of two-dimensional barcode that contains information encoded in a pattern of black and white squares. It was first developed in 1994 by a company named Denso Wave. Qr codes can be scanned by a smartphone or a dedicated QR code scanner, which uses the device's camer
6 min read
How to Build a Simple Alarm Setter App in Android?
In this article, we are going to see how to build a much interesting app named Alarm Setter. Alarm plays a vital role in our day-to-day life. Nowadays alarm has become our wake-up assistant. Every mobile phone is associated with an alarm app. We will create this app using android studio. Android Stu
5 min read
Android Manifest File in Android
Every project in Android includes a Manifest XML file, which is AndroidManifest.xml, located in the root directory of its project hierarchy. The manifest file is an important part of our app because it defines the structure and metadata of our application, its components, and its requirements. This
5 min read