Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

New Android Syllabus

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

List of Experiments

1. Draw Android Architecture and explain briefly? 2. Explain briefly about


Android Activity Lifecycle with neat diagram. 3. Write an android
program to print the set of alphabets/strings in a linear
layout and use button and Text view to your layout. 4. Write an android
program to design login page in a linear layout. 5. Write an android
program to switch from one activity to another using Intent. When the
activity is changed disable the use of back button to avoid going to
previous activity Views.

6. Write an android program to implement activity life cycle using toast


messages with proper positioning. 7. Write an android program to add
WebView to a layout. 8. Write Android Program to Make A Phone Call in
Android 9. Write Android program for Android Search View. 10. Write
Android program to Get Value from the Edit Text and Set value
to the Text View.
Experiment-1

Draw Android Architecture and explain briefly?

Android Architecture:

Android architecture or Android software stack is categorized into five parts:

1. Linux kernel 2. native


libraries (middleware), 3.
Android Runtime 4. Application
Framework 5. Applications
1) Linux kernel:

It is the heart of android architecture that exists at the root of android


architecture. Linux kernel is responsible for device drivers, power management,
memory management, device management and resource access.

2) Native Libraries:

On the top of linux kernel, their are Native libraries such as WebKit, OpenGL,
FreeType, SQLite, Media, C runtime library (libc) etc.
The WebKit library is responsible for browser support, SQLite is for database,
FreeType for font support, Media for playing and recording audio and video formats.

3) Android Runtime:
In android runtime, there are core libraries and DVM (Dalvik Virtual Machine) which is
responsible to run android application. DVM is like JVM but it is optimized for mobile
devices. It consumes less memory and provides fast performance.

4) Android Framework:

On the top of Native libraries and android runtime, there is android framework. Android
framework includes Android API's such as UI (User Interface), telephony, resources,
locations, Content Providers (data) and package managers. It provides a lot of classes
and interfaces for android application development.

5) Applications:

On the top of android framework, there are applications. All applications such as home,
contact, settings, games, browsers are using android framework that uses android
runtime and libraries. Android runtime and native libraries are using linux kernal.
Experiment-2

Explain briefly about Android Activity Lifecycle with neat diagram.

Android Activity Lifecycle: Android Activity Lifecycle is controlled by 7 methods of


android.app.Activity class. The android Activity is the subclass of
ContextThemeWrapper class.

An activity is the single screen in android. It is like window or frame of


Java.

By the help of activity, you can place all your UI components or widgets in a single
screen.

The 7-lifecycle method of Activity describes how activity will behave at different
states.

Android Activity Lifecycle methods:


File: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.androi d.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="example.javatpoint.com.activitylifecycle.MainActivity"
>

<TextVie
w
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

File: MainActivity.java:
package example;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity


{

@Override protected void onCreate(Bundle


savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("lifecycle","onCreate invoked"); }

@Override protected
​ void onStart() {
super.onStart();
Log.d("lifecycle","onStart invoked"); }

@Override protected
​ void onResume()
{
super.onResume();
Log.d("lifecycle","onResume invoked"); }
@Override protected void
onPause() {
super.onPause();
Log.d("lifecycle","onPause invoked"); }

@Override protected
​ void onStop() {
super.onStop();
Log.d("lifecycle","onStop invoked"); }

@Override protected
​ void onRestart()
{
super.onRestart();
Log.d("lifecycle","onRestart invoked"); }

@Override protected
​ void onDestroy() {
super.onDestroy();
Log.d("lifecycle","onDestroy invoked"); } }
Experiment-3:

Write an android program to print the set of alphabets/strings in a linear layout and
use button and Text view to your layout.

File: activity_main.xml

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


xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto
" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="A B C D E F G H I
J K L M N O P Q R S T U V W X Y Z" android:textColor="#3cff00"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="A B C D E F G H I
J K L M N O P Q R S T U V W X Y Z" android:textColor="#3cff00"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:layout_marginTop="100dp"
android:text="next"/> </LinearLayout>
Main2Activity.java:

package com.example.myapplication; import


android.support.v7.app.AppCompatActivity; import
android.os.Bundle; public class Main2Activity extends
AppCompatActivity {
@Override protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2); } }
Experiment-4

Write an android program to design login page in a linear layout.

File: activity_main.xml

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


xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto
" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".LoginPage"> <EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/e1"
android:drawableLeft="@drawable/ic_person_black_24dp"
android:layout_gravity="center_horizontal"
android:hint="Enter Username"/> <EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/e2"
android:drawableLeft="@drawable/ic_vpn_key_black_24dp"
android:layout_gravity="center_horizontal"
android:hint="Enter Password"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_gravity="center_horizontal"
android:id="@+id/button"/> </LinearLayout>

Main2Activity.java: package
com.example.myapplication; import
android.support.v7.app.AppCompatActivity; import
android.os.Bundle; public class Main2Activity extends
AppCompatActivity {
@Override protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2); } }
Experiment-5

Write an android program to switch from one activity to another using Intent. When the
activity is changed disable the use of back button to avoid going to previous activity
Views.

PROCEDURE: Step-1: Create a new project and name it Exp4. Step-2: Open app ->
package -> Exp4.java. In this step we open Exp4.java where we add the code

Exp4.java: package com.example.exp_5; import


android.support.v7.app.AppCompatActivity;
import android.os.Bundle; public class Exp4
extends AppCompatActivity { Button b;
@Override protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exp4);
b=findViewById(R.id.b); b.setOnClickListener(new
View.OnClickListener() {
@Override public void
onClick(View view) {
Intent intent=new Intent(getApplicationContext(),Main2Activity.class);

startActivity(intent); } }); ​}}

E4.java:

package com.example.exp_5; import


android.support.v7.app.AppCompatActivity;
import android.os.Bundle; public class E4 extends
AppCompatActivity {
@Override protected void onCreate(Bundle
savedInstanceState) {

super.onCreate(savedInstanceState); setContentView(R.layout.activity_e4); }} ​Step-3:

Open res -> layout -> activity_exp4.xml and add following code: In this step we open

an xml file and add the following code.
Activity_exp4.xml:

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


<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android
"
xmlns:app="http://schemas.android.com/apk/res-auto
" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Exp4"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="submit" android:textStyle="bold"
android:textAlignment="center"
android:textSize="40sp"
android:textColor="@color/colorPrimaryDark"/>
</android.support.constraint.ConstraintLayout>

Activity_e4.xml:

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


<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android
"
xmlns:app="http://schemas.android.com/apk/res-auto
" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".E4"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Experiment-4 is Completed"
android:textStyle="bold"
android:textAlignment="center"
android:textSize="40sp"
android:textColor="@color/colorPrimaryDark"/>
</android.support.constraint.ConstraintLayout>
Experiment-6

Write an android program to implement activity life cycle using toast messages with
proper positioning.

PROCEDURE: Step-1: Create a new project and name it Exp1. Step-2: Open app ->
package -> MainActivity.java. In this step we open MainActivity where we add the
following code.

MainActivity.java:

package com.example.exp_5; import


android.content.Intent; import
android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import
android.view.View; import android.widget.Button;
import android.widget.Toast; public class exp1
extends AppCompatActivity {
Button b1; @Override public void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exp1); b1=findViewById(R.id.b1);
Toast.makeText(exp1.this,"ON CREATE",
Toast.LENGTH_SHORT).show(); b1.setOnClickListener(new
View.OnClickListener() {
@Override public void
onClick(View view) {
Intent intent=new Intent(getApplicationContext(),Main2Activity.class);

startActivity(intent); } }); } ​@Override protected


​ void onStart() {
super.onStart(); Toast.makeText(exp1.this,"ON START",

Toast.LENGTH_SHORT).show(); } ​@Override protected


