Step by Step Android Projects
Step by Step Android Projects
A Beginner’s Guide
Introduction
• Select I have read and agree with the above terms and conditions. Then click to download
Android Studio.
• When opening Android Studio for the first time, AS will run the Android Studio Setup
Wizard.
Generally, keep Next and finally click Finish.
Next, the data will be downloaded from the network, please wait a moment.
• Step 3: Click New Project and choose the simplest traditional layout Empty Views
Activity.
• Step 3: Fill in the project details:
o Name: LoginApp
o Language: Java
o Minimum SDK: Leave the default
Then the source code will be opened and displayed. We wait for gradle to be downloaded and the
Android project to be loaded.
This step of Sync may take a long time, so please wait a moment.
After Sync is completed, click Run app to run it in the Emulator.
If you need to manage Android SDK, such as installing new SDK Platforms and SDK Tools, you
can configure it by opening Settings -> Languages & Frameworks -> Android SDK.
Click OK to download tools
Sample Code:
Screenshot: Code Editor with XML layout
Adding Functionality
Java Code:
package com.gasco.gee.example.simplelogin;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Bind views
editTextUsername = findViewById(R.id.editTextTextEmailAddress);
editTextPassword = findViewById(R.id.editTextTextPassword);
btnLogin = findViewById(R.id.btnLogin);
// Validate login
if (enteredUsername.isEmpty() || enteredPassword.isEmpty()) {
Toast.makeText(MainActivity.this, "Please enter both username and password.",
Toast.LENGTH_SHORT).show();
} else if (enteredUsername.equals(validUsername) && enteredPassword.equals(validPassword))
{
Toast.makeText(MainActivity.this, "Login Successful!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Invalid username or password.",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
Introduction
Click on Finish
Designing the Layout
Sample Code:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/edtNumber1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter first number"
android:inputType="numberDecimal" />
<EditText
android:id="@+id/edtNumber2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter second number"
android:inputType="numberDecimal" />
<Button
android:id="@+id/btnCalculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Add Numbers" />
<EditText
android:id="@+id/edtResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Result"
android:inputType="none"
android:focusable="false" />
</LinearLayout>
Adding Functionality
Java Code:
package com.example.additioncalculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize views
edtNumber1 = findViewById(R.id.edtNumber1);
edtNumber2 = findViewById(R.id.edtNumber2);
edtResult = findViewById(R.id.edtResult);
btnCalculate = findViewById(R.id.btnCalculate);
// Validate inputs
if (num1Text.isEmpty() || num2Text.isEmpty()) {
Toast.makeText(MainActivity.this, "Please enter both numbers",
Toast.LENGTH_SHORT).show();
return;
}
try {
// Perform addition
double num1 = Double.parseDouble(num1Text);
double num2 = Double.parseDouble(num2Text);
double result = num1 + num2;
// Display result
edtResult.setText(String.valueOf(result));
} catch (NumberFormatException e) {
Toast.makeText(MainActivity.this, "Invalid input", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Introduction
Sample Code:
Screenshot: Code Editor with XML layout.
Adding Functionality
Java Code:
Screenshot: Full code in the editor.
Introduction
• Name: GradingSystem
• Language: Java
• Minimum SDK: Leave default.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
package com.example.gradingsystem;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
EditText edtTotalMark;
Button btnCalculateGrade;
TextView tvGrade;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize views
edtTotalMark = findViewById(R.id.edtTotalMark);
btnCalculateGrade = findViewById(R.id.btnCalculateGrade);
tvGrade = findViewById(R.id.tvGrade);
if (markInput.isEmpty()) {
Toast.makeText(MainActivity.this, "Please enter a total mark",
Toast.LENGTH_SHORT).show();
return;
}
try {
// Convert input to integer
int totalMark = Integer.parseInt(markInput);
// Determine grade
String grade;
if (totalMark >= 90) {
grade = "A";
} else if (totalMark >= 80) {
grade = "B";
} else if (totalMark >= 70) {
grade = "C";
} else if (totalMark >= 60) {
grade = "D";
} else {
grade = "F";
}
// Display grade
tvGrade.setText("Grade: " + grade);
} catch (NumberFormatException e) {
Toast.makeText(MainActivity.this, "Invalid input", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Test the App
• Run the app on an emulator or physical device.
Introduction
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Page 1"
android:textSize="32sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="match_parent"
android:layout_height="231dp"
app:srcCompat="@drawable/facebook" />
</LinearLayout>
package com.gasco.gee.example.page1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
ImageButton img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.imageButton);
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, Page2.class);
startActivity(i);
}
});
}
}
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Page 2"
android:textSize="32sp"
android:textStyle="bold"
tools:layout_editor_absoluteX="165dp"
tools:layout_editor_absoluteY="292dp" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="125dp"
app:srcCompat="@drawable/back" />
</LinearLayout>
JAVA Codes for Page 2
package com.gasco.gee.example.page1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page2);
img = findViewById(R.id.imageView2);
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Page2.this, MainActivity.class);
startActivity(i);
}
});
}
}
Introduction
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="SQLLite Database Form"
android:textSize="24sp" />
<EditText
android:id="@+id/edtName"
android:layout_width="match_parent"
android:layout_height="41dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="NAME"
android:inputType="text" />
<EditText
android:id="@+id/edtAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="AGE"
android:layout_marginTop="10dp"
android:inputType="numberDecimal" />
<EditText
android:id="@+id/edtPhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:layout_marginTop="10dp"
android:hint="Phone Number"
android:inputType="phone" />
<Spinner
android:id="@+id/spinnerGender"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:text="Submit Data" />
<Button
android:id="@+id/btnViewData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="View All Saved Data" />
</LinearLayout>
• package com.gasco.gee.example.sqliteformapp;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
db.execSQL("DROP TABLE IF EXISTS users");
onCreate(db);
}
}
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
// Initialize Views
edtName = (EditText) findViewById(R.id.edtName);
edtAge = (EditText) findViewById(R.id.edtAge);
edtPhone = (EditText) findViewById(R.id.edtPhoneNumber);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnViewAll = (Button) findViewById(R.id.btnViewData);
genderSpinner = findViewById(R.id.spinnerGender);
// Populate Spinner
String[] genderOptions = {"Select Gender", "Male", "Female",
"Other"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, genderOptions);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdow
n_item);
genderSpinner.setAdapter(adapter);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get input data
String name = edtName.getText().toString();
String ageText = edtAge.getText().toString();
String phone = edtPhone.getText().toString();
String gender =
genderSpinner.getSelectedItem().toString();
btnViewAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new
Intent(MainActivity.this,ShowDataActivity.class);
startActivity(i);
}
});
}
}
Layout Design
XML Codes
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
ListView listView;
DatabaseHelper dbHelper = new DatabaseHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_data);
listView = findViewById(R.id.listview);
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
long clickTime = System.currentTimeMillis();
if (clickTime - lastClickTime < DOUBLE_CLICK_THRESHOLD)
{
// Double-click detected
int userId = userIds.get(position); // Get the ID
of the selected user
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete("users", "id=?", new
String[]{String.valueOf(userId)});
Toast.makeText(ShowDataActivity.this, "User
deleted", Toast.LENGTH_SHORT).show();
lastClickTime = clickTime;
}
});
}
}
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:gravity="center"
android:textStyle="bold"
android:text="SQLITE DATABASE FORM"
android:textSize="24sp" />
<EditText
android:id="@+id/edtName2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:ems="10"
android:gravity="center"
android:hint="NAME"
android:inputType="text"
android:textSize="24sp" />
<EditText
android:id="@+id/edtAge2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:ems="10"
android:gravity="center"
android:hint="ENTER YOUR AGE"
android:inputType="number" />
<EditText
android:id="@+id/edtPhoneNumber2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="3dp"
android:layout_marginBottom="10dp"
android:ems="10"
android:gravity="center"
android:hint="PHONE NUMBER"
android:inputType="text" />
<Spinner
android:id="@+id/spinnerGender2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="-3dp"
android:layout_marginBottom="10dp" />
<Button
android:id="@+id/btnUpdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="UPDATE DATA" />
<Button
android:id="@+id/btnViewData2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:text="VIEW ALL DATA" />
</LinearLayout>
package com.gasco.gee.example.sqliteformapp;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
//Variable declaration
Spinner genderSpinner;
EditText edtName, edtAge, edtPhone;
Button btnUpdate, btnViewAll;
DatabaseHelper dbHelper = new DatabaseHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
// Initialize Views
edtName = (EditText) findViewById(R.id.edtName2);
edtAge = (EditText) findViewById(R.id.edtAge2);
edtPhone = (EditText) findViewById(R.id.edtPhoneNumber2);
btnUpdate = (Button) findViewById(R.id.btnUpdate);
btnViewAll = (Button) findViewById(R.id.btnViewData2);
genderSpinner = findViewById(R.id.spinnerGender2);
SQLiteDatabase db = dbHelper.getReadableDatabase();
String selection = "id = ?";
String[] selectionArgs = {String.valueOf(cid)};
Cursor cursor = db.query("users", null, selection, selectionArgs, null, null, null);
genderAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Validate input
if (name.isEmpty() || ageString.isEmpty() || phone.isEmpty()) {
Toast.makeText(this, "Please fill in all fields", Toast.LENGTH_SHORT).show();
return;
}