Ap Lab
Ap Lab
DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING
LAB MANUAL
ANDROID PROGRAMMING
EXPERIMENTS:
2. Create an android application to call different activities by using Implicit and Explicit
Intents.
16 a) Create an android application to get the Bluetooth devices and list of devices using
Bluetooth and Vibrator Service.
b) Create an android application to get the System Announcements by using Broadcast
Receiver.
17. Create an android application to share the data between multiple applications by using
Content Provider.
19. Create an android application to display current location on Google maps by using
Google-Maps Service.
REFERENCES:
1. Android Application Development (with Kitkat Support), Black Book by Pradeep Kothari.
2. Beginning Android 4 Application Development by Wei-Meng Lee.
3. Android Application Development for Dummies by Michael Burton
5. Click Next.
6. In the Customize Instant App Support window, leave the default settings.
7. Click Next.
8. In the Add an Activity to Phone and Tablet window, select Empty Activity.
9. Click Next.
10. In the Configure Activity window, do the following:
o In the Instant App URL Host box, enter 'myfirstinstantapp.example.com'.
o In the Instant App URL route box, enter '/hello'.
After Android Studio has finished creating the project, you can run the instant
app. Make sure that you have already created an emulator that can run instant
apps, as described in Set up your device or emulator.
EXPERIMENT 1
a) Create an android application to display RGMCET Text Message.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Welcome to RGMCET"
android:textSize="40sp"
android:textColor=”#F000” />
</LinearLayout>
MainActivity.java
package cubexsoft.helloworld;
import android.os.Bundle;
import android.support.annotation.Nullable;
public class MainActivity extends android.app.Activity
{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Output:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Text:"
android:textSize="40sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et1"/>
<Button
android:id="@+id/b1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="GetText"
android:textSize="40sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="__________________"
android:textSize="40sp"
android:id="@+id/tv1"/>
</LinearLayout>
Prepared by: Dept. of CSE, RGMCET Page 11
ANDROID PROGRAMMING
MainActivity.java
package com.example.hi.welcomeApp;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends android.app.Activity
{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b=(Button)findViewById(R.id.b1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText et1=(EditText)findViewById(R.id.et1);
TextView tv1=(TextView)findViewById(R.id.tv1);
tv1.setText(et1.getText());
}
});
}
OUTPUT:
EXPERIMENT 2
Create an android application to call different activities by using Implicit and Explicit
Intents.
ActivityMain.xml File:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Number"
android:id="@+id/et1"
android:inputType="phone"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DIAL"
android:onClick="dial"
android:layout_gravity="center"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DIAL1"
android:onClick="dial1"
android:layout_gravity="center"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NEXT"
android:onClick="next"
android:layout_gravity="center"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WHATSAPP"
android:onClick="whatsapp"
android:layout_gravity="center"/>
</LinearLayout>
MainActivity.Java File:
package com.example.hi.appintent;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
}
}
Welcome.xml :
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="WELCOME TO CSE LAB2"
android:textSize="30sp"
android:textColor="#FF00"/>
</LinearLayout>
-Goto Java folder >> package folder >> Now right click packagefolder >>
WelcomeActivity.java
package com.example.hi.appintent;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hi.appintent">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
OUTPUT:
EXPERIMENT 3
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Country"
android:textSize="40sp"
android:textColor="#f00" />
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/actv"/>
</LinearLayout>
Strings.xml:
<resources>
<string name="app_name">ACTCApp</string>
<string-array name="country">
<item>India</item>
<item>Indonasia</item>
<item>Ierland</item>
<item>Srilanka</item>
<item>SouthAfrica</item>
Prepared by: Dept. of CSE, RGMCET Page 20
ANDROID PROGRAMMING
<item>Pakisthan</item>
<item>Bangladesh</item>
<item>Brezil</item>
</string-array>
</resources>
MainActivity.java:
package com.example.hi.actcapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AutoCompleteTextView
actv=(AutoCompleteTextView)findViewById(R.id.actv);
String[] values=getResources().getStringArray(R.array.country);
ArrayAdapter adapter=new
ArrayAdapter(MainActivity.this, android.R.layout.
simple_list_item_single_choice, values);
actv.setAdapter(adapter);
actv.setThreshold(1);
}
}
OUTPUT:
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select Country"
android:textSize="40sp" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/sp1"
android:entries="@array/countries">
</Spinner>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select Gender"
android:textSize="40sp" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/sp2">
</Spinner>
</LinearLayout>
Strings.xml
<resources>
<string name="app_name">SPINNERapp</string>
<string-array name="countries">
<item>------Select------</item>
<item>India</item>
<item>Ireland</item>
<item>Australia</item>
<item>China</item>
<item>Srilanka</item>
<item>Bangladesh</item>
</string-array>
</resources>
MainActivity.java
package com.example.hi.spinnerapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterViewAnimator;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
OUTPUT:
EXPERIMENT 4
Adapter.
activity_main.xml
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="First practise of android"
android:textSize="40sp" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lview">
</ListView>
</LinearLayout>
MainActivity.java:
package com.example.hi.listviewapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.io.File;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lview=(ListView)findViewById(R.id.lview);
String path="/storage/emulated/0/";
File f=new File(path);
String[] files=f.list();
ArrayAdapter<String> adapter=new
ArrayAdapter<String>(MainActivity.this,
R.layout.support_simple_spinner_dropdown_item, files);
lview.setAdapter(adapter);
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hi.listviewapp">
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Prepared by: Dept. of CSE, RGMCET Page 28
ANDROID PROGRAMMING
</intent-filter>
</activity>
</application>
</manifest>
OUTPUT:
activity_main.xml:
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lview">
</ListView>
</LinearLayout>
Indiview.xml:
<ImageView
android:layout_width="0dp"
android:layout_weight="0.2"
android:layout_height="100dp"
android:id="@+id/lview1"
android:src="@drawable/ic_launcher_background" />
<LinearLayout
Prepared by: Dept. of CSE, RGMCET Page 30
ANDROID PROGRAMMING
android:layout_width="0dp"
android:layout_weight="0.6"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv1"
android:text="File Name"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#FF0000" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv2"
android:text="File Size"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#0000FF" />
</LinearLayout>
<Button
android:layout_width="0dp"
android:layout_weight="0.2"
android:layout_height="wrap_content"
android:text="Del"
android:id="@+id/b1"/>
</LinearLayout>
MainActivity.java:
package com.example.hi.listview_customadapter;
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.io.File;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults[0]==PackageManager.PERMISSION_GRANTED);
readFiles();
}
Prepared by: Dept. of CSE, RGMCET Page 32
ANDROID PROGRAMMING
}
}
MyAdapater.java:
package com.example.hi.listview_customadapter;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.File;
/**
* Created by hi on 25-05-2021.
*/
public class MyAdapter extends BaseAdapter {
MainActivity mActivity;
File[] files;
MyAdapter(MainActivity mActivity, File[] files){
this.mActivity=mActivity;
this.files=files;
}
@Override
public int getCount() {
return files.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater=LayoutInflater.from(mActivity);
View v=inflater.inflate(R.layout.indiview,null);
ImageView iview=v.findViewById(R.id.lview1);
TextView tv1=v.findViewById(R.id.tv1);
TextView tv2=v.findViewById(R.id.tv2);
Button b1=v.findViewById(R.id.b1);
Uri u=Uri.parse(files[i].getAbsolutePath());
iview.setImageURI(u);
tv1.setText(files[i].getName());
tv2.setText(String.valueOf(files[i].length()));
b1.setOnClickListener(new View.OnClickListener() {
@Override
AndroidManifest.xml:
<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<activity android:name=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>
</manifest>
Prepared by: Dept. of CSE, RGMCET Page 35
ANDROID PROGRAMMING
OUTPUT:
EXPERIMENT 5
Create an android application to display WhatsApp videos in grid view by using
Custom Adapter.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Gallery
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/gal" >
</Gallery>
</LinearLayout>
indiview.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="10dp">
<VideoView
android:layout_width="120dp"
android:layout_height="120dp"
android:id="@+id/vview1"
android:padding="10dp"/>
<CheckBox
android:layout_width="120dp"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="FileName"
android:id="@+id/cb1" />
</LinearLayout>
MyAdapter.java:
package cubexsoft.gallerytest;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.VideoView;
import java.io.File;
@Override
public int getCount()
{
Prepared by: Dept. of CSE, RGMCET Page 38
ANDROID PROGRAMMING
return files.length;
}
@Override
public Object getItem(int i)
{
return null;
}
@Override
public long getItemId(int i)
{
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater=LayoutInflater.from(activity);
View v=inflater.inflate(R.layout.indiview,null);
final VideoView vview=v.findViewById(R.id.vview1);
CheckBox cb1=v.findViewById(R.id.cb1);
Uri u=Uri.fromFile(files[i]);
vview.setVideoURI(u);
cb1.setText(files[i].getName());
cb1.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean
b) {
if(b){
vview.start();
}else{
vview.stopPlayback();
}
}
});
return v;
}
}
MainActivity.java:
package com.example.hi.gallerytest;
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Gallery;
import android.widget.GridView;
import java.io.File;
if(status== PackageManager.PERMISSION_GRANTED){
readFiles( );
}else{
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
111);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults[0]==PackageManager.PERMISSION_GRANTED){
readFiles( );
}
}
OUTPUT:
EXPREIMENT 6
Create an android application to display webpage by using Web view
Component.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:id="@+id/et1"
android:hint="Enter URL:"/>
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:src="@drawable/magnifier"
android:id="@+id/srch"
android:onClick="load"
android:layout_margin="5dp"/>
</LinearLayout>
<WebView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.8"
android:id="@+id/wview">
</WebView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:src="@drawable/fb"
android:id="@+id/fb"
android:onClick="load" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:src="@drawable/google"
android:id="@+id/google"
android:onClick="load" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:src="@drawable/youtube"
android:id="@+id/youtube"
android:onClick="load" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:src="@drawable/html"
android:id="@+id/html"
android:onClick="load" />
</LinearLayout>
</LinearLayout>
MainActivity.java:
package com.example.hi.webviewapp;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
WebView wview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wview=(WebView)findViewById(R.id.wview);
wview.setWebViewClient(new WebViewClient(){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Toast.makeText(getApplicationContext(),"PAGE LOADING STARTED....",
Toast.LENGTH_LONG).show();
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Toast.makeText(getApplicationContext(),url,Toast.LENGTH_LONG).show();
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Toast.makeText(getApplicationContext(),"PAGE LOADING FINISHED",
Toast.LENGTH_LONG).show();
}
});
wview.getSettings().setJavaScriptEnabled(true);
wview.getSettings().setBuiltInZoomControls(true);
wview.addJavascriptInterface(this,"myinterface");
}
@JavascriptInterface
public void displayMsg(String name,String pass){
Toast.makeText(this, name+ "\n" +pass, Toast.LENGTH_LONG).show();
{
switch(v.getId())
{
case R.id.srch:
EditText et1=(EditText)findViewById(R.id.et1);
wview.loadUrl(et1.getText().toString());
break;
case R.id.fb:
wview.loadUrl("http://www.facebook.com");
break;
case R.id.google:
wview.loadUrl("http://www.google.com");
break;
case R.id.youtube:
wview.loadUrl("http://www.youtube.com");
break;
case R.id.html:
wview.loadUrl("file:///android_asset/login.html");
break;
}
}
}
login.html:
<html>
<head>
<script language="JavaScript">
function login(){
var name=document.getElementById("name").value;
var pass=document.getElementById("pass").value;
myinterface.displayMsg(name,pass);
}
login.html:
<html>
<head>
<script language="JavaScript">
function login(){
var name=document.getElementById("name").value;
var pass=document.getElementById("pass").value;
myinterface.displayMsg(name,pass);
}
</script>
</head>
<body bgcolor="#054" text="white">
<center>
<table border="1">
<tr>
<td colspan="2" align="center">
<h3>Login Form</h3>
</td>
</tr>
<tr>
<td>Enter Uname:</td>
<td> <input type="text" id="name"/>
</td>
</tr>
<tr>
<td>Enter Pass</td>
<td>
<input type="password" id="pass"/>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="button" value="Login" onclick="login()"/>
</td>
</tr>
</table>
</center>
</body>
</html>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hi.webviewapp">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
OUTPUT:
EXPERIMENT 7
Create an android application to display different webpages in fragments by
using Fragments Component.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:text="RGMCET"
android:textSize="30sp"
android:gravity="center"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:background="#054"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.75"
android:id="@+id/frag1" >
</FrameLayout>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.15"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#054" >
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="#FFFFFF"
android:text="HOME"
android:onClick="home"
android:layout_margin="5dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="#FFFFFF"
android:text="COURSES"
android:onClick="courses"
android:layout_margin="5dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="#FFFFFF"
android:text="PLACEMENTS"
android:onClick="placements"
android:layout_margin="5dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="#FFFFFF"
android:text="FACULTIES"
android:onClick="faculties"
android:layout_margin="5dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="#FFFFFF"
android:text="RESULTS"
android:onClick="results"
android:layout_margin="5dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="#FFFFFF"
android:text="EVENTS"
android:onClick="events"
android:layout_margin="5dp"/>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
Home.xml:
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#098">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="WELCOME TO RGMCET"/>
</LinearLayout>
HomeFragment.java
package com.example.hi.fragmentapp;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by hi on 27-05-2021.
*/
courses.xml:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="courses"
android:gravity="center"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sp1"
android:entries="@array/courses"
android:layout_gravity="center">
</Spinner>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GET INFO"
android:textSize="20sp"
android:id="@+id/b1"
android:textStyle="bold"
android:layout_gravity="center"/>
</LinearLayout>
Strings.xml:
<resources>
<string name="app_name">FragmentApp</string>
<string-array name="courses">
<item>-----Select-----</item>
<item>CSE</item>
<item>ECE</item>
<item>EEE</item>
<item>MCA</item>
<item>IT</item>
<item>CIVIL</item>
<item>MECH</item>
<item>MBA</item>
</string-array>
</resources>
courseFragment:
package com.example.hi.fragmentapp;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
/**
* Created by hi on 27-05-2021.
*/
Prepared by: Dept. of CSE, RGMCET Page 55
ANDROID PROGRAMMING
Toast.makeText(getActivity(),sp1.getSelectedItem().toString(),Toast.LENGTH_LON
G).show();
}
});
return v;
}
}
placements.xml:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="placements"
android:textSize="30sp"
Prepared by: Dept. of CSE, RGMCET Page 56
ANDROID PROGRAMMING
android:gravity="center"/>
</LinearLayout>
placementFragment:
package com.example.hi.fragmentapp;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by hi on 27-05-2021.
*/
faculties.xml:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="faculties"
android:gravity="center"/>
</LinearLayout>
FacultiesFragment:
package com.example.hi.fragmentapp;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by hi on 27-05-2021.
*/
events.xml:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="events"
android:gravity="center"/>
</LinearLayout>
Eventsfragment.java
package com.example.hi.fragmentapp;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by hi on 27-05-2021.
*/
public class EventsFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup
container, Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.events, container, false);
return v;
Prepared by: Dept. of CSE, RGMCET Page 59
ANDROID PROGRAMMING
}
}
results.xml:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="results"
android:gravity="center"/>
</LinearLayout>
resultsFragment.java
package com.example.hi.fragmentapp;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by hi on 27-05-2021.
*/
public class ResultsFragment extends Fragment {
@Nullable
@Override
Prepared by: Dept. of CSE, RGMCET Page 60
ANDROID PROGRAMMING
View v=inflater.inflate(R.layout.results,container,false);
return v;
}
}
mainActivity.java
package com.example.hi.fragmentapp;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
OUTPUT:
EXPERIMENT 8
Create an android application to store the data by using Shared
Preferences.
activity_main.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/flayout">
</FrameLayout>
</LinearLayout>
Login.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp" >
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter username"
android:id="@+id/l_uname" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter password"
android:id="@+id/l_pwd"
android:inputType="textPassword"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textColor="#FFFFFF"
android:background="#054"
android:id="@+id/l_login"
android:layout_gravity="center"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"
android:textColor="#FFFFFF"
android:background="#054"
android:id="@+id/l_register"
android:layout_gravity="center"
android:layout_marginTop="5dp"/>
</LinearLayout>
LoginFragment:
package com.android.developer.spftest;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
Prepared by: Dept. of CSE, RGMCET Page 65
ANDROID PROGRAMMING
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
if(luname.getText().toString().equals(username) &&
lupass.getText().toString().equals(userpwd)) {
FragmentManager fManager=getFragmentManager();
FragmentTransaction tx=fManager.beginTransaction();
tx.replace(R.id.flayout,new WelcomeFragment());
tx.commit();
}
else {
Toast.makeText(getActivity(),"Invalid credentials", Toast.LENGTH_LONG).show();
}
}
});
btn_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Prepared by: Dept. of CSE, RGMCET Page 66
ANDROID PROGRAMMING
FragmentManager fManager=getFragmentManager();
FragmentTransaction tx=fManager.beginTransaction();
tx.replace(R.id.flayout,new RegisterFragment());
tx.addToBackStack("true");
tx.commit();
}
});
return v;
//return super.onCreateView(inflater, container, savedInstanceState);
}
}
register.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter username"
android:id="@+id/r_uname"
android:textColorHint="#054"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter password"
android:inputType="textPassword"
android:id="@+id/r_upass"
android:textColorHint="#054"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter email address"
android:inputType="textEmailAddress"
android:id="@+id/r_email"
android:textColorHint="#054"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter mobile number"
android:inputType="phone"
android:id="@+id/r_mobno"
android:textColorHint="#054"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"
android:id="@+id/btn_r_register"
android:layout_gravity="center"
android:background="#054"
android:textColor="#FFFFFF"/>
</LinearLayout>
registerFragment:
package com.android.developer.spftest;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup
container, Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.register,container,false);
final EditText r_uname=(EditText)v.findViewById(R.id.r_uname);
final EditText r_upass=(EditText)v.findViewById(R.id.r_upass);
final EditText r_email=(EditText)v.findViewById(R.id.r_email);
final EditText r_mobno=(EditText)v.findViewById(R.id.r_mobno);
Button btn_register=(Button)v.findViewById(R.id.btn_r_register);
btn_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences spf=getActivity().getSharedPreferences("myspf",
Context.MODE_PRIVATE);
SharedPreferences.Editor spe=spf.edit();
spe.putString("uname",r_uname.getText().toString());
spe.putString("upass",r_upass.getText().toString());
spe.putString("email",r_email.getText().toString());
spe.putLong("mno", Long.parseLong(r_mobno.getText().toString()));
spe.commit();
FragmentManager fManager=getFragmentManager();
FragmentTransaction tx=fManager.beginTransaction();
tx.replace(R.id.flayout,new LoginFragment());
tx.commit();
}
});
return v;
//return super.onCreateView(inflater, container, savedInstanceState);
}
}
Welcome.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="WELCOME TO"
android:textSize="40sp"
android:textColor="#FF00"
android:layout_gravity="left" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="RGMCSE DEPT"
android:gravity="right"
android:textSize="40sp"
android:textColor="#708090"/>
</LinearLayout>
WelcomeFragment:
package com.android.developer.spftest;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
MainActivity.java:
package com.example.hi.spftest;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fManager=getFragmentManager();
FragmentTransaction tx=fManager.beginTransaction();
tx.add(R.id.flayout,new LoginFragment());
tx.commit();
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.developer.spftest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
OUTPUT:
EXPERIMENT 9
Create an android application to demonstrate concept of SQLite Database
Storage method.
activity_main.xml file:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter ID"
android:id="@+id/et1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="ENTER NAME"
android:id="@+id/et2"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="DESIG"
android:id="@+id/et3" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="DEPT"
android:id="@+id/et4"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="INSERT"
android:onClick="insert"
android:layout_weight="0.5"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="READ"
android:onClick="read"
android:layout_weight="0.5"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="UPDATE"
android:onClick="update"
android:layout_weight="0.5"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="DELETE"
android:onClick="delete"
android:layout_weight="0.5"/>
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lview">
</ListView>
</LinearLayout>
mainActivity.java:
package com.example.hi.sqlitedbapp;
import android.content.ContentValues;
import android.content.Context;
Prepared by: Dept. of CSE, RGMCET Page 75
ANDROID PROGRAMMING
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.util.ArrayList;
import static android.content.Context.MODE_PRIVATE;
dBase = openOrCreateDatabase("empdb",
Context.MODE_PRIVATE, null);
dBase.execSQL("create table if not exists tbemp (empid number,
empname varchar(100),empdesig varchar(100),empdept
varchar(100))");
lview = (ListView) findViewById(R.id.lview);
}
public void insert(View v) {
ContentValues cv = new ContentValues();
//
dBase.insert("employee",null,Integer.parseInt(et1.getText().toString());
cv.put("empid", et1.getText().toString());
cv.put("empname", et2.getText().toString());
cv.put("empdesig", et3.getText().toString());
cv.put("empdept", et4.getText().toString()); long status =
dBase.insert("tbemp", null, cv);
if (status != -1) {
Toast.makeText(MainActivity.this, "Data Insereted",
Toast.LENGTH_LONG).show();
et1.setText("");
et2.setText("");
et3.setText("");
et4.setText("");
read(v);
} else {
Toast.makeText(MainActivity.this, "Fails To Inserted",
Toast.LENGTH_LONG).show();
}
}
public void read(View v) {
Cursor c = dBase.query("tbemp", null, null, null, null, null, null);
ArrayList<String> list = new ArrayList<>();
while (c.moveToNext()) {
String msg = c.getInt(0) + "|" + c.getString(1) + "|" +
c.getString(2) + "|" + c.getString(3);
list.add(msg);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice, list);
lview.setAdapter(adapter);
}
Toast.makeText(MainActivity.this,"SUCCESS",Toast.LENGTH_LONG).
show();
}else{
Toast.makeText(MainActivity.this,
"UNSUCCEFUL",Toast.LENGTH_LONG).show();
}
}
}
OUTPUT:
EXPERIMENT 10
Create an android application to perform different types of operations
(send SMS, Making call and sending email) by using Telephony app.
First add these permissions in Manifest File:
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission
android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Go to Activity_main.XML file:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Mobile Num"
android:id="@+id/et1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Message"
android:id="@+id/et2" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="SEND SMS"
android:onClick="sendSms" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="CALL"
android:onClick="call" />
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et3"
android:hint="Enter Mail id"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et4"
android:hint="Enter Subject" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et5"
android:hint="Enter Message"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ATTACH"
android:onClick="attach"
android:layout_gravity="right"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="SEND MAIL"
android:onClick="send_mail"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="JAVA MAIL"
android:onClick="java_mail"/>
</LinearLayout>
</LinearLayout>
1) Goto java folder and rightclick on folder new Activity select Empty
Activity and provide name
Activity_delivary.xml file:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Message Delivered"
android:textSize="30sp"/>
</LinearLayout>
DelivaryActivity.java:
package com.example.hi.telephonetextapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delivary);
}
}
Goto java folder and rightclick on folder new Activity select Empty
Activity and provide name
Activity_send.xml file:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Message Send"
android:textSize="30sp"/>
</LinearLayout>
SendActivity.java:
package com.example.hi.telephonetextapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send);
}
}
MainActivity.Java
package com.example.hi.telephonetextapp;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.EditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=(EditText)findViewById(R.id.et1);
et2=(EditText)findViewById(R.id.et2);
}
i.setAction(Intent.ACTION_CALL);
i.setData(Uri.parse("tel:"+et1.getText().toString()));
startActivity(i);
}
public void attach(View v){
Intent i=new Intent();
i.setAction(Intent.ACTION_GET_CONTENT);
i.setType("*/*");
startActivityForResult(i,123);
}
Uri u;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
u=data.getData();
}
}
public void send_mail(View v){
EditText et3=(EditText)findViewById(R.id.et3);
EditText et4=(EditText)findViewById(R.id.et4);
EditText et5=(EditText)findViewById(R.id.et5);
Intent i=new Intent();
i.setAction(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_EMAIL,new String[]{et3.getText().toString()});
i.putExtra(Intent.EXTRA_SUBJECT,et4.getText().toString());
i.putExtra(Intent.EXTRA_TEXT, et5.getText().toString());
i.putExtra(Intent.EXTRA_STREAM, u);
i.setType("message/rfc822");
startActivity(i.createChooser(i,"Select any Email Client"));
}
}
OUTPUT:
EXPERIMENT 11
Write an android program to develop Media player application.
Setting Image:
Uri u= Uri.parse("android.resource://com.example.hi.mediaplayer/raw/dj"));
byte[] im_data=mr.getEmbeddedPicture();
Bitmap bmp= BitmapFactory.decodeByteArray(im_data, 0, im_data.length);
iview.setImageBitmap(bmp);
--First download the all media player images like logo image, backword image,
play image, forward image, pause image, stop image and files image. These
images download with png format.
-Go to res folder >> new >> Android resource directory >> now open dialogue
box come >> choose resource type to raw folder give directory name to “raw” then
click on Ok Button.
-After creating raw folder you can copy any one mp3 file in your device and place
with in raw folder.
activity_main.xml
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.6"
android:src="@drawable/logo"
android:id="@+id/iview1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
Prepared by: Dept. of CSE, RGMCET Page 89
ANDROID PROGRAMMING
android:text="Cur Pos:"
android:textSize="15sp"
android:gravity="left"
android:id="@+id/cp"
android:textStyle="bold"
android:layout_weight="0.5" />
<TextView
android:textStyle="bold"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="Tot Dur:"
android:textSize="15sp"
android:gravity="right"
android:id="@+id/td"
android:layout_weight="0.5"/>
</LinearLayout>
<SeekBar
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:id="@+id/s1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.16"
android:src="@drawable/bwd"
android:id="@+id/bwd"
android:onClick="media"
Prepared by: Dept. of CSE, RGMCET Page 90
ANDROID PROGRAMMING
android:padding="5dp" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.16"
android:src="@drawable/play"
android:id="@+id/play"
android:onClick="media"
android:padding="5dp" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.16"
android:src="@drawable/pause"
android:id="@+id/pause"
android:onClick="media"
android:padding="5dp" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.16"
android:src="@drawable/stop"
android:id="@+id/stop"
android:onClick="media"
android:padding="5dp" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.16"
android:src="@drawable/fwd"
android:id="@+id/fwd"
android:onClick="media"
android:padding="5dp" />
Prepared by: Dept. of CSE, RGMCET Page 91
ANDROID PROGRAMMING
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.16"
android:src="@drawable/files"
android:id="@+id/files"
android:onClick="media"
android:padding="5dp" />
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.hi.mediaplayer;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaMetadata;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s1=(SeekBar)findViewById(R.id.s1);
cp=(TextView)findViewById(R.id.cp);
td=(TextView)findViewById(R.id.td);
iview=(ImageView)findViewById(R.id.iview1);
}
void init(){
if(mPlayer==null) {
if(u==null) {
mPlayer = MediaPlayer.create(MainActivity.this,R.raw.dj);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
} });
case R.id.play:
init();
mPlayer.start();
break;
case R.id.pause:
mPlayer.pause();
break;
case R.id.stop:
mPlayer.stop();
mPlayer=null;
init();
break;
case R.id.fwd:
mPlayer.seekTo(mPlayer.getCurrentPosition()+mPlayer.getDuration()/10);
cp.setText("Cur Pos:"+mPlayer.getCurrentPosition());
break;
case R.id.files:
Intent i=new Intent();
i.setAction(Intent.ACTION_GET_CONTENT);
i.setType("audio/*");
startActivityForResult(i,123);
break;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mPlayer = null;
u=data.getData();
init();
mPlayer.start();
}
}
}
OUTPUT:
EXPERIMENT 12
a) Write an android program to develop Video view application
Activity_main.XML File:
<VideoView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:id="@+id/Vview"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LIST"
android:onClick="list"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="START"
android:onClick="start"/>
</LinearLayout>
</LinearLayout>
MainActivity.Java:
package com.example.hi.videoviewapp;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Vview=(VideoView)findViewById(R.id.Vview);
Vview.setMediaController(new MediaController(this));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
}
Manifestfile.xml
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
OUTPUT:
Activity_main.XML File:
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.7"
android:src="@drawable/ic_keyboard_voice_black_24dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:id="@+id/status"
android:text="Recording status"
android:textSize="30sp"
android:gravity="center"
android:textStyle="bold"
android:textColor="#0000FF"/>
<ImageView
android:id="@+id/r_ivew"
android:layout_width="60dp"
android:layout_height="0dp"
android:layout_weight="0.2"
android:layout_gravity="center"
android:src="@drawable/ic_radio_button_checked_black_24dp"
Prepared by: Dept. of CSE, RGMCET Page 101
ANDROID PROGRAMMING
android:onClick="record"/>
</LinearLayout>
MainActivity.java File:
import android.media.MediaRecorder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iview=(ImageView)findViewById(R.id.r_ivew);
tv1=(TextView)findViewById(R.id.status);
}
e.printStackTrace();
}
}
public void record(View v){
if(recorder==null){
init();
iview.setImageResource(R.drawable.ic_radio_button_checked_black_24dp);
tv1.setText("Recording is started");
try{
recorder.start();
}
catch (Exception e) {
e.printStackTrace();
}
}else{
iview.setImageResource(R.drawable.ic_brightness_1_black_24dp);
tv1.setText("Record is stoped..");
recorder.stop();
recorder=null;
}
}
}
1 <vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#CC0000"
android:pathData="M12,12m-10,0a10,10 0,1 1,20 0a10,10 0,1 1,-20 0"/>
</vector>
2 <vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#0000FF"
android:pathData="M12,15c1.66,0 2.99,-1.34 2.99,-3L15,6c0,-1.66 -1.34,-3 -
3,-3S9,4.34 9,6v6c0,1.66 1.34,3 3,3zM17.3,12c0,3 -2.54,5.1 -5.3,5.1S6.7,15
6.7,12L5,12c0,3.42 2.72,6.23 6,6.72L11,22h2v-3.28c3.28,-0.48 6,-3.3 6,-6.72h-
1.7z"/>
</vector>
3 <vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#38f820"
android:pathData="M12,7c-2.76,0 -5,2.24 -5,5s2.24,5 5,5 5,-2.24 5,-5 -2.24,-5
-5,-5zM12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2
12,2zM12,20c-4.42,0 -8,-3.58 -8,-8s3.58,-8 8,-8 8,3.58 8,8 -3.58,8 -8,8z"/>
</vector>
1) <uses-permission android:name="android.permission.RECORD_AUDIO"/>
2) <uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
OUTPUT:
EXPERIMENT 13
a) Write an android program to develop Video Recording
application.
Activity_main.xml file:
<SurfaceView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:id="@+id/sview1"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="START"
android:onClick="start"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="STOP"
android:onClick="stop"/>
</LinearLayout>
Prepared by: Dept. of CSE, RGMCET Page 106
ANDROID PROGRAMMING
</LinearLayout>
MainActivity.java file:
package com.example.hi.videorecordingapp;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
CamcorderProfile profile =
CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(profile);
recorder.setOutputFile("/storage/emulated/0/cse"+System.currentTimeMillis()
+".mp4");
SurfaceView sview=(SurfaceView)findViewById(R.id.sview1);
SurfaceHolder sholder=sview.getHolder();
recorder.setPreviewDisplay(sholder.getSurface());
try {
recorder.prepare();
} catch (Exception e) {
e.printStackTrace();
}
}
AndroidMainifest.xml file:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission
android:name="android.permission.RECORD_AUDIO"/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
OUTPUT:
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.9"
android:id="@+id/ivew1"
android:src="@mipmap/ic_launcher_round"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:text="CAMERA"
android:onClick="camera"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:text="GALLERY"
android:onClick="gallery"/>
Prepared by: Dept. of CSE, RGMCET Page 110
ANDROID PROGRAMMING
</LinearLayout>
</LinearLayout>
MainActivity.java file:
package com.example.hi.camera_galleryapp;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
ImageView iview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iview=(ImageView)findViewById(R.id.ivew1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==111 && resultCode==RESULT_OK){
Object ob= data.getExtras().get("data");
Bitmap bmp=(Bitmap)ob;
iview.setImageBitmap(bmp);
}else if(requestCode==123 && resultCode==RESULT_OK){
Uri u=data.getData();
iview.setImageURI(u);
}
}
}
AndroidMainifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hi.camera_galleryapp">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
OUTPUT:
EXPERIMENT 14
a) Create an android application to get latitude and longitude
value by using Location Service.
Activity_Main.xml file:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hi.locationserviceapp.MainActivity">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textStyle="bold"
android:textSize="30sp"
android:gravity="center"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="113dp"
android:layout_marginStart="113dp"
android:layout_marginTop="40dp" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:gravity="center"
android:textSize="30sp"
android:textStyle="bold"
android:layout_marginTop="143dp"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/tv1"
android:layout_alignStart="@+id/tv1" />
</RelativeLayout>
MainActivity.java:
package com.example.hi.locationserviceapp;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
lManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
lManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
1000, 1, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
double lati=location.getLatitude();
double longi=location.getLongitude();
Prepared by: Dept. of CSE, RGMCET Page 115
ANDROID PROGRAMMING
tv1.setText(String.valueOf(lati));
tv2.setText(String.valueOf(longi));
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
});
}
}
androidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hi.locationserviceapp">
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission
android:name="android.permission.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/AppTheme">
Prepared by: Dept. of CSE, RGMCET Page 116
ANDROID PROGRAMMING
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
>
</intent-filter>
</activity>
</application>
</manifest>
OUTPUT:
Activity_Main.xml file:
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="117dp"
android:text="TextView"
android:textSize="25sp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="TextView"
android:textSize="25sp"/>
</RelativeLayout>
MainActivity.java file:
package com.example.hi.sensorserviceapp;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView tv1, tv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.textView);
tv2 = (TextView) findViewById(R.id.textView2);
SensorManager sManager=(SensorManager)
getSystemService(Context.SENSOR_SERVICE);
sManager.registerListener(new SensorListener() {
@Override
public void onSensorChanged(int i, float[] floats) {
tv1.setText("X Value is:"+String.valueOf(floats[0]));
tv2.setText("Y Value is:"+String.valueOf(floats[1]));
}
@Override
public void onAccuracyChanged(int i, int i1) {
}
},SensorManager.SENSOR_ACCELEROMETER);
}
}
OUTPUT:
EXPERIMENT 15
15.a. Create an android application to get the notifications on Notification
Bar by Using Notification Service
ActivityMain.xml:
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="NOTIFY"
android:onClick="notify"/>
</RelativeLayout>
ManiActivity.java:
package com.example.hi.notificationserviceapp;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder=new
NotificationCompat.Builder(this);
builder.setTicker("SAMPLE NOTIFICATION");
builder.setSmallIcon(R.drawable.ic_beach_access_black_24dp);
builder.setContentTitle("SAMPLE NOTIFICATION");
builder.setContentText("sample notification for RGMCET");
Bitmap bmp=
BitmapFactory.decodeResource(getResources(),R.drawable.ic_beach_access_bl
ack_24dp);
builder.setLargeIcon(bmp);
Intent i=new Intent(this,MainActivity.class);
PendingIntent pIntent=PendingIntent.getActivity(this,0,i,0);
builder.setContentIntent(pIntent);
builder.setAutoCancel(true);
//nManager.notify(1,builder.build());
nManager.notify((int)System.currentTimeMillis(),builder.build());
}
}
OUTPUT:
Activity_Main.xml file:
<?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"
android:orientation="vertical"
tools:context="com.example.hi.wifiapp.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="WIFI"
android:gravity="center"
android:textSize="25sp"/>
<Switch
android:id="@+id/sw_wifi"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
Prepared by: Dept. of CSE, RGMCET Page 124
ANDROID PROGRAMMING
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:id="@+id/btn_get_wifi_device"
android:text="GET_WIFI_DEVICE"
android:onClick="getWifiDevice"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:id="@+id/btn_get_paired_wifi"
android:text="PAIRED_WIFI_DEVICE"
android:onClick="getPairedWifiDevice"/>
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lview">
</ListView>
</LinearLayout>
MainActivity.java file:
package com.example.hi.wifiapp;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.Switch;
import java.util.ArrayList;
import java.util.List;
ListView lview;
WifiManager wManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sw1=(Switch)findViewById(R.id.sw_wifi);
lview=(ListView)findViewById(R.id.lview);
wManager=(WifiManager)getApplicationContext().getSystemService(Context.
WIFI_SERVICE);
int wifi_status=wManager.getWifiState();
if(wifi_status== 0 || wifi_status== 1){
sw1.setChecked(false);
}
else if(wifi_status== 2 || wifi_status== 3){
sw1.setChecked(true);
}
sw1.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(isChecked){
wManager.setWifiEnabled(true);
}
else {
wManager.setWifiEnabled(false);
}
}
});
}
public void getWifiDevice(View v){
ArrayList<String> list=new ArrayList<>();
ArrayAdapter adapter=new
ArrayAdapter(this,android.R.layout.simple_list_item_single_choice, list);
lview.setAdapter(adapter);
List<ScanResult>sResult=wManager.getScanResults();
for (ScanResult item :sResult){
list.add(item.SSID +"\t\t"+ item.frequency);
adapter.notifyDataSetChanged();
}
}
public void getPairedWifiDevice(View v){
ArrayList<String>list=new ArrayList<>();
ArrayAdapter adapter=new ArrayAdapter(this,
android.R.layout.simple_list_item_single_choice, list);
lview.setAdapter(adapter);
List<WifiConfiguration>wifiConfigurationList=wManager.getConfiguredNetworks();
for (WifiConfiguration item:wifiConfigurationList){
list.add(item.SSID +"\t\t"+item.status);
adapter.notifyDataSetChanged();
}
}
}
String.xml file:
<resources>
<string name="app_name">WifiApp</string>
<string name="wifi_name">Wifi</string>
<string name="get_wifi_device"></string>
<string name="paired_wifi_device"></string>
</resources>
androidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hi.wifiapp">
<uses-permission
android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission
android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission
android:name="android.permission.ACCESS_COARSE_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/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER">
</intent-filter>
</activity>
</application>
</manifest>
OUTPUT:
EXPERIMENT 16
16.A. Create an android application to get the Bluetooth devices and list of
devices using Bluetooth and Vibrator Service.
activity_Main.xml file:
<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:text="GETBTDEVICES"
android:onClick="getBTDevices"
android:layout_gravity="center"
android:textSize="25sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="VIBRATE"
android:onClick="vibrate"
android:layout_gravity="center"
android:textSize="25sp"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lview">
</ListView>
</LinearLayout>
MainActivity.java file
package com.example.hi.btservicesapp;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.Switch;
import java.util.ArrayList;
s1=(Switch)findViewById(R.id.s1);
lview=(ListView)findViewById(R.id.lview);
bAdapter=BluetoothAdapter.getDefaultAdapter();
s1.setChecked(bAdapter.isEnabled());
s1.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton
compoundButton, boolean b) {
if(b) {
bAdapter.enable();
}else{
bAdapter.disable();
}
}
});
}
androidManifest.xml file
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission
android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
OUTPUT:
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEXTVIEW"
android:textSize="40sp"
android:textColor="#FF0000"
tools:layout_editor_absoluteY="-2dp"
tools:layout_editor_absoluteX="32dp" />
</android.support.constraint.ConstraintLayout>
MyReceiver.java:
package com.example.hi.broadcastreceiverapp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.TextView;
if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
tv.setText("POWER CONNECTED");
} else if
(intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)) {
tv.setText("POWER DISCONNECTED");
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
tv.setText("SCREEN ON");
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
tv.setText("SCREEN OF");
} else if
(intent.getAction().equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) {
tv.setText("AIRPLANE MODE CHANGED");
} else if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
tv.setText("HEADSET PLUGIN");
}
}
}
MainActivity.java:
package com.example.hi.broadcastreceiverapp;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.tv1);
filter.addAction(Intent.ACTION_HEADSET_PLUG);
filter.addAction(Intent.ACTION_POWER_CONNECTED)
;
filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
androidManifest.xml file:
OUTPUT:
EXPERIMENT 17
activity_Main.xml file:
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lview">
</ListView>
</LinearLayout>
Indiview.xml file:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="NAME"
Prepared by: Dept. of CSE, RGMCET Page 138
ANDROID PROGRAMMING
android:id="@+id/tv1"
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#FF0000"
android:gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="NUMBER"
android:id="@+id/tv2"
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#F0FF"
android:gravity="center"/>
</LinearLayout>
MainActivity.java file:
package com.example.hi.contentproviderapp;
import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
ContentResolver resolver=getContentResolver();
int[] to=new int[]{R.id.tv1,R.id.tv2};
//Cursor
c=resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,n
ull,null,null);
//you can test first above statement and second below statement
Cursor
c=resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
null,
null,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
String[] from=new
String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};
lview.setAdapter(adapter);
}
}
androidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hi.contentproviderapp">
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Prepared by: Dept. of CSE, RGMCET Page 140
ANDROID PROGRAMMING
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
OUTPUT:
EXPERIMENT 18
Create an android application to display different Dialog Boxes.
activity_Main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CUSTOM DIALOG"
android:onClick="CustomDailog"
android:layout_gravity="center"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ALERT DIALOG"
android:onClick="AlertDialog"
android:layout_gravity="center"/>
<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="Date Picker Dailog"
android:onClick="datepickerdailog"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/datepicker"
android:editable="false"/>
</LinearLayout>
<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="Time Picker Dailog"
android:onClick="timepickerdailog"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/timepicker"
android:editable="false"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Indeterminant Progress Dailog"
android:onClick="indeterminent"
android:layout_gravity="center"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Determinant Progress dailog"
Prepared by: Dept. of CSE, RGMCET Page 143
ANDROID PROGRAMMING
android:onClick="determinent"
android:layout_gravity="center"/>
</LinearLayout>
customDailog.xml:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Are You Sure To Exit"
android:textSize="25sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:id="@+id/btn_yes"
android:text="Yes"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.6"/>
Prepared by: Dept. of CSE, RGMCET Page 144
ANDROID PROGRAMMING
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:text="No"
android:id="@+id/btn_no"/>
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.hi.dialogapp;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.icu.util.Calendar;
import android.os.Vibrator;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import java.sql.Time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Prepared by: Dept. of CSE, RGMCET Page 145
ANDROID PROGRAMMING
setContentView(R.layout.activity_main);
}
Button yes_btn=(Button)d.findViewById(R.id.btn_yes);
Button no_btn=(Button)d.findViewById(R.id.btn_no);
yes_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
d.dismiss();
System.exit(0);
}
});
no_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
d.dismiss();
}
});
}
if(which== dialogInterface.BUTTON_POSITIVE){
dialogInterface.dismiss();
finish();
}else if(which== dialogInterface.BUTTON_NEGATIVE) {
dialogInterface.dismiss();
}
}
};
builder.setPositiveButton("YES",listener);
builder.setNegativeButton("NO",listener);
builder.show();
EditText et=(EditText)findViewById(R.id.datepicker);
et.setText(dayofmonth + "-" + monthofyear + "-" + year);
}
};
DatePickerDialog dpd=new
DatePickerDialog(MainActivity.this, listener,2020,4,20);
dpd.show();
}
public void timepickerdailog(View view){
java.util.Calendar cal=java.util.Calendar.getInstance();
final int hour=cal.get(java.util.Calendar.HOUR);
final int min=cal.get(java.util.Calendar.MINUTE);
TimePickerDialog tpd=new TimePickerDialog(this, new
TimePickerDialog.OnTimeSetListener() {
@Override
Prepared by: Dept. of CSE, RGMCET Page 147
ANDROID PROGRAMMING
}
},hour,min,false);
tpd.show();
}
OUTPUT:
EXPERIMENT 19
<fragment
android:name="com.google.android.gms.maps.SupportMapFragment"
………………./>
3) Use the following code in Activity to get the SupportMapFragment into Activity.
SupportMapFragment frag=(SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.XXX);
4) Get the GoogleMap object from SupportMapFragment.
frag.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
//write the logic, what you want to perform on GoogleMap
}
});
5) To work with any Google-API we have to get an API key from Google, go through
the following URL to get an API key.
http://code.google.com/apis/console
API Key:
Example Like:
AIzaSyAOeIcUosQIJD-FuZNCU0TkA-oQNWSfeZg
AIzaSyAAIzaSyAXQpjHUvOxg97SutBJN2itPpcCBd7IwkY JN2itPpcCBd7IwkY
AIzaSyAXQpjHUvOxg97SutBJN2itPpcCBd7Iwk
9) Use the following method to apply Zoom & move the goole map to a specific
location.
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new
LatLng(lati, longi),15f));
10) Use the following code to customize the icon, set the title
mOption.icon (BitmapDescriptorFactory.fromResource(R.drawable.car));
mOption.title(“title here”);
Activity_main.xml file:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
Prepared by: Dept. of CSE, RGMCET Page 151
ANDROID PROGRAMMING
android:id="@+id/fragment"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.9"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_latitude"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:text="Latitude"
android:gravity="center"/>
<TextView
android:id="@+id/tv_longitude"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:text="Longitude"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
MainActivity.java:
package com.example.hi.googlemaptest;
import android.Manifest;
import android.content.Context;
Prepared by: Dept. of CSE, RGMCET Page 152
ANDROID PROGRAMMING
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
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.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
SupportMapFragment smFragment;
TextView latitude, longitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latitude=(TextView)findViewById(R.id.tv_latitude);
longitude=(TextView)findViewById(R.id.tv_longitude);
int coarse_loc_status=
ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.ACCESS_COARSE_LOCATION);
int fine_loc_status=ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION);
fine_loc_status==PackageManager.PERMISSION_GRANTED) {
}
else {
ActivityCompat.requestPermissions(MainActivity.this,
new String[] {Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION}, 123);
}
smFragment=(SupportMapFragment) getSupportFragmentManager().
findFragmentById(R.id.fragment);
smFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(final GoogleMap googleMap) {
final LocationManager lManager=(LocationManager)
getSystemService(Context.LOCATION_SERVICE);
lManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
lManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
1000, 1, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
double lati=location.getLatitude();
double longi=location.getLongitude();
latitude.setText(String.valueOf(lati));
longitude.setText(String.valueOf(longi));
mOption.icon(BitmapDescriptorFactory.fromResource(R.drawable.car));
mOption.title("Simha-9000666090");
googleMap.addMarker(mOption);
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new
Prepared by: Dept. of CSE, RGMCET Page 154
ANDROID PROGRAMMING
LatLng(lati, longi),15f));
// googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
@Override
public void onProviderEnabled(String provider) {
@Override
public void onProviderDisabled(String provider) {
}
});
}
});
}
}
androidManifest.xml file:
</manifest>
OUTPUT:
1. Of the 25 marks for internal, 10 marks will be awarded for day-to-day work and 10 marks
to be awarded for the Record work and 5 marks to be awarded by conducting an internal
laboratory test.
2. Concerned Teachers have to do necessary corrections with explanations.
3. Concerned Lab teachers should enter marks in index page.
4. Internal exam will be conducted by two Staff members.
1. For Practical subjects there is a continuous evaluation during the semester for 25 Sessional
marks and 50 end examination marks.
2. The end examination shall be conducted by the teacher concerned (Internal Examiner) and
another External Examiner, recommended by Head of the Department with the approval of
principal.