Sei sulla pagina 1di 95

Ce373 16CE068

Practical-1

Aim : Installation of Android in Windows with Simulator and Introduction


to Android Life Cycle & Introduction to UI/XI.
STEP 1: DOWNLOAD AND INSTALL THE JAVA RUNTIME ENVIRONMENT
(JRE)

You can find the download file here:


http://www.oracle.com/technetwork/java/javase/downloads/jre-6u25-download-346243.html.

STEP 2: DOWNLOAD AND INSTALL THE JAVA DEVELOPMENT KIT.


On some Windows systems, the launcher script does not find where the JDK is
installed. If you encounter this problem, you need to set an environment variable
indicating the correct location.
Select Start menu > Computer > System Properties > Advanced System Properties.
Then open Advanced tab > Environment Variables and add a new system variable
JAVA_HOME that points to your JDK folder, for example C:\Program
Files\Java\jdk1.7.0_21.

1
Ce373 16CE068

STEP 3: DOWNLOAD ANDROID STUDIO

The Android Software Development Kit (referred to in this License Agreement as the "SDK"
and specifically including the Android system files, packaged APIs, and Google APIs add-ons)
is licensed to you subject to the terms of this License Agreement. This License Agreement
forms a legally binding contract between you and Google in relation to your use of the SDK.
You can install android studio by following link:

http://developer.android.com/sdk/index.html#top

STEP 4: ADDING SDK PACKAGES

The Android SDK does not include everything you need to start developing. The SDK separates tools,
platforms, and other components into packages you can download as needed using the Android SDK
Manager. So before you can start, there are a few packages you should add to your Android SDK.

To start adding packages, launch the Android SDK Manager in one of the following ways:

 In Android Studio, click SDK Manager in the toolbar.



 If you're not using Android Studio:

o Windows: Double-click the SDK Manager.exe file at the root of the Android SDK directory.

2
Ce373 16CE068

o Mac/Linux: Open a terminal and navigate to the tools/ directory in the location where the Android
SDK was installed, then execute android sdk.

When you open the SDK Manager for the first time, several packages are selected by default. Leave these
selected, but be sure you have everything you need to get started by following these steps:

Get the latest SDK tools

As a minimum when setting up the Android SDK, you should download the latest tools and Android
platform:

1. Open the Tools directory and select:

o Android SDK Tools

o Android SDK Platform-tools

o Android SDK Build-tools (highest version)

2. Open the first Android X.X folder (the latest version) and select:

o SDK Platform

o A system image for the emulator, such as


ARM EABI v7a System Image

Get the support library for additional APIs


The support library is required for:

Android Wear

Android TV

3
Ce373 16CE068

Google Cast



Navigation drawer

Swipe views

Backward-compatible action bar



The Android Support Library provides an extended set of APIs that are compatible with most versions of
Android.

Open the Extras directory and select:

 Android Support Repository

 Android Support Library



Get Google Play services for even more APIs

The Google Play services APIs provide a variety of features and services for your Android apps, such as:

User authentication

Google Maps

Google Cast

Games achievements and leaderboards

And much more



To develop with Google APIs, you need the Google Play services package:

Open the Extras directory and select:

 Google Repository

 Google Play services

Note: Google Play services APIs are not available on all Android-powered devices, but are available on all
devices with Google Play Store. To use these APIs in the Android emulator, you must also install the the
Google APIs system image from the latest Android X.X directory in the SDK Manager.

4
Ce373 16CE068

INSTALL THE PACKAGES

Once you've selected all the desired packages, continue to install:

1. Click Install X packages.

2. In the next window, double-click each package name on the left to accept the license agreement for
each.

3. Click Install.

The download progress is shown at the bottom of the SDK Manager window. Do not exit the SDK
Manager or it will cancel the download.

STEP 5: ANDROID PROJECT VIEW

By default, Android Studio displays your project files in the Android project view. This view shows a
flattened version of your project's structure that provides quick access to the key source files of Android
projects and helps you work with the Gradle-based build system. The Android project view:

 Shows the most important source directories at the top level of the module hierarchy.

 Groups the build files for all modules in a common folder.

 Groups all the manifest files for each module in a common folder.

 Shows resource files from all Gradle source sets.

 Groups resource files for different locales, orientations, and screen types in a single group per resource
type.

5
Ce373 16CE068

Figure: Show The Android Project View.

Figure: Show Project Build Files.

The Android project view shows all the build files at the top level of the project hierarchy under Gradle
Scripts. Each project module appears as a folder at the top level of the project hierarchy and contains these
four elements at the top level:
 java/ - Source files for the module.

 manifests/ - Manifest files for the module.

 res/ - Resource files for the module.

 Gradle Scripts/ - Gradle build and property files.

For example, Android project view groups all the instances of the ic_launcher.png resource for different
screen densities under the same element.

6
Ce373 16CE068

Note: The project structure on disk differs from this flattened representation. To switch to back to the
segregated project view, select Project from the Project drop-down.

Other Android Studio views

When you use the Project view in Android Studio, you should notice that the project structure appears
different than you may be used to in Eclipse. Each instance of Android Studio contains a project with one
or more application modules. Each application module folder contains the complete source sets for that
module, including src/main/ andsrc/androidTest/ directories, resources, build file and the Android manifest.
For the most part, you will need to modify the files under each module's src/main/ directory for source code
updates, the gradle.build file for build specification and the files under src/androidTest/ directory for test case
creation.

Figure: View Android Studio Project structure.

 
Android Virtual Device (AVD) Manager

AVD Manager has updated screens with links to help you select the most popular device
configurations, screen sizes and resolutions for your app previews.

Click the Android Virtual Device Manager in the toolbar to open it and create new virtual
devices for running your app in the emulator.

The AVD Manager comes with emulators for Nexus 6 and Nexus 9 devices and also supports
creating custom Android device skins based on specific emulator properties and assigning those
skins to hardware profiles. Android Studio installs the Intel® x86 Hardware Accelerated
Execution Manager (HAXM) emulator accelerator and creates a default emulator for quick app
prototyping.

7
Ce373 16CE068

For more information, see Managing AVDs.


 
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
Let's see the 7 lifecycle methods of android activity.

Method Description

onCreate called when activity is first created.

onStart called when activity is becoming visible to the user.

onResume called when activity will start interacting with the user.

onPause called when activity is not visible to the user.

onStop called when activity is no longer visible to the user.

onRestart called after your activity is stopped, prior to start.

onDestroy called before the activity is destroyed.

8
Ce373 16CE068

9
Ce373 16CE068

Practical-2

Aim : I. Create “Hello World” application. That will display “Hello World”
in the middle of the screen in the red color with white background.
II.To understand Activity, Intent Create sample application with login
module.(Check username and password) On successful login, go to
next screen. And on failing login, alert user using Toast. Also pass
username to next screen.

 in
Create “Hello World” application. That will display “Hello World”
the middle of the screen in the red color with white background
I)Hello World
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=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="69dp"
android:text="Hello World!"
android:textColor="@android:color/holo_red_dark"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

MainActivity.java
package com.example.jainilpatel.hello;
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); }}

10
Ce373 16CE068

2)Login
MainActivity.java
package com.example.jainilpatel.practical2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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.EditText;
import android.widget.Toast;

import java.lang.String;

public class MainActivity extends AppCompatActivity {

Button b1;
EditText t1;
EditText t2;
EditText t3;
EditText t4;
EditText t5;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=(EditText)findViewById(R.id.editText5);
t2=(EditText)findViewById(R.id.editText6);
t3=(EditText)findViewById(R.id.editText8);
t4=(EditText)findViewById(R.id.editText10);

11
Ce373 16CE068

public void signup(View v)


{
String s1=t1.getText().toString();
String s2=t2.getText().toString();
String s3=t3.getText().toString();
String s4=t4.getText().toString();

if(s2.endsWith("@charusat.edu.in")&&s4.equals("12345"))
{
Intent i=new Intent(this,Main2Activity.class);
i.putExtra("parcel_data", s1);
startActivity(i);
}
else
{
Toast.makeText(MainActivity.this, "LOGIN UNSUCCESSFUL" ,
Toast.LENGTH_SHORT).show();

}
}
}

Main2Activity.java
package com.example.jainilpatel.practical2;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity {


TextView t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
t1=(TextView)findViewById(R.id.textView);
Intent i=getIntent();
String s="WELCOME "+i.getStringExtra("parcel_data");
t1.setText(s);
}
}

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

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"

12
Ce373 16CE068

android:layout_marginEnd="35dp"
android:layout_marginTop="22dp"
android:id="@+id/editText5"
android:background="@android:color/holo_blue_bright" />

<TextView
android:text="name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/editText6"
android:layout_alignParentStart="true"
android:layout_marginStart="17dp"
android:id="@+id/textView7" />

<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/textView7"
android:layout_alignTop="@+id/editText6"
android:text="email" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText8"
android:layout_alignStart="@+id/textView7"
android:text="Confirm Password" />

<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/textView7"
android:layout_alignTop="@+id/editText10"
android:text="password" />

<EditText
android:id="@+id/editText8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignStart="@+id/editText5"
android:layout_marginTop="131dp"
android:background="@android:color/holo_blue_bright"
android:ems="10"
android:inputType="textPersonName" />

<EditText
android:id="@+id/editText10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignStart="@+id/editText5"
android:layout_marginTop="96dp"
android:background="@android:color/holo_blue_bright"
android:ems="10"
android:inputType="textPersonName" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_marginTop="13dp"
android:id="@+id/editText6"
android:background="@android:color/holo_blue_bright"

13
Ce373 16CE068

android:layout_below="@+id/editText5"
android:layout_alignStart="@+id/editText5" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="188dp"
android:onClick="signup"
android:text="Sign In" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="45dp"
android:text="DEVELOPED BY 16CE068" />

</RelativeLayout>

activity_main2.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=".Main2Activity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

Output:

14
Ce373 16CE068

Practical-3

Aim : Create login application where you will have to validate EmailID
(UserName). Till the username and password is not validated , login button
should remain disabled. Create and Login application as above . On
successful login , open browser with any URL.
Code:
Activitymain.java
package com.example.jainilpatel.practical3;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.net.Uri;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.lang.String;

public class MainActivity extends AppCompatActivity {

Button b1;
EditText t1;
EditText t2;
EditText t3;
EditText t4;
EditText t5;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
t1=(EditText)findViewById(R.id.editText5);
t2=(EditText)findViewById(R.id.editText6);
t3=(EditText)findViewById(R.id.editText8);
t4=(EditText)findViewById(R.id.editText10);
t4.setOnFocusChangeListener(new View.OnFocusChangeListener() {

@Override
public void onFocusChange(View v, boolean
hasFocus) {

{
xyz();

}
}
}
);

public void xyz()


{

String s1=t1.getText().toString();
String s2=t2.getText().toString();
String s3=t3.getText().toString();
String s4=t4.getText().toString();

15
Ce373 16CE068

if(s2.endsWith("@charusat.edu.in")&&s4.equals("12345"))
{
b1.setEnabled(true);

public void btnclick(View v)


{

String s1=t1.getText().toString();
String s2=t2.getText().toString();
String s3=t3.getText().toString();
String s4=t4.getText().toString();

if(s2.endsWith("@charusat.edu.in")&&s4.equals("12345"))
{
Toast.makeText(MainActivity.this, "LOGIN SUCCESSFUL" ,
Toast.LENGTH_SHORT).show();
Intent j=new Intent(Intent.ACTION_VIEW,
Uri.parse("http://google.com"));
startActivity(j);

}
else
{
Toast.makeText(MainActivity.this, "LOGIN UNSUCCESSFUL" ,
Toast.LENGTH_SHORT).show();

}
}

16
Ce373 16CE068

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

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="35dp"
android:layout_marginTop="22dp"
android:id="@+id/editText5"
android:background="@android:color/holo_blue_bright" />

<TextView
android:text="name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/editText6"
android:layout_alignParentStart="true"
android:layout_marginStart="17dp"
android:id="@+id/textView7" />

<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/textView7"
android:layout_alignTop="@+id/editText6"
android:text="email" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText8"
android:layout_alignStart="@+id/textView7"
android:text="Confirm Password" />

<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/textView7"
android:layout_alignTop="@+id/editText10"
android:text="password" />

<EditText
android:id="@+id/editText8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignStart="@+id/editText5"
android:layout_marginTop="131dp"
android:background="@android:color/holo_blue_bright"
android:ems="10"
android:inputType="textPersonName" />

<EditText

17
Ce373 16CE068

android:id="@+id/editText10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignStart="@+id/editText5"
android:layout_marginTop="96dp"
android:background="@android:color/holo_blue_bright"
android:ems="10"
android:inputType="textPersonName" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="197dp"
android:enabled="false"
android:onClick="btnclick"
android:text="click" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_marginTop="13dp"
android:id="@+id/editText6"
android:background="@android:color/holo_blue_bright"
android:layout_below="@+id/editText5"
android:layout_alignStart="@+id/editText5" />

</RelativeLayout>
Output:

18
Ce373 16CE068

Practical-4

Aim : Create an application that will pass some number to the next screen
and on the next screen that number of items should be display in the
list. Understand resource folders :
Create spinner with strings taken from resource folder(res >> value folder).
On changing spinner value, change image. Understand Menu option.
Create an application that will change color of the screen, based on selected
options from the menu.
MainActivity.java
package com.example.jainilpatel.myapplication;

import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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.EditText;
import android.widget.Toast;

import java.lang.String;

public class MainActivity extends AppCompatActivity {

Button b1;
EditText t1;
EditText t2;
EditText t3;
EditText t4;
EditText t5;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
t1=(EditText)findViewById(R.id.editText5);
t2=(EditText)findViewById(R.id.editText6);
t3=(EditText)findViewById(R.id.editText8);
t4=(EditText)findViewById(R.id.editText10);
t5=(EditText)findViewById(R.id.editText);
t4.setOnFocusChangeListener(new View.OnFocusChangeListener() {

@Override
public void onFocusChange(View v, boolean hasFocus) {

{
xyz();

}
}
}
);

public void xyz()

19
Ce373 16CE068

String s1=t1.getText().toString();
String s2=t2.getText().toString();
String s3=t3.getText().toString();
String s4=t4.getText().toString();

if(s2.endsWith("@charusat.edu.in")&&s4.equals("12345"))
{
b1.setEnabled(true);

public void btnclick(View v)


{

String s1=t1.getText().toString();
String s2=t2.getText().toString();
String s3=t3.getText().toString();
String s4=t4.getText().toString();

if(s2.endsWith("@charusat.edu.in")&&s4.equals("12345"))
{
Toast.makeText(MainActivity.this, "LOGIN SUCCESSFUL" ,
Toast.LENGTH_SHORT).show();
Intent j=new Intent(Intent.ACTION_VIEW,
Uri.parse("http://google.com"));
startActivity(j);
// Intent i=new Intent(this,Main2Activity.class);
//i.putExtra("parcel_data", s1);
// startActivity(i);
}
else
{
Toast.makeText(MainActivity.this, "LOGIN UNSUCCESSFUL" ,
Toast.LENGTH_SHORT).show();

}
//Toast.makeText(MainActivity.this, s1+" "+s2+ " "+ s3 +" "+s4 ,
Toast.LENGTH_SHORT).show();
}

public void signup(View v)


{
String s5=t5.getText().toString();
Intent i=new Intent(this,Main3Activity.class);
i.putExtra("parcel_data", s5);
startActivity(i);
}
}

20
Ce373 16CE068

Main2Activity.java
package com.example.jainilpatel.myapplication;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);


fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action",
Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}

21
Ce373 16CE068

Main3Activity.java
package com.example.jainilpatel.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class Main3Activity extends AppCompatActivity {

String s;
ListView list;
ArrayList<String> arr;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Intent intent=getIntent();
s=intent.getStringExtra("parcel_data");

list=(ListView)findViewById(R.id.list);
arr=new ArrayList<String>();
for(int i=1;i<=Integer.parseInt(s);i++)
{
arr.add("Person:"+i);
}

final ArrayAdapter arrayAdapter=new


ArrayAdapter(this,android.R.layout.simple_list_item_1,arr);

list.setAdapter(arrayAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String s="ITEM CLICKED"+(position+1);
Toast.makeText(Main3Activity.this, s, Toast.LENGTH_SHORT).show();

Intent i=new Intent(Main3Activity.this,Main5Activity.class);


i.putExtra("number","963851952"+position);
startActivity(i);
}
});
}
public void click(View v)
{
Intent i=new Intent(this,Main4Activity.class);

startActivity(i);
}
}

