KEMBAR78
Android File | PDF | Android (Operating System) | Databases
0% found this document useful (0 votes)
25 views39 pages

Android File

Uploaded by

avinashyadav2497
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views39 pages

Android File

Uploaded by

avinashyadav2497
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Experiment no.

01

Aim - Download and setup Android Environment Main

Install Android studio setup

Select location :-

1
Click on install button :-

Finish with the setup :-

Open the IDE for making programs :-


2
3
4
Simple program for the same :-

Main activity
packagecom.example.hello_android_sanfoundry;

importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.view.Menu;

publicclass MainActivity extends Activity {

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

@Override
publicboolean onCreateOptionsMenu(Menu menu){
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
returntrue;
}

XML file
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/andro
id"
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"
>

<!-- This is a comment -->

<!-- Create your comments like this -->

<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 com.example.addition;

import android.app.Activity;

import android.os.Bundle;
1.

2. import android.widget.EditText;
3.

4. import android.widget.TextView;
5.

6. import android.widget.Button;
7.

8. import android.view.View;
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. super.onCreate(savedInstanceState);
25. setContentView(R.layout.main);
26. initControls();
27. }
28. privatevoidinitControls()
29. {
30. amount1=(EditText)findViewById(R.id.amount1);
31. amount2=(EditText)findViewById(R.id.amount2);

9
32. tt=(TextView)findViewById(R.id.tt);
33. calculate=(Button)findViewById(R.id.calculate);
34. calculate.setOnClickListener(newButton.OnClickListener()
35. {publicvoid onClick
36. (View v){calculate();}});
37. }
38. privatevoidcalculate()
39. {
40. x=Double.parseDouble(amount1.getText().toString());
41. y=Double.parseDouble(amount2.getText().toString());
42. z=x+y;
43. tt.setText(Double.toString(z));
44. }
45. }

10
Experiment no. 03
Aim - Andorid Program to Demonstrate Usage of String.xml File

packagecom.example.string;

importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.view.Menu;

publicclass MainActivity extends Activity {

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

@Override
publicboolean onCreateOptionsMenu(Menu menu){
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.main, menu);
returntrue;
}

strings.xml
<?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="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/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.

packagecom.example.activity_life;

importandroid.annotation.SuppressLint;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.widget.Toast;

@SuppressLint("NewApi")
publicclass MainActivity extends Activity {

@Override
protectedvoid onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
notify("onCreate");
}

@Override
protectedvoid onPause(){
super.onPause();
notify("onPause");
}

@Override
protectedvoid onResume(){
super.onResume();
notify("onResume");
}

@Override
protectedvoid onStop(){
super.onStop();
notify("onStop");
}

@Override
protectedvoid onDestroy(){
super.onDestroy();
notify("onDestroy");
}

@Override
protectedvoid onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);

13
notify("onRestoreInstanceState");
}

@Override
protectedvoid onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
notify("onSaveInstanceState");
}

privatevoid notify(String methodName){


String name =this.getClass().getName();
String[] strings = name.split("\\.");
Toast.makeText(getApplicationContext(),
methodName+""+ strings[strings.length-1],
Toast.LENGTH_LONG).show();
}

14
Experiment no. 05

Aim – Write a android program for Assets and Images.

packagecom.example.alertdialog;

importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.app.AlertDialog;
importandroid.view.Menu;
importandroid.view.View;
importandroid.widget.Button;

publicclass MainActivity extends Activity {

@Override
protectedvoid onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button but =(Button) findViewById(R.id.button);
but.setOnClickListener(newView.OnClickListener(){

@Override
publicvoid onClick(View v){
// TODO Auto-generated method stub
// Create an alert dialog box
AlertDialog.Builder builder =newAlertDialog.Builder(
MainActivity.this);

// Set alert title


builder.setTitle(R.string.alerttitle);

// Set the value for the positive reaction from the user
// You can also set a listener to call when it is pressed
builder.setPositiveButton(R.string.ok, null);

// The message
builder.setMessage(R.string.message);

// Create the alert dialog and display it


AlertDialog theAlertDialog =builder.create();
theAlertDialog.show();

}
});

15
}

@Override
publicboolean onCreateOptionsMenu(Menu menu){
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.main, menu);
returntrue;
}

}
ActivityMain.xml

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/andro
id"
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">

<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

Aim – write a android program for Application Fundamentals


using calculator.

packagecom.example.calculator;

importandroid.R.color;
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.view.Menu;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.TextView;
importandroid.widget.Toast;

