You are on page 1of 23

1.

Write a program to demonstrate the use of Linear Layout and and Absolute Layout
ANS:
<?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="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name: Pradnya Hanumant Jogi" />

<TextView
android:id="@+id/textView"
android:layout_width="198dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Age: 18" />

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>


<AbsoluteLayout 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=".MainActivity">

<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="114dp"
android:layout_y="333dp"
android:text="Mobile No.:1234567890" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="113dp"
android:layout_y="291dp"
android:text="Name: Pradnya Hanumant Jogi" />
</AbsoluteLayout>

2. Develop a program to implement table layout and relative layout


ANS:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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=".MainActivity">

<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Pradnya Hanumant Jogi"
android:textSize="20dp"
></TextView>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="72.78%"
android:textSize="20dp"
android:paddingLeft="25dp"
android:paddingBottom="17dp"
android:paddingTop="20dp"
></TextView>
</TableRow>

<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Pradnya Hanumant Jogi"
android:textSize="20dp"
></TextView>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="72.78%"
android:textSize="20dp"
android:paddingLeft="25dp"
android:paddingBottom="17dp"

></TextView>
</TableRow>

</TableLayout>

<?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=".MainActivity">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Emter your name"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="30dp"/>

<EditText
android:id="@+id/e1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv1"
android:layout_marginTop="20dp"
android:hint="Username"
android:textSize="20sp"/>

</RelativeLayout>

3. Write a program to demonstrate the use of Autocomplete Textview


ANS:
<?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=".MainActivity">

<ImageView
android:id="@+id/imageView"
android:layout_width="229dp"
android:layout_height="110dp"
app:srcCompat="@drawable/google"
android:layout_centerHorizontal="true"/>

<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search..."
android:layout_centerHorizontal="true"
android:layout_below="@id/imageView"/>

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/autoCompleteTextView"
android:layout_marginTop="28dp"
android:text="Google search" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/autoCompleteTextView"
android:layout_toRightOf="@id/button"
android:layout_marginLeft="61dp"
android:layout_marginTop="28dp"
android:text="I'm Feeling Lucky" />
</RelativeLayout>

4. Write a program to demonstrate the use of Radio Button


ANS:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">

<RadioGroup
android:id="@+id/rgroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<RadioButton
android:id="@+id/radioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#9CAAE0"
android:text="Male" />

<RadioButton
android:id="@+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Female" />
</RadioGroup>

</LinearLayout>

5. Write a program to demonstrate the use of List View


ANS:
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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
activity_listview.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:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="90dp" />
</LinearLayout>
string.xml
<resources>
<string name="app_name">Experiment 14.1</string>
<string-array name="languagelist">
<item>Android</item>
<item>Java</item>
<item>PHP</item>
<item>Hadoop</item>
<item>Sap</item>
<item>Python</item>
<item>Ajax</item>
<item>C++</item>
<item>Ruby</item>
<item>Rails</item>
<item>Javascript</item>
</string-array>

</resources>
mainActivity.java
package com.example.experiment141;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

ListView simplelist;
String
languagelist[]={"Android","Java","PHP","Hadoop","Sap","Python","Ajax","C++","Ruby","R
ails","Javascript"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

simplelist=(ListView)findViewById(R.id.listview);
ArrayAdapter adapter=new
ArrayAdapter(this,R.layout.activity_listview,R.id.textview,languagelist);
simplelist.setAdapter(adapter);

simplelist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String value = (String) adapter.getItem(position);
Toast.makeText(MainActivity.this,value,Toast.LENGTH_LONG).show();
}
});
}
}

6. Write a program to create a of Custom Toast


ANS:
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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<CheckBox
android:id="@+id/chkjava"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Java" />

<CheckBox
android:id="@+id/chkc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="C" />

<CheckBox
android:id="@+id/chkandroid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="Android" />
<CheckBox
android:id="@+id/chkphp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="PHP" />

<CheckBox
android:id="@+id/chkjavascript"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Javascript" />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Display" />

</LinearLayout>

MainActivity.java
package com.example.experiment11;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

CheckBox ch1,ch2,ch3,ch4,ch5;
Button btndisplay;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

addListenerOnButton();
addListenerOnch1();

private void addListenerOnch1() {


ch1=(CheckBox)findViewById(R.id.chkjava);
ch1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(((CheckBox) v).isChecked()){
Toast.makeText(MainActivity.this, "Try another checkbox",
Toast.LENGTH_LONG).show();
}
}
});
}

private void addListenerOnButton() {


ch1=(CheckBox)findViewById(R.id.chkjava);
ch2=(CheckBox)findViewById(R.id.chkc);
ch3=(CheckBox)findViewById(R.id.chkandroid);
ch4=(CheckBox)findViewById(R.id.chkphp);
ch5=(CheckBox)findViewById(R.id.chkjavascript);
btndisplay=(Button)findViewById(R.id.button);
btndisplay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer result= new StringBuffer();
result.append("Java Check:").append(ch1.isChecked());
result.append("C Check:").append(ch2.isChecked());
result.append("Android Check:").append(ch3.isChecked());
result.append("PHP Check:").append(ch4.isChecked());
result.append("Javascript Check:").append(ch5.isChecked());

Toast.makeText(MainActivity.this,result.toString(),Toast.LENGTH_LONG).show();
}
});
}
}