22
Ce373 16CE068

Main4Activity.java
package com.example.jainilpatel.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;

import java.util.ArrayList;
import java.util.List;

public class Main4Activity extends AppCompatActivity implements


AdapterView.OnItemSelectedListener{
Spinner s;
ImageView im;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
s=(Spinner)findViewById(R.id.spinner);
im=(ImageView)findViewById(R.id.imageView);
s.setOnItemSelectedListener(this);

// Spinner Drop down elements

// Creating adapter for spinner


ArrayAdapter<CharSequence> dataAdapter =
ArrayAdapter.createFromResource(this,R.array.country,R.layout.support_simple_spinne
r_dropdown_item);

// Drop down layout style - list view with radio button

dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

// attaching data adapter to spinner


s.setAdapter(dataAdapter);

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long
id) {
switch(position)
{
case 0:
im.setImageResource(R.drawable.ic_launcher_background);
break;
case 1:
im.setImageResource(R.drawable.ic_launcher_foreground);
break;
case 2:
im.setImageResource(R.drawable.ic_launcher_background);
break;
case 3:
im.setImageResource(R.drawable.ic_launcher_foreground);
break;
case 4:
im.setImageResource(R.drawable.ic_launcher_background);
break;
case 5:
im.setImageResource(R.drawable.ic_launcher_foreground);

23
Ce373 16CE068

break;
case 6:
im.setImageResource(R.drawable.ic_launcher_background);
break;
}
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
}

Main5Activity.java
package com.example.jainilpatel.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Main5Activity extends AppCompatActivity {


TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
t=(TextView)findViewById(R.id.textView2);
Intent i=getIntent();
String s=i.getStringExtra("number");
t.setText("NUMBER IS "+s);
}
}

24
Ce373 16CE068

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

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="35dp"
android:layout_marginTop="22dp"
android:id="@+id/editText5"
android:background="@android:color/holo_blue_bright" />

<TextView
android:text="name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/editText6"
android:layout_alignParentStart="true"
android:layout_marginStart="17dp"
android:id="@+id/textView7" />

<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/textView7"
android:layout_alignTop="@+id/editText6"
android:text="email" />

<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignStart="@+id/textView7"
android:layout_marginTop="170dp"
android:text="NUMBER" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText8"
android:layout_alignStart="@+id/textView7"
android:text="Confirm Password" />

<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/textView7"
android:layout_alignTop="@+id/editText10"
android:text="password" />

<EditText
android:id="@+id/editText8"
android:layout_width="wrap_content"

25
Ce373 16CE068

android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignStart="@+id/editText5"
android:layout_marginTop="131dp"
android:background="@android:color/holo_blue_bright"
android:ems="10"
android:inputType="textPersonName" />

<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/editText5"
android:layout_alignTop="@+id/textView9"
android:background="@android:color/holo_blue_bright"
android:ems="10"
android:inputType="textPersonName" />

<EditText
android:id="@+id/editText10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignStart="@+id/editText5"
android:layout_marginTop="96dp"
android:background="@android:color/holo_blue_bright"
android:ems="10"
android:inputType="textPersonName" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="197dp"
android:enabled="false"
android:onClick="btnclick"
android:text="click" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_marginTop="13dp"
android:id="@+id/editText6"
android:background="@android:color/holo_blue_bright"
android:layout_below="@+id/editText5"
android:layout_alignStart="@+id/editText5" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="196dp"
android:onClick="signup"
android:text="Sign Up" />

</RelativeLayout>

26
Ce373 16CE068

Activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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=".Main2Activity">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main2" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

Context_main2.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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".Main2Activity"
tools:showIn="@layout/activity_main2">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome To Charusat"
android:textColor="@android:color/holo_red_dark"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

27
Ce373 16CE068

Activity_main3.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=".Main3Activity">

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:onClick="click"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.711" />

<ListView
android:id="@+id/list"
android:layout_width="344dp"
android:layout_height="305dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>

Activity_main4.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=".Main4Activity">
<ImageView
android:id="@+id/imageView"
android:layout_width="229dp"
android:layout_height="311dp"
android:layout_marginTop="184dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@android:drawable/presence_audio_online" />
<Spinner
android:id="@+id/spinner"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

28
Ce373 16CE068

Activity_main5.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=".Main5Activity">

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Output:

29
Ce373 16CE068

30
Ce373 16CE068

Understand Menu option. Create an application that will change color of the
screen, based on selected options from the menu.
CODE:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" android:id="@+id/mainactivity">

<TextView android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent" android:text="Hit Menu Button"
android:gravity="center_vertical|center_horizontal"
android:textAppearance="?android:attr/textAppearanceLarge"
/>

</LinearLayout>

MainActivity.java
package com.example. jainilpatel.practical43;

import android.app.Activity; import android.graphics.Color; import


android.os.Bundle; import android.view.Menu; import
android.view.MenuItem; import android.view.View;
import android.widget.AdapterView;
import android.widget.Button; import android.widget.EditText;
import android.widget.ImageView; import android.widget.Spinner;

public class MainActivity extends Activity { View view1;

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

view1 = (View) findViewById(R.id.mainactivity);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present. getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.

if(item.getTitle().equals("Red"))

31
Ce373 16CE068

{
view1.setBackgroundColor(Color.RED);
}
else if(item.getTitle().equals("Green"))
{
view1.setBackgroundColor(Color.GREEN);
}
else if(item.getTitle().equals("Blue"))
{
view1.setBackgroundColor(Color.BLUE);
}

return super.onOptionsItemSelected(item);
}
}

OUTPUT:

32
Ce373 16CE068

PRACTICAL 5

AIM: Create an application that will display toast (Message) on specific interval of time.
Create an background application that will open activity on specific time. Create an
application that will have spinner with list of animation names. On selecting animation
name, that animation should affect on the images displayed below.
CODE:
Create an application that will display toast (Message) on specific interval of time.
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=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text will display after"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Chronometer
android:id="@+id/timer"
android:layout_width="112dp"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="Chronometer"
android:textAlignment="center"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:text="START"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />

<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="40dp"
android:layout_marginTop="64dp"

33
Ce373 16CE068

