CA20304-MAD - Lab (SSV) - 12 12 22

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 86

Ex.No.1(a). Develop an application that uses GUI components, CheckBox demo.

Date :

AIM:

To develop an application using GUI components

ALGORITHM:

1. Open eclipse or android studio and select new android project.

2. Give project name and select next.

3. Choose the android version. Choose the lowest android version (Android 2.2) and select
next.

4. Enter the package name. Package name must be two word separated by comma and
click finish.

5. Go to package explorer in the left hand side. Select our project.

6. Go to res folder and select layout. Double click the main.xml file.

7. Now you can see the graphics layout window.

8. Click the main.xml file and type the code.

9. Go to project explorer and src folder. Now select mainactivity.java file and type the code.

10. Now go to the main.xml and right click. Select run as option and select run configuration.

11. Android output is present in the android emulator.


PROGRAM :
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="com.example.admin.check2.MainActivity">

<CheckBox
android:id="@+id/c1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:checked="false"
android:padding="10dp"
android:text="Under Graduate" />

<CheckBox
android:id="@+id/c2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:layout_centerHorizontal="true"
android:padding="10dp"
android:text="Post Graduate" />

<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_gravity="center_horizontal" />

<EditText
android:id="@+id/e1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</LinearLayout>
MainActivity.java

package com.example.admin.check2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;
public class MainActivity extends AppCompatActivity
{
CheckBox cc1,cc2;
TextView tt1;
EditText ee1;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cc1=(CheckBox)findViewById(R.id.c1);
cc2=(CheckBox)findViewById(R.id.c2);
tt1=(TextView)findViewById(R.id.t1);
ee1=(EditText)findViewById(R.id.e1);
cc1.setOnCheckedChangeListener(new s());
cc2.setOnCheckedChangeListener(new s());
}
class s implements CompoundButton.OnCheckedChangeListener
{
public void onCheckedChanged(CompoundButton button, boolean isChecked)
{
if(cc1.isChecked())
{ ee1.setText("Under Graduate"); }
if(cc2.isChecked())
{ ee1.setText("Post Graduate"); } } } }
OUTPUT :
Ex.No.1(b). Develop an application that uses GUI components, RadioButton Demo.
Date :

AIM:

To develop an application using GUI components

ALGORITHM:

1. Open eclipse or android studio and select new android project.

2. Give project name and select next.

3. Choose the android version. Choose the lowest android version (Android 2.2) and select
next.

4. Enter the package name. Package name must be two word separated by comma and
click finish.

5. Go to package explorer in the left hand side. Select our project.

6. Go to res folder and select layout. Double click the main.xml file.

7. Now you can see the graphics layout window.

8. Click the main.xml file and type the code.

9. Go to project explorer and src folder. Now select mainactivity.java file and type the code.

10. Now go to the main.xml and right click. Select run as option and select run configuration.

11. Android output is present in the android emulator.


PROGRAM :

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="com.example.admin.radioexam1.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:layout_marginLeft="100dp"
android:textSize="18dp"
android:text="Select Your Course"
android:textStyle="bold"
android:id="@+id/txtView"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/rdGroup"
android:layout_below="@+id/txtView">
<RadioButton
android:id="@+id/rdbJava"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="100dp"
android:text="Java"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/rdbPython"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="100dp"
android:text="Python"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/rdbAndroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="100dp"
android:text="Android"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/rdbAngular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="100dp"
android:text="AngularJS"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
<Button
android:id="@+id/getBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_below="@+id/rdGroup"
android:text="Get Course" />
</RelativeLayout>
MainActivity.java

package com.example.admin.radioexam1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity


{
RadioButton android, java, angular, python;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

android = (RadioButton)findViewById(R.id.rdbAndroid);
angular = (RadioButton)findViewById(R.id.rdbAngular);
java = (RadioButton)findViewById(R.id.rdbJava);
python = (RadioButton)findViewById(R.id.rdbPython);
Button btn = (Button)findViewById(R.id.getBtn);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String result = "Selected Course: ";
result+=
(android.isChecked())?"Android":(angular.isChecked())?"AngularJS":(java.isChecked())?"Java":
(python.isChecked())?"Python":"";
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
}
});
}
public void onRadioButtonClicked(View view)
{
boolean checked = ((RadioButton)
view).isChecked(); String str="";
switch(view.getId())
{
case R.id.rdbAndroid:
if(checked)
str = "Android Selected";
break;
case R.id.rdbAngular:
if(checked)
str = "AngularJS Selected";
break;
case R.id.rdbJava:
if(checked)
str = "Java Selected";
break;
case R.id.rdbPython:
if(checked)
str = "Python Selected";
break;
}
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}
}
OUTPUT:
Ex.No. 2. Add a Spinner. When the Spinner is selected, there should be three option (e.g.,
Android, Java, Testing).
Date :

AIM:

To develop an application using Spinner Components

ALGORITHM:

1. Open eclipse or android studio and select new android project.

2. Give project name and select next.

3. Choose the android version. Choose the lowest android version (Android 2.2) and select
next.

4. Enter the package name. Package name must be two word separated by comma and
click finish.