​ void onResume() {
super.onResume(); Toast.makeText(exp1.this,"ON RESUME",

Toast.LENGTH_SHORT).show(); } ​@Override protected


​ void onPause() {
super.onPause()
;
Toast.makeText(exp1.this,"ON PAUSE",
Toast.LENGTH_SHORT).show(); } ​@Override protected
​ void onRestart() {
super.onRestart(); Toast.makeText(exp1.this,"ON RESTART",
Toast.LENGTH_SHORT).show(); } ​@Override protected
​ void onStop() {
super.onStop(); Toast.makeText(exp1.this,"ON STOP",

Toast.LENGTH_SHORT).show(); } ​@Override protected


​ void onDestroy()
{
// TODO Auto-generated method stub super.onDestroy();
Toast.makeText(exp1.this,"ON DESTROY", Toast.LENGTH_SHORT).show(); } }

Step-3: Open res -> layout -> activity_main.xml and add following code.

Activity_main.xml:

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


xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto
" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"
android:textColor="#3cff00"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</LinearLayout>

.​-
Experiment-7

Write an android program to add WebView to a layout.

File: activity_main.xml <?xml version="1.0"


encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:
android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="example.javatpoint.com.webview.MainActivity"
>
<WebVie
w
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

File: MainActivity.java
package example.webview; import
android.support.v7.app.AppCompatActivity; import
android.os.Bundle; import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {

@Override protected void onCreate(Bundle


savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); WebView mywebview =
(WebView) findViewById(R.id.webView);
mywebview.loadUrl("file:///android_asset/myresource.html"); } }
Experiment-8

Write Android Program to Make A Phone Call in Android

File: activity_main.xml Drag the EditText and Button from the pallete, now the
activity_main.xml file will like this: File: activity_main.xml
<RelativeLayout
xmlns:androclass="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools
" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true
" android:layout_marginTop="118dp"
android:text="Call" />

<EditTex
t
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true
" android:layout_marginTop="25dp"
android:ems="10" />

</RelativeLayout
>

Write the permission code in Android-Manifest.xml file You


need to write CALL_PHONE permission as given below:
1. <uses-permission android:name="android.permission.CALL_PHONE" />

File: Android-Manifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest
xmlns:androclass="http://schemas.android.com/apk/res/android"
package="com.example.phonecall
" android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />

<uses-permission android:name="android.permission.CALL_PHONE"
/> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.phonecall.MainActivity"
android:label="@string/app_name" > <intent-filter>
<action android:name="android.intent.action.MAIN"
/>

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter> </activity> </application>

</manifest>

Activity class Let's write the code to make the phone


call via intent. File: MainActivity.java
package com.example.phonecall;

import android.net.Uri; import


android.os.Bundle; import
android.app.Activity; import
android.content.Intent; import
android.view.Menu; import
android.view.View; import
android.view.View.OnClickListener; import
android.widget.Button; import
android.widget.EditText;
public class MainActivity extends Activity
{
EditText edittext1; Button button1; @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Getting the edittext and button instance


edittext1=(EditText)findViewById(R.id.editText1)
; button1=(Button)findViewById(R.id.button1);
//Performing action on button click
button1.setOnClickListener(new OnClickListener(){

@Override public void


onClick(View arg0) {
String number=edittext1.getText().toString(); Intent
callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+number));
startActivity(callIntent); }

}); }
Experiment-9

Write Android program for Android Search View.

File: activity_main.xml

Create an activity_main.xml file in layout folder containing Scroll View and List
View.

File: activity_main.xml <?xml version="1.0" encoding="utf-8"?>


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android: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.test.searchview.MainActivity">

<ListVie
w
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv1"
android:divider="#ad5"
android:dividerHeight="2dp"
android:layout_below="@+id/searchView"/>

