Experiment No.
1
Aim: Develop an application that uses GUI components, fonts and colours.
XML Code:
<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout xmlns:android=[Link]
xmlns:app=[Link]
xmlns:tools=[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#f3ea45"
tools:context=".MainActivity">
<TextView
android:id=”@+id/textView1”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_margin=”20sp”
android:gravity=”center”
android:text=”Hello World”
android:textSize="20sp"
android:fontFamily="sans-serif-medium"
android:textStyle="bold" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Change font size"
android:textSize="20sp" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Change colour"
android:textSize="20sp" />
</LinearLayout>
Java Code:
package [Link].exno1;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
int ch=1; float font=30;
@Override protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
final TextView t= (TextView) findViewById([Link]);
Button b1= (Button) findViewById([Link].button1);
[Link](new [Link]() {
@Override public void onClick(View v) {
[Link](font);
font = font + 5;
if (font == 50) font = 30;
}
});
Button b2= (Button) findViewById([Link].button2);
[Link](new [Link]() {
@Override public void onClick(View v) {
switch (ch) {
case 1: [Link]([Link]);
break;
case 2: [Link]([Link]);
break;
case 3: [Link]([Link]);
break;
case 4: [Link]([Link]);
break;
case 5: [Link]([Link]);
break;
case 6: [Link]([Link]);
break;
}
ch++;
if (ch == 7) ch = 1;
}
});
}
}
Output:
Experiment No. 2
Aim: Develop an application that uses Layout Managers and event listeners.
XML Code:
<?xml version="1.0" encoding="utf-8">
<RelativeLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=”.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:text="Details Form"
android:textSize="25sp"
android:gravity="center">
</LinearLayout>
<GridLayout
android:id="@+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="100dp"
android:layout_marginBottom="200dp"
android:columnCount="2"
android:rowCount "3">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="0"
android:layout_column="0"
android:text "Name"
android:textSize="20sp"
android:gravity="center">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="0"
android:layout_column="1"
android:ems="10"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="1"
android:layout_column="0"
android:text="[Link]"
android:textSize="20sp"
android:gravity="center"/>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="1"
android:layout_column="1"
android:inputType="number"
android:ems="10"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="2"
android:layout_column="0"
android:text="Dept"
android:textSize="20sp"
android:gravity="center"/>
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="2"
android:layout_column="1"
android:spinnerMode="dropdown"/>
</GridLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_marginBottom="150dp"
android:text="Submit"/>
</RelativeLayout>
Code for Activity_second.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="[Link]"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="New Text"
android:textSize="30sp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="New Text"
android:textSize="30sp"/
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="New Text"
android:textSize="30sp"/>
</LinearLayout>
Code for [Link]:
package [Link].exno2;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
EditText e1,e2;
Button bt;
Spinner s;
String [] dept_array={"CSE","ECE","IT","Mech","Civil"};
String name,reg,dept;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
e1= (EditText) findViewById([Link]);
e2= (EditText) findViewById([Link].editText2);
bt= (Button) findViewById([Link]);
s= (Spinner) findViewById([Link]);
ArrayAdapter adapter= new
ArrayAdapter([Link],[Link].simple_spinner_item,dept_arra
[Link](adapter);
[Link](new [Link]() {
@Override
public void onClick(View v) {
//Getting the Values from Views(Edittext & Spinner)
name=[Link]().toString();
reg=[Link]().toString();
dept=[Link]().toString();
Intent i = new Intent([Link],[Link]);
[Link]("name_key", name);
[Link]("reg_key",reg);
[Link]("dept_key", dept);
startActivity(i);
}
});
}
}
Code for [Link]:
package [Link].exno2;
import [Link];
import [Link];
import [Link]; import
[Link];
public class SecondActivity extends AppCompatActivity
{ TextView t1,t2,t3;
String name,reg,dept;
@Override
protected void onCreate(Bundle savedInstanceState)
{ [Link](savedInstanceState);
setContentView([Link].activity_second);
t1= (TextView) findViewById([Link].textView1);
t2= (TextView) findViewById([Link].textView2);
t3= (TextView) findViewById([Link].textView3);
Intent i = getIntent();
name=[Link]("name_key");
reg=[Link]("reg_key");
dept=[Link]("dept_key");
//Setting the Values to Intent
[Link](name);
[Link](reg);
[Link](dept);
}
}
Output:
Experiment No. 3
Aim: Develop a native calculator application
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="[Link]">
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="153dp"
tools:layout_editor_absoluteY="9dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
tools:layout_editor_absoluteY="58dp"
tools:layout_editor_absoluteX="16dp"
android:layout_above="@+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="31dp" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
tools:layout_editor_absoluteY="125dp"
tools:layout_editor_absoluteX="16dp"
android:layout_above="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="43dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD"
android:onClick="add"
tools:layout_editor_absoluteX="34dp"
tools:layout_editor_absoluteY="192dp"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/sub"
android:layout_toStartOf="@+id/sub"
android:layout_marginRight="52dp"
android:layout_marginEnd="52dp" />
<Button
android:id="@+id/sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_toEndOf="@+id/editText2"
android:layout_toRightOf="@+id/editText2"
android:text="Sub"
android:onClick="sub"/>
<Button
android:id="@+id/mul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/div"
android:layout_alignBottom="@+id/div"
android:layout_alignLeft="@+id/button"
android:layout_alignStart="@+id/button"
android:text="Mul"
android:onClick="mul" />
<Button
android:id="@+id/div"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/sub"
android:layout_alignRight="@+id/sub"
android:layout_below="@+id/sub"
android:layout_marginTop="23dp"
android:text="Div"
android:onClick="div" />
</RelativeLayout>
Java Code:
package [Link];
import [Link];
import [Link]; import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState)
{ [Link](savedInstanceState);
setContentView([Link].activity_main);
}
public void add(View v)
{
int num1, num2,sum;
EditText t1 = (EditText)findViewById([Link]);
EditText t2 = (EditText)findViewById([Link].editText2);
TextView t3 = (TextView)findViewById([Link]);
num1 = [Link]([Link]().toString()); num2 =
[Link]([Link]().toString()); sum = num1 +
num2;
[Link]([Link](sum));
}
public void sub(View v)
{
int num1, num2,sum;
EditText t1 = (EditText)findViewById([Link]);
EditText t2 = (EditText)findViewById([Link].editText2);
TextView t3 = (TextView)findViewById([Link]);
num1 = [Link]([Link]().toString()); num2 =
[Link]([Link]().toString()); sum = num1 -
num2;
[Link]([Link](sum));
}
public void mul(View v)
{
int num1, num2,sum;
EditText t1 = (EditText)findViewById([Link]);
EditText t2 = (EditText)findViewById([Link].editText2);
TextView t3 = (TextView)findViewById([Link]);
num1 = [Link]([Link]().toString()); num2 =
[Link]([Link]().toString()); sum = num1 *
num2;
[Link]([Link](sum));
}
public void div(View v)
{
int num1, num2,sum;
EditText t1 = (EditText)findViewById([Link]);
EditText t2 = (EditText)findViewById([Link].editText2);
TextView t3 = (TextView)findViewById([Link]);
num1 = [Link]([Link]().toString()); num2 =
[Link]([Link]().toString()); sum = num1 /
num2;
[Link]([Link](sum));
}
}
Output:
Experiment No. 4
Aim: Write an application that draws basic graphical primitives on the screen.
XML Code:
?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView"/>
</RelativeLayout>
Java Code:
import [Link]: import [Link];
import [Link];
import [Link];
import [Link];
import android [Link];
import [Link];
import [Link];
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
[Link](savedInstanceState);
setContentView([Link].activity_main);
//Creating a Bitmap
Bitmap bg= [Link](720, 1280, [Link].ARGB_8888);
//Setting the Bitmap as background for the Image View
ImageView i = (ImageView) findViewById([Link]); [Link](new
BitmapDrawable(bg));
//Creating the Canvas Object
Canvas canvas = new Canvas(bg);
//Creating the Paint Object and set its color & TextSize Paint paint = new Paint();
[Link]([Link]);
[Link](50);
//To draw a Rectangle
[Link]("Rectangle", 420, 150, paint);
[Link](400, 200, 650, 700, paint);
//To draw a Circle
[Link]("Circle", 120, 150, paint);
[Link](200, 350, 150, paint);
//To draw a Square
[Link]("Square", 120, 800, paint);
[Link](50, 850, 350, 1150, paint);
//To draw a Line
[Link]("Line", 480, 800, paint); [Link](520, 850, 520, 1150, paint);
}
}
Output:
Experiment No. 5
Aim: Develop an application that makes use of databases.
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="50dp"
android:layout_y="20dp"
android:text="Student Details"
android:textSize="30sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp"
android:layout_y="110dp"
android:text="Enter Rollno:"
android:textSize="20sp" />
<EditText
android:id="@+id/Rollno"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="175dp"
android:layout_y="100dp"
android:inputType="number"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp"
android:layout_y="160dp"
android:text="Enter Name:"
android:textSize="20sp" />
<EditText
android:id="@+id/Name"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="175dp"
android:layout_y="150dp"
android:inputType="text"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp"
android:layout_y="210dp"
android:text="Enter Marks:"
android:textSize="20sp" />
<EditText
android:id="@+id/Marks"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="175dp"
android:layout_y="200dp"
android:inputType="number"
android:textSize="20sp" />
<Button
android:id="@+id/Insert"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="25dp"
android:layout_y="300dp"
android:text="Insert"
android:textSize="30dp" />
<Button
android:id="@+id/Delete"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="200dp"
android:layout_y="300dp"
android:text="Delete"
android:textSize="30dp" />
<Button
android:id="@+id/Update"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="25dp"
android:layout_y="400dp"
android:text="Update"
android:textSize="30dp" />
<Button
android:id="@+id/View"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="200dp"
android:layout_y="400dp"
android:text="View"
android:textSize="30dp" />
<Button
android:id="@+id/ViewAll"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_x="100dp"
android:layout_y="500dp"
android:text="View All"
android:textSize="30dp" />
</AbsoluteLayout>
Main [Link]:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity implements
[Link]{
EditText Rollno,Name,Marks;
Button Insert,Delete,Update,View,ViewAll;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
Rollno=(EditText)findViewById([Link]);
Name=(EditText)findViewById([Link]);
Marks=(EditText)findViewById([Link]);
Insert=(Button)findViewById([Link]);
Delete=(Button)findViewById([Link]);
Update=(Button)findViewById([Link]);
View=(Button)findViewById([Link]);
ViewAll=(Button)findViewById([Link]);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
db=openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
[Link]("CREATE TABLE IF NOT EXISTS student(rollno
VARCHAR,name VARCHAR," +
"marks VARCHAR);");
}
public void onClick(View view)
{
if(view==Insert)
{
if([Link]().toString().trim().length()==0||
[Link]().toString().trim().length()==0||
[Link]().toString().trim().length()==0)
{
showMessage("Error", "Please enter all values");
return;
}
[Link]("INSERT INTO student
VALUES('"+[Link]()+"','"+[Link]()+
"','"+[Link]()+"');");
showMessage("Success", "Record added");
clearText();
}
if(view==Delete)
{
if([Link]().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=[Link]("SELECT * FROM student WHERE
rollno='"+[Link]()+"'", null);
if([Link]())
{
[Link]("DELETE FROM student WHERE rollno='"+[Link]()+"'");
showMessage("Success", "Record Deleted");
}
else
{
showMessage("Error", "Invalid Rollno");
}
clearText();
}
if(view==Update)
{
if([Link]().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=[Link]("SELECT * FROM student WHERE
rollno='"+[Link]()+"'", null);
if([Link]()) {
[Link]("UPDATE student SET name='" + [Link]() + "',marks='" +
[Link]() +
"' WHERE rollno='"+[Link]()+"'");
showMessage("Success", "Record Modified");
}
else {
showMessage("Error", "Invalid Rollno");
}
clearText();
}
if(view==View)
{
if([Link]().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=[Link]("SELECT * FROM student WHERE
rollno='"+[Link]()+"'", null);
if([Link]())
{
[Link]([Link](1));
[Link]([Link](2));
}
else
{
showMessage("Error", "Invalid Rollno");
clearText();
}
}
if(view==ViewAll)
{
Cursor c=[Link]("SELECT * FROM student", null);
if([Link]()==0)
{
showMessage("Error", "No records found");
return;
}
StringBuffer buffer=new StringBuffer();
while([Link]())
{
[Link]("Rollno: "+[Link](0)+"\n");
[Link]("Name: "+[Link](1)+"\n");
[Link]("Marks: "+[Link](2)+"\n\n");
}
showMessage("Student Details", [Link]());
}
}
public void showMessage(String title,String message)
{
[Link] builder=new [Link](this);
[Link](true);
[Link](title);
[Link](message);
[Link]();
}
public void clearText()
{
[Link]("");
[Link]("");
[Link]("");
[Link]();
}
}
Output: