0% found this document useful (0 votes)
117 views

Mobile Application LabManual

The document describes an Android application that allows a user to select a contact from a list. When a contact is selected, its name appears at the top of the list in a large, italicized, blue font. The application is implemented using Java code, XML layout files, and string resources. It contains code for displaying the contact list, handling item selection, and updating the text view with the selected contact.

Uploaded by

Chaithu Gowdru
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views

Mobile Application LabManual

The document describes an Android application that allows a user to select a contact from a list. When a contact is selected, its name appears at the top of the list in a large, italicized, blue font. The application is implemented using Java code, XML layout files, and string resources. It contains code for displaying the contact list, handling item selection, and updating the text view with the selected contact.

Uploaded by

Chaithu Gowdru
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

1.

Design an application that contains Phone Contacts in vertical linear


manner. Selected contact appears at the top of the list with a large
italicized font and a blue background.
MainActivity.java
package com.example.myrajesh;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {


    ListView listView;
    TextView txtSelect;
    String[] listitem;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       listitem=getResources().getStringArray(R.array.li_contact);
       listView=(ListView) findViewById(R.id.listView1);
       txtSelect=(TextView) findViewById(R.id.textView1);
       final ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1,
android.R.id.text1,listitem);
       listView.setAdapter(adapter);
       listView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
        @Override
    public void onItemClick(AdapterView<?> adapterView, View view,
int position, long l) {
    String value=adapter.getItem(position);  
      txtSelect.setText(value);
      txtSelect.setTypeface(null, Typeface.ITALIC);
    }
    });                
}
}
ActivityMain.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/textView1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:background="@android:color/holo_blue_bright"
       android:gravity="center"       />
   <ListView
       android:id="@+id/listView1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" >    
</ListView>
</LinearLayout>
listcontact.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"  
   android:id="@+id/textView"  
   android:layout_width="wrap_content"  
   android:layout_height="wrap_content"  
   android:text="Medium Text"  
   android:textStyle="bold"  
   android:textAppearance="?android:attr/textAppearanceMedium"  
   android:layout_marginLeft="10dp"  
   android:layout_marginTop="5dp"  
   android:padding="2dp"  
   android:textColor="#4d4d4d"       />

Strings.xml   [Available in res→value→strings.xml folder]


<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">labprogram1</string>
   <string name="action_settings">Settings</string>
   <string-array name="li_contact">  
       <item>Rajesh Rao K 1234567890</item>  
       <item>Suresh Babu 9087654321</item>  
       <item>Ramakrishna 1234554321</item>          
   </string-array>
  </resources>
2.Create an application that uses Layout Managers and Event
Listeners.
Mainactivity.java             
package com.example.program2;
import android.support.v7.app.ActionBarActivity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {


    Button btnConvert;
    TextView txtView;
    String[] dept;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       btnConvert=(Button) findViewById(R.id.btnConvert);
       dept=getResources().getStringArray(R.array.depts);
       Spinner s1=(Spinner) findViewById(R.id.spDept);
       ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this,android.R.layout.simple_spinner_dropdown_item,dept);
       s1.setAdapter(adapter);
       s1.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
        int index =arg0.getSelectedItemPosition();
        if(index>0){
    Toast.makeText(getBaseContext(), "you have selected
"+dept[index],Toast.LENGTH_SHORT).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
    }    });
           btnConvert.setOnClickListener(new View.OnClickListener() {
@Override
    public void onClick(View v) {
        txtView=(TextView) findViewById(R.id.edTxt);
        txtView.setTypeface(null, Typeface.BOLD_ITALIC);
    }    
});    
}
}
Activitymain.xml
<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:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context="com.example.program2.MainActivity" >
   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_centerHorizontal="true"
       android:orientation="vertical" >
       <TextView
           android:id="@+id/textView1"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:text="Lab Program Two"
           android:textAppearance="?android:attr/textAppearanceLarge"