5. Go to package explorer in the left hand side. Select our project.

6. Go to res folder and select layout. Double click the main.xml file.

7. Now you can see the graphics layout window.

8. Click the main.xml file and type the code.

9. Go to project explorer and src folder. Now select mainactivity.java file and type the code.

10. Now go to the main.xml and right click. Select run as option and select run configuration.

11. Android output is present in the android emulator.


PROGRAM:

activity_main.xml

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<TextView
android:id="@+id/txtVw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="150dp"
android:text="Select User:"
android:textStyle="bold"
android:textSize="15dp" />

<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/txtVw"
android:layout_toRightOf="@+id/txtVw" />
</RelativeLayout>
MainActivity.java

package com.tutlane.spinnerexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements


AdapterView.OnItemSelectedListener
{
String[] users = { "Android", "Java", "Testing" };
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spin = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, users);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter);
spin.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id)
{
Toast.makeText(getApplicationContext(), "Selected User: "+users[position],
Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0)
{ } }
OUTPUT:
Ex.No.3. Calculator Application

Date:

AIM :

To develop a Calculator application and to manipulate operations addition, subtraction,


multiplication and division.

ALGORITHM :

1. In the layout create a textbox and buttons for numbers and operators.
2. For each buttons get their id using findviewbyid() and setonclicklistener.
3. Create onclick() method to get the value of each button.
4. using switch statement compare the ids of the button.
5. Once matched invoke compute function to perform various operations.
PROGRAM :

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:id="@+id/relative1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/edt1"
android:layout_width="match_parent"
android:layout_height="75dp"
android:textSize="28dp" />

<Button
android:id="@+id/button1"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="80dp"
android:textSize="28dp"
android:text="1" />

<Button
android:id="@+id/button2"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="110dp"
android:layout_marginTop="80dp"
android:textSize="28dp"
android:text="2" />
<Button
android:id="@+id/button3"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="190dp"
android:layout_marginTop="80dp"
android:textSize="28dp"
android:text="3" />

<Button
android:id="@+id/button4"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="160dp"
android:textSize="28dp"
android:text="4" />

<Button
android:id="@+id/button5"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="110dp"
android:layout_marginTop="160dp"
android:textSize="28dp"
android:text="5" />

<Button
android:id="@+id/button6"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="190dp"
android:layout_marginTop="160dp"
android:textSize="28dp"
android:text="6" />

<Button
android:id="@+id/button7"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="240dp"
android:textSize="28dp"
android:text="7" />

<Button
android:id="@+id/button8"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="110dp"
android:layout_marginTop="240dp"
android:textSize="28dp"
android:text="8" />

<Button
android:id="@+id/button9"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="190dp"
android:layout_marginTop="240dp"
android:textSize="28dp"
android:text="9" />

<Button
android:id="@+id/buttonadd"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="270dp"
android:layout_marginTop="80dp"
android:textSize="28dp"
android:text="+" />

<Button
android:id="@+id/buttonsub"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="270dp"
android:layout_marginTop="160dp"
android:textSize="28dp"
android:text="-" />

<Button
android:id="@+id/buttonmul"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="270dp"
android:layout_marginTop="240dp"
android:textSize="28dp"
android:text="*" />

<Button
android:id="@+id/button10"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="320dp"
android:textSize="28dp"
android:text="." />

<Button
android:id="@+id/button0"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="110dp"
android:layout_marginTop="320dp"
android:textSize="28dp"
android:text="0" />

<Button
android:id="@+id/buttonC"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="190dp"
android:layout_marginTop="320dp"
android:textSize="28dp"
android:text="C" />

<Button
android:id="@+id/buttondiv"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="270dp"
android:layout_marginTop="320dp"
android:textSize="28dp"
android:text="/" />

<Button
android:id="@+id/buttoneql"
android:layout_width="315dp"
android:layout_height="75dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="400dp"
android:textSize="28dp"
android:text="=" />
</RelativeLayout>
MainActivity.java