7. Write a program to demonstrate the use of DatePicker


ANS:
Activity_main.xml
<?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=".MainActivity">

<EditText
android:id="@+id/editdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:layout_marginTop="82dp"/>

<EditText
android:id="@+id/edittime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editdate"
android:ems="10"
android:inputType="textPersonName" />

<Button
android:id="@+id/btn1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginTop="82dp"
android:layout_toRightOf="@id/editdate"
android:backgroundTint="#8F2323"
android:text="select date"/>

<Button
android:id="@+id/btn2"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_below="@id/btn1"
android:layout_toRightOf="@id/edittime"
android:backgroundTint="#A3D5EC"
android:text="select time" />

</RelativeLayout>

MainActivity.java
package com.example.experiment162;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
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.time.Year;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{


Button btnDatePicker,btnTimePicker;
EditText txtDate,txtTime;
int mYear,mMonth,mDate,mHour,mMinute;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnDatePicker=(Button)findViewById(R.id.btn1);
btnTimePicker=(Button)findViewById(R.id.btn2);
txtDate=(EditText)findViewById(R.id.editdate);
txtTime=(EditText)findViewById(R.id.edittime);

btnDatePicker.setOnClickListener(this);
btnTimePicker.setOnClickListener(this);

@Override
public void onClick(View v) {
if(v==btnDatePicker){
final Calendar c=Calendar.getInstance();
mYear=c.get(Calendar.YEAR);
mMonth=c.get(Calendar.MONTH);
mDate=c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog datePickerDialog=new DatePickerDialog(this, new


DatePickerDialog.OnDateSetListener() {

@SuppressLint("SetTextI18n")
@Override
public void onDateSet(DatePicker view, int year, int month, int
dayOfMonth) {
txtDate.setText(dayOfMonth+"-"+(month+1)+"-"+ year);

}
},mYear,mMonth,mDate);
datePickerDialog.show();

}
if (v==btnTimePicker) {
final Calendar c=Calendar.getInstance();

mHour=c.get(Calendar.HOUR_OF_DAY);
mMinute=c.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog=new TimePickerDialog(this, new
TimePickerDialog.OnTimeSetListener() {

@SuppressLint("SetTextI18n")
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
txtTime.setText(hourOfDay+":"+minute);

}
},mHour,mMinute,false);
timePickerDialog.show();
}
}
}

8. Android Program to enable and disable WiFi Connection


ANS:
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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Enable WiFi"/>

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Disable WiFi"/>

</LinearLayout>

MainActivity.java
package com.example.experiment201;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


Button enableButton,disableButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

enableButton=(Button)findViewById(R.id.button1);
disableButton=(Button)findViewById(R.id.button2);
enableButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
WifiManager wifi = (WifiManager)
getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(true);
}
});
disableButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
WifiManager wifi = (WifiManager)
getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(false);
}
});
}
}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.experiment201">
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<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/Theme.Experiment201">
<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>

9. Develop a program to implement explicit intent and implicit intent


ANS:
Explicit intent
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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="Enter_the_website"
tools:layout_editor_absoluteX="99dp"
tools:layout_editor_absoluteY="96dp"
tools:ignore="MissingConstraints"
android:autofillHints=""/>

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Navigation"
tools:layout_editor_absoluteX="151dp"
tools:layout_editor_absoluteY="176dp"
tools:ignore="MissingConstraints"/>

</LinearLayout>

MainActivity.java
package com.example.experiment181;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button button;
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText)findViewById(R.id.editText);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
String url=editText.getText().toString();
Intent i1 = new Intent(Intent.ACTION_VIEW, Uri.parse(url)) ;
startActivity(i1);
}
});
}
}

Implicit intent
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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">

<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="35dp" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:backgroundTint="#B086F7"
android:text="Start dialer" />

</LinearLayout>

MainActivity.xml
package com.example.experiment182;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

EditText editText;
Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.edittext);
button = (Button) findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onClick(View v) {

String phone = editText.getText().toString();


Intent phone_intent = new Intent(Intent.ACTION_CALL);
phone_intent.setData(Uri.parse(phone));
startActivity(phone_intent);
}
});
}
}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.experiment182">

<uses-permission android:name="android.permission.CALL_PHONE"/>
<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/Theme.Experiment182">
<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>

10.Write a program to send SMS


ANS:
Activity_main.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:id="@+id/fstTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="150dp"
android:text="Mobile No" />
<EditText
android:id="@+id/mblTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10"/>

<TextView
android:id="@+id/secTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message"
android:layout_marginLeft="100dp" />
<EditText
android:id="@+id/msgTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10" />
<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="Send SMS" />
</LinearLayout>