/>
       <TextView
           android:id="@+id/textView2"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="" />

       <TextView
           android:id="@+id/edTxt"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:text="RNSIT"
           android:textAppearance="?android:attr/textAppearanceLarge"
/>
       <Button
           android:id="@+id/btnConvert"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:text="Convert to Italic" />
       <TextView
           android:id="@+id/textView4"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textAppearance="?android:attr/textAppearanceLarge"
/>
       <TextView
           android:id="@+id/textView5"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Select Your Dept"
           android:textAppearance="?android:attr/textAppearanceLarge"
/>
       <Spinner     android:id="@+id/spDept"
           android:layout_width="match_parent"
           android:layout_height="wrap_content" />
   </LinearLayout>
</RelativeLayout>

Strings.xml[Available in res→value→strings.xml folder]


<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">program2</string>
   <string name="hello_world">Hello world!</string>
   <string name="action_settings">Settings</string>
   <string-array name="depts">
      <item>Select</item>       
       <item>MCA</item>
       <item>MBA</item>
   </string-array>
</resources>
3.Develop a standard calculator application to perform basic
calculations like addition, subtraction, multiplication and division.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns: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/result"
android:layout_width="123dp"
android:layout_height="73dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="19dp"
android:layout_marginTop="91dp"
android:fontFamily="serif"
android:textSize="10pt"
tools:layout_editor_absoluteX="153dp"
tools:layout_editor_absoluteY="9dp" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
tools:layout_editor_absoluteY="58dp"
tools:layout_editor_absoluteX="16dp"
android:layout_above="@+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="31dp" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
tools:layout_editor_absoluteY="125dp"
tools:layout_editor_absoluteX="16dp"
android:layout_above="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="43dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD"
android:onClick="add"
tools:layout_editor_absoluteX="34dp"
tools:layout_editor_absoluteY="192dp"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/sub"
android:layout_toStartOf="@+id/sub"
android:layout_marginRight="52dp"
android:layout_marginEnd="52dp" />
<Button
android:id="@+id/sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_toEndOf="@+id/editText2"
android:layout_toRightOf="@+id/editText2"
android:text="Sub"
android:onClick="sub"/>
<Button
android:id="@+id/mul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/div"
android:layout_alignBottom="@+id/div"
android:layout_alignLeft="@+id/button"
android:layout_alignStart="@+id/button"
android:text="Mul"
android:onClick="mul" />
<Button
android:id="@+id/div"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/sub"
android:layout_alignRight="@+id/sub"
android:layout_below="@+id/sub"
android:layout_marginTop="23dp"
android:text="Div"
android:onClick="div" />
</RelativeLayout>
MainActivity.java
package com.example.srivatsa.malab_3;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void add(View v) {
int num1, num2, sum;
EditText t1 = (EditText) findViewById(R.id.editText);
EditText t2 = (EditText) findViewById(R.id.editText2);
TextView t3 = (TextView) findViewById(R.id.result);
num1 = Integer.parseInt(t1.getText().toString());
num2 = Integer.parseInt(t2.getText().toString());
sum = num1 + num2;
t3.setText("Result:\n"+Integer.toString(sum));
}
public void sub(View v) {
int num1, num2, sum;
EditText t1 = (EditText) findViewById(R.id.editText);
EditText t2 = (EditText) findViewById(R.id.editText2);
TextView t3 = (TextView) findViewById(R.id.result);
num1 = Integer.parseInt(t1.getText().toString());
num2 = Integer.parseInt(t2.getText().toString());
sum = num1 - num2;
t3.setText("Result:\n"+Integer.toString(sum));
}
public void mul(View v) {
int num1, num2, sum;
EditText t1 = (EditText) findViewById(R.id.editText);
EditText t2 = (EditText) findViewById(R.id.editText2);
TextView t3 = (TextView) findViewById(R.id.result);
num1 = Integer.parseInt(t1.getText().toString());
num2 = Integer.parseInt(t2.getText().toString());
sum = num1 * num2;
t3.setText("Result: \n"+Integer.toString(sum));
}
public void div(View v){
int num1, num2,sum;
EditText t1 = (EditText)findViewById(R.id.editText);
EditText t2 = (EditText)findViewById(R.id.editText2);
TextView t3 = (TextView)findViewById(R.id.result);
num1 = Integer.parseInt(t1.getText().toString());
num2 = Integer.parseInt(t2.getText().toString());
sum = num1 / num2;
t3.setText("Result:\n"+Integer.toString(sum));
}
}
4.Devise an application that draws basic graphical primitives
(rectangle, circle) on the screen.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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_graphics"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_alignParentLeft="true"
       android:layout_alignParentStart="true"
       android:layout_alignParentTop="true"
       android:layout_marginTop="13dp" />
