Android End FILE
Android End FILE
-: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
Code -
MainActvitiy.java -
package com.example.fragmentsapp;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
buttonFragmentOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
replaceFragment(new FragmentOne());
}
});
buttonFragmentTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
replaceFragment(new FragmentTwo());
}
});
}
1
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer, fragment);
fragmentTransaction.commit();
}
}
FragmentOne.java –
package com.example.fragmentsapp;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
FragmentTwo –
package com.example.fragmentsapp;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
2
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_two, container, false);
}
}
XML 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">
<Button
android:id="@+id/buttonFragmentOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load Fragment One" />
<Button
android:id="@+id/buttonFragmentTwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load Fragment Two" />
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
Fragment_one.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
3
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Fragment One"
android:textSize="20sp" />
</LinearLayout>
Fragment_two.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Fragment Two"
android:textSize="20sp" />
</LinearLayout>
Output :
4
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
Code –
package com.example.arrayadapterdemo;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
5
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creating an ArrayAdapter
this,
android.R.layout.simple_list_item_1,
fruits
);
listView.setAdapter(adapter);
XML CODE :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content
6
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
android:layout_height="wrap_content
android:orientation="vertical"
android:padding="16dp">
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@android:color/darker_gray"
android:dividerHeight="1dp" />
</LinearLayout>
Output –
7
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
CODE –
package com.example.databaseapp;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
@Override
8
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
@Override
onCreate(db);
SQLiteDatabase db = this.getWritableDatabase();
contentValues.put(COL_2 name);
contentValues.put(COL_3age);
SQLiteDatabase db = this.getWritableDatabase();
SQLiteDatabase db = this.getWritableDatabase();
contentValues.put(COL_2, name);
contentValues.put(COL_3, age);
9
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
SQLiteDatabase db = this.getWritableDatabase();
Javacode 2 :
package com.example.databaseapp;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
DatabaseHelper databaseHelper;
10
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
ListView listView;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editName = findViewById(R.id.editName);
editAge = findViewById(R.id.editAge);
editId = findViewById(R.id.editId);
btnAdd = findViewById(R.id.btnAdd);
btnView = findViewById(R.id.btnView);
btnUpdate = findViewById(R.id.btnUpdate);
btnDelete = findViewById(R.id.btnDelete);
listView = findViewById(R.id.listView);
btnAdd.setOnClickListener(v -> {
});
11
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
btnUpdate.setOnClickListener(v -> {
String id = editId.getText();
});
btnDelete.setOnClickListener(v -> {
String id = editId.getText().toString();
});
if (cursor.getCount() == 0) {
return;
while (cursor.moveToNext()) {
12
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
listView.setAdapter(adapter);
Xml 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/editName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
<EditText
13
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
android:id="@+id/editAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Age"
android:inputType="number" />
<EditText
android:id="@+id/editId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
<Button
android:id="@+id/btAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
<Button
android:id="@+id/btViews"
android:layout_width="match_parent"
android:layout_height="wrap_content"
<Button
android:id="@+id/btUpdate"
14
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
android:layout_width="match_parent"
android:layout_height="wrap_content"
<Button
android:id="@+id/btDelete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Output –
15
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
16
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
CODE :
Javafile 1 –
package com.example.crudapp;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
@Override
17
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
@Override
onCreate(db);
// CREATE
SQLiteDatabase db = this.getWritableDatabase();
contentValues.put(COL_2, name);
contentValues.put(COL_3, age);
// READ
SQLiteDatabase db = this.getWritableDatabase();
// UPDATE
SQLiteDatabase db = this.getWritableDatabase();
18
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
contentValues.put(COL_2, name);
contentValues.put(COL_3, age);
// DELETE
SQLiteDatabase db = this.getWritableDatabase();
JavaFile 2 –
package com.example.crudapp;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
19
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
import java.util.ArrayList;
DatabaseHelper databaseHelper;
ListView listView;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editName = findViewById(R.id.editName);
editAge = findViewById(R.id.editAge);
editId = findViewById(R.id.editId);
btnDelete = findViewById(R.id.btnDelete);
listView = findViewById(R.id.listView);
// CREATE
btnAdd.setOnClickListener(v -> {
20
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
});
// READ
// UPDATE
btnUpdate.setOnClickListener(v -> {
String id = editId.getText().toString();
});
// DELETE
btnDelete.setOnClickListener(v -> {
String id = editId.getText().toString();
});
if (cursor.getCount() == 0) {
21
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
return;
while (cursor.moveToNext()) {
listView.setAdapter(adapter);
XML 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/editName"
android:layout_width="match_parent"
22
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
android:layout_height="wrap_content"
android:hint="Enter Name" />
<EditText
android:id="@+id/editAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Age"
android:inputType="number" />
<EditText
android:id="@+id/editd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter ID (for update/delete)" />
<Button
android:id="@+id/btnAddd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Data" />
<Button
android:id="@+id/btnView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View All Data" />
<Button
android:id="@+id/btnUpdate"
android:layout_width="wrap_content
android:layout_height="wrap_content"
android:text="Update Data" />
<Button
android:id="@+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete Data" />
23
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
OUTPUT :
24
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
CODE :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlemapapp">
25
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="true"
android:label="GoogleMapApp"
android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="lajdflkjasdflasjdflkasldfwlwl23lsd" />
<activity
android:name=".MapsActivity"
android:exported="true">
<intent-filter>
</intent-filter>
</activity>
</application>
</manifest>
26
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
Javacode :
package com.example.googlemapapp;
import androidx.fragment.app.FragmentActivity;
import android.os.Bundle;
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;
import com.example.googlemapapp.databinding.ActivityMapsBinding;
@Override
super.onCreate(savedInstanceState);
binding = ActivityMapsBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
27
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
@Override
mMap = googleMap;
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in
Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney,
10));
Xml code :
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
28
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
OUTPUT :
Code :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
29
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
package="com.example.imeinumber">
<uses-permission
android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:label="IMEI App"
android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
<activity android:name=".MainActivity">
<intent-filter>
</intent-filter>
</activity>
</application>
</manifest>
Java code :
package com.example.imeinumber;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.TextView;
30
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imeiTextView = findViewById(R.id.imeiTextView);
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_PHONE_STATE},
PERMISSION_REQUEST_CODE);
} else {
displayIMEI();
31
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
try {
if (telephonyManager != null) {
} else {
} catch (SecurityException e) {
@Override
super.onRequestPermissionsResult(requestCode, permissions,
grantResults);
32
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
if (requestCode == PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0) {
displayIMEI();
} else {
Xml Code :
<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/imeiTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_centerInParent="true" />
33
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
</RelativeLayout>
OUTPUT :
CODE :
Javacode :
package com.example.loginapp;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
34
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
// Initialize views
usernameEditText = findViewById(R.id.username);
passwordEditText = findViewById(R.id.password);
loginButton = findViewById(R.id.loginButton);
// Basic validation
if (username.equals("user") && password.equals("password123"))
{
Toast.makeText(MainActivity.this, "Login successful",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Invalid credentials",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
xmlcode :
<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">
35
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="text" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login" />
</LinearLayout>
OUTPUT :
36
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501
37