MainActivity.java
package com.example.smssend;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


private EditText txtMobile;
private EditText txtMessage;
private Button btnSms;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtMobile = (EditText)findViewById(R.id.mblTxt);
txtMessage = (EditText)findViewById(R.id.msgTxt);
btnSms = (Button)findViewById(R.id.btnSend);
btnSms.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
SmsManager smgr = SmsManager.getDefault();

smgr.sendTextMessage(txtMobile.getText().toString(),null,txtMessage.getText().toStrin
g(),null,null);
Toast.makeText(MainActivity.this, "SMS Sent Successfully",
Toast.LENGTH_SHORT).show();
}
catch (Exception e){
Toast.makeText(MainActivity.this, "SMS Failed to Send, Please try
again", Toast.LENGTH_SHORT).show();
}
}
});
}
}

AndroidManifest.xml
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true">
<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>

11.Android to display all available sensors in a mobile device.


ANS:
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Shake to switch color" />

</RelativeLayout>

MainActivity.java
package com.example.sensor;

import android.app.Activity;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity implements SensorEventListener {

private SensorManager sensorManager;


private boolean isColor = false;
private View view;
private long lastUpdate;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = findViewById(R.id.textView);
view.setBackgroundColor(Color.GREEN);

sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);


lastUpdate = System.currentTimeMillis();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
getAccelerometer(event);
}

private void getAccelerometer(SensorEvent event) {


float[] values = event.values;
// Movement
float x = values[0];
float y = values[1];
float z = values[2];

float accelationSquareRoot = (x * x + y * y + z * z)
/ (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);

long actualTime = System.currentTimeMillis();

if (accelationSquareRoot >= 2) //it will be executed if you shuffle


{

if (actualTime - lastUpdate < 200) {


return;
}
lastUpdate = actualTime;//updating lastUpdate for next shuffle
if (isColor) {
view.setBackgroundColor(Color.GREEN);

} else {
view.setBackgroundColor(Color.RED);
}
isColor = !isColor;
}
}

@Override
protected void onResume() {
super.onResume();
// register this class as a listener for the orientation and
// accelerometer sensors

sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_ACCELE
ROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
protected void onPause() {
// unregister listener
super.onPause();
sensorManager.unregisterListener(this);
}
}

12.Write a Bluetooth Android program to Turn ON Bluetooth, Turn OFF Bluetooth, make device
visible and display the list of all available Bluetooth devices.
ANS:
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="30dp"
tools:context=".MainActivity"
android:transitionGroup="true">

<TextView android:text="Bluetooth Example"


android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:textSize="35dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn On"
android:id="@+id/button"
android:layout_below="@+id/textview"
android:clickable="true"
android:onClick="on" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get visible"
android:onClick="visible"
android:id="@+id/button2"
android:layout_below="@+id/textview"
android:layout_toRightOf="@+id/button" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List devices"
android:onClick="list"
android:layout_below="@+id/button"
android:id="@+id/button3"
android:layout_toRightOf="@+id/button4"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="turn off"
android:onClick="off"
android:id="@+id/button4"
android:layout_below="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/button"
android:layout_alignStart="@+id/button"
android:layout_below="@+id/textView2" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Paired devices:"
android:id="@+id/textView2"
android:textColor="#ff34ff06"
android:textSize="25dp"
android:layout_below="@+id/button4"
android:layout_alignLeft="@+id/listView"
android:layout_alignStart="@+id/listView" />

</RelativeLayout>

MainActivity.java
package com.example.bluetooth;

import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;

import android.content.Intent;
import android.view.View;

import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.Set;

public class MainActivity extends AppCompatActivity {


Button b1,b2,b3,b4;
private BluetoothAdapter BA;
private Set<BluetoothDevice>pairedDevices;
ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

b1 = (Button) findViewById(R.id.button);
b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button3);
b4=(Button)findViewById(R.id.button4);

BA = BluetoothAdapter.getDefaultAdapter();
lv = (ListView)findViewById(R.id.listView);
}

public void on(View v){


if (!BA.isEnabled()) {
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
Toast.makeText(getApplicationContext(), "Turned
on",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Already on",
Toast.LENGTH_LONG).show();
}
}

public void off(View v){


BA.disable();
Toast.makeText(getApplicationContext(), "Turned off"
,Toast.LENGTH_LONG).show();
}

public void visible(View v){


Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(getVisible, 0);
}

public void list(View v){


pairedDevices = BA.getBondedDevices();

ArrayList list = new ArrayList();

for(BluetoothDevice bt : pairedDevices) list.add(bt.getName());


Toast.makeText(getApplicationContext(), "Showing Paired
Devices",Toast.LENGTH_SHORT).show();

final ArrayAdapter adapter = new


ArrayAdapter(this,android.R.layout.simple_list_item_1, list);

lv.setAdapter(adapter);
}
}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bluetooth">
<uses-permission android:name="android.permission.BLUETOOTH" />
<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/Theme.Bluetooth">
<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>

You might also like