android:ems="10"
android:hint="Enter Time"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textView2"
android:layout_width="93dp"
android:layout_height="44dp"
android:layout_marginEnd="264dp"
android:layout_marginTop="64dp"
android:text="Time"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="TIME" />

</android.support.constraint.ConstraintLayout>

activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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=".Main2Activity">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main2" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

34
Ce373 16CE068

MainActivity.java
package com.example.jainilpatel.myapplication;

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.Chronometer;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


EditText e;
Button b;
TextView t;
Chronometer c;
int i=1;
int count=5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e=findViewById(R.id.editText);
b=findViewById(R.id.button);
t=findViewById(R.id.textView);
c=findViewById(R.id.timer);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final int
time=Integer.parseInt(e.getText().toString());
//Toast.makeText(MainActivity.this,
String.valueOf(time), Toast.LENGTH_SHORT).show();

c.start();
c.setOnChronometerTickListener(new
Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer
chronometer) {
t.setText("Your text will be displayed
after "+(time-i)+" Seconds");
i++;
if(time-i==-1&&count>0)
{
Toast.makeText(MainActivity.this,
"MESSAGE DISPLAYED", Toast.LENGTH_SHORT).show();
i=0;
Intent intent=new
Intent(MainActivity.this,Main2Activity.class);

intent.putExtra("time",String.valueOf(time));
startActivity(intent);
count--;
}
if(count==0)
{
c.stop();
}

35
Ce373 16CE068

}
});
}
});
}
}

Main2Activity.java

package com.example.jainilpatel.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

public class Main2Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent i=getIntent();
Integer t=Integer.parseInt(i.getStringExtra("time"));

FloatingActionButton fab = (FloatingActionButton)


findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action",
Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//Do something after ms
Main2Activity.super.onBackPressed();
}
}, t*1000);

//startActivity(new
Intent(Main2Activity.this,MainActivity.class));
}
}

36
Ce373 16CE068

OUTPUT:

37
Ce373 16CE068

CODE:
Create a background application that will open activity on specific time.
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=".MainActivity">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="132dp"
android:text="Start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:text="Stop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
</android.support.constraint.ConstraintLayout>

musicplayer.java
package com.example.jainilpatel.myapplication;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.provider.Settings;
import android.support.annotation.Nullable;

public class musicplayer extends Service{


MediaPlayer player;

@Override
public IBinder onBind(Intent intent) {
return null;
}

public int onStartCommand(Intent intent, int flags, int


startId) {
player=MediaPlayer.create(this,
Settings.System.DEFAULT_RINGTONE_URI);
player.setLooping(true);
player.start();

38
Ce373 16CE068

return START_STICKY;
//return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
player.stop();
}
}

MainActivity.java
package com.example.jainilpatel.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity{

Button start,stop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start=(Button)findViewById(R.id.button);
stop=(Button)findViewById(R.id.button2);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startService(new
Intent(MainActivity.this,musicplayer.class));
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopService(new
Intent(MainActivity.this,musicplayer.class));
}
});
}
}

39
Ce373 16CE068

OUTPUT:

40
Ce373 16CE068

CODE:
Create an application that will have spinner with list of animation names. On selecting
animation name, that animation should affect on the images displayed below.
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=".MainActivity"
android:id="@+id/relativeLayout">

<ImageView
android:id="@+id/imageView"
android:layout_width="318dp"
android:layout_height="328dp"
android:layout_marginBottom="32dp"
android:src="@drawable/i1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />

<Spinner
android:id="@+id/spinner"
android:layout_width="292dp"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

res\anim\alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0"
android:toAlpha="1.0"
android:fillAfter="true"
android:duration="7000"
/>
</set>

res\anim\rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="-360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="7000"

41
Ce373 16CE068

/>
</set>

res\anim\scale.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:pivotY="50%"
android:pivotX="50%"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="2.0"
android:toYScale="2.0"
android:duration="7000"
/>
</set>

res\nim\spin.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:pivotY="50%"
android:pivotX="50%"
android:fromDegrees="0"
android:toDegrees="360"
android:duration="7000"
android:repeatCount="infinite"
/>
</set>

res\nim\translate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="110"
android:fromYDelta="190"
android:toXDelta="-120"
android:toYDelta="0"
android:duration="4000"
android:fillAfter="true"
/>
</set>

MainActivity.java
package com.example. jainilpatel.practical5animations;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;

public class MainActivity extends AppCompatActivity implements


AdapterView.OnItemSelectedListener{
Spinner spnr;

42
Ce373 16CE068

ImageView imgmaster;
ArrayAdapter<CharSequence> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

spnr=(Spinner) findViewById(R.id.spinner);
imgmaster=(ImageView) findViewById(R.id.imageView);

adapter =
ArrayAdapter.createFromResource(this,R.array.Animation,android.R.la
yout.simple_spinner_item);
spnr.setAdapter(adapter);
spnr.setOnItemSelectedListener(this);
}

@Override
public void onItemSelected(AdapterView<?> adapterView, View
view, int position, long l) {
Animation anim =
AnimationUtils.loadAnimation(this,R.anim.alpha);
if(spnr.getSelectedItem().equals("Alpha")){
anim = AnimationUtils.loadAnimation(this,R.anim.alpha);
}
else if(spnr.getSelectedItem().equals("Rotate")){
anim =
AnimationUtils.loadAnimation(this,R.anim.rotate);
}
else if(spnr.getSelectedItem().equals("Scale")){
anim = AnimationUtils.loadAnimation(this,R.anim.scale);
}
else if(spnr.getSelectedItem().equals("Spin")){
anim = AnimationUtils.loadAnimation(this,R.anim.spin);
}
else if(spnr.getSelectedItem().equals("Translate")){
anim =
AnimationUtils.loadAnimation(this,R.anim.translate);
}
imgmaster.startAnimation(anim);
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}
}

43
Ce373 16CE068

OUTPUT:

44
Ce373 16CE068

PRACTICAL 6
AIM: Understanding of UI :
Create an UI such that , one screen have list of all the types of cars.
On selecting of any car name, next screen should show Car details like : name ,
launched date ,company name, images(using gallery) if available, show different
colors in which it is available. Understanding content providers and permissions:
Read phonebook contacts using content providers and display in list. Read
messages from the mobile and display it on the screen.
CODE:
Understanding of UI :
Create an UI such that , one screen have list of all the types of cars.
On selecting of any car name, next screen should show Car details like : name ,
launched date ,company name, images(using gallery) if available, show different
colors in which it is available.

