Mobile App Development
Mobile App Development
Go to the https://developer.android.com/studio
Windows:
After setup, click on New Project to verify the IDE is working correctly.
Go to File > Settings > Appearance & Behavior > System Settings > Android SDK.
Select the SDK Platforms tab, choose the latest version, and click Apply.
Output :
Result :
Program :
activity_main.xml: This is the layout file where you define your UI elements. You can
find it under app > res > layout.
By default, a TextView saying "Hello World!" is already placed in the layout file.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/helloWorldText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_centerInParent="true"
android:textSize="24sp"/>
</RelativeLayout>
Usually, no changes are needed unless you want to add functionality to the button or
other elements.
Here's a basic example of MainActivity.java:
// src/main/java/com/example/helloworldapp/MainActivity.java
package com.example.helloworldapp;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Output :
Result :
Aim :
To Create a application that takes the name from a text box and shows hello message
along with the name entered in text box, when the user clicks the OK button
Program :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editTextName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:inputType="textPersonName"/>
<Button
android:id="@+id/buttonOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:layout_below="@id/editTextName"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>
<TextView
android:id="@+id/textViewMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonOK"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textSize="18sp"/>
</RelativeLayout>
3. Modify the Main Activity (MainActivity.java):
// src/main/java/com/example/hellonameapp/MainActivity.java
package com.example.hellonameapp;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText editTextName;
private TextView textViewMessage;
private Button buttonOK;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize views
editTextName = findViewById(R.id.editTextName);
textViewMessage = findViewById(R.id.textViewMessage);
buttonOK = findViewById(R.id.buttonOK);
// Set a click listener on the OK button
buttonOK.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get the name entered in the EditText
String name = editTextName.getText().toString().trim();
// Display the hello message
if (!name.isEmpty()) {
textViewMessage.setText("Hello, " + name + "!");
} else {
textViewMessage.setText("Please enter your name.");
}
}
});
}}
4. Run the Application:
• Click the Run button (green triangle) at the top of the Android Studio or press Shift +
F10.
• Choose your connected device or emulator to run the app.
Output :
Result :
Hence the Program of Creating a application that takes the name
from a text box and shows hello message along with the name entered in text box,
when the user clicks the OK button has been Executed Successfully.
4. Create a screen that has input boxes for User Name, Password, Address,
Gender (radio buttons for male and female), Age (numeric), Date of Birth
(Date Picket), State (Spinner) and a Submit button. On clicking the submit
button, print all the data below the Submit Button
Aim :
To Create Create a screen that has input boxes for User Name, Password,
Address, Gender (radio buttons for male and female), Age (numeric), Date of Birth
(Date Picket), State (Spinner) and a Submit button. On clicking the submit button, print
all the data below the Submit Button.
Program :
</LinearLayout>
</ScrollView>
• Add logic to capture user input when the Submit button is clicked and display the
entered information.
// src/main/java/com/example/userformapp/MainActivity.java
package com.example.userformapp;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private EditText editTextUserName, editTextPassword, editTextAddress,
editTextAge, editTextDOB;
private RadioGroup radioGroupGender;
private Spinner spinnerState;
private Button buttonSubmit;
private TextView textViewResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize views
editTextUserName = findViewById(R.id.editTextUserName);
editTextPassword = findViewById(R.id.editTextPassword);
editTextAddress = findViewById(R.id.editTextAddress);
editTextAge = findViewById(R.id.editTextAge);
editTextDOB = findViewById(R.id.editTextDOB);
radioGroupGender = findViewById(R.id.radioGroupGender);
spinnerState = findViewById(R.id.spinnerState);
buttonSubmit = findViewById(R.id.buttonSubmit);
textViewResult = findViewById(R.id.textViewResult);
// Set up the state spinner with sample states
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this,
R.array.states_array,
android.R.layout.simple_spinner_item);adapter.setDropDownViewResource(an
droid.R.layout.simple_spinner_dropdown_item);
spinnerState.setAdapter(adapter);
// Set up Date Picker dialog for Date of Birth
editTextDOB.setOnClickListener(v -> showDatePickerDialog());
// Set up Submit button click listener
buttonSubmit.setOnClickListener(v -> displayUserData());
}
// Function to show DatePicker dialog
private void showDatePickerDialog() {
final Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
(view, year1, monthOfYear, dayOfMonth) ->
editTextDOB.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year1),
year, month, day);
datePickerDialog.show();
}
// Function to display user data below the Submit button
private void displayUserData() {
String userName = editTextUserName.getText().toString();
String password = editTextPassword.getText().toString();
String address = editTextAddress.getText().toString();
String age = editTextAge.getText().toString();
String dob = editTextDOB.getText().toString();
String gender = ((RadioButton)
findViewById(radioGroupGender.getCheckedRadioButtonId())).getText().toStr
ing();
String state = spinnerState.getSelectedItem().toString();
Output :
Result :
Hence the Program of Creating a screen that has input boxes for User
Name, Password, Address, Gender (radio buttons for male and female),
Age (numeric), Date of Birth (Date Picket), State (Spinner) and a Submit
button. On clicking the submit button, print all the data below the Submit
Button has been Executed Successfully.
5. Design an android application to create page using Intent and one
Button and pass the Values from one Activity to second Activity.
Aim :
To Design an android application to create page using Intent and one Button and
pass the Values from one Activity to second Activity.
Procedure:
• Go to File > New > Activity > Empty Activity to create a second activity.
• Name the second activity as SecondActivity.java (for Java) or SecondActivity.kt (for
Kotlin).
• Ensure that SecondActivity.java is automatically added to the AndroidManifest.xml.
// src/main/java/com/example/intentexampleapp/MainActivity.java
package com.example.intentexampleapp;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText editTextName;
private Button buttonSend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize views
editTextName = findViewById(R.id.editTextName);
buttonSend = findViewById(R.id.buttonSend);
// Set onClickListener for the button to send data
buttonSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get the name from EditText
String name = editTextName.getText().toString();
// Create an Intent to navigate to the SecondActivity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
// Pass the name using putExtra
intent.putExtra("user_name", name);
// Start SecondActivity
startActivity(intent);
}
});
}
}
6. Modify the SecondActivity.java:
// src/main/java/com/example/intentexampleapp/SecondActivity.java
package com.example.intentexampleapp;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
private TextView textViewResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
// Initialize TextView
textViewResult = findViewById(R.id.textViewResult);
// Get the Intent that started this activity
Intent intent = getIntent();
// Retrieve the name from the Intent extras
String userName = intent.getStringExtra("user_name");
// Display the name in the TextView
textViewResult.setText("Hello, " + userName);
}
}
7. Update AndroidManifest.xml:
• Click the Run button or press Shift + F10 to run the application.
• Choose your emulator or connected device.
Output :
Result :
Hence the Program of Designing an android application to
create page using Intent and one Button and pass the Values from one
Activity to second Activity has been Executed Successfully.
6. Design an android application Send SMS using Internet
Aim :
Click the Run button or press Shift + F10 to run the application.
Output :
No Output.
Result :
Aim :
2. Create Fragments
package com.example.fragmentapp;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class FragmentOne extends Fragment {
public FragmentOne() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_one, container, false);
// Button to switch fragments
Button button = view.findViewById(R.id.buttonGoToFragmentTwo);
button.setOnClickListener(v -> {
// Fragment transaction to replace FragmentOne with FragmentTwo
FragmentTwo fragmentTwo = new FragmentTwo();
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, fragmentTwo)
.addToBackStack(null)
.commit();
});
return view;
}
}
FragmentTwo.java (Second Fragment)
package com.example.fragmentapp;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentTwo extends Fragment {
public FragmentTwo() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_two, container, false);
}
}
MainActivity.java
In MainActivity, we'll load the first fragment (FragmentOne) when the activity starts.
package com.example.fragmentapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
This layout defines a container (FrameLayout) where fragments will be dynamically loaded.
fragment_one.xml
This is the layout for FragmentOne, containing a button that switches to FragmentTwo.
fragment_two.xml
Output :
Result :
Hence the Program of Create an android application using
Fragments has been Executed Successfully.
Aim :
We will use RadioGroup to group the radio buttons and ensure only one option can be selected
at a time. A button will trigger the action to display the selected radio button's value.
MainActivity.java
package com.example.radiobuttonapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
RadioGroup radioGroup;
RadioButton selectedRadioButton;
Button buttonSubmit;
TextView textViewResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = findViewById(R.id.radioGroup);
buttonSubmit = findViewById(R.id.buttonSubmit);
textViewResult = findViewById(R.id.textViewResult);
// Set OnClickListener on the Submit button
buttonSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get the selected radio button ID from the RadioGroup
int selectedId = radioGroup.getCheckedRadioButtonId();
// Check if any radio button is selected
if (selectedId != -1) {
// Find the selected RadioButton by its ID
selectedRadioButton = findViewById(selectedId);
String selectedOption = selectedRadioButton.getText().toString();
// Display the selected option in the TextView
textViewResult.setText("Selected: " + selectedOption);
} else {
// Show a toast if no option is selected
Toast.makeText(MainActivity.this, "Please select an option",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
activity_main.xml
Result :
Hence the Program of Android Application Using Radio Buttons
has been Executed Successfully.
9. Design an android application with menu
Aim :
We will define the menu items in an XML file stored in the res/menu folder.
The menu is added to the toolbar in the activity, and clicking the menu items will trigger
specific actions.
MainActivity.java
package com.example.menuapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.settings:
Toast.makeText(this, "Settings selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.about:
Toast.makeText(this, "About selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.help:
Toast.makeText(this, "Help selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Create the Menu XML File
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
Run the Application:
1. When the app runs, you'll see the main screen with a TextView.
2. Tap the three-dot menu in the top-right corner to see the menu options (Settings, About,
Help).
3. When you tap one of the menu options, a Toast message displays the corresponding action
(e.g., "Settings selected").
Output :
Result :
Hence the Program of Design an android application with menu
has been Executed Successfully.
10. Create a user registration application that stores the user details in a
database table.
Aim :
To Create a Create a user registration application that stores the user details in a
database table.
MainActivity.java
package com.example.registrationapp;
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;
public class MainActivity extends AppCompatActivity {
EditText username, password, email, phone;
Button registerButton;
DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize views
username = findViewById(R.id.editTextUsername);
password = findViewById(R.id.editTextPassword);
email = findViewById(R.id.editTextEmail);
phone = findViewById(R.id.editTextPhone);
registerButton = findViewById(R.id.buttonRegister);
// Initialize database helper
databaseHelper = new DatabaseHelper(this);
// Set onClickListener for the register button
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String user = username.getText().toString();
String pass = password.getText().toString();
String mail = email.getText().toString();
String phoneNo = phone.getText().toString();
// Check if any field is empty
if(user.isEmpty() || pass.isEmpty() || mail.isEmpty() || phoneNo.isEmpty()) {
Toast.makeText(MainActivity.this, "All fields are required", Toast.LENGTH_SHORT).show();
} else {
// Insert data into database
boolean isInserted = databaseHelper.insertUserData(user, pass, mail, phoneNo);
if(isInserted) {
Toast.makeText(MainActivity.this, "User Registered Successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Registration Failed", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
DatabaseHelper.java
package com.example.registrationapp;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
// Database name and version
private static final String DATABASE_NAME = "UserDB";
private static final int DATABASE_VERSION = 1;
// Table and column names
private static final String TABLE_NAME = "users";
private static final String COLUMN_ID = "id";
private static final String COLUMN_USERNAME = "username";
private static final String COLUMN_PASSWORD = "password";
private static final String COLUMN_EMAIL = "email";
private static final String COLUMN_PHONE = "phone";
// Constructor
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// Create users table
String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COLUMN_USERNAME + " TEXT,"
+ COLUMN_PASSWORD + " TEXT,"
+ COLUMN_EMAIL + " TEXT,"
+ COLUMN_PHONE + " TEXT" + ")";
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
// Create tables again
onCreate(db);
}
// Insert user data into database
public boolean insertUserData(String username, String password, String email, String phone) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_USERNAME, username);
contentValues.put(COLUMN_PASSWORD, password);
contentValues.put(COLUMN_EMAIL, email);
contentValues.put(COLUMN_PHONE, phone);
// Insert data into table
long result = db.insert(TABLE_NAME, null, contentValues);
db.close();
// Check if insertion is successful
return result != -1;
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username" />
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
<EditText
android:id="@+id/editTextEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/editTextPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone Number"
android:inputType="phone" />
<Button
android:id="@+id/buttonRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register" />
</LinearLayout>
Run the Application:
1. The user enters their details (username, password, email, and phone number).
2. The user clicks the Register button.
3. If all fields are filled, the app stores the user’s data in the SQLite database and displays a
success message.
Output :
Result :
Hence the Program of Create a user registration application that
stores the user details in a database table has been Executed Successfully.