package com.crunchify.tutorials.crunchifycalculator;
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 button0, button1, button2, button3, button4, button5, button6, button7, button8, button9,
buttonAdd, buttonSub, buttonDivision, buttonMul, button10, buttonC, buttonEqual;
EditText crunchifyEditText;
float mValueOne, mValueTwo;
boolean crunchifyAddition, mSubtract, crunchifyMultiplication, crunchifyDivision;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button0 = (Button) findViewById(R.id.button0);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button8 = (Button) findViewById(R.id.button8);
button9 = (Button) findViewById(R.id.button9);
button10 = (Button) findViewById(R.id.button10);
buttonAdd = (Button) findViewById(R.id.buttonadd);
buttonSub = (Button) findViewById(R.id.buttonsub);
buttonMul = (Button) findViewById(R.id.buttonmul);
buttonDivision = (Button) findViewById(R.id.buttondiv);
buttonC = (Button) findViewById(R.id.buttonC);
buttonEqual = (Button) findViewById(R.id.buttoneql);
crunchifyEditText = (EditText) findViewById(R.id.edt1);
button1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{ crunchifyEditText.setText(crunchifyEditText.getText() + "1"); }
});
button2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{ crunchifyEditText.setText(crunchifyEditText.getText() + "2"); }
});
button3.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{ crunchifyEditText.setText(crunchifyEditText.getText() + "3"); }
});
button4.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{ crunchifyEditText.setText(crunchifyEditText.getText() + "4"); }
});
button5.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{ crunchifyEditText.setText(crunchifyEditText.getText() + "5"); }
});
button6.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{ crunchifyEditText.setText(crunchifyEditText.getText() + "6"); }
});
button7.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{ crunchifyEditText.setText(crunchifyEditText.getText() + "7"); }
});
button8.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{ crunchifyEditText.setText(crunchifyEditText.getText() + "8"); }
});
button9.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{ crunchifyEditText.setText(crunchifyEditText.getText() + "9"); }
});
button0.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{ crunchifyEditText.setText(crunchifyEditText.getText() + "0"); }
});
buttonAdd.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
if (crunchifyEditText == null)
{ crunchifyEditText.setText(""); }
else {
mValueOne = Float.parseFloat(crunchifyEditText.getText() + "");
crunchifyAddition = true;
crunchifyEditText.setText(null);
}
}
});

buttonSub.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{ mValueOne = Float.parseFloat(crunchifyEditText.getText() +
""); mSubtract = true;
crunchifyEditText.setText(null); }
});
buttonMul.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{ mValueOne = Float.parseFloat(crunchifyEditText.getText() + "");
crunchifyMultiplication = true;
crunchifyEditText.setText(null); }
});
buttonDivision.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{ mValueOne = Float.parseFloat(crunchifyEditText.getText() + "");
crunchifyDivision = true;
crunchifyEditText.setText(null); }
});
buttonEqual.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{ mValueTwo = Float.parseFloat(crunchifyEditText.getText() +
""); if (crunchifyAddition == true)
{ crunchifyEditText.setText(mValueOne + mValueTwo + "");
crunchifyAddition = false; }
if (mSubtract == true)
{ crunchifyEditText.setText(mValueOne - mValueTwo + "");
mSubtract = false; }
if (crunchifyMultiplication == true)
{ crunchifyEditText.setText(mValueOne * mValueTwo +
""); crunchifyMultiplication = false; }
if (crunchifyDivision == true)
{ crunchifyEditText.setText(mValueOne / mValueTwo +
""); crunchifyDivision = false; }
}
});
buttonC.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{ crunchifyEditText.setText(""); }
});
button10.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{ crunchifyEditText.setText(crunchifyEditText.getText() + "."); }
});
}
}
OUTPUT:
Ex.No.4. Add two EditText. When a number is entered in Edit Text 1, the square of that
number should be displayed in Edit Text 2 as a separate Activity.

Date :

AIM:

To develop an application using GUI Components(EditText, Button)

ALGORITHM:

1. Open eclipse or android studio and select new android project.

2. Give project name and select next.

3. Choose the android version. Choose the lowest android version (Android 2.2) and select
next.

4. Enter the package name. Package name must be two word separated by comma and
click finish.

5. Go to package explorer in the left hand side. Select our project.

6. Go to res folder and select layout. Double click the main.xml file.

7. Now you can see the graphics layout window.

8. Click the main.xml file and type the code.

9. Go to project explorer and src folder. Now select mainactivity.java file and type the code.

10. Now go to the main.xml and right click. Select run as option and select run configuration.

11. Android output is present in the android emulator.


PROGRAM:

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"
tools:layout_editor_absoluteY="81dp">

<EditText
android:id="@+id/et1"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginLeft="100dp"
android:layout_marginTop="40dp"
android:inputType="number" />

<EditText
android:id="@+id/et2"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginLeft="100dp"
android:layout_marginTop="90dp"
android:inputType="number"/>

<Button
android:id="@+id/bb1"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="110dp"
android:layout_marginTop="200dp"
android:text="Result" /> </RelativeLayout>
MainActivity.java

package com.example.admin.new1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity


{
EditText e1,e2;
Button b1;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1 =
(EditText)findViewById(R.id.et1); e2
= (EditText)findViewById(R.id.et2);
b1 = (Button)findViewById(R.id.bb1);
b1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
double n1 = Double.parseDouble(e1.getText().toString());
double ans = (n1 * n1);
e2.setText(Double.toString(ans));
}
});
}
}
OUTPUT:
Ex.No.5. Create applications to include Menus, Dialogs and Notifications in Android.
5 (a) Create a Application using Context Menu Demo
Date :

AIM:

To develop an application using GUI Components (Context Menu)

ALGORITHM:

1. Open eclipse or android studio and select new android project.

2. Give project name and select next.

3. Choose the android version. Choose the lowest android version (Android 2.2) and select
next.

4. Enter the package name. Package name must be two word separated by comma and
click finish.

5. Go to package explorer in the left hand side. Select our project.

6. Go to res folder and select layout. Double click the main.xml file.

7. Now you can see the graphics layout window.

8. Click the main.xml file and type the code.

9. Go to project explorer and src folder. Now select mainactivity.java file and type the code.

