Open In App

Java For Android – Building Your First Android App

Last Updated : 21 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

projectconfigAndroid

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.

openProject

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

androidFileStructure


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.

XML
<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editTextName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Name"
        android:layout_margin="16dp"/>

    <EditText
        android:id="@+id/editTextEducation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Education"
        android:layout_below="@id/editTextName"
        android:layout_margin="16dp"/>

    <Button
        android:id="@+id/buttonSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit"
        android:layout_below="@id/editTextEducation"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="32dp"/>

</RelativeLayout>

Step 6: In the same folder, create activity_details.xml for the UI design for displaying the user data.It will be located in the layout folder of your project. In this file, we can design a single text view.

Go to app > layouts > activity_details.xml and enter the below code.

XML
<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".DetailsActivity">

    <TextView
        android:id="@+id/textViewDetails"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:layout_centerInParent="true"/>

</RelativeLayout>
XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AndroidApp"
        tools:targetApi="31">
        <activity
            android:name=".DetailsActivity"
            android:exported="false"
            android:label="@string/title_activity_details"
            android:theme="@style/Theme.AndroidApp.NoActionBar" />
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.AndroidApp">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Step 7: Now we create the functionalities of the Android app using Java programming. Open the MainActivity.java file in the com.example.androidapp package.

Go to app > java > com.example.androidapp > MainActivity.java and put the below code.

Java
package com.example.androidapp;

import android.content.Intent;
import android.os.Bundle;

import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;

import android.view.View;

import androidx.core.view.WindowCompat;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.androidapp.databinding.ActivityMainBinding;

import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private EditText editTextName;
    private EditText editTextEducation;
    private Button buttonSubmit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editTextName = findViewById(R.id.editTextName);
        editTextEducation = findViewById(R.id.editTextEducation);
        buttonSubmit = findViewById(R.id.buttonSubmit);

        buttonSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = editTextName.getText().toString();
                String education = editTextEducation.getText().toString();

                Intent intent = new Intent(MainActivity.this, DetailsActivity.class);
                intent.putExtra("name", name);
                intent.putExtra("education", education);
                startActivity(intent);
            }
        });


    }

}

Step 8: In this same package, we can create one more activity to display the user data and write the functionalities of the Android app using Java programming. Create the DetailsActivity.java file in the com.example.androidapp package and open it.

Go to app > java > com.example.androidapp > DetailsActivity.java and enter the below code.

Java
package com.example.androidapp;

import android.os.Bundle;

import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.View;
import android.widget.TextView;

import com.example.androidapp.databinding.ActivityDetailsBinding;

public class DetailsActivity extends AppCompatActivity {
    private TextView textViewDetails;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        textViewDetails = findViewById(R.id.textViewDetails);

        // Get data passed from MainActivity
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String name = extras.getString("name");
            String education = extras.getString("education");
            textViewDetails.setText("Name: " + name + "\nEducation: " + education);
        }



    }
}

Step 9: Once you have successfully built your first Android app, you can run into the Android Studio. Once successfully built, the Android app shows the output. For better understanding, refer to the below image.

launchApp-compressed

Step 10: 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.

androidOuput1

Note: Once you enter the data, click on the submit button. After that, open another activity and display your entered data.

androidoutput2

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 | 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 the java folder, observe the first folder containi
3 min read
How to Share Text of Your App with Another App and Vice Versa in Android?
Most of the time while using an app we want to share text from the app to another app. While using Many Social Media Platforms we find this feature to be very useful when we want to share information from one app to another. The Android intent resolver is used when sending data to another app as part of a well-defined task flow. To use the Android
5 min read
How to Share Image of Your App with Another App in Android?
Most of the time while using an app we want to share images from the app to another app. While using Many Social Media Platforms we find this feature to be very useful when we want to share information from one app to another. The Android Intent resolver is used when sending data to another app as part of a well-defined task flow. To use the Androi
4 min read
Flutter - Building an Alarm Clock App
Many applications like the Todo app, Exercise App, require alarm clocks to keep track of time in activities. Or if you want to create an alarm clock app for yourself, you will learn this today. If your app contains features like setting the alarm or setting the timer, how you can do this in Flutter? Don’t worry, it's easy with the flutter_alarm_clo
9 min read
Building a Task Reminder App with Next.js
In this article, we’ll walk through the step-by-step process of creating a basic task reminder app with Next.JS. This application will provide users with a user-friendly interface for adding the title of the task, its description, and its due date &amp; time. The user will also be notified through a pop-up 1 minute before the task scheduled time. O
4 min read
How to Integrate In-App Review API into Android App?
Once we publish our app over the PlayStore and as it became live there, app rating and reviews become very crucial to driving audience and downloads to your app. In order to increase this, we ask our users to rate the app through a popup window and redirecting them to the PlayStore. But this nowadays creates little problems, once users went to Play
3 min read
7 Major Reasons Why Your Android App is Performing Slow - Must Read For Developers!
"Nothing worse than a laggy slow app!". Laggy apps deliver bad experiences to users. It’s true that most of the end-users can’t wait to get a response from a mobile app. The absolute loading time for a mobile app is about two seconds. Still, according to research by Akamai research, for every extra second that the app consumes, the conversion rate
5 min read
How to Add Fingerprint Authentication in Your Android App without Using any Library?
Biometric Fingerprint authentication is one method of protecting sensitive information or premium content of your app. Nowadays, all payment apps use this feature in their app. It is very easy to implement. A sample Video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project us
6 min read
How to Check if Application is Installed in Your Android Phone and Open the App?
In this article, we are going to check if a particular app is installed on our phones or not. If yes then we will be having an option to open the app. Else it will show a toast message saying Not available. So here we are going to learn how to implement that feature. Note that we are going to implement this project using the Java language. Step by
3 min read
How to Add SHA Fingerprints For Your Firebase Android App?
In the field of cryptography, SHA-1 (Secure Hash Algorithm 1) is a hash function that accepts an input and generates a 160-bit (20-byte) hash value known as a message digest, which is commonly represented by 40 hexadecimal digits. Despite being cryptographically weak, SHA-1 is still frequently used. You must supply the SHA-1 of your signing certifi
2 min read
Article Tags :
Practice Tags :