CarMainActivity.java
package com.example. jainilpatel.Ex11;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class CarMainActivity extends ListActivity {


SQLiteDatabase db;
String records[];
int i = 0;

protected void onCreate(Bundle savedInstanceState) {


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

setupDB();

Cursor cur = db.rawQuery("select name from CarDetail", null);


int len = cur.getCount();
records = new String[len];
cur.moveToFirst();
for (int i = 0; i < len; i++) {
records[i] = cur.getString(0);
cur.moveToNext();
}
ArrayAdapter<String> arr = new
ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,
records);
setListAdapter(arr);

private void setupDB() {

45
Ce373 16CE068

db =
openOrCreateDatabase("car.db",SQLiteDatabase.CREATE_IF_NECESSARY,
null);
db.execSQL("create table if not exists CarDetail(id INTEGER PRIMARY
KEY AUTOINCREMENT, name TEXT, launch_dt TEXT,companynm text,colors
Text);");
// db.execSQL("drop table CarDetail");
db.execSQL("delete from CarDetail");
db.execSQL("insert into CarDetail(name,launch_dt,companynm,colors)
values('CAR-A','01/03/1987','A','White, Blue, Red, Light Yellow');");
db.execSQL("insert into CarDetail(name,launch_dt,companynm,colors)
values('CAR-B','15/10/2010','B','Gray, Blue, Black, Maganta');");
db.execSQL("insert into CarDetail(name,launch_dt,companynm,colors)
values('CAR-C','11/11/2011','C','Black,Green,Golden,Red');");

@Override
protected void onListItemClick(ListView l, View v, int position, long
id) {
super.onListItemClick(l, v, position, id);

try {
Intent i = new Intent(v.getContext(), view.class);
i.putExtra("CarName", records[position]);
startActivity(i);
} catch (Exception e) {
Log.e("Ex11", e.getMessage());
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

view.java
package com.example. jainilpatel.Ex11;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;

public class view extends Activity {


TextView tv;
SQLiteDatabase db;
String records[];
private Gallery gallery;

46
Ce373 16CE068

String str = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);

db = openOrCreateDatabase("car.db", SQLiteDatabase.CREATE_IF_NECESSARY,
null);

tv = (TextView) findViewById(R.id.textView1);
gallery = (Gallery) findViewById(R.id.imgGallery);

Intent i = getIntent();
str = i.getStringExtra("CarName");

gallery.setAdapter(new ImageItem(this, str));


String myQuery = "select name,launch_dt,companynm,colors from CarDetail
where name='"
+ str + "'";
Cursor cur = db.rawQuery(myQuery, null);

cur.moveToFirst();
tv.setText("Model: " + cur.getString(0) + "\n\n" + "Launched: "
+ cur.getString(1) + "\n\n" + "Company: " + cur.getString(2)
+ "\n\n" + "Available Colors: " + cur.getString(3));
cur.moveToNext();
}
}

class ImageItem extends BaseAdapter {


int GalItemBg;
private Context cont;
String SelectedCar;
private Integer[] carA = { R.drawable.b1, R.drawable.b2, R.drawable.b3 };
private Integer[] carB = { R.drawable.h1, R.drawable.h2, R.drawable.h3 };
private Integer[] carC = { R.drawable.m1, R.drawable.m2, R.drawable.m3 };

public ImageItem(Context c, String pos) {


cont = c;
SelectedCar = pos;
}

public int getCount() {

if (SelectedCar.equals("CAR-A")) {
return carA.length;
}

else if (SelectedCar.equals("CAR-B")) {
return carB.length;
}

else if (SelectedCar.equals("CAR-C")) {
return carC.length;
} else {
return carA.length;
}
}

public Object getItem(int position) {

47
Ce373 16CE068

return position;
}

public long getItemId(int position) {


return position;
}

public View getView(int position, View convertView, ViewGroup parent) {


ImageView imgView = new ImageView(cont);
if (SelectedCar.equals("CAR-A")) {
imgView.setImageResource(carA[position]);
} else if (SelectedCar.equals("CAR-B")) {
imgView.setImageResource(carB[position]);
} else if (SelectedCar.equals("CAR-C")) {
imgView.setImageResource(carC[position]);
} else {
imgView.setImageResource(carA[position]);
}
// Fixing width & height for image to display
imgView.setLayoutParams(new Gallery.LayoutParams(150, 150));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(GalItemBg);
return imgView;
}
}

activity_main.xml
<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"
tools:context=".MainActivity" >

<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</RelativeLayout>

<ListView
android:id="@+android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" >

</ListView>

</RelativeLayout>

view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

48
Ce373 16CE068

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="199dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />

<Gallery
android:id="@+id/imgGallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />

</RelativeLayout>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package=" com.example. jainilpatel.Ex11"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="CarMainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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


</intent-filter>
</activity>
<activity android:name="view"></activity>
</application>

</manifest>

49
Ce373 16CE068

OUTPUT:

50
Ce373 16CE068

CODE:
Understanding content providers and permissions:
Read phonebook contacts using content providers and display in list.

MainActivity.java
package com.example.jainil.contact;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.PhoneLookup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {


ListView lvContacts;

/** Called when the activity is first created. */


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

lvContacts = (ListView) findViewById(R.id.lvContacts);

Cursor c = managedQuery(Uri.parse("content://contacts/phones"), null, null, null, null);


String names[] = new String[ c.getCount() ];

int i=0;
while (c.moveToNext())
names[i++] = c.getString(c.getColumnIndex(PhoneLookup.DISPLAY_NAME)) + "\n" +
c.getString(c.getColumnIndex(PhoneLookup.NUMBER));

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


names);
lvContacts.setAdapter(adpt);
}
}

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

51
Ce373 16CE068

<ListView android:layout_x="26dp" android:layout_y="34dp" android:layout_width="270dp"


android:layout_height="425dp" android:id="@+id/lvContacts"></ListView>
</AbsoluteLayout>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jainil.contact">
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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


</intent-filter>
</activity>
</application>
</manifest>
OUTPUT:

52
Ce373 16CE068

CODE:
Read messages from the mobile and display it on the screen.

MainActivity.java
package com.example.jainil.sms;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity


{

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView view = new TextView(this);
Uri uriSMS = Uri.parse("content://sms/inbox");
Cursor c = getContentResolver().query(uriSMS, null, null, null,null);
String sms = "";
while (c.moveToNext()) {
sms += "From :" + c.getString(2) + " : " + c.getString(12)+"\n";
}
view.setText(sms);
setContentView(view);
}
}

activitymain_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=".MainActivity">

<TextView
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>

53
Ce373 16CE068

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jainil.sms">
<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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


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

</manifest>

OUTPUT:

54
Ce373 16CE068

PRACTICAL 7
AIM: Create an application to call specific entered number by user in the EditText
Create an application that will create database with table of User credential.
Create an application to read file from asset folder and copy it in memory card.
Create an application that will play a media file from the memory card. Create an
application to make Insert , update , Delete and retrieve operation on the
database.

CODE:
Create an application to call specific entered number by user in the EditText

ActivityMain.java
package com.example.jainil.phonecall;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

Button btnCall;
EditText txtNo;

// @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnCall = (Button) findViewById(R.id.button);


txtNo = (EditText) findViewById(R.id.editText);
btnCall.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {

if (txtNo.getText().toString().equals("")) {
Toast.makeText(MainActivity.this, "Enter Number", Toast.LENGTH_LONG).show();
txtNo.requestFocus();
} else {
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + txtNo.getText().toString()));
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) !=
PackageManager.PERMISSION_GRANTED) {

55
Ce373 16CE068

// TODO: Consider calling


// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
startActivity(callIntent);
}
catch (Exception e)
{
Toast.makeText(MainActivity.this, " "+e.toString(), Toast.LENGTH_LONG).show();
}
}

}
});

}
//@Override
public void onDestroy()
{
super.onDestroy();

}
}

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=".MainActivity"
tools:layout_editor_absoluteY="81dp">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Call"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="142dp"

56
Ce373 16CE068

android:ems="10"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jainil.phonecall">
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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


</intent-filter>
</activity>
</application>
</manifest>
OUTPUT:

57
Ce373 16CE068

CODE:
Create an application that will create database with table of User credential.

Contact.java
package com.example.jainil.jainil2;

public class contact {


int id;
public String name,uname,pass,email;

public int getId() {


return id;
}

public String getName() {


return name;
}

public String getUname() {


return uname;
}

public String getPass() {


return pass;
}

public String getEmail() {


return email;
}

public void setId(int id) {


this.id = id;
}

public void setName(String name) {


this.name = name;
}

public void setUname(String uname) {


this.uname = uname;
}

public void setPass(String pass) {


this.pass = pass;
}

public void setEmail(String email) {


this.email = email;
}
}

Databasehelper.java
package com.example.jainil.jainil2;

58
Ce373 16CE068