10. Now go to the main.xml and right click. Select run as option and select run configuration.

11. Android output is present in the android emulator.


PROGRAM

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

<Button
android:id="@+id/btnShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Long press me"
android:layout_marginTop="200dp" android:layout_marginLeft="100dp"/>
</LinearLayout>
MainActivity.java
package com.tutlane.contextmenuexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity


{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btnShow);
registerForContextMenu(btn);
}
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "Upload");
menu.add(0, v.getId(), 0, "Search");
menu.add(0, v.getId(), 0, "Share");
menu.add(0, v.getId(), 0, "Bookmark");
}
public boolean onContextItemSelected(MenuItem item) {
Toast.makeText(this, "Selected Item: " +item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
}
OUTPUT :
5(b). Create a Application using Alert Dialog Demo.
Date :

AIM:

To develop an application using GUI Components (Dialogbox)

ALGORITHM:

1. Open eclipse or android studio and select new android project.

2. Give project name and select next.

3. Choose the android version. Choose the lowest android version (Android 2.2) and select
next.

4. Enter the package name. Package name must be two word separated by comma and
click finish.

5. Go to package explorer in the left hand side. Select our project.

6. Go to res folder and select layout. Double click the main.xml file.

7. Now you can see the graphics layout window.

8. Click the main.xml file and type the code.

9. Go to project explorer and src folder. Now select mainactivity.java file and type the code.

10. Now go to the main.xml and right click. Select run as option and select run configuration.

11. Android output is present in the android emulator.


PROGRAM

Activity_main.xml

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


<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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.alertdialogexample.MainActivity">

<Button
android:text="@string/exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:onClick="exit"
android:textStyle="normal|bold"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="131dp"/>

<TextView
android:text="@string/click_over_button_to_exit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="68dp"
android:id="@+id/textView2"
android:layout_above="@+id/button"
android:layout_centerHorizontal="true"
android:textSize="18sp"
android:textStyle="normal|bold"
android:gravity="center"/>
</RelativeLayout>
MainActivity.java

package com.example.alertdialogexample;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

publicclassMainActivityextendsAppCompatActivity{

protectedvoid onCreate(Bundle savedInstanceState){


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
publicvoidexit(View view){
AlertDialog.Builder alertDialogBuilder=newAlertDialog.Builder(this);
alertDialogBuilder.setTitle("Confirm Exit..!!!");
alertDialogBuilder.setIcon(R.drawable.question);
alertDialogBuilder.setMessage("Are you sure,You want to exit");
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setPositiveButton("Yes",newDialogInterface.OnClickListener()
{ publicvoid onClick(DialogInterface arg0,int arg1){
finish();
}
});
alertDialogBuilder.setNegativeButton("No",newDialogInterface.OnClickListener(){
publicvoid onClick(DialogInterface dialog,int which){
Toast.makeText(MainActivity.this,"You clicked over No",Toast.LENGTH_SHORT).show();
}
});
alertDialogBuilder.setNeutralButton("Cancel",newDialogInterface.OnClickListener()
{ publicvoid onClick(DialogInterface dialog,int which){
Toast.makeText(getApplicationContext(),"You clicked on
Cancel",Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog =alertDialogBuilder.create();
alertDialog.show();
}
}
OUTPUT :
Ex.No. 6. SMS Demo Example Program
Date :

AIM:

To develop an application using SMS Demonstration

ALGORITHM:

1. Open eclipse or android studio and select new android project.

2. Give project name and select next.

3. Choose the android version. Choose the lowest android version (Android 2.2) and select
next.

4. Enter the package name. Package name must be two word separated by comma and
click finish.

5. Go to package explorer in the left hand side. Select our project.

6. Go to res folder and select layout. Double click the main.xml file.

7. Now you can see the graphics layout window.

8. Click the main.xml file and type the code.

9. Go to project explorer and src folder. Now select mainactivity.java file and type the code.

10. Now go to the main.xml and right click. Select run as option and select run configuration.

11. Android output is present in the android emulator.


PROGRAM :

Activity_main.xml

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


<RelativeLayoutxmlns: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="MainActivity">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sending SMS Example"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp"/>

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point "
android:textColor="#ff87ff09"
android:textSize="30dp"
android:layout_below="@+id/textView1"
android:layout_alignRight="@+id/imageButton"
android:layout_alignEnd="@+id/imageButton"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:src="@drawable/abc"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"/>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:hint="Enter Phone Number"
android:phoneNumber="true"
android:textColorHint="@color/abc_primary_text_material_dark"
android:layout_below="@+id/imageButton"
android:layout_centerHorizontal="true"/>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:layout_below="@+id/editText"
android:layout_alignLeft="@+id/editText"
android:layout_alignStart="@+id/editText"
android:textColorHint="@color/abc_primary_text_material_dark"
android:layout_alignRight="@+id/imageButton"
android:layout_alignEnd="@+id/imageButton"
android:hint="Enter SMS"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Sms"
android:id="@+id/btnSendSMS"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"/>

</RelativeLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<stringname="app_name">tutorialspoint</string>
</resources>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tutorialspoint">
<uses-permissionandroid:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">

<activity
android:name="com.example.tutorialspoint.MainActivity"
android:label="@string/app_name">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java

package com.example.tutorialspoint;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

publicclassMainActivityextendsActivity{
privatestaticfinalint MY_PERMISSIONS_REQUEST_SEND_SMS =0;
Button sendBtn;
EditText txtphoneNo, txtMessage;
String phoneNo, message;

protectedvoid onCreate(Bundle savedInstanceState){


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendBtn=(Button) findViewById(R.id.btnSendSMS);
txtphoneNo=(EditText) findViewById(R.id.editText);
txtMessage=(EditText) findViewById(R.id.editText2);
sendBtn.setOnClickListener(newView.OnClickListener(){
publicvoid onClick(View view)
{ sendSMSMessage(); }
});
}
protectedvoid sendSMSMessage(){
phoneNo= txtphoneNo.getText().toString();
message= txtMessage.getText().toString();
if(ContextCompat.checkSelfPermission(this,
Manifest.permission.SEND_SMS)
!=PackageManager.PERMISSION_GRANTED){
if(ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.SEND_SMS)){
}else{ ActivityCompat.requestPermissio
ns(this,
newString[]{Manifest.permission.SEND_SMS},
MY_PERMISSIONS_REQUEST_SEND_SMS);
}
}
}
publicvoid onRequestPermissionsResult(int requestCode,String permissions[],int[]
grantResults){
switch(requestCode){
case MY_PERMISSIONS_REQUEST_SEND_SMS:{
if(grantResults.length >0
&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
SmsManager smsManager =SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo,null, message,null,null);
Toast.makeText(getApplicationContext(),"SMS sent.",
Toast.LENGTH_LONG).show();
}else{ Toast.makeText(getApplicationCo
ntext(),
"SMS faild, please try again.",Toast.LENGTH_LONG).show();
return;
}
}
}
}
}
OUTPUT :
Ex.No.7. Create a Camera Application, Where you can click a picture and then save it as
the wallpaper.
Date :

AIM:

To develop an application using Camera Demonstration

ALGORITHM:

1. Open eclipse or android studio and select new android project.

2. Give project name and select next.

3. Choose the android version. Choose the lowest android version (Android 2.2) and select
next.

4. Enter the package name. Package name must be two word separated by comma and click
finish.

5. Go to package explorer in the left hand side. Select our project.

6. Go to res folder and select layout. Double click the main.xml file.

7. Now you can see the graphics layout window.

8. Click the main.xml file and type the code.

9. Go to project explorer and src folder. Now select mainactivity.java file and type the code.

10. Now go to the main.xml and right click. Select run as option and select run configuration.

11. Android output is present in the android emulator.


PROGRAM

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

<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="34dp"
android:layout_marginTop="36dp"
android:contentDescription="@string/hello_world"
android:src="@drawable/ic_launcher" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/imageView1"
android:text="@string/tap" android:textAppearance="?
android:attr/textAppearanceLarge" />
</RelativeLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Camera</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="tap">Tap the image to open the camera!!</string>
</resources>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.c package="com.example.camera"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.camera.Mainandroid:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java

package com.example.camera;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class MainActivity extends Activity


{
ImageViewimgFavorite;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgFavorite = (ImageView)findViewById(R.id.imageView1);
imgFavorite.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{ open(); }
});
}
public void open()
{
Intent intent = new
Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
}
protected void onActivityResult(intrequestCode, intresultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bp = (Bitmap) data.getExtras().get("data");
imgFavorite.setImageBitmap(bp);
}
public booleanonCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
OUTPUT :
Ex.No.8. Create a user login form and registration form. First time users have to register
through the registration form and the details should be stored in the database. Then they
can login using the login page.

Date :

AIM:
To develop an application using GUI components

ALGORITHM:

1. Create three activities for login, registration and welcome page.


2. Add the necessary gui components like textbox,label,radio button, checkbox to the
layout page.
3. Set the properties of the gui components by right->click edit text, edit id or modify the
layout.xml file
4. Write the implementation code in java file. Include the necessary pacakages.write the
code inside create() function.
5. Get the values entered in the textbox, radio buttons and checkboxes using
findViewById().
6. In login activity perform validation and move to next page using intent() class.
7. Invoke putExtra(“keyvalue”,classname) method to send data from one page to other.
8. Invoke getStringExtra(“keyvalue”) method to receive data from one page.
9. Write button onclick listener to perform operation when button is clicked
Database - Creation
SQLiteDatabase mydatabase = openOrCreateDatabase("your database name",
MODE_PRIVATE, null);

Database - Insertion
mydatabase.execSQL("CREATE TABLE IF NOT EXISTS exam(Username
VARCHAR,Password VARCHAR);");
mydatabase.execSQL("INSERT INTO exam VALUES('admin','admin');");

Database - Fetching
Cursor resultSet = mydatbase.rawQuery("Select * from exam",null);
resultSet.moveToFirst();
String username = resultSet.getString(0);
String password = resultSet.getString(1);

Database – Helper
public class DBHelper extends SQLiteOpenHelper
{
public DBHelper()
{ super(context,DATABASE_NAME,null,1); }
public void onCreate(SQLiteDatabase db) {}
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {}
}
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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp"
android:text="Data Base" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials Point"
android:id="@+id/textView2"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:textSize="35dp"
android:textColor="#ff16ff01" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:src="@drawable/logo"/>

<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/scrollView"
android:layout_below="@+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">

<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</ListView>
</ScrollView>

</RelativeLayout>
MainActivity.java
package com.example.sairamkrishna.myapplication;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ActionBarActivity {


public final static String EXTRA_MESSAGE = "MESSAGE";
private ListView obj;
DBHelper mydb;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mydb = new DBHelper(this);
ArrayList array_list = mydb.getAllCotacts();
ArrayAdapter arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,
array_list);
obj = (ListView)findViewById(R.id.listView1);
obj.setAdapter(arrayAdapter);
obj.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
int id_To_Search = arg2 + 1;
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", id_To_Search);
Intent intent = new Intent(getApplicationContext(),DisplayContact.class);
intent.putExtras(dataBundle);
startActivity(intent);
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
super.onOptionsItemSelected(item);
switch(item.getItemId()) {
case R.id.item1:Bundle dataBundle = new Bundle();
dataBundle.putInt("id", 0);
Intent intent = new Intent(getApplicationContext(),DisplayContact.class);
intent.putExtras(dataBundle);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public boolean onKeyDown(int keycode, KeyEvent event)
{ if (keycode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
}
return super.onKeyDown(keycode, event);
}
}
DisplayContact.java

package com.example.sairamkrishna.myapplication;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class DisplayContact extends Activity {


int from_Where_I_Am_Coming = 0;
private DBHelper mydb;
TextView name, phone, email, street, place;
int id_To_Update = 0;

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_contact);
name = (TextView) findViewById(R.id.editTextName);
phone = (TextView) findViewById(R.id.editTextPhone);
email = (TextView) findViewById(R.id.editTextStreet);
street = (TextView) findViewById(R.id.editTextEmail);
place = (TextView) findViewById(R.id.editTextCity);
mydb = new DBHelper(this);
Bundle extras = getIntent().getExtras();
if(extras !=null) {
int Value = extras.getInt("id");
if(Value>0){
Cursor rs = mydb.getData(Value);
id_To_Update = Value;
rs.moveToFirst();
String nam =
rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_NAME));
String phon =
rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_PHONE));
String emai =
rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_EMAIL));
String stree =
rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_STREET));
String plac =
rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_CITY));
if (!rs.isClosed()) { rs.close(); }
Button b = (Button)findViewById(R.id.button1);
b.setVisibility(View.INVISIBLE);
name.setText((CharSequence)nam);
name.setFocusable(false);
name.setClickable(false);
phone.setText((CharSequence)phon);
phone.setFocusable(false);
phone.setClickable(false);
email.setText((CharSequence)emai);
email.setFocusable(false);
email.setClickable(false);
street.setText((CharSequence)stree);
street.setFocusable(false);
street.setClickable(false);
place.setText((CharSequence)plac);
place.setFocusable(false);
place.setClickable(false);
} } }
public boolean onCreateOptionsMenu(Menu menu) {
Bundle extras = getIntent().getExtras();
if(extras !=null) {
int Value = extras.getInt("id");
if(Value>0){
getMenuInflater().inflate(R.menu.display_contact, menu);
} else{
getMenuInflater().inflate(R.menu.menu_main menu);
}
}
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()) {
case R.id.Edit_Contact:
Button b = (Button)findViewById(R.id.button1);
b.setVisibility(View.VISIBLE);
name.setEnabled(true);
name.setFocusableInTouchMode(true);
name.setClickable(true);
phone.setEnabled(true);
phone.setFocusableInTouchMode(true);
phone.setClickable(true);
email.setEnabled(true);
email.setFocusableInTouchMode(true);
email.setClickable(true);
street.setEnabled(true);
street.setFocusableInTouchMode(true);
street.setClickable(true);
place.setEnabled(true);
place.setFocusableInTouchMode(true);
place.setClickable(true);
return true;
case R.id.Delete_Contact:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.deleteContact)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mydb.deleteContact(id_To_Update);
Toast.makeText(getApplicationContext(), "Deleted Successfully",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
}
})
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog d = builder.create();
d.setTitle("Are you sure");
d.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void run(View view) {
Bundle extras = getIntent().getExtras();
if(extras !=null) {
int Value = extras.getInt("id");
if(Value>0){
if(mydb.updateContact(id_To_Update,name.getText().toString(),
phone.getText().toString(), email.getText().toString(),
street.getText().toString(), place.getText().toString())){
Toast.makeText(getApplicationContext(), "Updated",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
} else{
Toast.makeText(getApplicationContext(), "not Updated",
Toast.LENGTH_SHORT).show();
}
} else{
if(mydb.insertContact(name.getText().toString(), phone.getText().toString(),
email.getText().toString(), street.getText().toString(),
place.getText().toString())){
Toast.makeText(getApplicationContext(), "done",
Toast.LENGTH_SHORT).show();
} else{
Toast.makeText(getApplicationContext(), "not done",
Toast.LENGTH_SHORT).show();
}
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
}
}
}
}
DBHelper.java

