Android Development Setup Guide
Android Development Setup Guide
01
Select location :-
1
Click on install button :-
Main activity
[Link].hello_android_sanfoundry;
[Link];
[Link];
[Link];
@Override
protectedvoid onCreate(Bundle savedInstanceState){
[Link](savedInstanceState);
setContentView([Link].activity_main);
}
@Override
publicboolean onCreateOptionsMenu(Menu menu){
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate([Link], menu);
returntrue;
}
XML file
<RelativeLayoutxmlns:android="[Link]
id"
xmlns:tools="[Link]
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
5
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:text="@string/hello_sanfoundry_android"/>
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="53dp"
android:text="This is a Large Text View"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="16dp"
android:text="This is Medium Text View"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginTop="14dp"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/textView4"
android:layout_marginTop="16dp"
android:text="Button"/>
<ToggleButton
android:id="@+id/toggleButton1"
6
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_alignParentRight="true"
android:text="ToggleButton"/>
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button1"
android:layout_centerHorizontal="true"
android:text="Small Button"/>
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/toggleButton1"
android:layout_below="@+id/toggleButton1"
android:layout_marginTop="93dp"
android:text="CheckBox"/>
<CheckedTextView
android:id="@+id/checkedTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox1"
android:layout_alignBottom="@+id/checkBox1"
android:layout_alignLeft="@+id/button1"
android:text="CheckedTextView"/>
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkedTextView1"
android:layout_below="@+id/checkBox1"
android:layout_marginTop="34dp"
android:text="RadioButton"/>
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioButton1"
android:layout_below="@+id/radioButton1"
7
android:text="RadioButton"/>
</RelativeLayout>
8
Experiment no. 02
Aim - A Simple Android Application For Adding Two Numbers
package [Link];
import [Link];
import [Link];
1.
2. import [Link];
3.
4. import [Link];
5.
6. import [Link];
7.
8. import [Link];
9.
10.
11. publicclasstestextendsActivity
12. {
13. EditText amount1;
14. EditText amount2;
15. TextView tt;
16. Button calculate;
17. double x=0;
18. double y=0;
19. double z=0;
20. /** Called when the activity is first created. */
21. @Override
22. publicvoidonCreate(Bundle savedInstanceState)
23. {
24. [Link](savedInstanceState);
25. setContentView([Link]);
26. initControls();
27. }
28. privatevoidinitControls()
29. {
30. amount1=(EditText)findViewById([Link].amount1);
31. amount2=(EditText)findViewById([Link].amount2);
9
32. tt=(TextView)findViewById([Link]);
33. calculate=(Button)findViewById([Link]);
34. [Link]([Link]()
35. {publicvoid onClick
36. (View v){calculate();}});
37. }
38. privatevoidcalculate()
39. {
40. x=[Link]([Link]().toString());
41. y=[Link]([Link]().toString());
42. z=x+y;
43. [Link]([Link](z));
44. }
45. }
10
Experiment no. 03
Aim - Andorid Program to Demonstrate Usage of [Link] File
[Link];
[Link];
[Link];
[Link];
@Override
protectedvoid onCreate(Bundle savedInstanceState){
[Link](savedInstanceState);
setContentView([Link].activity_main);
@Override
publicboolean onCreateOptionsMenu(Menu menu){
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate([Link], menu);
returntrue;
}
[Link]
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">string</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="my_text">Definition of text!</string>
<string-array name="operating_systems">
<item>Android</item>
<item>iPhone</item>
<item>Windows Mobile</item>
<item>Linux </item>
<item>Windows 7.0</item>
<item>Windows 8.0</item>
11
<item>Windows 8.1</item>
<item>Chrome OS</item>
<item>Unix</item>
<item>Symbian</item>
</string-array>
</resources>
<RelativeLayout
xmlns:android="[Link]
xmlns:tools="[Link]
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/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView2"
android:layout_marginTop="110dp"
android:text="@string/my_text"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</RelativeLayout>
12
Experiment no. 04
Aim – Write a Android program creating user interface.
[Link].activity_life;
[Link];
[Link];
[Link];
[Link];
@SuppressLint("NewApi")
publicclass MainActivity extends Activity {
@Override
protectedvoid onCreate(Bundle savedInstanceState){
[Link](savedInstanceState);
notify("onCreate");
}
@Override
protectedvoid onPause(){
[Link]();
notify("onPause");
}
@Override
protectedvoid onResume(){
[Link]();
notify("onResume");
}
@Override
protectedvoid onStop(){
[Link]();
notify("onStop");
}
@Override
protectedvoid onDestroy(){
[Link]();
notify("onDestroy");
}
@Override
protectedvoid onRestoreInstanceState(Bundle savedInstanceState){
[Link](savedInstanceState);
13
notify("onRestoreInstanceState");
}
@Override
protectedvoid onSaveInstanceState(Bundle outState){
[Link](outState);
notify("onSaveInstanceState");
}
14
Experiment no. 05
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
@Override
protectedvoid onCreate(Bundle savedInstanceState){
[Link](savedInstanceState);
setContentView([Link].activity_main);
Button but =(Button) findViewById([Link]);
[Link]([Link](){
@Override
publicvoid onClick(View v){
// TODO Auto-generated method stub
// Create an alert dialog box
[Link] builder =[Link](
[Link]);
// Set the value for the positive reaction from the user
// You can also set a listener to call when it is pressed
[Link]([Link], null);
// The message
[Link]([Link]);
}
});
15
}
@Override
publicboolean onCreateOptionsMenu(Menu menu){
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate([Link], menu);
returntrue;
}
}
[Link]
<RelativeLayoutxmlns:android="[Link]
id"
xmlns:tools="[Link]
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">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="SHOW DIALOG"/>
</RelativeLayout>
16
Experiment no. 06
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
@Override
protectedvoid onCreate(Bundle savedInstanceState){
[Link](savedInstanceState);
setContentView([Link].activity_main);
disp=(TextView) findViewById([Link].textView1);
res=(TextView) findViewById([Link].textView2);
Button add =(Button) findViewById([Link]);
Button mul =(Button) findViewById([Link]);
Button divide =(Button) findViewById([Link]);
Button sub =(Button) findViewById([Link]);
Button log =(Button) findViewById([Link]);
Button clear =(Button) findViewById([Link]);
Button calc =(Button) findViewById([Link]);
Button change =(Button) findViewById([Link]);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
17
@Override
publicboolean onCreateOptionsMenu(Menu menu){
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate([Link], menu);
returntrue;
}
@Override
publicvoid onClick(View v){
// TODO Auto-generated method stub
float result =0, num1, num2;
Toast var;
switch([Link]()){
case [Link]:
String str =(String) [Link]();
num1 =[Link](str);
[Link]("");
var= [Link]([Link], "Enter other number",
Toast.LENGTH_SHORT);
[Link]();
str=(String) [Link]();
num2 =[Link](str);
result= num1 + num2;
break;
case [Link]:
num1 =[Link]((String) [Link]());
[Link]("");
var= [Link]([Link], "Enter other number",
Toast.LENGTH_SHORT);
[Link]();
num2 =[Link]((String) [Link]());
result= num1 - num2;
break;
case [Link]:
num1 =[Link]((String) [Link]());
[Link]("");
var= [Link]([Link], "Enter other number",
Toast.LENGTH_SHORT);
[Link]();
num2 =[Link]((String) [Link]());
result= num1 * num2;
break;
case [Link]:
num1 =[Link]((String) [Link]());
[Link]("");
var= [Link]([Link], "Enter other number",
18
Toast.LENGTH_SHORT);
[Link]();
num2 =[Link]((String) [Link]());
result= num1 / num2;
break;
case [Link]:
num1 =[Link]((String) [Link]());
result=(float)[Link](num1);
break;
case [Link]:
[Link]("");
[Link]("");
break;
case [Link]:
int rand =(int)(([Link]()*10)%3);
Button b1 =(Button) findViewById([Link]);
Button b2 =(Button) findViewById([Link]);
Button b3 =(Button) findViewById([Link]);
Button b4 =(Button) findViewById([Link]);
Button b5 =(Button) findViewById([Link]);
Button b6 =(Button) findViewById([Link]);
Button b7 =(Button) findViewById([Link]);
Button b8 =(Button) findViewById([Link]);
rand=2;
switch(rand){
case0:
[Link](color.holo_blue_bright);
[Link](color.holo_blue_bright);
[Link](color.holo_blue_bright);
[Link](color.holo_blue_bright);
[Link](color.holo_blue_bright);
[Link](color.holo_blue_bright);
[Link](color.holo_blue_bright);
[Link](color.holo_blue_bright);
break;
case1:
[Link](color.holo_green_dark);
[Link](color.holo_green_dark);
[Link](color.holo_green_dark);
[Link](color.holo_green_dark);
[Link](color.holo_green_dark);
[Link](color.holo_green_dark);
[Link](color.holo_green_dark);
[Link](color.holo_green_dark);
break;
case2:
[Link](color.holo_red_dark);
[Link](color.holo_red_dark);
19
[Link](color.holo_red_dark);
[Link](color.holo_red_dark);
[Link](color.holo_red_dark);
[Link](color.holo_red_dark);
[Link](color.holo_red_dark);
[Link](color.holo_red_dark);
break;
default:
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
break;
}
break;
case [Link]:
[Link]([Link](result));
break;
}
}
}
Xml
<TableLayoutxmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
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">
<EditText
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0"
android:textSize="30sp"/>
20
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="30sp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_weight="0.29">
<Button
android:id="@+id/add"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="+"
android:textSize="20dp">
</Button>
<Button
android:id="@+id/sub"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_centerHorizontal="true"
android:text="-"/>
<Button
android:id="@+id/mul"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_alignParentRight="true"
android:text="X"/>
<Button
android:id="@+id/calculate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="64dp"
android:textAlignment="center"
android:text="Calculate"/>
21
<Button
android:id="@+id/color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="ChangeColor"
android:textAlignment="center"/>
<Button
android:id="@+id/divide"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/add"
android:layout_marginTop="47dp"
android:text="/"/>
<Button
android:id="@+id/log"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/divide"
android:layout_alignBottom="@+id/divide"
android:layout_alignLeft="@+id/sub"
android:text="log"/>
<Button
android:id="@+id/clear"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/log"
android:layout_alignBottom="@+id/log"
android:layout_alignParentRight="true"
android:text="C"/>
</RelativeLayout>
</TableLayout>
22
Experiment no. 07
Aim- write a android program for Content Providers.
[Link].create_menu1;
[Link];
[Link];
[Link];
@Override
protectedvoid onCreate(Bundle savedInstanceState){
[Link](savedInstanceState);
setContentView([Link].activity_main);
}
@Override
publicboolean onCreateOptionsMenu(Menu menu){
// Inflate the menu; this adds items to the action bar if it is present.
[Link](menu);
getMenuInflater().inflate([Link], menu);
returntrue;
}
}
Xml
Main
<RelativeLayoutxmlns:android="[Link]
id"
xmlns:tools="[Link]
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:layout_width="match_parent"
android:layout_height="40dp"
android:text="A Menu Activity"
android:textSize="30dp"
android:layout_marginTop="120dp"
android:gravity="center_vertical|center_horizontal"/>
</RelativeLayout>
Menu
23
<menuxmlns:android="[Link]
<item
android:id="@+id/Menu_label1"
android:alphabeticShortcut="1"
android:title="Menu_label"/>
<item
android:id="@+id/Menu_label2"
android:alphabeticShortcut="2"
android:title="Menu_label2"/>
</menu>
24
Experiment no. 08
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
@Override
protectedvoid onCreate(Bundle savedInstanceState){
// TODO Auto-generated method stub
[Link](savedInstanceState);
setContentView([Link].activity_main);
img=(ImageView) findViewById([Link].imageView1);
// oncheckedChanged function
gen=(Button) findViewById([Link].button1);
[Link]([Link](){
@Override
publicvoid onClick(View v){
// TODO Auto-generated method stub
}
25
});
@Override
publicvoid onCheckedChanged(RadioGroup group, int checkedId){
// TODO Auto-generated method stub
switch(checkedId){
case [Link].radioButton1:
[Link]([Link].image4);
break;
case [Link].radioButton2:
[Link]([Link].image5);
break;
case [Link].radioButton3:
[Link]([Link].image6);
break;
case [Link].radioButton4:
[Link]([Link].image7);
break;
default:
break;
}
}
}
[Link]
<RelativeLayoutxmlns:android="[Link]
id"
xmlns:tools="[Link]
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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="40px"
android:weightSum="2">
26
<RadioGroup
android:id="@+id/rg1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:text="Image2"/>
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/radioButton1"
android:layout_alignBottom="@+id/radioButton1"
android:layout_alignParentLeft="true"
android:text="Image1"/>
</RadioGroup>
<RadioGroup
android:id="@+id/rg2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Image3"/>
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/radioButton3"
android:layout_alignBottom="@+id/radioButton3"
android:layout_alignParentRight="true"
android:text="Image4"/>
</RadioGroup>
27
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="Change Image"/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button1"
android:layout_marginTop="50dp"
android:src="@drawable/ic_launcher"/>
</RelativeLayout>
[Link] application which will display following Notifications
[Link].hello_android_sanfoundry;
[Link];
[Link];
[Link];
@Override
protectedvoid onCreate(Bundle savedInstanceState){
[Link](savedInstanceState);
setContentView([Link].activity_main);
}
@Override
publicboolean onCreateOptionsMenu(Menu menu){
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate([Link], menu);
returntrue;
}
28
This is the Xml code.
In order to set image as the background of your activity place the
image in the /res/drawable-hdpi and click on Project and then
select clean to clean the current project this will load the image in
your current working project , in this Application i have set the
background using [Link].
<RelativeLayoutxmlns:android="[Link]
id"
xmlns:tools="[Link]
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"
android:background="@drawable/background"
>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:text="@string/hello_sanfoundry_android"/>
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="53dp"
android:text="This is a Large Text View"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="16dp"
android:text="This is Medium Text View"
android:textAppearance="?android:attr/textAppearanceMedium"/>
29
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginTop="14dp"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/textView4"
android:layout_marginTop="16dp"
android:text="Button"/>
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_alignParentRight="true"
android:text="ToggleButton"/>
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button1"
android:layout_centerHorizontal="true"
android:text="Small Button"/>
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/toggleButton1"
android:layout_below="@+id/toggleButton1"
android:layout_marginTop="93dp"
android:text="CheckBox"/>
<CheckedTextView
android:id="@+id/checkedTextView1"
android:layout_width="wrap_content"
30
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox1"
android:layout_alignBottom="@+id/checkBox1"
android:layout_alignLeft="@+id/button1"
android:text="CheckedTextView"/>
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkedTextView1"
android:layout_below="@+id/checkBox1"
android:layout_marginTop="34dp"
android:text="RadioButton"/>
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioButton1"
android:layout_below="@+id/radioButton1"
android:text="RadioButton"/>
</RelativeLayout>
31
Experiment no. 09
[Link].creating_sound_button;
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
MediaPlayer button_sound;
@Override
protectedvoid onCreate(Bundle savedInstanceState){
[Link](savedInstanceState);
setContentView([Link].activity_main);
Button b1 =(Button) findViewById([Link].button1);
Button b2 =(Button) findViewById([Link].button2);
Button b3 =(Button) findViewById([Link].button3);
Button b4 =(Button) findViewById([Link].button4);
[Link]([Link](){
@Override
publicvoid onClick(View v){
// TODO Auto-generated method stub
button_sound =[Link]([Link],
[Link].but_click);
button_sound.start();
}
});
[Link]([Link](){
@Override
publicvoid onClick(View v){
// TODO Auto-generated method stub
button_sound =[Link]([Link],
[Link].sound1);
button_sound.start();
}
});
[Link]([Link](){
32
@Override
publicvoid onClick(View v){
// TODO Auto-generated method stub
button_sound =[Link]([Link],
[Link].sound2);
button_sound.start();
}
});
[Link]([Link](){
@Override
publicvoid onClick(View v){
// TODO Auto-generated method stub
button_sound =[Link]([Link],
[Link].sound3);
button_sound.start();
}
});
@Override
publicboolean onCreateOptionsMenu(Menu menu){
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate([Link], menu);
returntrue;
}
@Override
protectedvoid onPause(){
// TODO Auto-generated method stub
[Link]();
}
Xml
<RelativeLayoutxmlns:android="[Link]
id"
xmlns:tools="[Link]
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"
33
tools:context=".MainActivity"
android:background="@drawable/background">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="58dp"
android:text="Sound1"/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="59dp"
android:text="Sound2"/>
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_below="@+id/button3"
android:layout_marginTop="36dp"
android:text="Sound4"/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:layout_marginTop="40dp"
android:text="Sound3"/>
</RelativeLayout>
34
Experiment no. 10
Aim – Write a Android program for Preference & Data Storage.
Main [Link]
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
publicclassMainActivity extendsActivity {
SharedPreferences sharedpreferences;
TextView name;
TextView email;
publicstaticfinalString mypreference = "mypref";
publicstaticfinalString Name = "nameKey";
publicstaticfinalString Email = "emailKey";
@Override
protectedvoidonCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
name = (TextView) findViewById([Link]);
email = (TextView) findViewById([Link]);
sharedpreferences = getSharedPreferences(mypreference,
Context.MODE_PRIVATE);
if([Link](Name)) {
[Link]([Link](Name, ""));
}
if([Link](Email)) {
[Link]([Link](Email,
""));
publicvoidSave(View view) {
String n = [Link]().toString();
String e = [Link]().toString();
[Link] editor =
[Link]();
[Link](Name, n);
[Link](Email, e);
[Link]();
}
publicvoidclear(View view) {
name = (TextView) findViewById([Link]);
email = (TextView) findViewById([Link]);
[Link]("");
[Link]("");
publicvoidGet(View view) {
name = (TextView) findViewById([Link]);
35
email = (TextView) findViewById([Link]);
sharedpreferences = getSharedPreferences(mypreference,
Context.MODE_PRIVATE);
if([Link](Name)) {
[Link]([Link](Name, ""));
}
if([Link](Email)) {
[Link]([Link](Email,
""));
}
}
@Override
publicbooleanonCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
if it is present.
getMenuInflater().inflate([Link].menu_main, menu);
returntrue;
}
Activity_main.xml
<RelativeLayoutxmlns:android="[Link]
/android"
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">
<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="Save"
android:text="Save"/>
<Button
android:id="@+id/btnRetr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="Get"
android:text="Retrieve"/>
<Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/etEmail"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="clear"
36
android:text="Clear"/>
<EditText
android:id="@+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress"
android:layout_below="@+id/etName"
android:layout_marginTop="20dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Name"
android:inputType="text"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/etEmail"
android:layout_alignStart="@+id/etEmail"/>
</RelativeLayout>
b. SQLite database
b.i. Create app to create database using Open helper
[Link]. Create app to read, write and delete database entries
[Link] {
// Database Info
privatestaticfinalStringDATABASE_NAME="postsDatabase";
privatestaticfinalintDATABASE_VERSION=1;
// Table Names
privatestaticfinalStringTABLE_POSTS="posts";
privatestaticfinalStringTABLE_USERS="users";
publicPostsDatabaseHelper(Contextcontext) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
[Link](CREATE_POSTS_TABLE);
[Link](CREATE_USERS_TABLE);
}
Inserting records
addContact()
// Adding new contact
publicvoidaddContact(Contact contact) {
SQLiteDatabase db = [Link]();
38
[Link](KEY_PH_NO, [Link]()); // Contact
Phone Number
// Inserting Row
[Link](TABLE_CONTACTS, null, values);
[Link](); // Closing database connection
}
getContact()
Contact contact =
newContact([Link]([Link](0)),
[Link](1), [Link](2));
// return contact
returncontact;
}
39