Program 2:
Creating an application that displays message based on the screen orientation.
Step 1: Create a New Android Project
1. Open Android Studio and create a new project.
2. Configure your new project:
- Name: OrientationExample
- Package name: com.example.orientationexample
- Save location: Choose a location on your computer
- Language: Java
- Minimum API level: API 24 (Android 7.0 Nougat)
3. Click Finish to create the project.
Step 2: Create Layouts for Different Orientations
You will create two separate layout files, one for portrait and one for landscape
orientation.
1. Create the Portrait Layout:
- Open `res/layout/activity_main.xml` and update it with the following content:
xml
<?xml version="1.0" encoding="utf-8"?>
<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/textViewMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Portrait Mode"
android:textSize="24sp"
android:textColor="@android:color/black"
android:layout_centerInParent="true" />
</RelativeLayout>
2. Create the Landscape Layout:
- Right-click on `res` and select `New > Android Resource Directory`.
- In the `Resource Directory` dialog, select `layout-land` as the directory name
and `layout` as the resource type, then click `OK`.
- Right-click on `layout-land` and select `New > Layout Resource File`.
- Name the file `activity_main.xml` and click `OK`.
- Open the newly created `activity_main.xml` file under `layout-land` and
update it with the following content:
xml
<?xml version="1.0" encoding="utf-8"?>
<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/textViewMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Landscape Mode"
android:textSize="24sp"
android:textColor="@android:color/black"
android:layout_centerInParent="true" />
</RelativeLayout>
Step 3: Update MainActivity.java
1. Open `MainActivity.java`and update it with the following content:
java
package com.example.orientationexample;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private TextView textViewMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewMessage = findViewById(R.id.textViewMessage);
if (getResources().getConfiguration().orientation ==
android.content.res.Configuration.ORIENTATION_PORTRAIT) {
textViewMessage.setText("Portrait Mode");
} else if (getResources().getConfiguration().orientation ==
android.content.res.Configuration.ORIENTATION_LANDSCAPE) {
textViewMessage.setText("Landscape Mode");
}
}
}
PROGRAM 3:
Create an application to develop login window using UI controls
Step 1: Create a New Android Project
1. Open Android Studio and create a new project.
2. Configure your new project:
- Name: LoginExample
- Package name: com.example.loginexample
- Save location: Choose a location on your computer
- Language: Java
- Minimum API level: API 24 (Android 7.0 Nougat)
3. Click Finish to create the project.
Step 2: Create Layout for MainActivity
1. Open `res/layout/activity_main.xml` and update it with the following content:
xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Username" />
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Password"
android:inputType="textPassword"
android:layout_below="@id/editTextUsername"
android:layout_marginTop="16dp" />
<Button
android:id="@+id/buttonLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_below="@id/editTextPassword"
android:layout_marginTop="16dp" />
<TextView
android:id="@+id/textViewStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonLogin"
android:layout_marginTop="16dp"
android:textSize="18sp"
android:textColor="@android:color/holo_green_dark" />
</RelativeLayout>
Step 3: Implement Login Functionality in MainActivity
1. Open `MainActivity.java` and update it with the following content:
java
package com.example.loginexample;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText editTextUsername;
private EditText editTextPassword;
private Button buttonLogin;
private TextView textViewStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextUsername = findViewById(R.id.editTextUsername);
editTextPassword = findViewById(R.id.editTextPassword);
buttonLogin = findViewById(R.id.buttonLogin);
textViewStatus = findViewById(R.id.textViewStatus);
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = editTextUsername.getText().toString();
String password = editTextPassword.getText().toString();
checkLogin(username, password);
}
});
}
private void checkLogin(String username, String password) {
// Example hardcoded username and password
String correctUsername = "user";
String correctPassword = "pass";
if (username.equals(correctUsername) &&
password.equals(correctPassword)) {
textViewStatus.setText("Login Successful");
} else {
Toast.makeText(MainActivity.this, "Login Failed",
Toast.LENGTH_SHORT).show();
}
}
}
PROGRAM 4:
REFER LAB MANUAL
PROGRAM 5:
Create an application that displays custom designed Opening Screen
Step 1: Create the Splash Screen Layout
1. Open `res/layout/activity_splash.xml` and replace the content with the
following XML:
xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_light">
<ImageView
android:id="@+id/logoImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:layout_centerInParent="true"/>
</RelativeLayout>
Step 2: Create the Splash Activity
1. Open `SplashActivity.java` and replace its content with the following:
java
package com.example.simplesplashscreen;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import androidx.appcompat.app.AppCompatActivity;
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
// Delay for 2 seconds and then start MainActivity
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(mainIntent);
finish();
}
}, 2000); // 2 seconds delay
}
}
Step 3: Configure the Main Activity
1. Open `MainActivity.java` and ensure it looks like this:
java
package com.example.simplesplashscreen;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
2. Update `res/layout/activity_main.xml` to show a simple welcome message:
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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to the Main Activity"
android:textSize="24sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
Step 4: Update the AndroidManifest.xml
1. Open the `AndroidManifest.xml` file and update it to set `SplashActivity` as
the launcher activity:
xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.simplesplashscreen">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity" />
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Step 5: Run Your Application
1. Connect your Android device or start an emulator.
2. Run your application by clicking the Run button in Android Studio.
PROGRAM 6 & 7:
REFER LAB MANUAL
PROGRAM 8:
Read/ write the Local data
Step 1: Create a New Android Project
1. Open Android Studio and create a new project.
2. Configure your new project:
- Name: SharedPreferencesExample
- Package name: com.example.sharedpreferencesexample
- Save location: Choose a location on your computer
- Language: Java
- Minimum API level: API 24 (Android 7.0 Nougat)
3. Click Finish to create the project.
Step 2: Create a Simple Layout for MainActivity
1. Open `res/layout/activity_main.xml` and update it with the following content:
xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name" />
<Button
android:id="@+id/buttonSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:layout_below="@id/editTextName"
android:layout_marginTop="16dp" />
<Button
android:id="@+id/buttonLoad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Load"
android:layout_below="@id/buttonSave"
android:layout_marginTop="16dp" />
<TextView
android:id="@+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your name is: "
android:textSize="18sp"
android:layout_below="@id/buttonLoad"
android:layout_marginTop="16dp" />
</RelativeLayout>
Step 3: Implement Reading and Writing Using SharedPreferences
1. Open `MainActivity.java` and update it with the following content:
java
package com.example.sharedpreferencesexample;
import android.content.SharedPreferences;
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 textViewName;
private Button buttonSave, buttonLoad;
private static final String PREFS_NAME = "MyPrefsFile";
private static final String KEY_NAME = "name";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextName = findViewById(R.id.editTextName);
textViewName = findViewById(R.id.textViewName);
buttonSave = findViewById(R.id.buttonSave);
buttonLoad = findViewById(R.id.buttonLoad);
buttonSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveData();
}
});
buttonLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadData();
}
});
}
private void saveData() {
String name = editTextName.getText().toString();
SharedPreferences sharedPreferences =
getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(KEY_NAME, name);
editor.apply();
editTextName.setText("");
}
private void loadData() {
SharedPreferences sharedPreferences =
getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
String name = sharedPreferences.getString(KEY_NAME, "No name defined");
textViewName.setText("Your name is: " + name);
}
}
Explanation:
- XML Layout:
- `EditText`: For the user to enter their name.
- `Button`: Two buttons for saving and loading data.
- `TextView`: To display the loaded name.
- Java Activity:
- saveData(): Retrieves the name from the `EditText` and saves it to
SharedPreferences.
- loadData(): Retrieves the name from SharedPreferences and displays it in the
`TextView`.
Step 4: Run Your Application
1. Connect your Android device or start an emulator.
2. Run your application by clicking the Run button in Android Studio.
PROGRAM 9:
Create / Read / Write data with database (SQL Lite) simple program
Step 1: Create a New Android Project
1. Open Android Studio and create a new project.
2. Configure your new project:
- Name: SQLiteExample
- Package name: com.example.sqliteexample
- Save location: Choose a location on your computer
- Language: Java
- Minimum API level: API 24 (Android 7.0 Nougat)
3. Click Finish to create the project.
Step 2: Create Database Helper Class
1. Create a new Java class in your project named `DatabaseHelper`:
java
package com.example.sqliteexample;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "example.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "users";
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_AGE = "age";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_NAME + " TEXT, " +
COLUMN_AGE + " INTEGER)";
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public void addUser(String name, int age) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, name);
values.put(COLUMN_AGE, age);
db.insert(TABLE_NAME, null, values);
db.close();
}
public Cursor getUsers() {
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT * FROM " + TABLE_NAME;
return db.rawQuery(query, null);
}
}
Step 3: Create Layout for MainActivity
1. Open `res/layout/activity_main.xml` and update it with the following content:
xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter name" />
<EditText
android:id="@+id/editTextAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter age"
android:layout_below="@id/editTextName"
android:layout_marginTop="16dp" />
<Button
android:id="@+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add User"
android:layout_below="@id/editTextAge"
android:layout_marginTop="16dp" />
<Button
android:id="@+id/buttonLoad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Load Users"
android:layout_below="@id/buttonAdd"
android:layout_marginTop="16dp" />
<TextView
android:id="@+id/textViewUsers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonLoad"
android:layout_marginTop="16dp"
android:textSize="16sp"/>
</RelativeLayout>
Step 4: Implement Database Operations in MainActivity
1. Open `MainActivity.java` and update it with the following content:
java
package com.example.sqliteexample;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText editTextName;
private EditText editTextAge;
private Button buttonAdd;
private Button buttonLoad;
private TextView textViewUsers;
private DatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextName = findViewById(R.id.editTextName);
editTextAge = findViewById(R.id.editTextAge);
buttonAdd = findViewById(R.id.buttonAdd);
buttonLoad = findViewById(R.id.buttonLoad);
textViewUsers = findViewById(R.id.textViewUsers);
dbHelper = new DatabaseHelper(this);
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addUser();
}
});
buttonLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadUsers();
}
});
}
private void addUser() {
String name = editTextName.getText().toString();
String ageString = editTextAge.getText().toString();
if (name.isEmpty() || ageString.isEmpty()) {
Toast.makeText(this, "Please enter both name and age",
Toast.LENGTH_SHORT).show();
return;
}
int age = Integer.parseInt(ageString);
dbHelper.addUser(name, age);
editTextName.setText("");
editTextAge.setText("");
Toast.makeText(this, "User added", Toast.LENGTH_SHORT).show();
}
private void loadUsers() {
Cursor cursor = dbHelper.getUsers();
StringBuilder users = new StringBuilder();
if (cursor.moveToFirst()) {
do {
int id = cursor.getInt(0);
String name = cursor.getString(1);
int age = cursor.getInt(2);
users.append("ID: ").append(id).append(", Name:
").append(name).append(", Age: ").append(age).append("\n");
} while (cursor.moveToNext());
}
textViewUsers.setText(users.toString());
}
}
Explanation:
1. DatabaseHelper:
- Creates the database and a table for users.
- Provides methods to add users and retrieve users from the database.
2. MainActivity:
- Provides UI to input user data and buttons to add and load users.
- Implements methods to add users to the database and display all users.
Step 5: Run Your Application
1. Connect your Android device or start an emulator.
2. Run your application by clicking the Run button in Android Studio.
REMAINING PROGRAMS REFER LAB MANUAL ONLY .
NOTE: FOR 13TH WRITE THE SAME PROGRAM 3.