package com.example.sairamkrishna.myapplication;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;

public class DBHelper extends SQLiteOpenHelper {


public static final String DATABASE_NAME = "MyDBName.db";
public static final String CONTACTS_TABLE_NAME = "contacts";
public static final String CONTACTS_COLUMN_ID = "id";
public static final String CONTACTS_COLUMN_NAME = "name";
public static final String CONTACTS_COLUMN_EMAIL = "email";
public static final String CONTACTS_COLUMN_STREET = "street";
public static final String CONTACTS_COLUMN_CITY = "place";
public static final String CONTACTS_COLUMN_PHONE = "phone";
private HashMap hp;
public DBHelper(Context context)
{ super(context, DATABASE_NAME , null, 1); }
public void onCreate(SQLiteDatabase db)
{ db.execSQL(
"create table contacts " +
"(id integer primary key, name text,phone text,email text, street text,place text)"
);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db); }
public boolean insertContact (String name, String phone, String email, String street,String place)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("phone", phone);
contentValues.put("email", email);
contentValues.put("street", street);
contentValues.put("place", place);
db.insert("contacts", null, contentValues);
return true;
}
public Cursor getData(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from contacts where id="+id+"", null );
return res;
}
public int numberOfRows(){
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, CONTACTS_TABLE_NAME);
return numRows;
}
public boolean updateContact (Integer id, String name, String phone, String email, String
street,String place) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("phone", phone);
contentValues.put("email", email);
contentValues.put("street", street);
contentValues.put("place", place);
db.update("contacts", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
return true;
}
public Integer deleteContact (Integer id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("contacts", "id = ? ",new String[] { Integer.toString(id) });
}
public ArrayList<String> getAllCotacts() {
ArrayList<String> array_list = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from contacts", null );
res.moveToFirst();
while(res.isAfterLast() == false){
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME)));
res.moveToNext();
}
return array_list;
}
}
OUTPUT :
Ex.No.9. Create a media player, which plays an mp3 song and will record the
sound. Date :