import android.content.ContentValues;
import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class databasehelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;


private static final String DATABASE_NAME = "credentialpatelanil.db";
private static final String table_name = "credentialpatelj";
private static final String COLUMN_ID = "id";
private static final String COLUMN_Name = "name";
private static final String COLUMN_UName = "uname";
private static final String COLUMN_Email = "email";
private static final String COLUMN_Pass = "pass";
SQLiteDatabase db;

public databasehelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {


super(context, name, factory, version);
}

public databasehelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version,


DatabaseErrorHandler errorHandler) {
super(context, name, factory, version, errorHandler);
}

public databasehelper(Context context)


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

@Override
public void onCreate(SQLiteDatabase db) {
// db.execSQL("create table "+table_name+" (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT
NULL, uname TEXT NOT NULL, email TEXT NOT NULL, pass TEXT NOT NULL );");
db.execSQL("create table if not exists credentialpatelj(id integer primary key AUTOINCREMENT,name
text,uname text,email text,pass text)");

this.db=db;

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
String query="DROP TABLE IF EXISTS "+table_name;
db.execSQL(query);
this.onCreate(db);
}

public void insertcontact(contact c)


{

59
Ce373 16CE068

db=this.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(COLUMN_Name,c.getName());
values.put(COLUMN_Email,c.getEmail());
values.put(COLUMN_Pass,c.getPass());
values.put(COLUMN_UName,c.getUname());
db.insert(table_name,null,values);
db.close();

}
}

mainactivity.java
package com.example.jainil.jainil2;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {


Button b;
EditText username;
EditText pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b=(Button)findViewById(R.id.button);
username=(EditText)findViewById(R.id.editText);
pass=(EditText)findViewById(R.id.editText2);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,signup.class);
startActivity(intent);
}
});
}

signup.java
package com.example.jainil.jainil2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

60
Ce373 16CE068

import android.widget.EditText;
import android.widget.Toast;

public class signup extends AppCompatActivity {


databasehelper helper=new databasehelper(this);
String namestring;
String usernamestring;
String emailstring;
String passstring;
String confirmstring;
EditText name;
EditText username;
EditText email;
EditText pass;
EditText confirmpass;
Button b;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
name=(EditText)findViewById(R.id.editText7);
username=(EditText)findViewById(R.id.editText6);
email=(EditText)findViewById(R.id.editText5);
pass=(EditText)findViewById(R.id.editText4);
confirmpass=(EditText)findViewById(R.id.editText3);
b=(Button)findViewById(R.id.button3);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
namestring=name.getText().toString();
usernamestring=username.getText().toString();
emailstring=email.getText().toString();
passstring=pass.getText().toString();
confirmstring=confirmpass.getText().toString();
if(!passstring.equals(confirmstring))
{
Toast.makeText(signup.this, "Password doesnot matched", Toast.LENGTH_SHORT).show();
}
else
{
contact c=new contact();
c.setEmail(emailstring);
c.setPass(passstring);
c.setName(namestring);
c.setUname(usernamestring);
helper.insertcontact(c);
Toast.makeText(signup.this,"Data Inserted",Toast.LENGTH_LONG).show();
}
}
});

}
}

61
Ce373 16CE068

activitymain_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=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="17dp"
android:text="Username"
app:layout_constraintBaseline_toBaselineOf="@+id/editText"
app:layout_constraintEnd_toStartOf="@+id/editText"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="18dp"
android:layout_marginTop="9dp"
android:text="Password"
app:layout_constraintEnd_toStartOf="@+id/editText2"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/editText2" />

<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="19dp"
android:layout_marginTop="23dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="19dp"
android:layout_marginTop="25dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"

62
Ce373 16CE068

app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/editText" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Signup"
app:layout_constraintStart_toStartOf="@+id/button2"
app:layout_constraintTop_toBottomOf="@+id/button2" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="33dp"
android:layout_marginTop="30dp"
android:text="signin"
app:layout_constraintStart_toStartOf="@+id/editText2"
app:layout_constraintTop_toBottomOf="@+id/editText2" />
</android.support.constraint.ConstraintLayout>

Activity_signup.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=".signup"
tools:layout_editor_absoluteY="81dp">
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Name"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText7"
android:layout_width="331dp"
android:layout_height="46dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView8" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="22dp"

63
Ce373 16CE068

android:layout_marginStart="8dp"
android:text="Email"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText6" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Last name"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText7" />
<EditText
android:id="@+id/editText6"
android:layout_width="331dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView7" />
<EditText
android:id="@+id/editText5"
android:layout_width="331dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView6" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="confirm password"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText4" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="21dp"
android:layout_marginStart="8dp"
android:text="password"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText5" />
<EditText
android:id="@+id/editText4"
android:layout_width="331dp"
android:layout_height="53dp"
android:layout_marginStart="8dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="parent"

64
Ce373 16CE068

app:layout_constraintTop_toBottomOf="@+id/textView5" />
<EditText
android:id="@+id/editText3"
android:layout_width="331dp"
android:layout_height="46dp"
android:layout_marginStart="8dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
<Button
android:id="@+id/button3"
android:layout_width="86dp"
android:layout_height="wrap_content"
android:layout_marginBottom="172dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Sign up"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.469"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>

OUTPUT:

65
Ce373 16CE068

CODE:
Create an application to read file from asset folder and copy it in memory card.

E16.java
package com.example.jainil.exa;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;
import android.widget.Toast;

public class E16 extends Activity


{
FileOutputStream fos ;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.s1);

try
{
String destPath = "/sdcard/bsr.txt";
File f = new File(destPath);
if (!f.exists())
{
CopyDB( getBaseContext().getAssets().open("file.txt"),new FileOutputStream(destPath));
}
dispFile();
}
catch (Exception e)
{
Toast.makeText(E16.this, " "+e.toString(), Toast.LENGTH_LONG).show();
}
}
public void dispFile()
{
try
{
File fileDir = Environment.getExternalStorageDirectory();
File directory = new File(fileDir.getAbsolutePath());
File file = new File(directory , "bsr.txt");
FileInputStream fis = new FileInputStream(file);
String str = null;
StringBuffer sbuffer = new StringBuffer();
DataInputStream dataio = new DataInputStream(fis);

66
Ce373 16CE068

while((str = dataio.readLine()) != null)


{
sbuffer.append(str + "\n");
}
TextView txt=(TextView)findViewById(R.id.showTxt);
txt.setText(sbuffer);
}
catch (Exception e)
{
Toast.makeText(this, " "+e.toString(), Toast.LENGTH_LONG).show();
}

}
public void CopyDB(InputStream inputStream,OutputStream outputStream)throws IOException
{
//---copy 1K bytes at a time---
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) //check the length of file
{
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}
}

screen1.java
package com.example.jainil.exa;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;
import android.widget.Toast;

public class screen1 extends Activity


{

public void onCreate(Bundle b)


{
super.onCreate(b);
setContentView(R.layout.s1);
try
{
File fileDir = Environment.getExternalStorageDirectory();
File directory = new File(fileDir.getAbsolutePath());
File file = new File(directory , "bsr.txt");
FileInputStream fis = new FileInputStream(file);
String str = null;
StringBuffer sbuffer = new StringBuffer();

67
Ce373 16CE068

DataInputStream dataio = new DataInputStream(fis);


while((str = dataio.readLine()) != null)
{
sbuffer.append(str + "\n");
}
TextView txt=(TextView)findViewById(R.id.showTxt);
txt.setText(sbuffer);
}
catch (Exception e)
{
Toast.makeText(this, " "+e.toString(), Toast.LENGTH_LONG).show();
}
}
}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0" package="bsr.exa">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

