Charusat Android Practical List.
Charusat Android Practical List.
Charusat Android Practical List.
Practical-1
1
Ce373 16CE068
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
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:
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:
As a minimum when setting up the Android SDK, you should download the latest tools and Android
platform:
2. Open the first Android X.X folder (the latest version) and select:
o SDK Platform
Android Wear
Android TV
3
Ce373 16CE068
Google Cast
Navigation drawer
Swipe views
Google Maps
Google Cast
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
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.
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
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.
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.
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
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
onResume called when activity will start interacting with the user.
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;
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
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;
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;
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();
}
}
}
);
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);
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;
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();
}
}
}
);
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);
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();
}
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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;
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);
}
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();
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;
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
@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;
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>
<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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@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>
<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;
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;
@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"));
//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;
@Override
public IBinder onBind(Intent intent) {
return null;
}
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;
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;
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;
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;
46
Ce373 16CE068
@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");
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();
}
}
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;
}
}
47
Ce373 16CE068
return position;
}
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" />
</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;
int i=0;
while (c.moveToNext())
names[i++] = c.getString(c.getColumnIndex(PhoneLookup.DISPLAY_NAME)) + "\n" +
c.getString(c.getColumnIndex(PhoneLookup.NUMBER));
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
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" />
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;
@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" />
</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;
Button btnCall;
EditText txtNo;
// @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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
}
});
}
//@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" />
57
Ce373 16CE068
CODE:
Create an application that will create database with table of User credential.
Contact.java
package com.example.jainil.jainil2;
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;
@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);
}
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;
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;
@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;
@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
}
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;
67
Ce373 16CE068
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>
</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;
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;
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();
}
}
);
}
72
Ce373 16CE068
// @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";
@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);
}
74
Ce373 16CE068
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;
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;
@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
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;
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();
}
});
}
83
Ce373 16CE068
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;
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jainil.emulator">
<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" />
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;
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" />
</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;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try
{
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
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;
@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