<SearchView
android:id="@+id/searchView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:queryHint="Search Here"
android:iconifiedByDefault="false"
android:layout_alignParentTop="true" />

</RelativeLayout
>

File: MainActivity.java
package com.example.searchview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import
android.widget.ArrayAdapter; import
android.widget.Filter; import
android.widget.ListView; import
android.widget.SearchView; import
android.widget.Toast;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


SearchView searchView; ListView listView;
ArrayList<String> list; ArrayAdapter<String >
adapter; @Override protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

searchView = (SearchView) findViewById(R.id.searchView);


listView = (ListView) findViewById(R.id.lv1);

list = new ArrayList<>();


list.add("Apple");
list.add("Banana");
list.add("Pineapple");
list.add("Orange");
list.add("Lychee");
list.add("Gavava");
list.add("Peech");
list.add("Melon");
list.add("Watermelon");
list.add("Papaya");

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1


,list); ​listView.setAdapter(adapter);

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{
@Override public boolean
onQueryTextSubmit(String query) {
if(list.contains(query))
{
adapter.getFilter().filter(query); }else{
Toast.makeText(MainActivity.this, "No Match found",Toast.LENGTH_
LONG).show();

} ​return false; }​ ​@Override public


​ boolean
onQueryTextChange(String newText) { //
adapter.getFilter().filter(newText);
return false;
} }); }
}
Experiment-10

Write Android program to Get Value from the EditText and Set value to the
TextView.

File: activity_main.xml: <?xml version="1.0" encoding="utf-8"?>


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" android:background="#FFFF8D"
tools:context="com.example.akshay.studytonightandroid.MainActivity"
>

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="NAME"
android:textSize="20sp"
android:layout_margin="20dp" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="PASSWORD"
android:layout_marginTop="38dp"
android:layout_below="@+id/textView"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView" />

<EditTex
t
android:id="@+id/editName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="Enter Name"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignLeft="@+id/editPassword"
android:layout_alignStart="@+id/editPassword" />

<EditTex
t
android:id="@+id/editPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" android:hint="Enter
Password" android:inputType="textPassword"
android:layout_alignBottom="@+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="18dp"
android:layout_marginEnd="18dp" />

<Button
android:id="@+id/buttonSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/textView2"
android:layout_marginTop="20dp"
android:text="SUBMIT" />

<Button
android:id="@+id/buttonReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RESET"
android:layout_alignBaseline="@+id/buttonSubmit"
android:layout_alignBottom="@+id/buttonSubmit"
android:layout_centerHorizontal="true" />

<TextView
android:id="@+id/tvResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="143dp"
android:textSize="30sp" />
</RelativeLayout
>
Complete Code for MainActivity.java

package
com.example.MainActivity;

import android.os.Bundle; import


android.support.v7.app.AppCompatActivity;
import android.view.View; import
android.widget.Button; import
android.widget.EditText; import
android.widget.TextView;

public class MainActivity extends AppCompatActivity


{
// These are the global variables
EditText editName, editPassword;
TextView result; Button
buttonSubmit, buttonReset;

@Override protected void onCreate(Bundle


savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

editName = (EditText) findViewById(R.id.editName);


editPassword = (EditText) findViewById(R.id.editPassword);
result = (TextView) findViewById(R.id.tvResult);
buttonSubmit = (Button) findViewById(R.id.buttonSubmit);
buttonReset = (Button) findViewById(R.id.buttonReset); /*

Submit Button */ ​buttonSubmit.setOnClickListener(new

View.OnClickListener() {
@Override public void
onClick(View v) {
String name = editName.getText().toString(); String password =
editPassword.getText().toString(); result.setText("Name:\t" +
name + "\nPassword:\t" + password );

}
});

/*

Reset Button */ ​buttonReset.setOnClickListener(new

View.OnClickListener() {
@Override public void
onClick(View v) {
editName.setText("");
editPassword.setText("");
result.setText("");
editName.requestFocus(); } }); } }

You might also like