<application android:icon="@drawable/icon" android:label="@string/app_name">


<activity android:label="@string/app_name" android:name=".E16">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
</manifest>
OUTPUT:

68
Ce373 16CE068

CODE:
Create an application that will play a media file from the memory card.

MainActivity.java
package com.example.jainil.music;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

MediaPlayer player=null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start=(Button)findViewById(R.id.button1);
Button stop=(Button)findViewById(R.id.button2);
getSystemService(Context.AUDIO_SERVICE);
start.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0)
{

try
{
player=null;
player=new MediaPlayer();
String audioFilePath="/sdcard/a.mid";
player.setDataSource(audioFilePath);
player.prepare();
player.start();
}
catch(Exception e)
{
Toast.makeText(MainActivity.this,""+e,Toast.LENGTH_LONG).show();

}
}
});
stop.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0)
{
player.stop();
}
});

69
Ce373 16CE068

}
}

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=".MainActivity">

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:text="Start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:text="Stop"
app:layout_constraintStart_toStartOf="@+id/button1"
app:layout_constraintTop_toBottomOf="@+id/button1" />
</android.support.constraint.ConstraintLayout>

OUTPUT:

70
Ce373 16CE068

CODE:
Create an application to make Insert , update , Delete and retrieve operation on the
database..

MainActivity.java
package com.example.jainil.sqllite;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.database.Cursor;
//import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

DatabaseHelper myDb;
EditText editName,editSurname,editMarks ,editTextId;
Button btnAddData;
Button btnviewAll;
Button btnDelete;

Button btnviewUpdate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);

editName = (EditText)findViewById(R.id.editText_name);
editSurname = (EditText)findViewById(R.id.editText_surname);
editMarks = (EditText)findViewById(R.id.editText_Marks);
editTextId = (EditText)findViewById(R.id.editText_id);
btnAddData = (Button)findViewById(R.id.button_add);
btnviewAll = (Button)findViewById(R.id.button_viewAll);
btnviewUpdate= (Button)findViewById(R.id.button_update);
btnDelete= (Button)findViewById(R.id.button_delete);
AddData();
viewAll();
UpdateData();
DeleteData();
}
public void DeleteData() {
btnDelete.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Integer deletedRows = myDb.deleteData(editTextId.getText().toString());

71
Ce373 16CE068

if(deletedRows > 0)
Toast.makeText(MainActivity.this,"Data Deleted",Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this,"Data not Deleted",Toast.LENGTH_LONG).show();
}
}
);
}
public void UpdateData() {
btnviewUpdate.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isUpdate = myDb.updateData(editTextId.getText().toString(),
editName.getText().toString(),
editSurname.getText().toString(),editMarks.getText().toString());
if(isUpdate == true)
Toast.makeText(MainActivity.this,"Data Update",Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this,"Data not Updated",Toast.LENGTH_LONG).show();
}
}
);
}
public void AddData() {
btnAddData.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isInserted = myDb.insertData(editName.getText().toString(),
editSurname.getText().toString(),
editMarks.getText().toString() );
if(isInserted == true)
Toast.makeText(MainActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this,"Data not Inserted",Toast.LENGTH_LONG).show();
}
}
);
}

public void viewAll() {


btnviewAll.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor res = myDb.getAllData();
if(res.getCount() == 0) {
// show message
showMessage("Error","Nothing found");
return;
}

StringBuffer buffer = new StringBuffer();


while (res.moveToNext()) {
buffer.append("Id :"+ res.getString(0)+"\n");
buffer.append("Name :"+ res.getString(1)+"\n");

72
Ce373 16CE068

buffer.append("Surname :"+ res.getString(2)+"\n");


buffer.append("Marks :"+ res.getString(3)+"\n\n");
}

// Show all data


showMessage("Data",buffer.toString());
}
}
);
}

public void showMessage(String title,String Message){


AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}

// @Override
// public boolean onCreateOptionsMenu(Menu menu) {
//// // Inflate the menu; this adds items to the action bar if it is present.
//// getMenuInflater().inflate(R.menu.menu_main, menu);
//// return true;
//// }
////
//// @Override
//// public boolean onOptionsItemSelected(MenuItem item) {
//// // Handle action bar item clicks here. The action bar will
//// // automatically handle clicks on the Home/Up button, so long
//// // as you specify a parent activity in AndroidManifest.xml.
//// int id = item.getItemId();
////
//// //noinspection SimplifiableIfStatement
//// if (id == R.id.action_settings) {
//// return true;
//// }
////
//// return super.onOptionsItemSelected(item);
//// }
}

DatabaseHelper.java
package com.example.jainil.sqllite;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
* Created by ProgrammingKnowledge on 4/3/2015.

73
Ce373 16CE068

*/
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Student.db";
public static final String TABLE_NAME = "student_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "NAME";
public static final String COL_3 = "SURNAME";
public static final String COL_4 = "MARKS";

public DatabaseHelper(Context context) {


super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME
TEXT,SURNAME TEXT,MARKS INTEGER)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}

public boolean insertData(String name,String surname,String marks) {


SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3,surname);
contentValues.put(COL_4,marks);
long result = db.insert(TABLE_NAME,null ,contentValues);
if(result == -1)
return false;
else
return true;
}

public Cursor getAllData() {


SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
return res;
}

public boolean updateData(String id,String name,String surname,String marks) {


SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1,id);
contentValues.put(COL_2,name);
contentValues.put(COL_3,surname);
contentValues.put(COL_4,marks);
db.update(TABLE_NAME, contentValues, "ID = ?",new String[] { id });
return true;
}

public Integer deleteData (String id) {


SQLiteDatabase db = this.getWritableDatabase();

74
Ce373 16CE068

return db.delete(TABLE_NAME, "ID = ?",new String[] {id});


}
}

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"
tools:context=".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:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/editText_name"
android:text="Surname"
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/editText_surname"
android:text="Marks"
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:id="@+id/editText_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView"
android:layout_toEndOf="@+id/textView"
android:layout_toRightOf="@+id/textView" />

<EditText
android:id="@+id/editText_surname"
android:layout_width="match_parent"
android:layout_height="wrap_content"

75
Ce373 16CE068

android:layout_alignTop="@+id/textView2"
android:layout_toEndOf="@+id/textView2"
android:layout_toRightOf="@+id/textView2" />

<EditText
android:id="@+id/editText_Marks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editText_surname"
android:layout_toEndOf="@+id/textView3"
android:layout_toRightOf="@+id/textView3" />

<Button
android:id="@+id/button_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/editText_Marks"
android:layout_marginTop="76dp"
android:text="Add Data" />

<Button
android:id="@+id/button_viewAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button_update"
android:layout_centerHorizontal="true"
android:text="View All" />

<Button
android:id="@+id/button_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/button_add"
android:text="Update" />

<Button
android:id="@+id/button_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button_viewAll"
android:layout_alignStart="@+id/button_viewAll"
android:layout_below="@+id/button_viewAll"
android:layout_centerVertical="true"
android:text="Delete" />

<TextView
android:id="@+id/textView_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/editText_Marks"

76
Ce373 16CE068

android:text="id"
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:id="@+id/editText_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView_id"
android:layout_toEndOf="@+id/textView3"
android:layout_toRightOf="@+id/textView3" />

</RelativeLayout>

OUTPUT:

77
Ce373 16CE068

PRACTICAL 8
AIM: Create an application to read file from the sdcard and display that file content to
the screen. Create an application to draw line on the screen as user drag his
finger. Create an application to send message between two emulators. Create an
application to take picture using native application.

CODE:
Create an application to read file from the sdcard and display that file content to
the screen

MainActivity.java
package com.example.jainil.fileinsdcard;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

