You are on page 1of 9

6. Attempt any TWO of the following.

12 Marls

a) Design an android application to show the list of paired devices by Bluetooth.


Ans:

activity_main.xml

<RelativeLayout xmlns:androclass="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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="61dp"
android:text="Showing Paired Devices:" />

</RelativeLayout>

Provide Permission

 You need to provide following permissions in AndroidManifest.xml file.


<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

MainActivity.java

package com.example.bluetoothshowpaired;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import java.util.Set;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.widget.TextView;

public class MainActivity extends Activity


{
TextView textview1;
private static final int REQUEST_ENABLE_BT = 1;
BluetoothAdapter btAdapter;

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

textview1 = (TextView) findViewById(R.id.textView1);

btAdapter = BluetoothAdapter.getDefaultAdapter();
textview1.append("\nAdapter: " + btAdapter);

CheckBluetoothState();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_ENABLE_BT)
{
CheckBluetoothState();
}
}

@Override
protected void onDestroy() {
super.onDestroy();
}

private void CheckBluetoothState()


{
if(btAdapter==null)
{
textview1.append("\nBluetooth NOT supported. Aborting.");
return;
}
else
{
if (btAdapter.isEnabled())
{
textview1.append("\nBluetooth is enabled...");

// Listing paired devices


textview1.append("\nPaired Devices are:");
Set<BluetoothDevice> devices = btAdapter.getBondedDevices();
for (BluetoothDevice device : devices)
{
textview1.append("\n Device: " + device.getName() + ", " + device);
}
}
else
{
Intent enableBtIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

b) Develop an android application for sending Short Message Service (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.sendsmsexample;
import android.content.Intent;
import android.net.Uri;
import android.provider.Telephony;
import android.support.v7.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().toString(),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
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sendsmsexample">
<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"
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>
c) Explain how linear and frame layout is used to design an android application with
suitable example.
Ans:

Frame Layout :
Frame Layout is one of the simplest layout to organize view controls.
Used to block an area on the screen.
Frame Layout should be used to hold child view, because it can be difficult to display single
views at a specific area on the screen without overlapping each other.
Multiple children can be added to a FrameLayout and control their position by assigning gravity
to each child, using the android:layout_gravity attribute.

Attributes of Frame Layout:

1. android:id
This is the unique id which identifies the layout in the R.java file.

2. android:foreground
Foreground defines the drawable to draw over the content and this may be a color value. Possible
color values can be in the form of “#rgb”, “#argb”, “#rrggbb”, or “#aarrggbb”.

3. android:foregroundGravity
This defines the gravity to apply to the foreground drawable. Default value of gravity is fill. We
can set values in the form of “top”, ”center_vertical” , ”fill_vertical”, ”center_horizontal”,
”fill_horizontal”, ”center”, ”fill”, ”clip_vertical”, ”clip_horizontal”, ”bottom”, ”left” or ”right” .

4. android:visibility
This determine whether to make the view visible, invisible or gone.
visible – the view is present and also visible
invisible – The view is present but not visible
gone – The view is neither present nor visible

5. android:measureAllChildren
This determines whether to measure all children including gone state visibility or just those
which are in the visible or invisible state of measuring visibility. The default value of
measureallchildren is false. We can set values in the form of Boolean i.e. “true” OR “false”.
activity_main.xml

Example:

activity_main.xml

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


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
>
<TextView android:text="LeftTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="RightTop"
android:layout_gravity="top|right" />

<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="CentreTop"
android:layout_gravity="top|center_horizontal" />

<TextView android:text="Left"
android:layout_gravity="left|center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Right"
android:layout_gravity="right|center_vertical" />

<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Centre"
android:layout_gravity="center" />

<TextView android:text="LeftBottom"
android:layout_gravity="left|bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="RightBottom"
android:layout_gravity="right|bottom" />
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="CenterBottom"
android:layout_gravity="center|bottom" />
</FrameLayout>

MainActivity.java

package com.example.frametesting;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

Linear Layout :
Linear layout is a simple layout used in android for layout designing.
In the Linear layout all the elements are displayed in linear fashion means all the childs/elements
of a linear layout are displayed according to its orientation.
The value for orientation property can be either horizontal or vertical.

Types Of Linear Layout Orientation


There are two types of linear layout orientation:
1. Vertical
2. Horizontal

Main Attributes In Linear Layout:


1. orientation: The orientation attribute used to set the childs/views horizontally or vertically. In
Linear layout default orientation is vertical.

2. gravity: The gravity attribute is an optional attribute which is used to control the alignment of
the layout like left, right, center, top, bottom etc.

3. layout_weight: The layout weight attribute specify each child control’s relative importance
within the parent linear layout.

4. weightSum: weightSum is the sum up of all the child attributes weight. This attribute is
required if we define weight property of the childs.

Example:
<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="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Linear Layout (Without Weight)"
android:id="@+id/textView"
android:layout_gravity="center_horizontal" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 1"
android:background="#009300" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 2"
android:background="#e6cf00" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 3"
android:background="#0472f9" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 4"
android:background="#e100d5" />
</LinearLayout>

You might also like