AIM:

To develop an application using Media Player Demonstration

ALGORITHM:

1. Open eclipse or android studio and select new android project.

2. Give project name and select next.

3. Choose the android version. Choose the lowest android version (Android 2.2) and select
next.

4. Enter the package name. Package name must be two word separated by comma and
click finish.

5. Go to package explorer in the left hand side. Select our project.

6. Go to res folder and select layout. Double click the main.xml file.

7. Now you can see the graphics layout window.

8. Click the main.xml file and type the code.

9. Go to project explorer and src folder. Now select mainactivity.java file and type the code.

10. Now go to the main.xml and right click. Select run as option and select run configuration.

11. Android output is present in the android emulator.

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:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"

android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TextView android:text="Music Palyer" 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" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Tutorials point"

android:id="@+id/textView"

android:layout_below="@+id/textview"

android:layout_centerHorizontal="true"

android:textColor="#ff7aff24"

android:textSize="35dp" />

<ImageView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/imageView"

android:layout_below="@+id/textView"

android:layout_centerHorizontal="true"

android:src="@drawable/abc"/>

<Button
android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/forward"

android:id="@+id/button"

android:layout_alignParentBottom="true"

android:layout_alignParentLeft="true"

android:layout_alignParentStart="true" />

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/pause"

android:id="@+id/button2"

