Mad Lab Programs Mannual
Mad Lab Programs Mannual
XML CODE:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.438" />
<Button
android:id="@+id/hello"
android:layout_width="309dp"
android:layout_height="166dp"
android:background="#535538"
android:text="click Me"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.627"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.55" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.myapplication1;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.activity.EdgeToEdge;
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 b;
b=findViewById(R.id.hello);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"Hey! we are using Android
Application",Toast.LENGTH_SHORT).show();
}
});
}
}
SNAPSHOT
Program 2: Creating an application that displays message based on the screen orientation.
XML CODE:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.438" />
<Button
android:id="@+id/hello"
android:layout_width="309dp"
android:layout_height="166dp"
android:background="#535538"
android:text="click Me"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.627"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.55" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.myapplication3;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.Button;
import androidx.activity.EdgeToEdge;
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);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
Button l,p;
l = findViewById(R.id.button);
p = findViewById(R.id.button2);
l.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Toast.makeText(MainActivity.this, "Hey We are in Landscape",
Toast.LENGTH_SHORT).show();
}
});
p.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Toast.makeText(MainActivity.this, "Hey We are in Portrait Orientation",
Toast.LENGTH_SHORT).show();
}
});
}
}
SNAPSHOTS
XML CODE:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@drawable/bg_outer">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerInParent="true"
android:orientation="vertical"
android:background="@drawable/bg_inner"
android:padding="30dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LOGIN PAGE"
android:textSize="32sp"
android:textStyle="bold"
android:fontFamily="sans-serif-condensed-medium"
android:textColor="@color/black"
android:paddingBottom="20dp" />
<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:hint="Username"
android:minHeight="48dp" />
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editTextUsername"
android:layout_marginBottom="16dp"
android:hint="Password"
android:inputType="textPassword"
android:minHeight="48dp" />
<Button
android:id="@+id/buttonLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_below="@id/editTextPassword"/>
</LinearLayout>
</RelativeLayout>
bg_outer.xml (Inside Drawable Folder)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="12dp" />
<gradient
android:startColor="#B388FF"
android:endColor="#397C9A"
android:angle="100"/>
</shape>
bg_inner.xml (Inside Drawable Folder)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#84FFFF"
android:endColor="#f08"
android:angle="100"/>
<corners android:radius="20dp"/>
</shape>
MainActivity.java
package com.example.myapplication4;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import com.example.myapplication4.R;
public class MainActivity extends AppCompatActivity {
private EditText editTextUsername,editTextPassword;
private Button buttonLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
editTextUsername = findViewById(R.id.editTextUsername);
editTextPassword = findViewById(R.id.editTextPassword);
buttonLogin = findViewById(R.id.buttonLogin);
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = editTextUsername.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
if(username.equals("admin") && password.equals("pass")){
Toast.makeText(MainActivity.this, "Login successful",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Invalid username or password",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
SNAPSHOTS
Program 4: Create an application to implement new activity using explicit intent, implicit
intent and content provider.
XML CODE:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Explicit Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java:
package com.example.myapplication5;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.activity.EdgeToEdge;
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 {
Button btnExplicitContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnExplicitContent=findViewById(R.id.button);
btnExplicitContent.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,
SecondActivity.class);
startActivity(intent);
}
});
}
}
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".SecondActivity">
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Implicit Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
SecondActivity.java
package com.example.myapplication5;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class SecondActivity extends AppCompatActivity {
Button btnImplicitContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
btnImplicitContent = findViewById(R.id.button2);
btnImplicitContent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Uri webpage = Uri.parse("https://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
startActivity(intent);
}
});
}
}
SNAPSHOT
Program 5: Create an application that displays custom designed Opening Screen.
XML CODE:
<?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:id="@+id/idRLContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/back_drawable">
<TextView
android:id="@+id/idTVHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="20dp"
android:gravity="center"
android:padding="10dp"
android:text="Welcome to My App"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
MainActivity.java
package com.example.a4thprogram;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.RelativeLayout;
import com.example.a4thprogram.R;
public class MainActivity extends AppCompatActivity {
private RelativeLayout containerRL;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize the RelativeLayout container
containerRL = findViewById(R.id.idRLContainer);
// Set background for the RelativeLayout using the drawable resource
containerRL.setBackground(getResources().getDrawable(R.drawable.back_drawable));
}
}
package com.example.menuexample;import
android.content.Intent;
import android.os.Bundle; import
android.view.Menu;
import android.view.MenuInflater;import
android.view.MenuItem; import
android.widget.Toast;
import androidx.activity.EdgeToEdge;import
androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;import
androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat; public class
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreatePanelMenu(int featureId, @NonNull Menu menu) {MenuInflater
inflater=getMenuInflater(); inflater.inflate(R.menu.menus,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if(item.getItemId()==R.id.php) {
Toast.makeText(this, "Php Page", Toast.LENGTH_SHORT).show();
}
if(item.getItemId()==R.id.java) {
Toast.makeText(this, "Java Page", Toast.LENGTH_SHORT).show();
}
if(item.getItemId()==R.id.csharp) {
Toast.makeText(this, "C# Page", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
}
Set the Uses-Permission in AndroidManifest.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">
<uses-permission android:name="android.permission.INTERNET" />
<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.AppCompat.Light"
tools:targetApi="31">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Output
Program 8: Read/ write the Local data.
activity_main.xml
</LinearLayout>
MainActivity.java
package com.bca.localdata;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
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 {
Button btnsave,btnnext;
EditText etUserName,etPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnsave=(Button) findViewById(R.id.btnsave);
btnnext = (Button) findViewById(R.id.btnnext);
etUserName = (EditText)findViewById(R.id.etUserName);
etPassword = (EditText)findViewById(R.id.etPassword);
btnsave.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View view) {
// Writing data to SharedPreferences
SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("username",
etUserName.getText().toString());
editor.putString("password",
etPassword.getText().toString());
editor.apply();
Toast.makeText(getApplicationContext(),"Saved
successfully",Toast.LENGTH_LONG).show();
}
});
btnnext.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View view) {
Intent intent = new
Intent(getApplicationContext(),MainActivity2.class);
startActivity(intent);
}
});
}
}
activity_main2.xml
<Button android:id="@+id/btnFetch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fetch" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="User Name"></TextView>
<EditText
android:id="@+id/etUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></EditText>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Password"></TextView>
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></EditText>
</LinearLayout>
MainActivity2.java
package com.bca.localdata;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity2 extends AppCompatActivity {
Button btnFetch;
EditText etUserName,etPassword;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
btnFetch = (Button) findViewById(R.id.btnFetch);
etUserName = (EditText)findViewById(R.id.etUserName);
etPassword = (EditText)findViewById(R.id.etPassword);
btnFetch.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View view)
{
// Reading data from SharedPreferences
SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs",
Context.MODE_PRIVATE);
String username = sharedPreferences.getString("username", "");
String password = sharedPreferences.getString("password", "");
etUserName.setText(username);
etPassword.setText(password);
}
});
}
}
Output
Program 9: Create / Read / Write data with database (SQLite).
Steps:
Click Start - Android Studio, a Welcome to Android
Studio dialog box will appear. Click New Project, the
New Project Dialog box appears.
Choose Empty Views Activity then click Next.
Specify the Name of your project, Select the Language as Java, and Select the
SDK as API 24(“Nougat”,Android 7.0).Click Finish Button.
Update the following code in activity_main.xml, activity_view.xml,
MainActivity.java and ViewActivity.java
Create a class file right click app- new- java class name it as student and update
the following code in student.java
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Course
Registation" android:textColor="@color/colorAccent"
android:textSize="30dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"android:text="Name"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" android:ems="10"
android:id="@+id/name"
android:textAlignment="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Course"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" android:ems="10"
android:id="@+id/course"
android:textAlignment="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"android:text="Fee"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" android:ems="10"
android:id="@+id/fee"
android:textAlignment="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" android:id="@+id/bt1"
android:text="Ok"
android:background="@color/colorPrimary"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" android:id="@+id/bt2"
android:text="View"
android:background="@color/colorAccent"
/>
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.bca.sqlite;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement
;import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import ndroid.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText ed1,ed2,ed3;
Button b1,b2;
@Override
ed1 = findViewById(R.id.name);
ed2 = findViewById(R.id.course);
ed3 = findViewById(R.id.fee);
b1 = findViewById(R.id.bt1);
b2 = findViewById(R.id.bt2);
b2.setOnClickListener(new View.OnClickListener()
{@Override
public void onClick(View v)
{
Intent i = new Intent(getApplicationContext(),ViewActivity.class);
startActivity(i);
}
});
b1.setOnClickListener(new View.OnClickListener() {@Override
insert();
}
});
}
public void insert()
{
try
{
String name = ed1.getText().toString();
SQLiteDatabase db = openOrCreateDatabase("SliteDb",
Context.MODE_PRIVATE, null);
statement.execute();
Toast.makeText(this,"Record addded",Toast.LENGTH_LONG).show();
ed1.setText("");
ed2.setText("");
ed3.setText("");
ed1.requestFocus();
}
catch (Exception ex)
{
Toast.makeText(this,"Record Fail",Toast.LENGTH_LONG).show();
}
}
}
/* Add Student.class file (Right click on package name */
Student.class
package com.bca.sqlite;
String id;
String name;
String course;
String fee;
String titles;
}
activity_view.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ViewActivity"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"android:id="@+id/lst1"
/>
</LinearLayout>
ViewActivity.java
package com.bca.sqlite;
import androidx.appcompat.app.AppCompatActivity;
import ndroid.content.Context;
import android.content.Intent;
import ndroid.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import ndroid.widget.AdapterView;
import ndroid.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class ViewActivity extends AppCompatActivity {
ListView lst1;
ArrayAdapter arrayAdapter;
@Override
SQLiteDatabase db = openOrCreateDatabase("SliteDb",Context.MODE_PRIVATE,null);
lst1 = findViewById(R.id.lst1);
final Cursor c = db.rawQuery("select * from records",null);
int id = c.getColumnIndex("id");
int name = c.getColumnIndex("name");
titles.clear();
lst1.setAdapter(arrayAdapter);
final ArrayList<Student> stud = new ArrayList<Student>();
if(c.moveToFirst())
{
do {
Student stu = new Student();
stu.id = c.getString(id);
stu.name = c.getString(name);
stu.course = c.getString(course)
;stu.fee = c.getString(fee);
stud.add(stu);
} while(c.moveToNext());
arrayAdapter.notifyDataSetChanged();
lst1.invalidateViews();
}
lst1.setOnItemClickListener(new AdapterView.OnItemClickListener()
{@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
String aa = titles.get(position).toString();
Intent(getApplicationContext(),EditActivity.class);
i.putExtra("id",stu.id);
i.putExtra("name",stu.name);
i.putExtra("course",stu.course);
i.putExtra("fee",stu.fee);
startActivity(i);
}
});
}
}
activity_edit.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:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".EditActivity" android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Course Registation"
android:textColor="@color/colorAccent"
android:textSize="30dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ID"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" android:ems="10"
android:id="@+id/id"
android:textAlignment="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" android:ems="10"
android:id="@+id/name"
android:textAlignment="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Course"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" android:ems="10"
android:id="@+id/course"
android:textAlignment="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fee"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:id="@+id/fee"
android:textAlignment="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" android:id="@+id/bt1"
android:text="Edit"
android:background="@color/colorPrimary"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" android:id="@+id/bt2"
android:text="Delete"
android:background="@color/colorAccent"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" android:id="@+id/bt3"
android:text="Back"
android:background="@color/colorPrimaryDark"
/>
</LinearLayout>
</LinearLayout>
EditActivity.java
package com.bca.sqlite;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText; import android.widget.Toast;
public class EditActivity extends AppCompatActivity {
EditText ed1,ed2,ed3,ed4;
Button b1,b2,b3; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_edit);
ed1 = findViewById(R.id.name);
ed2 = findViewById(R.id.course);
ed3 = findViewById(R.id.fee);
ed4 = findViewById(R.id.id);
b1 = findViewById(R.id.bt1);
b2 = findViewById(R.id.bt2);
b3 = findViewById(R.id.bt3);
Intent i = getIntent();
String t1 = i.getStringExtra("id").toString();
String t2 = i.getStringExtra("name").toString();
String t3 = i.getStringExtra("course").toString();
String t4 = i.getStringExtra("fee").toString(); ed4.setText(t1);
ed1.setText(t2); ed2.setText(t3); ed3.setText(t4);
b2.setOnClickListener(new View.OnClickListener()
{@Override
public void onClick(View v) { try
{
String id = ed4.getText().toString();SQLiteDatabase db =
openOrCreateDatabase("SliteDb",Context.MODE_PRIVATE,null);
statement.execute();
Toast.makeText(EditActivity.this,"RecordDeleted",Toast.LENGTH_LONG).show();
ed1.setText("");
ed2.setText("");
ed3.setText("");
ed1.requestFocus();
}
catch (Exception ex)
{
Toast.makeText(EditActivity.this,"Record Fail",Toast.LENGTH_LONG).show();
}
}
});
b3.setOnClickListener(new View.OnClickListener() {@Override
startActivity(i);
}
});
b1.setOnClickListener(new View.OnClickListener() {@Override
try {
String id = ed4.getText().toString();
String sql = "update records set name = '" + name + "',course='" +course +
"',fee='" + fee + "' where id= " + id + "";
statement.execute();
ed1.setText("");
ed2.setText("");
ed3.setText("");
ed1.requestFocus();
});
}}
Output
Program 10: Create an application to send SMS and receive SMS
Steps:
Click Start- Android Studio, a Welcome to Android Studio dialog box will appear.
Click New Project, the New Project Dialog box appears.
Choose Empty Views Activity then click Next.
Specify the Name of your project, Select the Language as Java, and Select the
SDK as API 24(“Nougat”,Android 7.0).Click Finish Button.
Update the following code in activity_main.xml and MainActivity.java
Click Run app or shift+F10 to execute the application.
activity_main.xml
MainActivity.java
package com.bca.sms;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.Manifest;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity
{
private static final int SMS_PERMISSION_CODE = 101;
private EditText editTextPhoneNumber;
return;
}
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, null, null);
Toast.makeText(this, "Message sent", Toast.LENGTH_SHORT).show();
}
catch (IllegalArgumentException e)
{
Toast.makeText(this, "Invalid phone number format",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, "Failed to send message", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
// Request SMS permission
private void requestSMSPermission() { ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.SEND_SMS}, SMS_PERMISSION_CODE);
}
// SMS receiver
private final BroadcastReceiver smsReceiver = new BroadcastReceiver()
{ @Override
public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras();
if (bundle != null)
{
Object[] pdus = (Object[]) bundle.get("pdus");
if (pdus != null) {
for (Object pdu : pdus)
{
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu);
String senderPhoneNumber = smsMessage.getDisplayOriginatingAddress();
String messageBody = smsMessage.getMessageBody();
textViewReceivedMessages.append("From: " + senderPhoneNumber + "\n");
textViewReceivedMessages.append("Message: " +
messageBody + "\n\n");
}
}
}
}
};
}
Output
Program 11: Create an application to send an e-mail.
Steps:
Click Start- Android Studio, a Welcome to Android Studio dialog box will
appear. Click New Project, the New Project Dialog box appears.
android:text="Send"/>
</RelativeLayout>
Mainactivity.java
package com.bca.email;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText editTextTo, editTextSubject, editTextMessage;
Button buttonSend;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextTo = findViewById(R.id.editTextTo);
editTextSubject = findViewById(R.id.editTextSubject);
editTextMessage = findViewById(R.id.editTextMessage);
buttonSend = findViewById(R.id.buttonSend);
buttonSend.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
sendEmail();
}
});
}
@SuppressLint("QueryPermissionsNeeded")
private void sendEmail() {
String to = editTextTo.getText().toString().trim();
String subject = editTextSubject.getText().toString().trim();
String message = editTextMessage.getText().toString().trim();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{to});
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, message);
if (intent.resolveActivity(getPackageManager()) != null)
{
startActivity(Intent.createChooser(intent, "Choose an email client"));
}
}
}
OUTPUT
Program 12: Display Map based on the Current/given location.
Steps:
Click Start- Android Studio, a Welcome to Android Studio dialog box will appear.
Click New Project, the New Project Dialog box appears.
MainActivity.java
import android.os.Bundle;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
@Override
setContentView(R.layout.activity_main);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager()
.findFragmentById(R.id.map);
if (mapFragment != null) {
mapFragment.getMapAsync(this);
} else {
}
}
@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
mMap = googleMap;
mMap.addMarker(new MarkerOptions().position(location).title("Marker"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 15));
}
}
activity_main.xml
<?xml version="1.0" encoding="utf8"?>
<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">
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yourpackage.yourappname">
<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">
<! Google Maps API Key >
<metadata
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY_HERE" />
<activity android:name=".MainActivity">
<intentfilter>
</intentfilter>
</activity>
</application>
</manifest>
Program 13: Create a sample application with login module(check user name and password)
On successful login change Textview “Login Successful”. On login fail alert using Toast “login
fail”
Steps:
Click Start- Android Studio, a Welcome to Android Studio dialog box will appear.
Click New Project, the New Project Dialog box appears.
android:text="Password" />
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:padding="8dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="30dp"/>
<Button
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="18sp"
android:layout_marginTop="16dp"/>
<TextView android:id="@+id/tvMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Password" />
</LinearLayout>
Mainactivity.java
package com.bca.loginprgrm;
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;
public class MainActivity extends AppCompatActivity {
EditText etUsername,etPassword; Button btnLogin;
TextView tvMessage;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnLogin = (Button) findViewById(R.id.btnLogin);
etUsername = (EditText) findViewById(R.id.etUsername);
etPassword = (EditText) findViewById(R.id.etPassword);
tvMessage = (TextView) findViewById(R.id.tvMessage);
btnLogin.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View view)
{
if(etUsername.getText().toString().isEmpty())
{
etUsername.setError("Enter User name");
} else if (etPassword.getText().toString().isEmpty()) {
etPassword.setError("Enter Password");
}
else if(etUsername.getText().toString().equals("isbr") &&
etPassword.getText().toString().equals("isbr"))
{
tvMessage.setText("Valid Login");
}
else
{
tvMessage.setText("Invalid login");
}
}
});
}
}
OUTPUT
Program14: Learn to deploy Android applications.
Steps to Deploy an Android Application
1. Prepare App (use Program 1 Hello world for this program)
Optimize performance and test thoroughly.
Ensure compatibility with various devices.
activity_main.xml
Follow the prompts to create a new keystore or use an existing one. A keystore is a
binary file that contains a set of private keys.
Create a new app entry if this is your first release or select an existing app.
Complete all the required information for the app listing, including the title, description,
screenshots, and categorization.
Once everything is set, click the "Publish" button to release your app to the Google Play
Store.
5. Other Distribution Channels (Optional):
Besides Google Play, you can distribute your app through other channels such as
Amazon Appstore, Samsung Galaxy Store, or thirdparty app marketplaces.
Each distribution channel may have its own requirements and submission process, so
be sure to follow their guidelines.
6. Monitor and Update:
Keep an eye on user feedback and app performance metrics through the Google Play
Console.
Regularly update your app to fix bugs, add new features, and improve user experience
based on feedback.