publicclass MainActivity extends Activity implements OnClickListener {

TextView disp, res;

@Override
protectedvoid onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
disp=(TextView) findViewById(R.id.textView1);
res=(TextView) findViewById(R.id.textView2);
Button add =(Button) findViewById(R.id.add);
Button mul =(Button) findViewById(R.id.mul);
Button divide =(Button) findViewById(R.id.divide);
Button sub =(Button) findViewById(R.id.sub);
Button log =(Button) findViewById(R.id.log);
Button clear =(Button) findViewById(R.id.clear);
Button calc =(Button) findViewById(R.id.calculate);
Button change =(Button) findViewById(R.id.color);

add.setOnClickListener(this);
mul.setOnClickListener(this);
divide.setOnClickListener(this);
sub.setOnClickListener(this);
log.setOnClickListener(this);
clear.setOnClickListener(this);
calc.setOnClickListener(this);
change.setOnClickListener(this);

17
@Override
publicboolean onCreateOptionsMenu(Menu menu){
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.main, menu);
returntrue;
}

@Override
publicvoid onClick(View v){
// TODO Auto-generated method stub
float result =0, num1, num2;
Toast var;
switch(v.getId()){
case R.id.add:
String str =(String) disp.getText();
num1 =Integer.parseInt(str);
disp.setText("");
var= Toast.makeText(MainActivity.this, "Enter other number",
Toast.LENGTH_SHORT);
var.show();
str=(String) disp.getText();
num2 =Integer.parseInt(str);
result= num1 + num2;
break;

case R.id.sub:
num1 =Integer.parseInt((String) disp.getText());
disp.setText("");
var= Toast.makeText(MainActivity.this, "Enter other number",
Toast.LENGTH_SHORT);
var.show();
num2 =Integer.parseInt((String) disp.getText());
result= num1 - num2;
break;
case R.id.mul:
num1 =Integer.parseInt((String) disp.getText());
disp.setText("");
var= Toast.makeText(MainActivity.this, "Enter other number",
Toast.LENGTH_SHORT);
var.show();
num2 =Integer.parseInt((String) disp.getText());
result= num1 * num2;
break;
case R.id.divide:
num1 =Integer.parseInt((String) disp.getText());
disp.setText("");
var= Toast.makeText(MainActivity.this, "Enter other number",
18
Toast.LENGTH_SHORT);
var.show();
num2 =Integer.parseInt((String) disp.getText());
result= num1 / num2;
break;
case R.id.log:
num1 =Integer.parseInt((String) disp.getText());
result=(float)Math.log(num1);
break;
case R.id.clear:
disp.setText("");
res.setText("");
break;
case R.id.color:
int rand =(int)((Math.random()*10)%3);
Button b1 =(Button) findViewById(R.id.add);
Button b2 =(Button) findViewById(R.id.mul);
Button b3 =(Button) findViewById(R.id.divide);
Button b4 =(Button) findViewById(R.id.sub);
Button b5 =(Button) findViewById(R.id.log);
Button b6 =(Button) findViewById(R.id.clear);
Button b7 =(Button) findViewById(R.id.calculate);
Button b8 =(Button) findViewById(R.id.color);

rand=2;
switch(rand){
case0:
b1.setTextColor(color.holo_blue_bright);
b2.setTextColor(color.holo_blue_bright);
b3.setTextColor(color.holo_blue_bright);
b4.setTextColor(color.holo_blue_bright);
b5.setTextColor(color.holo_blue_bright);
b6.setTextColor(color.holo_blue_bright);
b7.setTextColor(color.holo_blue_bright);
b8.setTextColor(color.holo_blue_bright);
break;
case1:
b1.setTextColor(color.holo_green_dark);
b2.setTextColor(color.holo_green_dark);
b3.setTextColor(color.holo_green_dark);
b4.setTextColor(color.holo_green_dark);
b5.setTextColor(color.holo_green_dark);
b6.setTextColor(color.holo_green_dark);
b7.setTextColor(color.holo_green_dark);
b8.setTextColor(color.holo_green_dark);
break;
case2:
b1.setTextColor(color.holo_red_dark);
b2.setTextColor(color.holo_red_dark);
19
b3.setTextColor(color.holo_red_dark);
b4.setTextColor(color.holo_red_dark);
b5.setTextColor(color.holo_red_dark);
b6.setTextColor(color.holo_red_dark);
b7.setTextColor(color.holo_red_dark);
b8.setTextColor(color.holo_red_dark);
break;

default:
b1.setTextColor(color.black);
b2.setTextColor(color.black);
b3.setTextColor(color.black);
b4.setTextColor(color.black);
b5.setTextColor(color.black);
b6.setTextColor(color.black);
b7.setTextColor(color.black);
b8.setTextColor(color.black);
break;
}

break;
case R.id.calculate:
res.setText(Float.toString(result));
break;
}
}

}
Xml

<TableLayoutxmlns: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: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.
packagecom.example.create_menu1;

importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.view.Menu;

publicclass MainActivity extends Activity {

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

@Override
publicboolean onCreateOptionsMenu(Menu menu){
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main, menu);
returntrue;
}

}
Xml
Main

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/andro
id"
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: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="http://schemas.android.com/apk/res/android">

