Android Lab3
Android Lab3
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
SVFGC CTA 1
Android Programming
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import
[Link];
RecyclerView recyclerView;
ArrayList<File> videoList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
recyclerView = findViewById([Link]);
[Link](new GridLayoutManager(this, 2));
checkPermission();
}
{ String permission;
SVFGC CTA 2
Android Programming
} else {
permission = [Link].READ_EXTERNAL_STORAGE;
}
if ([Link](this, permission)
== PackageManager.PERMISSION_GRANTED) {
readFiles();
} else {
[Link](
this,
new String[]{permission},
REQUEST_CODE);
}
}
@Override
public void
onRequestPermissionsResult( int
requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {
[Link]( req
uestCode,
permissions,
grantResults);
if (requestCode == REQUEST_CODE
&& [Link] > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
SVFGC CTA 3
Android Programming
readFiles();
}
}
String path =
"/storage/emulated/0/Android/media/[Link]/WhatsApp/Media/WhatsApp Video/";
if (![Link]())
return;
if (files == null)
return;
[Link]();
if ([Link]()
&& ([Link]().endsWith(".mp4")
|| [Link]().endsWith(".3gp"))) {
[Link](file);
}
}
[Link](
new VideoAdapter(this, videoList));
SVFGC CTA 4
Android Programming
}
}
[Link]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
<application
android:allowBackup="true"
android:label="WhatsApp Videos"
android:theme="@style/[Link]">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>
[Link]
package [Link];
import [Link];
import [Link];
SVFGC CTA 5
Android Programming
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
Context context;
ArrayList<File> files;
[Link] = context;
[Link] = files;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(
@NonNull ViewGroup parent,
int viewType) {
SVFGC CTA 6
Android Programming
.inflate(
[Link].item_video,
parent,
false);
@Override
public void
onBindViewHolder( @NonNull
ViewHolder holder, int position)
{
[Link](bitmap);
}
@Override
public int getItemCount()
{ return [Link]();
}
{ ImageView imgVideo;
SVFGC CTA 7
Android Programming
imgVideo = [Link]([Link]);
}
}
}
Output:
SVFGC CTA 8
Android Programming
Component. activity_main.xml
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
webView = findViewById([Link]);
[Link](new WebViewClient());
[Link]().setJavaScriptEnabled(true);
SVFGC CTA 9
Android Programming
[Link]("[Link]
}
}
[Link]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
<uses-permission android:name="[Link]"/>
<application
android:allowBackup="true"
android:label="WebViewApp"
android:supportsRtl="true"
android:theme="@style/[Link]">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]"/>
<category
android:name="[Link]"/>
</intent-filter>
</activity>
</application>
</manifest>
SVFGC CTA 10
Android Programming
Output:
SVFGC CTA 11
Android Programming
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
{ @Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
return [Link](
[Link].fragment_courses,
container,
false);
}
}
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
{ @Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
return [Link](
[Link].fragment_faculty,
container,
false);
}
}
SVFGC CTA 12
Android Programming
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
{ @Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
return [Link](
[Link].fragment_home,
container,
false);
}
}
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
FragmentManager fm;
@Override
protected void onCreate(Bundle savedInstanceState)
{ [Link](savedInstanceState);
setContentView([Link].activity_main);
fm = getSupportFragmentManager();
[Link]()
.replace([Link].frag1, new HomeFragment())
SVFGC CTA 13
Android Programming
.commit();
}
activity_main.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home"
android:onClick="home"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Courses"
android:onClick="courses"/>
<Button
SVFGC CTA 14
Android Programming
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Faculty"
android:onClick="faculties"/>
</LinearLayout>
<FrameLayout
android:id="@+id/frag1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Fragment_course.xml
Fragment_faculty.xml
Fragment_home.xml
SVFGC CTA 15
Android Programming
Output:
SVFGC CTA 16
Android Programming
fragment_login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<EditText
android:id="@+id/etUser"
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="Enter Username"/>
<EditText
android:id="@+id/etPass"
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="Enter Password"
android:inputType="textPassword"/>
<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content
" android:text="Save"/>
<TextView
android:id="@+id/tvResult"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:textSize="18sp"/>
</LinearLayout>
SVFGC CTA 17
Android Programming
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle savedInstanceState)
{ [Link](savedInstanceState);
setContentView([Link].activity_main);
FragmentManager fm = getFragmentManager();
FragmentTransaction tx = [Link]();
[Link]
package [Link];
import
[Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
SharedPreferences sp;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
SVFGC CTA 18
Android Programming
Bundle savedInstanceState) {
View v = [Link](
[Link].fragment_login,
container,
false);
etUser = [Link]([Link]);
etPass = [Link]([Link]);
btnSave = [Link]([Link]);
tvResult =
[Link]([Link]);
sp =
getActivity().getSharedPreferences( "
MyData",
getActivity().MODE_PRIVATE);
[Link](new [Link]()
{ @Override
public void onClick(View view) {
[Link]("username", user);
[Link]("password", pass);
[Link]();
return v;
}
}
[Link]
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher
" android:label="@string/app_name"
SVFGC CTA 19
Android Programming
android:theme="@style/[Link]">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
</activity>
</application>
</manifest>
Output:
SVFGC CTA 20
Android Programming
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle savedInstanceState)
{ [Link](savedInstanceState);
setContentView([Link].activity_main);
et1 =
findViewById([Link].et1); et2
= findViewById([Link].et2);
et3 =
findViewById([Link].et3); et4
= findViewById([Link].et4);
lview =
findViewById([Link]); dBase
= openOrCreateDatabase(
"empdb",
Context.MODE_PRIVATE,
null);
[Link](
"CREATE TABLE IF NOT EXISTS tbemp("
SVFGC CTA 21
Android Programming
SVFGC CTA 22
Android Programming
"empdesig TEXT," +
"empdept TEXT)");
}
[Link]("empid",
[Link]([Link]().toString()));
[Link]("empname",
[Link]().toString());
[Link]("empdesig",
[Link]().toString());
[Link]("empdept",
[Link]().toString());
long status =
[Link]( "tbemp",
null,
cv);
if(status != -1) {
[Link](
this,
"Data Inserted",
Toast.LENGTH_SHORT).show();
read(v);
} else {
[Link](
this,
"Insert Failed",
Toast.LENGTH_SHORT).show();
}
}
Cursor c = [Link](
"tbemp"
, null,
null,
null,
null,
null,
null);
ArrayList<String> list =
SVFGC CTA 23
Android Programming
new ArrayList<>();
while([Link]()) {
String msg =
[Link](0) + " | " +
[Link](1) + " | " +
[Link](2) + " | " +
[Link](3);
[Link](msg);
}
[Link]();
ArrayAdapter<String> adapter =
new ArrayAdapter<>(
this,
[Link].simple_list_item_1,
list);
[Link](adapter);
}
[Link]("empname",
[Link]().toString());
[Link]("empdesig",
[Link]().toString());
[Link]("empdept",
[Link]().toString());
int status =
[Link]( "tbe
mp",
cv,
"empid=?",
new String[]{
[Link]().toString()
});
if(status > 0) {
[Link](
this,
"Updated Successfully",
Toast.LENGTH_SHORT).show();
SVFGC CTA 24
Android Programming
read(v);
} else {
[Link](
this,
"Update Failed",
Toast.LENGTH_SHORT).show();
}
}
{ int status =
[Link](
"tbemp",
"empid=?",
new String[]{
[Link]().toString()
});
if(status > 0) {
[Link](
this,
"Deleted Successfully",
Toast.LENGTH_SHORT).show();
read(v);
} else {
[Link](
this,
"Delete Failed",
Toast.LENGTH_SHORT).show();
}
}
SVFGC CTA 25
Android Programming
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="Employee ID"
android:inputType="number"/>
<EditText
android:id="@+id/et2"
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="Employee Name"/>
<EditText
android:id="@+id/et3"
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="Employee Designation"/>
<EditText
android:id="@+id/et4"
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="Employee Department"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Insert"
android:onClick="insert"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Read"
SVFGC CTA 26
Android Programming
android:onClick="read"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update"
android:onClick="update"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete"
android:onClick="delete"/>
</LinearLayout>
<ListView
android:id="@+id/lview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
Output:
SVFGC CTA 27
Android Programming
SVFGC CTA 28
Android Programming
Create:
app
└─ src
└─ main
└─ res
└─ raw
└─ sample.mp4
activity_main.xml
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="300dp" />
</LinearLayout>
SVFGC CTA 29
Android Programming
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState)
{ [Link](savedInstanceState);
setContentView([Link].activity_main);
videoView = findViewById([Link]);
String path =
"[Link]://" +
getPackageName() +
"/" +
[Link];
[Link](uri);
MediaController mediaController =
new MediaController(this);
[Link](videoView);
[Link](
mediaController);
[Link]();
}
SVFGC CTA 30
Android Programming
Output:
SVFGC CTA 31
Android Programming
PART B
1. Create an android application to perform different types of operations (Send SMS, Making
call and sending email) by using Telephony app.
activity_main.xml
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/etPhone"
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="Phone Number" />
<EditText
android:id="@+id/etMessage"
android:layout_width="match_parent
" android:layout_height="48dp"
android:hint="Message" />
<EditText
android:id="@+id/etEmail"
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="Email Address" />
<Button
android:id="@+id/btnSms"
android:layout_width="match_parent"
android:layout_height="wrap_content
" android:text="Send SMS" />
<Button
android:id="@+id/btnCall"
android:layout_width="match_parent"
android:layout_height="wrap_content
" android:text="Make Call" />
<Button
android:id="@+id/btnEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content
" android:text="Send Email" />
</LinearLayout>
SVFGC CTA 32
Android Programming
[Link]
<application
android:allowBackup="true"
android:label="TelephonyApp"
android:theme="@style/[Link]">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import
[Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle savedInstanceState)
{ [Link](savedInstanceState);
setContentView([Link].activity_main);
etPhone = findViewById([Link]);
SVFGC CTA 33
Android Programming
etMessage = findViewById([Link]);
etEmail = findViewById([Link]);
btnSms = findViewById([Link]);
btnCall = findViewById([Link]);
btnEmail = findViewById([Link]);
[Link](v -> {
startActivity(intent);
});
[Link](v -> {
startActivity(intent);
});
[Link](v -> {
Intent(Intent.ACTION_SEND);
[Link]("message/rfc822");
[Link](
Intent.EXTRA_EMAIL,
new String[]{[Link]().toString()});
[Link](
Intent.EXTRA_SUBJECT,
"Test Mail");
[Link](
Intent.EXTRA_TEXT,
"Hello from Android Application");
startActivity(
[Link](
intent,
"Select Email App"));
});
}
}
SVFGC CTA 34
Android Programming
Output:
SVFGC CTA 35
Android Programming
SVFGC CTA 36
Android Programming
└── res/
├── layout/activity_main.xml
└── raw/song.mp3 (IMPORTANT)
Step2:
Step3:
SVFGC CTA 37
Android Programming
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btnPlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Play"
android:textColor="#FFFFFF"/>
<Button
android:id="@+id/btnPause"
android:layout_width="match_parent"
android:layout_height="wrap_content"
SVFGC CTA 38
Android Programming
android:text="Pause"
android:textColor="#FFFFFF"/>
<Button
android:id="@+id/btnStop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Stop"
android:textColor="#FFFFFF"/>
</LinearLayout>
</FrameLayout>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
MediaPlayer mediaPlayer;
Button btnPlay, btnPause, btnStop;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
SVFGC CTA 39
Android Programming
setContentView([Link].activity_main);
btnPlay = findViewById([Link]);
btnPause = findViewById([Link]);
btnStop = findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
[Link]();
}
});
[Link](new [Link]() {
@Override
public void onClick(View v)
{ if
([Link]()) {
[Link]();
}
}
});
[Link](new [Link]() {
@Override
public void onClick(View v) {
[Link]();
mediaPlayer = [Link]([Link], [Link]);
}
});
}
SVFGC CTA 40
Android Programming
@Override
protected void onDestroy() {
[Link]();
if (mediaPlayer != null) {
[Link]();
}
}
}
Output:
SVFGC CTA 41
Android Programming
import [Link];
import [Link];
import [Link];
import [Link];
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
webView = findViewById([Link]);
[Link](new WebViewClient());
SVFGC CTA 42
Android Programming
String url =
"[Link]
+ lat + "," + lon + ",15z";
[Link](url);
}
}
[Link]
<manifest xmlns:android="[Link]
<uses-permission android:name="[Link]"/>
<application
android:allowBackup="true"
android:label="Map Image App"
android:theme="@style/[Link]">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]"/>
<category android:name="[Link]"/>
</intent-filter>
</activity>
</application>
</manifest>
Output:
SVFGC CTA 43
Android Programming
<uses-permission android:name="[Link].RECORD_AUDIO"/>
<application
android:allowBackup="true"
android:label="AudioRecorder"
android:supportsRtl="true"
android:theme="@style/[Link]">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]"/>
<category
android:name="[Link]"/>
</intent-filter>
</activity>
</application>
</manifest>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
SVFGC CTA 44
Android Programming
import [Link];
MediaRecorder mediaRecorder;
MediaPlayer mediaPlayer;
String fileName;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
btnRecord = findViewById([Link]);
btnStop = findViewById([Link]);
btnPlay = findViewById([Link]);
fileName = getExternalFilesDir(null).getAbsolutePath()
+ "/recorded_audio.3gp";
if (!checkPermissions()) {
requestPermissions();
}
[Link]([Link]);
[Link]([Link].THREE_GPP);
[Link]([Link].AMR_NB);
[Link](fileName);
try {
[Link]();
[Link]();
[Link](this,
SVFGC CTA 45
Android Programming
"Recording Started",
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
[Link]();
}
}
{ if (mediaRecorder != null)
{
[Link]();
[Link]();
mediaRecorder = null;
[Link](this,
"Recording Saved",
Toast.LENGTH_SHORT).show();
}
}
{ mediaPlayer = new
MediaPlayer(); try {
[Link](fileName);
[Link]();
[Link]();
[Link](this,
"Playing Audio",
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
[Link]();
}
}
SVFGC CTA 46
Android Programming
new String[]{
[Link].RECORD_AUDIO
},
REQUEST_PERMISSION_CODE);
}
@Override
public void
onRequestPermissionsResult( int
requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {
[Link](
requestCode,
permissions,
grantResults);
if (requestCode == REQUEST_PERMISSION_CODE) {
if ([Link] > 0
&& grantResults[0]
== PackageManager.PERMISSION_GRANTED) {
[Link](this,
"Permission Granted",
Toast.LENGTH_SHORT).show();
} else {
[Link](this,
"Permission Denied",
Toast.LENGTH_SHORT).show();
}
}
}
}
[Link]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
<uses-permission android:name="[Link].RECORD_AUDIO"/>
<application
android:allowBackup="true"
android:label="AudioRecorder"
android:supportsRtl="true"
android:theme="@style/[Link]">
SVFGC CTA 47
Android Programming
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]"/>
<category
android:name="[Link]"/>
</intent-filter>
</activity>
</application>
</manifest>
Output:
SVFGC CTA 48
Android Programming
<Button
android:id="@+id/btnRecord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Record Video"/>
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="20dp"/>
</LinearLayout>
[Link]
package [Link];
import
[Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
{ Button btnRecord;
VideoView videoView;
ActivityResultLauncher<Intent> videoLauncher;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
SVFGC CTA 49
Android Programming
btnRecord = findViewById([Link]);
videoView = findViewById([Link]);
videoLauncher = registerForActivityResult(
new [Link](),
result -> {
if ([Link]() == RESULT_OK
&& [Link]() != null) {
[Link](videoUri);
MediaController mediaController =
new MediaController(this);
[Link](videoView);
[Link](mediaController);
[Link]();
}
});
[Link](v ->
{ Intent intent =
new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
[Link](intent);
});
}
}
[Link]
<manifest xmlns:android="[Link]
<uses-feature
android:name="[Link]"
android:required="false" />
<application
android:allowBackup="true"
android:label="VideoRecorder"
android:theme="@style/[Link]">
<activity
android:name=".MainActivity"
android:exported="true">
SVFGC CTA 50
Android Programming
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>
dependencies {
implementation '[Link]:appcompat:1.7.0'
implementation '[Link]:material:1.12.0'
Output:
SVFGC CTA 51
Android Programming
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Camera"
android:onClick="camera"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gallery"
android:onClick="gallery"/>
<ImageView
android:id="@+id/ivew1"
android:layout_width="250dp"
android:layout_height="250dp" />
</LinearLayout>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
ImageView iview;
@Override
protected void onCreate(Bundle savedInstanceState)
{ [Link](savedInstanceState);
setContentView([Link].activity_main);
SVFGC CTA 52
Android Programming
iview = findViewById([Link].ivew1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
[Link](requestCode, resultCode, data);
{ if (requestCode == 111) {
Bitmap bmp = (Bitmap) [Link]().get("data");
[Link](bmp);
}
if (requestCode == 123)
{ Uri u = [Link]();
[Link](u);
}
}
}
}
[Link]
<uses-permission android:name="[Link]"/>
<uses-feature
android:name="[Link]"
android:required="false"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher
"
SVFGC CTA 53
Android Programming
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/[Link]">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]"/>
<category android:name="[Link]"/>
</intent-filter>
</activity>
</application>
</manifest>
Output:
SVFGC CTA 54
Android Programming
7. Create an android application to get latitude and longitude value by using Location Service
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Latitude"
android:textSize="30sp"
android:textStyle="bold"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Longitude"
android:textSize="30sp"
android:textStyle="bold"
android:layout_below="@id/tv1"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>
</RelativeLayout>
[Link]
package [Link];
import [Link];
import
[Link];
import [Link];
import [Link];
import
[Link];
import
[Link];
import [Link];
import [Link];
import [Link];
SVFGC CTA 55
Android Programming
@Override
protected void onCreate(Bundle savedInstanceState)
{ [Link](savedInstanceState);
setContentView([Link].activity_main);
tv1 =
findViewById([Link].tv1); tv2
= findViewById([Link].tv2);
LocationManager lManager =
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (checkSelfPermission([Link].ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED &&
checkSelfPermission([Link].ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(
new String[]{
[Link].ACCESS_FINE_LOCATION,
[Link].ACCESS_COARSE_LOCATION
},
100);
return;
}
[Link]( LocationMan
ager.NETWORK_PROVIDER, 1000,
1,
new LocationListener()
{ @Override
public void onLocationChanged(Location location) {
SVFGC CTA 56
Android Programming
[Link]
<uses-permission android:name="[Link].ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="[Link].ACCESS_FINE_LOCATION"/>
<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/[Link]">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]"/>
<category android:name="[Link]"/>
</intent-filter>
</activity>
</application>
</manifest>
[Link]
dependencies {
implementation("[Link]:play-services-location:21.3.0")
}
Output:
SVFGC CTA 57
Android Programming
8. Create an android application to get the Bluetooth devices and list of devices using
Bluetooth and Vibrator Service.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Switch
android:id="@+id/s1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="BLUETOOTH"
android:textSize="25sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="getBTDevices"
android:text="GET BT DEVICES"
android:textSize="25sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="vibrate"
android:text="VIBRATE"
android:textSize="25sp" />
SVFGC CTA 58
Android Programming
<ListView
android:id="@+id/lview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
{ Switch s1;
SVFGC CTA 59
Android Programming
ListView lview;
BluetoothAdapter bAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
s1 = findViewById([Link].s1);
lview = findViewById([Link]);
bAdapter = [Link]();
if (bAdapter != null) {
[Link]([Link]());
[Link](
new [Link]() {
@Override
public void
onCheckedChanged( CompoundBut
ton buttonView, boolean
isChecked) {
if (isChecked) {
[Link]();
} else {
[Link]();
}
}
});
}
}
SVFGC CTA 60
Android Programming
ArrayList<>();
[Link](adapter);
IntentFilter filter =
new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(new BroadcastReceiver() {
@Override
public void
onReceive( Context
context, Intent
intent) {
BluetoothDevice device =
[Link](
BluetoothDevice.EXTRA_DEVICE);
if (device != null) {
SVFGC CTA 61
Android Programming
[Link](
SVFGC CTA 62
Android Programming
name + "\n" +
[Link]());
[Link]();
}
}
}, filter);
if (bAdapter != null) {
[Link]();
}
}
Vibrator vib =
(Vibrator) getSystemService(
Context.VIBRATOR_SERVICE);
if (vib != null) {
if ([Link].SDK_INT >=
[Link].VERSION_CODES.O) {
[Link](
[Link]( 1
000,
VibrationEffect.DEFAULT_AMPLITUDE));
} else {
[Link](1000);
}
SVFGC CTA 63
Android Programming
}
}
}
[Link]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
<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/[Link]">
<activity
android:name=".MainActivity"
android:exported="true">
SVFGC CTA 64
Android Programming
<intent-filter>
<action android:name="[Link]"/>
<category
android:name="[Link]"/>
</intent-filter>
</activity>
</application>
</manifest>
Output:
SVFGC CTA 65
Android Programming
<Button
android:id="@+id/btnNotify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Notification" />
</LinearLayout>
[Link]
package [Link];
import [Link];
import
[Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
SVFGC CTA 66
Android Programming
Button btnNotify;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
btnNotify = findViewById([Link]);
createNotificationChannel();
if ([Link].SDK_INT >=
[Link].VERSION_CODES.TIRAMISU) {
if (checkSelfPermission(
[Link].POST_NOTIFICATIONS)
!= [Link].PERMISSION_GRANTED) {
requestPermissions( ne
w String[]{
[Link].POST_NOTIFICATIONS
},
100);
return;
}
}
SVFGC CTA 67
Android Programming
[Link] builder =
new [Link](this, "my_channel")
.setSmallIcon([Link].ic_dialog_info)
.setContentTitle("Notification Service")
.setContentText(
"Welcome to Android Notification Service")
.setPriority(
NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManager =
[Link](this);
[Link](1, [Link]());
}
NotificationChannel channel =
new NotificationChannel(
"my_channel",
"My
Notifications",
NotificationManager.IMPORTANCE_DEFAULT);
[Link]("Notification Channel");
NotificationManager manager =
getSystemService([Link]);
[Link](channel);
}
}
}
SVFGC CTA 68
Android Programming
[Link]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
<uses-permission
android:name="[Link].POST_NOTIFICATIONS" />
<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/[Link]">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>
Output:
SVFGC CTA 69
Android Programming
10. Create an android application to display available Wi-Fi devices and Paired Wi-Fi devices
by using Wi-Fi Service.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<Button
android:id="@+id/btnScan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Scan Wi-Fi
Networks"/>
<ListView
android:id="@+id/listWifi"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
[Link]
package [Link];
SVFGC CTA 70
Android Programming
import [Link];
import [Link];
import
[Link];
import [Link];
import [Link];
import
[Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle savedInstanceState)
{ [Link](savedInstanceState);
SVFGC CTA 71
Android Programming
setContentView([Link].activity_main);
SVFGC CTA 72
Android Programming
btnScan = findViewById([Link]);
listWifi = findViewById([Link]);
wifiManager = (WifiManager)
getApplicationContext()
.getSystemService(Context.WIFI_SERVICE);
[Link](adapter);
requestPermissionsIfNeeded();
registerReceiver( wifi
Receiver, new
IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
if ([Link].SDK_INT >=
Build.VERSION_CODES.TIRAMISU) {
SVFGC CTA 73
Android Programming
if
([Link]
n( this,
[Link].NEARBY_WIFI_DEVICES)
!= PackageManager.PERMISSION_GRANTED) {
[Link](
this,
new String[]{
[Link].NEARBY_WIFI_DEVICES
},
REQUEST_CODE);
}
} else {
if
([Link]
n( this,
[Link].ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
||
[Link]( thi
s,
[Link].ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
[Link](
this,
new String[]{
[Link].ACCESS_COARSE_LOCATION,
[Link].ACCESS_FINE_LOCATION
},
SVFGC CTA 74
Android Programming
REQUEST_CODE);
}
SVFGC CTA 75
Android Programming
}
}
@SuppressLint("MissingPermission")
private void scanWifi() {
try {
if ([Link].SDK_INT >=
Build.VERSION_CODES.TIRAMISU) {
if
([Link](
this,
[Link].NEARBY_WIFI_DEVICES)
!= PackageManager.PERMISSION_GRANTED) {
[Link](
this,
"Wi-Fi permission required",
Toast.LENGTH_SHORT).show();
return;
}
} else {
if
([Link](
this,
[Link].ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
||
[Link]( this
,
SVFGC CTA 76
Android Programming
[Link].ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
SVFGC CTA 77
Android Programming
[Link](
this,
"Location permission required",
Toast.LENGTH_SHORT).show();
return;
}
}
if (!success) {
[Link](
this,
"Wi-Fi Scan Failed",
Toast.LENGTH_SHORT).show();
}
} catch (SecurityException e) {
[Link](
this,
"Permission denied",
Toast.LENGTH_SHORT).show();
}
}
@SuppressLint("MissingPermission")
@Override
SVFGC CTA 78
Android Programming
try {
List<ScanResult> results =
[Link]();
[Link]();
[Link](
"SSID : " + [Link] + "\
nBSSID : " + [Link] +
"\nSignal : " + [Link] + " dBm");
}
[Link]();
} catch (SecurityException e) {
[Link](
context,
"Unable to read Wi-Fi networks",
Toast.LENGTH_SHORT).show();
}
}
};
@Override
public void onRequestPermissionsResult(
SVFGC CTA 79
Android Programming
int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {
[Link](
requestCode,
permissions,
grantResults);
if (requestCode == REQUEST_CODE) {
if ([Link] > 0
&& grantResults[0]
== PackageManager.PERMISSION_GRANTED) {
[Link](
this,
"Permission Granted",
Toast.LENGTH_SHORT).show();
} else {
[Link](
this,
"Permission Denied",
Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onDestroy() {
SVFGC CTA 80
Android Programming
[Link]();
try {
unregisterReceiver(wifiReceiver);
} catch (Exception ignored) {
}
}
}
[Link]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
<uses-permission
android:name="[Link].ACCESS_WIFI_STATE"/>
<uses-permission
android:name="[Link].CHANGE_WIFI_STATE"/>
<uses-permission
android:name="[Link].ACCESS_COARSE_LOCATION"/>
<uses-permission
android:name="[Link].ACCESS_FINE_LOCATION"/>
<uses-permission
android:name="[Link].NEARBY_WIFI_DEVICES"
android:usesPermissionFlags="neverForLocation"/>
<application
android:allowBackup="true"
android:label="WiFi Scanner"
android:theme="@style/[Link]">
<activity
android:name=".MainActivity"
SVFGC CTA 81
Android Programming
android:exported="true">
<intent-filter>
<action android:name="[Link]"/>
<category
android:name="[Link]"/>
</intent-filter>
</activity>
</application>
</manifest>
Output:
SVFGC CTA 82
Android Programming
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="120dp"
android:text="X Value: 0.0"
android:textSize="25sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Y Value: 0.0"
android:textSize="25sp" />
</RelativeLayout>
SVFGC CTA 83
Android Programming
[Link]
package [Link];
import [Link];
import [Link];
import
[Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
tv1 = findViewById([Link]);
tv2 = findViewById([Link].textView2);
SVFGC CTA 84
Android Programming
if (sensorManager != null)
{ accelerometer =
[Link](Sensor.TYPE_ACCELEROMETER);
}
}
@Override
protected void onResume() {
[Link]();
if (accelerometer != null) {
[Link](
this,
accelerometer,
SensorManager.SENSOR_DELAY_NORMAL
);
}
}
@Override
protected void onPause() {
[Link]();
[Link](this);
}
@Override
public void onSensorChanged(SensorEvent event) {
if ([Link]() == Sensor.TYPE_ACCELEROMETER)
{
float x = [Link][0];
SVFGC CTA 85
Android Programming
float y = [Link][1];
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// Not required
}
}
Output:
SVFGC CTA 86
Android Programming
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Waiting for System Broadcast..."
android:textSize="24sp"
android:textColor="#000000"
android:layout_centerInParent="true"/>
</RelativeLayout>
[Link]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
package="[Link]">
<application
android:allowBackup="true"
android:label="Broadcast Receiver App"
android:theme="@style/[Link]">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]"/>
SVFGC CTA 87
Android Programming
<category android:name="[Link]"/>
</intent-filter>
</activity>
</application>
</manifest>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
TextView tv;
MyReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
tv = findViewById([Link].tv1);
SVFGC CTA 88
Android Programming
[Link](Intent.ACTION_POWER_DISCONNECTED);
[Link](Intent.ACTION_SCREEN_ON);
[Link](Intent.ACTION_SCREEN_OFF);
[Link](Intent.ACTION_AIRPLANE_MODE_CHANGED);
[Link](Intent.ACTION_HEADSET_PLUG);
registerReceiver(receiver, filter);
}
@Override
protected void onDestroy() {
[Link]();
unregisterReceiver(receiver);
}
}
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
TextView tv;
@Override
public void onReceive(Context context, Intent intent) {
SVFGC CTA 89
Android Programming
switch (action) {
case Intent.ACTION_POWER_CONNECTED:
[Link]("POWER CONNECTED");
break;
case Intent.ACTION_POWER_DISCONNECTED:
[Link]("POWER DISCONNECTED");
break;
case
Intent.ACTION_SCREEN_ON:
[Link]("SCREEN ON");
break;
case
Intent.ACTION_SCREEN_OFF:
[Link]("SCREEN OFF");
break;
case Intent.ACTION_AIRPLANE_MODE_CHANGED:
[Link]("AIRPLANE MODE CHANGED");
break;
case Intent.ACTION_HEADSET_PLUG:
[Link]("HEADSET PLUGGED");
break;
}
}
SVFGC CTA 90
Android Programming
SVFGC CTA 91
Android Programming
Output:
SVFGC CTA 92