public void onCreate(Bundle b)


{
super.onCreate(b);
setContentView(R.layout.activity_main);
try
{
File fileDir = Environment.getExternalStorageDirectory();
File directory = new File(fileDir.getAbsolutePath());
File file = new File(directory , "jainil.txt");
FileInputStream fis = new FileInputStream(file);
String str = null;
StringBuffer sbuffer = new StringBuffer();
DataInputStream dataio = new DataInputStream(fis);
while((str = dataio.readLine()) != null)
{
sbuffer.append(str + "\n");
}
TextView txt=(TextView)findViewById(R.id.showTxt);
txt.setText(sbuffer);
}
catch (Exception e)
{
Toast.makeText(this, " "+e.toString(), Toast.LENGTH_LONG).show();
}
}
}

78
Ce373 16CE068

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=".MainActivity">

<TextView
android:id="@+id/showTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

OUTPUT:

79
Ce373 16CE068

CODE:
Create an application to draw line on the screen as user drag his finger.
MainActivity.java
package com.example.jainil.drawing;

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

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
setContentView(new ExploreTouchEvent(this, null));
}
}

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=".MainActivity">

<TextView
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>

ExploreTouchEvent.java
package com.example.jainil.drawing;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

80
Ce373 16CE068

public class ExploreTouchEvent extends View {


private Paint paint = new Paint();
private Path path = new Path();

public ExploreTouchEvent(Context context, AttributeSet attrs) {


super(context, attrs);

paint.setAntiAlias(true);
paint.setStrokeWidth(6f);
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
}

@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(eventX, eventY);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(eventX, eventY);
break;
case MotionEvent.ACTION_UP:
// nothing to do
break;
default:
return false;
}

// Schedules a repaint.
invalidate();
return true;
}
}

81
Ce373 16CE068

OUTPUT:

82
Ce373 16CE068

CODE:
Create an application to send message between two emulators

MainActivity.java
package com.example.jainil.emulator;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

Button btnSendSMS;
EditText txtPhoneNo;
EditText txtMessage;

//@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSendSMS = (Button) findViewById(R.id.button);
txtPhoneNo = (EditText) findViewById(R.id.editText);
txtMessage = (EditText) findViewById(R.id.editText2);

/*
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "Content of the SMS goes here...");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
*/

btnSendSMS.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String phoneNo = txtPhoneNo.getText().toString();
String message = txtMessage.getText().toString();
if (phoneNo.length()>0 && message.length()>0)
sendSMS(phoneNo, message);
else
Toast.makeText(getBaseContext(),"Please enter both phone number and message.",
Toast.LENGTH_SHORT).show();
}
});
}

//---sends a SMS message to another device---

83
Ce373 16CE068

private void sendSMS(String phoneNumber, String message)


{

String SENT = "SMS_SENT";


String DELIVERED = "SMS_DELIVERED";

PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT),0);

PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);

//---when the SMS has been sent---


registerReceiver(new BroadcastReceiver(){
// @Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));

//---when the SMS has been delivered---


registerReceiver(new BroadcastReceiver(){
//@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered", Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));

SmsManager sms = SmsManager.getDefault();


sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
}

84
Ce373 16CE068

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=".MainActivity">

<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="79dp"
android:ems="10"
android:hint="phone no"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="49dp"
android:ems="10"
android:hint="message"
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="@+id/editText"
app:layout_constraintTop_toBottomOf="@+id/editText" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:text="send"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText2" />
</android.support.constraint.ConstraintLayout>

SmsReceiver.java
package com.example.jainil.emulator;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

85
Ce373 16CE068

import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SmsReceiver extends BroadcastReceiver


{

public void onReceive(Context context, Intent intent)


{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}

AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jainil.emulator">

<uses-permission android:name="android.permission.READ_SMS" />


<uses-permission android:name="android.permission.SEND_SMS" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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


</intent-filter>

86
Ce373 16CE068

</activity>
<activity android:name=".SmsReceiver"></activity>
</application>

</manifest>

OUTPUT:

87
Ce373 16CE068

CODE:
Create an application to take picture using native application.

MainActivity.java
package com.example.jainil.camera;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {


/** Called when the activity is first created. */
ImageView iv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView)findViewById(R.id.imageView1);
Button b =(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {


Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, 0);
}
});
}
protected void OnActivityResult(int requestCode,int resultcode,Intent data)
{
Bitmap bm=(Bitmap)data.getExtras().get("data");
iv.setImageBitmap(bm);
}
}

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=".MainActivity">

<ImageView
android:id="@+id/imageView1"
android:layout_width="290dp"
android:layout_height="0dp"
android:layout_marginBottom="30dp"

88
Ce373 16CE068

android:layout_marginEnd="35dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toTopOf="@+id/button1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="74dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="click photos"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView1" />
</android.support.constraint.ConstraintLayout>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jainil.camera">
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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


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

</manifest>

89
Ce373 16CE068

OUTPUT:

90
Ce373 16CE068

PRACTICAL 9
AIM: Create an application to pick up any image from the native application gallery and
display it on the screen. Create an application to open any URL inside the
application and clicking on any link from that URl should not open Native browser
but that URL should open the same screen.

CODE:
Create an application to pick up any image from the native application gallery and
display it on the screen

MainActivity.java
package com.example.jainil.imagegallery;

import java.io.File;
import java.io.FileInputStream;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
try
{

String[] projection={Media.DISPLAY_NAME, Media.DATA, Media.SIZE};


Cursor c=managedQuery(Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
ImageView ivImage=(ImageView)findViewById(R.id.myImg);
File file = null;
String imgName = null ;

if(c.getCount()>0)
{
while(c.moveToNext())
{
file=new File(c.getString(1)); //after completing loop, it take last image from native Gallery to
display.
imgName=c.getString(0);
}
c.close();

91
Ce373 16CE068

FileInputStream fis=new FileInputStream(file);


byte[] buffer=new byte[fis.available()];
fis.read(buffer);
Bitmap bm=BitmapFactory.decodeByteArray(buffer, 0, buffer.length);

TextView txtTitle=(TextView)findViewById(R.id.txtTitle);
txtTitle.setText(imgName.toString());
ivImage.setImageBitmap(bm);
}
}
catch(Exception e)
{
Toast.makeText(MainActivity.this, "Error: "+e, Toast.LENGTH_LONG).show();
}
}
}

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=".MainActivity">

<ImageView
android:id="@+id/myImg"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="34dp"
android:layout_marginEnd="15dp"
android:layout_marginStart="15dp"
android:layout_marginTop="16dp"
app:layout_constraintBottom_toTopOf="@+id/txtTitle"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="103dp"
android:text="name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/myImg" />
</android.support.constraint.ConstraintLayout>

92
Ce373 16CE068

OUTPUT:

93
Ce373 16CE068

CODE:
Create an application to open any URL inside the application and clicking on any link from
that URl should not open Native browser but that URL should open the same screen.

MainActivity.java
package com.example.jainil.url;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WebView wv=(WebView) findViewById(R.id.webView1);
final Button btnGo=(Button) findViewById(R.id.button1);
final EditText etURI=(EditText) findViewById(R.id.editText1);

btnGo.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

wv.loadUrl(etURI.getText().toString());
}
} );
}
}

Activity_main.xml
<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"
tools:context=".Ex24Activity" >

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/webView1"
android:layout_alignParentLeft="true"

94
Ce373 16CE068

android:layout_alignParentTop="true"
android:layout_alignRight="@+id/textView1"
android:ems="10" >

<requestFocus />
</EditText>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView1"
android:text="Go" />

<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/textView1"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Made by jainil" />

</RelativeLayout>

OUTPUT:

95

Potrebbero piacerti anche