</android.support.constraint.ConstraintLayout>

MainActivity.java
package com.example.srivatsa.malab4;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {


   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       Bitmap bg= Bitmap.createBitmap(720,1280,Bitmap.Config.ARGB_8888);
       ImageView I= (ImageView)findViewById(R.id.imageView_graphics);
       I.setBackgroundDrawable(new BitmapDrawable(bg));

       Canvas canvas = new Canvas(bg);


       Paint paint = new Paint();
       paint.setColor(Color.BLUE);
       paint.setTextSize(50);
       canvas.drawText("Rectangle", 420, 150, paint);
       canvas.drawRect(400, 200, 650, 700, paint);
       paint.setColor(Color.RED);
       canvas.drawText("Circle", 120, 150, paint);
       canvas.drawCircle(200, 350, 150, paint);
       paint.setColor(Color.GREEN);
       canvas.drawText("Square", 120, 800, paint);
       canvas.drawRect(50, 850, 350, 1150, paint);
       paint.setColor(Color.BLACK);
       canvas.drawText("Line", 480, 800, paint);
       canvas.drawLine(520, 850, 520, 1150, paint);
   }
}
5.Build a mobile application that create, save, update and delete data
in a database.
6.Devise an application that implements Multithreading.
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" >

<ImageView
android:id="@+id/imageView"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_margin="50dp"
android:layout_gravity="center" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="center"
android:text="Load Image 1" />

<Button
android:id="@+id /button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="center"
android:text="Load image 2" />
</LinearLayout>
Once the above code is placed in the said file, the Graphical view will contain an ImageView
component and two buttons. Now place two image files india1.png and india2.png into the folders
drawable-hdpi, drawable-ldpi etc. under res folder.

Code for MainActivity.java :

package com.example.program6;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

publicclass MainActivity extends Activity


{
ImageView img;
Button bt1, bt2;
@Override
protectedvoid onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1 = (Button)findViewById(R.id.button);
bt2= (Button) findViewById(R.id.button2);
img = (ImageView)findViewById(R.id.imageView);

bt1.setOnClickListener(new View.OnClickListener(){
@Override
publicvoid onClick(View v) {
new Thread(new Runnable(){
@Override
publicvoid run(){
img.post(new Runnable(){
@Override
publicvoid run(){
img.setImageResource(R.drawable.india1);
}
});
}
}).start();
}
});
bt2.setOnClickListener(new View.OnClickListener(){
@Override
publicvoid onClick(View v)
{
new Thread(new Runnable(){
@Override
publicvoid run(){
img.post(new Runnable(){
@Override
publicvoid run() {
img.setImageResource(R.drawable.india2);
}
});
}
}).start();
}
});
}
}

7.Develop a mobile application that uses GPS location information.


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="fill_parent"
android:layout_height="fill_parent">

<Button
android:id="@+id/retrieve_location_button"
android:text="Retrieve Location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>

