Android Mobile Application Development
Android Mobile Application Development
1
STEP 3: DOWNLOAD ANDROID STUDIO
The Android Software Development Kit (referred to in this License Agreement as the "SDK"
and specifically including the Android system files, packaged APIs, and Google APIs add-ons)
is licensed to you subject to the terms of this License Agreement. This License Agreement
forms a legally binding contract between you and Google in relation to your use of the SDK.
You can install android studio by following link:
http://developer.android.com/sdk/index.html#top
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
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
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
INSTALL THE PACKAGES
2. In the next window, double-click each package name on the left to accept the license agreement for
each.
3. Click Install.
The download progress is shown at the bottom of the SDK Manager window. Do not exit the SDK
Manager or it will cancel the download.
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
Figure: Show The Android Project View.
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
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
For more information, see Managing AVDs.
Android Activity Lifecycle
Android Activity Lifecycle is controlled by 7 methods of android.app.Activity class.
The android Activity is the subclass of ContextThemeWrapper class.
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
9
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
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
}
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
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
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
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
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
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
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
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
{
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
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
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
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
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
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
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
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
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"
28
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
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
30
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:id="@+id/mainactivity"
android:layout_height="match_parent"
tools:context="com.example.jainilpatel.menu.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Created by Jainil Patel(16CE068)"
android:textSize="20sp"
android:textAlignment="center"/>
</LinearLayout>
MainActivity.java
package com.example.jainilpatel.menu;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getTitle().equals("Red")){
view1.setBackgroundColor(Color.RED);
}
if(item.getTitle().equals("Blue")){
view1.setBackgroundColor(Color.BLUE);
}
if(item.getTitle().equals("Yellow")){
31
view1.setBackgroundColor(Color.YELLOW);
}
return super.onOptionsItemSelected(item);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Red">Red</item>
<item android:title="Blue">Blue</item>
<item android:title="Yellow">Yellow</item>
</menu>
OUTPUT:
32
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
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
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
}
});
}
});
}
}
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));
}
}
OUTPUT:
36
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">
37
<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;
}
MainActivity.java
38
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));
}
});
}
}
OUTPUT:
39
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"
/>
</set>
40
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;
41
@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) {
}
}
42
OUTPUT:
43