1. ANDROID APPLICATION
DEPARTMET OF COMPUTER ENGINEERING
PRACTICAL-2
A. Create “hello world” application. That will display “Hello World” in the middle of the
screen in the black color with wheat color or white background.
B. create hello world using java
HelloMainActivity.java
package com.example.raj.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Activity_hello_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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#fafad2"
tools:context="com.example.raj.myapplication.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Hello World"
android:textSize="30sp"
android:textColor="#B22222"
android:layout_centerVertical="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
2. ANDROID APPLICATION
DEPARTMET OF COMPUTER ENGINEERING
android:layout_marginTop="99dp" />
</RelativeLayout>
Output:
B. Create hello world using java
package com.example.raj.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text = new TextView(this);
text.setText("Hello World");
setContentView(text);
}
}
4. ANDROID APPLICATION
DEPARTMET OF COMPUTER ENGINEERING
PRACTICAL-3
Create an Android App Which Has one Activity to get two numbers N1 and N2.on pressing
Button has four operation Add, Multiply, Divide, Subtract ,on select any one operation it
will Display Result in Text View of Activity
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:background="#fafad2"
tools:context="com.example.raj.myapplication.MainActivity"
android:weightSum="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="No 1:"
android:id="@+id/tv1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etno1"
android:ems="10"
android:hint="Enter No1"
android:inputType="number"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="No 2:"
android:id="@+id/tv2" />
6. ANDROID APPLICATION
DEPARTMET OF COMPUTER ENGINEERING
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.raj.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView tv1, tv2, Result;
EditText et1, et2;
Button Add, Sub, Mul, Div;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText)findViewById(R.id.etno1);
et2 = (EditText)findViewById(R.id.etno2);
Result = (TextView)findViewById(R.id.tv3);
Add = (Button)findViewById(R.id.Add);
Sub = (Button)findViewById(R.id.Sub);
Mul = (Button)findViewById(R.id.Mul);
Div = (Button)findViewById(R.id.Div);
Add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO Auto-Generated Method...
if(et1.getText().toString().equals(""))
{
et1.setError("Please fill the No1");
}
else if(et2.getText().toString().equals(""))
{
et2.setError("Please fill the No2");
}
else
{
7. ANDROID APPLICATION
DEPARTMET OF COMPUTER ENGINEERING
int a = Integer.parseInt(et1.getText().toString());
int b = Integer.parseInt(et2.getText().toString());
int c = a + b;
Result.setText("Answer="+c);
}
}
});
Sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO Auto-Generated Method...
if(et1.getText().toString().equals(""))
{
et1.setError("Please fill the No1");
}
else if(et2.getText().toString().equals(""))
{
et2.setError("Please fill the No2");
}
else
{
int a = Integer.parseInt(et1.getText().toString());
int b = Integer.parseInt(et2.getText().toString());
int c = a - b;
Result.setText("Answer="+c);
}
}
});
Mul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO Auto-Generated Method...
if(et1.getText().toString().equals(""))
{
et1.setError("Please fill the No1");
}
else if(et2.getText().toString().equals(""))
{
et2.setError("Please fill the No2");
}
else
{
int a = Integer.parseInt(et1.getText().toString());
8. ANDROID APPLICATION
DEPARTMET OF COMPUTER ENGINEERING
int b = Integer.parseInt(et2.getText().toString());
int c = a * b;
Result.setText("Answer="+c);
}
}
});
Div.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO Auto-Generated Method...
if(et1.getText().toString().equals(""))
{
et1.setError("Please fill the No1");
}
else if(et2.getText().toString().equals(""))
{
et2.setError("Please fill the No2");
}
else
{
int a = Integer.parseInt(et1.getText().toString());
int b = Integer.parseInt(et2.getText().toString());
int c = a / b;
Result.setText("Answer="+c);
}
}
});
}
}
10. ANDROID APPLICATION
DEPARTMET OF COMPUTER ENGINEERING
PRACTICAL-4
Create application for demonstration of android activity life cycle.
Activity_life_cycle.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: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.raj.lifecycle.LifeCycle">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Android Activity Life Cycle"
android:id="@+id/tv1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
LifeCycle.java
package com.example.raj.lifecycle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class LifeCycle extends AppCompatActivity {
TextView tv1;
String text;
@Override
11. ANDROID APPLICATION
DEPARTMET OF COMPUTER ENGINEERING
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_life_cycle);
tv1 = (TextView)findViewById(R.id.tv1);
text = tv1.getText().toString();
text = text + "n InsideCreate Method";
tv1.setText(text);
}
public void onStart()
{
super.onStart();
text = tv1.getText().toString();
text = text + "n InsideStart Method";
tv1.setText(text);
}
public void onRestart()
{
super.onRestart();
text = tv1.getText().toString();
text = text + "n InsideRestart Method";
tv1.setText(text);
}
public void onResume()
{
super.onResume();
text = tv1.getText().toString();
text = text + "n InsideResume Method";
tv1.setText(text);
}
public void onPause()
{
super.onPause();
text = tv1.getText().toString();
text = text + "n InsidePause Method";
tv1.setText(text);
12. ANDROID APPLICATION
DEPARTMET OF COMPUTER ENGINEERING
}
public void onStop()
{
super.onStop();
text = tv1.getText().toString();
text = text + "n InsideStop Method";
tv1.setText(text);
}
public void onDestroy()
{
super.onDestroy();
text = tv1.getText().toString();
text = text + "n InsideDestroy Method";
tv1.setText(text);
}
}
OUTPUT:
22. ANDROID APPLICATION
DEPARTMET OF COMPUTER ENGINEERING
PRACTICAL-7
Create an Android App which has one activity to get Fern hit or Celsius. On Pressing
“Convert” button it shows the Result will be displayed on Text View of Activity.
Activity_fernhit.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: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.raj.fernhit.Fernhit">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/et1"
android:ems="15"
android:hint="Enter Value"
android:layout_marginTop="52dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/tvResult"
android:text="Result"
android:layout_below="@+id/et1"
android:layout_alignLeft="@+id/et1"
android:layout_alignStart="@+id/et1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Celcius"
android:id="@+id/cb"
android:layout_below="@+id/tvResult"
android:layout_alignLeft="@+id/tvResult"
android:layout_alignStart="@+id/tvResult"
android:layout_marginTop="45dp"