android:layout_alignParentBottom="true"

android:layout_alignLeft="@+id/imageView"

android:layout_alignStart="@+id/imageView" />

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/back"

android:id="@+id/button3"

android:layout_alignTop="@+id/button2"

android:layout_toRightOf="@+id/button2"

android:layout_toEndOf="@+id/button2" />

<Button

android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:text="@string/rewind"

android:id="@+id/button4"

android:layout_alignTop="@+id/button3"

android:layout_toRightOf="@+id/button3"

android:layout_toEndOf="@+id/button3" />

<SeekBar

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/seekBar"

android:layout_alignLeft="@+id/textview"

android:layout_alignStart="@+id/textview"

android:layout_alignRight="@+id/textview"

android:layout_alignEnd="@+id/textview"

android:layout_above="@+id/button" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textAppearance="?android:attr/textAppearanceSmall"

android:text="Small Text"

android:id="@+id/textView2"

android:layout_above="@+id/seekBar"

android:layout_toLeftOf="@+id/textView"

android:layout_toStartOf="@+id/textView" />

<TextView
android:layout_width="wrap_content"

android:layout_height="wrap_content" android:textAppearance="?

android:attr/textAppearanceSmall" android:text="Small Text"

android:id="@+id/textView3"

android:layout_above="@+id/seekBar"

android:layout_alignRight="@+id/button4"

android:layout_alignEnd="@+id/button4" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textAppearance="?android:attr/textAppearanceMedium"

android:text="Medium Text"

android:id="@+id/textView4"

android:layout_alignBaseline="@+id/textView2"

android:layout_alignBottom="@+id/textView2"

android:layout_centerHorizontal="true" />

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

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

<activity
android:name="com.example.sairamkrishna.myapplication.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>
</manifest>
MainActivity.java

package com.example.sairamkrishna.myapplication;

import android.app.Activity;

import android.media.MediaPlayer;

import android.os.Bundle;

import android.os.Handler;

import android.view.View;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.SeekBar;

import android.widget.TextView;

import android.widget.Toast;

import java.util.concurrent.TimeUnit;

public class MainActivity extends Activity

{ private Button b1,b2,b3,b4;

private ImageView iv;

private MediaPlayer mediaPlayer;

private double startTime = 0;

private double finalTime = 0;

private Handler myHandler = new

Handler();; private int forwardTime = 5000;

private int backwardTime = 5000;

private SeekBar seekbar;

private TextView tx1,tx2,tx3;


public static int oneTimeOnly = 0;

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);

iv = (ImageView)findViewById(R.id.imageView);

tx1 = (TextView)findViewById(R.id.textView2);

tx2 = (TextView)findViewById(R.id.textView3);

tx3 = (TextView)findViewById(R.id.textView4);

tx3.setText("Song.mp3");

mediaPlayer = MediaPlayer.create(this, R.raw.song);

seekbar = (SeekBar)findViewById(R.id.seekBar);

seekbar.setClickable(false);

b2.setEnabled(false);

b3.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

Toast.makeText(getApplicationContext(), "Playing

sound",Toast.LENGTH_SHORT).show();

mediaPlayer.start();

finalTime = mediaPlayer.getDuration();

startTime = mediaPlayer.getCurrentPosition();

if (oneTimeOnly == 0) {

seekbar.setMax((int) finalTime);

oneTimeOnly = 1;

}
tx2.setText(String.format("%d min, %d sec",

TimeUnit.MILLISECONDS.toMinutes((long) finalTime),

TimeUnit.MILLISECONDS.toSeconds((long) finalTime) -

TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long)

finalTime)))

);

tx1.setText(String.format("%d min, %d sec",

TimeUnit.MILLISECONDS.toMinutes((long) startTime),

TimeUnit.MILLISECONDS.toSeconds((long) startTime) -

TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long)

startTime)))

);

seekbar.setProgress((int)startTime);

myHandler.postDelayed(UpdateSongTime,100);

b2.setEnabled(true);

b3.setEnabled(false);

});

b2.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

Toast.makeText(getApplicationContext(), "Pausing

sound",Toast.LENGTH_SHORT).show();

mediaPlayer.pause();

b2.setEnabled(false);

b3.setEnabled(true);

});

b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)

{ int temp = (int)startTime;

if((temp+forwardTime)<=finalTime){

startTime = startTime + forwardTime;

mediaPlayer.seekTo((int) startTime);

Toast.makeText(getApplicationContext(),"You have Jumped forward 5

seconds",Toast.LENGTH_SHORT).show();

}else{

Toast.makeText(getApplicationContext(),"Cannot jump forward 5

seconds",Toast.LENGTH_SHORT).show();

});

b4.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

int temp = (int)startTime;

if((temp-backwardTime)>0){

startTime = startTime - backwardTime;

mediaPlayer.seekTo((int) startTime);

Toast.makeText(getApplicationContext(),"You have Jumped backward 5

seconds",Toast.LENGTH_SHORT).show();

}else{

Toast.makeText(getApplicationContext(),"Cannot jump backward 5

seconds",Toast.LENGTH_SHORT).show();

});

}
private Runnable UpdateSongTime = new Runnable() {

public void run() {

startTime = mediaPlayer.getCurrentPosition();

tx1.setText(String.format("%d min, %d sec",

TimeUnit.MILLISECONDS.toMinutes((long) startTime),

TimeUnit.MILLISECONDS.toSeconds((long) startTime) -

TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.

toMinutes((long) startTime)))

);

seekbar.setProgress((int)startTime);

myHandler.postDelayed(this, 100);

};

}
OUTPUT:
Ex. No.11. Create a thread, which performs a single task, and perform multithreading in
Android.

Date:

AIM:
To implement an application that implements Multithreading.

ALGORITHM:
1. In the design layout add two textviews and a button.
2. In mainactivity java file,create object for handler class.
3. using handler object invoke postDelayedfuntion.
4. Create object for runnable class.
5. Inside run method call a userdefined function.This function has an explicit intent
to invoke a webpage.
6. Oncreate thread starts followed by runnable thread.
PROGRAM :

MainActivity.java

package com.example.multithread;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity


{
Handler hand = new Handler();
Button clickme;
TextView timer;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timer = (TextView)
findViewById(R.id.timer);
clickme = (Button) findViewById(R.id.clickme);
hand.postDelayed(run, 1000);
}
Runnable run = new Runnable()
{
public void run()
{
updateTime();
}
};
public void updateTime()
{
Timer.setText("" + (Integer.parseInt(timer.getText().toString()) - 1));
if (Integer.parseInt(timer.getText().toString()) == 0)
{

Intent browserIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://google.com/"));


startActivity(browserIntent);
}
else
{
hand.postDelayed(run, 1000);
}
}
}
OUTPUT:

You might also like