MainActivity.java
package com.example.program7;
import android.app.Activity;
import android.os.Bundle;
importandroid.view.Menu;
importandroid.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.content.Context;
importandroid.util.Log;
import android.widget.Button;
import android.widget.Toast;

publicclass MainActivity extends Activity {


privatestaticfinallongMINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in
Meters
privatestaticfinallongMINIMUM_TIME_BETWEEN_UPDATES = 1000; // in
Milliseconds

protected LocationManager locationManager;


protected Button retrieveLocationButton;
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
retrieveLocationButton = (Button)
findViewById(R.id.retrieve_location_button);
locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
retrieveLocationButton.setOnClickListener(new OnClickListener(){
publicvoid onClick(View v) {
showCurrentLocation();
}
});

protectedvoid showCurrentLocation() {

Location location =
locationManager.getLastKnownLocation(
LocationManager.GPS_PROVIDER);
if (location != null) {
String message = String.format("Current Location \n
Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(MainActivity.this, message,
Toast.LENGTH_LONG).show();
}
}

privateclass MyLocationListener implements LocationListener {

publicvoid onLocationChanged(Location location) {


String message = String.format("New Location \n Longitude:
1$s \n Latitude: %2$s",location.getLongitude(),
location.getLatitude()
);
Toast.makeText(MainActivity.this, message,
Toast.LENGTH_LONG).show();
}

publicvoid onStatusChanged(String s, int i, Bundle b) {


Toast.makeText(MainActivity.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}

publicvoid onProviderDisabled(String s) {
Toast.makeText(MainActivity.this, "Provider disabled by the
user.GPS turned off", Toast.LENGTH_LONG).show();
}

publicvoid onProviderEnabled(String s) {
Toast.makeText(MainActivity.this, "Provider enabled by the
user. GPS turned on",Toast.LENGTH_LONG).show();
}
}
}

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.program7"
android:versionCode="1"
android:versionName="1.0">

<application android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission
android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-sdk android:minSdkVersion="3" />
</manifest>
8.Create an application that writes data to the SD card.
MainActivity.java
package in.wptraffcianalyzer.filereadwritedemo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
    EditText etPath;
    EditText etContent;
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);   
       String path = getPreferences(MODE_PRIVATE).getString("fpath",
"/sdcard/wpta_file1");                
       etPath = (EditText) findViewById(R.id.et_path);
       etPath.setText(path);
       etContent = (EditText) findViewById(R.id.et_content);
       OnClickListener saveClickListener = new OnClickListener(){
           
            @Override
  public void onClick(View v) {
    File file = new File(etPath.getText().toString());
    FileWriter writer=null;
    try {
writer = new FileWriter(file);
writer.write(etContent.getText().toString());
writer.close();
SharedPreferences.Editor editor =
getPreferences(MODE_PRIVATE).edit()editor.putString
("fpath", file.getPath());
editor.commit();
Toast.makeText(getBaseContext(), "Successfully
saved", Toast.LENGTH_SHORT).show();   
} catch (IOException e) {
        e.printStackTrace();
        }    
} };
        OnClickListener readClickListener = new OnClickListener(){
   
@Override
    public void onClick(View v) {
   File file = new File(etPath.getText().toString());
   String strLine="";
   StringBuilder text = new StringBuilder();
    try {
    FileReader fReader = new FileReader(file);   
                BufferedReader bReader = new BufferedReader(fReader);
    while( (strLine=bReader.readLine()) != null  ){
    text.append(strLine+"\n");   
}
                Toast.makeText(getBaseContext(), "Successfully
loaded",Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
e.printStackTrace();     }
    etContent.setText(text);
            }    
};
Button btnSave = (Button) findViewById(R.id.btn_save);
btnSave.setOnClickListener(saveClickListener);
Button btnRead = (Button) findViewById(R.id.btn_read);
btnRead.setOnClickListener(readClickListener);
       }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

Activitymain.xml
<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" >
   <EditText
       android:id="@+id/et_path"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:inputType="text" />
   <EditText
       android:id="@+id/et_content"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:inputType="textMultiLine"
       android:layout_below="@id/et_path"
       android:minLines="4"
       android:maxLines="4"         />
   <RelativeLayout
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_centerHorizontal="true"
       android:layout_below="@id/et_content"       >
   <Button
    android:id="@+id/btn_read"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/str_btn_read" />
 
<Button
    android:id="@+id/btn_save"
           android:layout_toRightOf="@id/btn_read"           
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@string/str_btn_save" />  
   </RelativeLayout>
   </RelativeLayout>

String.Xml
<resources>
   <string name="app_name">FileReadWriteDemo</string>
   <string name="hello_world">Hello world!</string>
   <string name="menu_settings">Settings</string>
   <string name="title_activity_main">FileReadWrite Demo</string>    
   <string name="str_btn_read">Read</string>
   <string name="str_btn_save">Save</string>
</resources>
9.Implement an application that creates an alert upon receiving a
message.
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">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message!"
android:textSize="30sp"/>

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="30sp"/>

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:layout_gravity="center"
android:text="notify"
android:textSize="30sp"
/>

</LinearLayout>
MainActivity.java
package com.example.administrator.expg09;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity{

Button notify;
EditText e;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notify=(Button) findViewById(R.id.button);
e=(EditText)findViewById(R.id.editText);
notify.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
Intent intent=new Intent(MainActivity.this,
SecondActivity.class);
PendingIntent pending=
PendingIntent.getActivity(MainActivity.this,0,intent,0);
Notification noti=new Notification.Builder(
MainActivity.this).setContentTitle("New Message").
setContentText(e.getText().toString()).
setSmallIcon(R.mipmap.ic_launcher).
setContentIntent(pending).build();
NotificationManager manager=(NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
noti.flags|=Notification.FLAG_AUTO_CANCEL;
manager.notify(0,noti);
}
});
}
}

10.Devise a mobile application that creates alarm clock.


AlarmReceiver.java
package com.example.administrator.alarm;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.widget.Toast;

public class AlarmReceiver extends BroadcastReceiver


{
@Override
public void onReceive(Context context, Intent intent){
Toast.makeText(context, "Alarm! Wake up! Wake up!",
Toast.LENGTH_LONG).show();
Uri alarmUri =
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null){
alarmUri =
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
Ringtone ringtone=RingtoneManager.getRingtone(context,alarmUri);
ringtone.play();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.alarm">

<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>
<receiver android:name=".AlarmReceiver">
</receiver>
</application>
</manifest>

MainActivity.java
package com.example.administrator.alarm;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.View;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.ToggleButton;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity{


TimePicker alarmTimePicker;
PendingIntent pendingIntent;
AlarmManager alarmManager;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarmTimePicker = (TimePicker) findViewById(R.id.timePicker);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
}
public void OnToggleClicked(View view){
long time;
if (((ToggleButton) view).isChecked()){
Toast.makeText(MainActivity.this, "ALARM ON",
Toast.LENGTH_SHORT).show();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,
alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE,
alarmTimePicker.getCurrentMinute());
Intent intent = new Intent(this, AlarmReceiver.class);
pendingIntent=PendingIntent.getBroadcast(this, 0, intent,0);

time=(calendar.getTimeInMillis()-
(calendar.getTimeInMillis()%60000));
if(System.currentTimeMillis()>time){
if (calendar.AM_PM == 0)
time = time + (1000*60*60*12);
else
time = time + (1000*60*60*24);
}
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time,
10000, pendingIntent);
}
else{
alarmManager.cancel(pendingIntent);
Toast.makeText(MainActivity.this, "ALARM OFF",
Toast.LENGTH_SHORT).show();
}
}
}

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">

<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />

<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:checked="false"
android:onClick="OnToggleClicked" />
</LinearLayout>

You might also like