<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

Aim – Write a android program for Broadcast Receivers.

packagecom.example.changeiamge;

importandroid.app.Activity;
importandroid.graphics.Typeface;
importandroid.os.Bundle;
importandroid.renderscript.Type;
importandroid.view.Gravity;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.EditText;
importandroid.widget.ImageView;
importandroid.widget.RadioGroup;
importandroid.widget.RadioGroup.OnCheckedChangeListener;
importandroid.widget.TextView;

publicclass MainActivity extends Activity implements


OnCheckedChangeListener {

RadioGroup group1, group2;


Button gen;
ImageView img;

@Override
protectedvoid onCreate(Bundle savedInstanceState){
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

group1 =(RadioGroup) findViewById(R.id.rg1);


group1.setOnCheckedChangeListener((OnCheckedChangeListener)this);

group2 =(RadioGroup) findViewById(R.id.rg2);


group2.setOnCheckedChangeListener((OnCheckedChangeListener)this);

img=(ImageView) findViewById(R.id.imageView1);
// oncheckedChanged function
gen=(Button) findViewById(R.id.button1);
gen.setOnClickListener(newView.OnClickListener(){

@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 R.id.radioButton1:
img.setImageResource(R.drawable.image4);
break;

case R.id.radioButton2:
img.setImageResource(R.drawable.image5);
break;

case R.id.radioButton3:
img.setImageResource(R.drawable.image6);
break;

case R.id.radioButton4:
img.setImageResource(R.drawable.image7);
break;

default:
break;
}
}
}
ActivityMain.xml
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/andro
id"
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">

<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>
9.Create application which will display following Notifications

packagecom.example.hello_android_sanfoundry;

importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.view.Menu;

publicclass MainActivity extends Activity {

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

@Override
publicboolean onCreateOptionsMenu(Menu menu){
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.main, 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 background.png.
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/andro
id"
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"
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

Aim – Write a Android program for adding sound button.

packagecom.example.creating_sound_button;

importandroid.media.MediaPlayer;
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.view.Menu;
importandroid.view.View;
importandroid.widget.Button;

publicclass MainActivity extends Activity {

MediaPlayer button_sound;

@Override
protectedvoid onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 =(Button) findViewById(R.id.button1);
Button b2 =(Button) findViewById(R.id.button2);
Button b3 =(Button) findViewById(R.id.button3);
Button b4 =(Button) findViewById(R.id.button4);

b1.setOnClickListener(newView.OnClickListener(){

@Override
publicvoid onClick(View v){
// TODO Auto-generated method stub
button_sound =MediaPlayer.create(MainActivity.this,
R.raw.but_click);
button_sound.start();
}
});
b2.setOnClickListener(newView.OnClickListener(){

@Override
publicvoid onClick(View v){
// TODO Auto-generated method stub
button_sound =MediaPlayer.create(MainActivity.this,
R.raw.sound1);
button_sound.start();
}
});
b3.setOnClickListener(newView.OnClickListener(){

32
@Override
publicvoid onClick(View v){
// TODO Auto-generated method stub
button_sound =MediaPlayer.create(MainActivity.this,
R.raw.sound2);
button_sound.start();
}
});
b4.setOnClickListener(newView.OnClickListener(){

@Override
publicvoid onClick(View v){
// TODO Auto-generated method stub
button_sound =MediaPlayer.create(MainActivity.this,
R.raw.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(R.menu.main, menu);
returntrue;
}

@Override
protectedvoid onPause(){
// TODO Auto-generated method stub
super.onPause();

}
Xml

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/andro
id"
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"
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 Activity.java

packagecom.journaldev.sharedpreferences;

importandroid.app.Activity;
importandroid.content.Context;
importandroid.content.SharedPreferences;
importandroid.os.Bundle;
importandroid.view.Menu;
importandroid.view.View;
importandroid.widget.TextView;

publicclassMainActivity extendsActivity {
SharedPreferences sharedpreferences;
TextView name;
TextView email;
publicstaticfinalString mypreference = "mypref";
publicstaticfinalString Name = "nameKey";
publicstaticfinalString Email = "emailKey";

@Override
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (TextView) findViewById(R.id.etName);
email = (TextView) findViewById(R.id.etEmail);
sharedpreferences = getSharedPreferences(mypreference,
Context.MODE_PRIVATE);
if(sharedpreferences.contains(Name)) {
name.setText(sharedpreferences.getString(Name, ""));
}
if(sharedpreferences.contains(Email)) {
email.setText(sharedpreferences.getString(Email,
""));

publicvoidSave(View view) {
String n = name.getText().toString();
String e = email.getText().toString();
SharedPreferences.Editor editor =
sharedpreferences.edit();
editor.putString(Name, n);
editor.putString(Email, e);
editor.commit();
}

publicvoidclear(View view) {
name = (TextView) findViewById(R.id.etName);
email = (TextView) findViewById(R.id.etEmail);
name.setText("");
email.setText("");

publicvoidGet(View view) {
name = (TextView) findViewById(R.id.etName);
35
email = (TextView) findViewById(R.id.etEmail);
sharedpreferences = getSharedPreferences(mypreference,
Context.MODE_PRIVATE);

if(sharedpreferences.contains(Name)) {
name.setText(sharedpreferences.getString(Name, ""));
}
if(sharedpreferences.contains(Email)) {
email.setText(sharedpreferences.getString(Email,
""));

}
}

@Override
publicbooleanonCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
returntrue;
}

Activity_main.xml

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res
/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
b.ii. Create app to read, write and delete database entries

b.ipublicclassPostsDatabaseHelperextendsSQLiteOpenHelper {
// Database Info
privatestaticfinalStringDATABASE_NAME="postsDatabase";
privatestaticfinalintDATABASE_VERSION=1;

// Table Names
privatestaticfinalStringTABLE_POSTS="posts";
privatestaticfinalStringTABLE_USERS="users";

// Post Table Columns


privatestaticfinalStringKEY_POST_ID="id";
privatestaticfinalStringKEY_POST_USER_ID_FK="userId";
privatestaticfinalStringKEY_POST_TEXT="text";

// User Table Columns


privatestaticfinalStringKEY_USER_ID="id";
privatestaticfinalStringKEY_USER_NAME="userName";
privatestaticfinalStringKEY_USER_PROFILE_PICTURE_URL="profilePictureUrl";

publicPostsDatabaseHelper(Contextcontext) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Called when the database connection is being configured.


// Configure database settings for things like foreign key support, write-
ahead logging, etc.
@Override
publicvoidonConfigure(SQLiteDatabasedb) {
super.onConfigure(db);
db.setForeignKeyConstraintsEnabled(true);
}

// Called when the database is created for the FIRST time.


// If a database already exists on disk with the same DATABASE_NAME, this
method will NOT be called.
@Override
37
publicvoidonCreate(SQLiteDatabasedb) {
StringCREATE_POSTS_TABLE="CREATE TABLE "+TABLE_POSTS+
"("+
KEY_POST_ID+" INTEGER PRIMARY KEY,"+// Define a primary key
KEY_POST_USER_ID_FK+" INTEGER REFERENCES "+TABLE_USERS+","+// Define a foreign
key
KEY_POST_TEXT+" TEXT"+
")";

StringCREATE_USERS_TABLE="CREATE TABLE "+TABLE_USERS+


"("+
KEY_USER_ID+" INTEGER PRIMARY KEY,"+
KEY_USER_NAME+" TEXT,"+
KEY_USER_PROFILE_PICTURE_URL+" TEXT"+
")";

db.execSQL(CREATE_POSTS_TABLE);
db.execSQL(CREATE_USERS_TABLE);
}

// Called when the database needs to be upgraded.


// This method will only be called if a database already exists on disk with
the same DATABASE_NAME,
// but the DATABASE_VERSION is different than the version of the database that
exists on disk.
@Override
publicvoidonUpgrade(SQLiteDatabasedb, intoldVersion, intnewVersion) {
if (oldVersion != newVersion) {
// Simplest implementation is to drop all old tables and recreate them
db.execSQL("DROP TABLE IF EXISTS "+TABLE_POSTS);
db.execSQL("DROP TABLE IF EXISTS "+TABLE_USERS);
onCreate(db);
}
}
}

b.ii. Create app to read, write and delete database entries


// Adding new contact
publicvoidaddContact(Contact contact) {}

// Getting single contact


publicContact getContact(intid) {}

// Getting All Contacts


publicList<Contact> getAllContacts() {}

// Getting contacts Count


publicintgetContactsCount() {}
// Updating single contact
publicintupdateContact(Contact contact) {}

// Deleting single contact


publicvoiddeleteContact(Contact contact) {}

Inserting records

addContact()
// Adding new contact
publicvoidaddContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = newContentValues();


values.put(KEY_NAME, contact.getName()); // Contact Name

38
values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact
Phone Number

// Inserting Row
db.insert(TABLE_CONTACTS, null, values);
db.close(); // Closing database connection
}

getContact()

// Getting single contact


publicContact getContact(intid) {
SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor = db.query(TABLE_CONTACTS, newString[] {


KEY_ID,
KEY_NAME, KEY_PH_NO }, KEY_ID + "=?",
newString[] { String.valueOf(id) }, null, null,
null, null);
if(cursor != null)
cursor.moveToFirst();

Contact contact =
newContact(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
// return contact
returncontact;
}

39

You might also like