Sample Android Practical File
Sample Android Practical File
FILE OF
Android Programming
(UGCA1944)
1|GGNIMT
Index
Sr. No. Practical Page No.
1. Installation of Java, android Framework 03 – 04
2. Android SDK Manager and its all components 05
Programs based on the overriding, constructor, classes in 06
3.
Java
Directory Structure of an Android Project, Common 07 – 10
4.
Default Resources Folders, Leveraging Android XML.
5. Applications based on Text Boxes and Button 11 – 15
6. Applications based on Radio Buttons 16 – 18
7. Applications based on Intents 19 – 22
8. Applications based on Activities and services 23 – 29
9. Applications based on Animations 38 – 44
10. Applications based on ListView 47 – 49
11. Applications based on CheckBox 50 – 52
12. Applications based on Rating Bar 53 – 54
2|GGNIMT
Practical 1
Installation of Android Studio
System Requirements
1. Android application development may be performed on any of the following system
types:
2. Windows 7/8/10 (32-bit or 64-bit)
3. macOS 10.10 or later (Intel based systems only)
4. Linux systems with version 2.19 or later of GNU C Library (glibc) Minimum of 3GB
of RAM (8GB is preferred)
5. Approximately 4GB of available disk space
6. 1280 x 800 minimum screen resolution
Downloading the Android Studio Package: The latest release of Android Studio may be
downloaded from the primary download page which can be found at the following URL:
[Link]
Installation on Windows
1. Locate the downloaded Android Studio installation executable file (named android-studio-
bundle-<version>.exe) in a Windows Explorer window and double-click on it to start the
installation process, clicking the Yes button in the User Account Control dialog if it appears.
2. Once the Android Studio setup wizard appears, work through the various screens to configure
the installation to meet your requirements in terms of the file system location into which Android
Studio should be installed and whether or not it should be made available to other users of the
system. When prompted to select the components to install, make sure that the Android Studio,
Android SDK and Android Virtual Device options are all selected.
3. Although there are no strict rules on where Android Studio should be installed on the system, the
installation was performed into C:\Program Files\Android\Android Studio and that the Android
SDK packages have been installed into the user’s AppData\Local\Android\sdk sub- folder. Once
the options have been configured, click on the Install button to begin the installation process.
4. On versions of Windows with a Start menu, the newly installed Android Studio can belaunched
from the entry added to that menu during the installation. The executable may be pinned to the
task bar for easy access by navigating to the Android Studio\bin directory, right-clicking on the
executable and selecting the Pin to Taskbar menu option. Note that theexecutable is provided in
32-bit (studio) and 64-bit (studio64) executable versions. If you are running a 32-bit system be
sure to use the studio executable
3|GGNIMT
Installation on Linux
1. Having downloaded the Linux Android Studio package, open a terminal window, change
directory to the location where Android Studio is to be installed and execute the following
command:
2. unzip /<path to package>/android-studio-ide-<version>[Link]
Note that the Android Studio bundle will be installed into a sub-directory named
android-studio. Assuming, therefore, that the above command was executed in
/home/demo, the software packages will be unpacked into/home/demo/android-studio.
3. To launch Android Studio, open a terminal window, change directory to the android-studio/bin
sub-directory and execute the following command:
./[Link]
When running on a 64-bit Linux system, it will be necessary to install some 32-bit support libraries
before Android Studio will run. On Ubuntu these libraries can be installed using the following
command:
sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386 lib32z1 libbz:1.0:i386
On RedHat and Fedora based 64-bit systems, use the following command:
sudo yum install zlib.i686 ncurses-libs.i686 bzip2-libs.i686
4|GGNIMT
Practical 2
Android SDK Manager and its all components
• Android SDK
It is a collection of software development tools and libraries which are required for building any
android application in any IDE whether it may be android studio, eclipse or any other. When
Google releases any new features in a specific android version or releases a new version for
android then they also release a new SDK version for it which consist of that updated features
within it. These SDK tools are essential for developing any android application. Android SDK can
be installed in any system whether it may be windows, Mac OS or Linux.
• SDK Platform
Whenever a newer version of android is released such as Android 9,10 or it may be also called as
Android Oreo, Android Marshmallow or others. New SDK platform tools are also available to
download in SDK Platform tools. Each SDK platform tools package consists of android platform
and sources pertaining to an API level by default. The latest package of SDK Platform tools comes
with the latest updates related to android such as Android Tiramisu comes with updated clipboard
manager and we can also get to see other updated features in the latest SDK Platform tools.
• SDK Tools
SDK tools is a package of many essential tools which are required for development of any android
application. SDK tools consist of several different components such as Android SDK Build
Tools,NDK,Android SDK Command Line Tools, CMake and other. When any new update is
released by google for the updates of these tools. These tools are updated automatically to get the
latest one.
• Android Emulator
Android Emulator is a virtual android mobile device which is provided by android studio IDE. We
can use this virtual device to run our android studio applications on it. We can generate so many
types of different emulators depending on our requirements which might have different android
versions. With the help of an android emulator we can test our android application on a wide range
of devices. With the help of emulators we can create applications for mobile devices, watches,
tablets as well as smart televisions without the requirements of the actual device.
5|GGNIMT
Practical 3
Programs based on the overriding, constructor, classes in Java
void show()
{
[Link]("constructor show()");
}
}
class Child extends constructor{
@Override
void show()
{
[Link]("Child show()");
}
}
class Main
{
OUTPUT:
6|GGNIMT
Practical 4
Directory Structure of an Android Project, Common Default Resources
Folders, Leveraging Android XML
• src: It contains the packages and the java files under the packages.
• gen: It is generated automatically. It contains the reference of the GUI components used in
XML and other resources.
• Android (version): It contains a jar file ([Link]) which have all the class available for
current version.
• Android Dependencies: It also contains a jar file, but dependencies only. Main contents
are available in Android (version) directory.
• assets: It contains the HTML files (if used) in the project.
• bin: When the project is compiled and ready to be installed, then the files are transferred from
the project and a copy of all assets and resources are stored in this directory. Here you can find
the apk file which can be sent to any supporting android device and the application (if
compiled correctly) can be installed.
• libs: Contains private libraries, if any.
• res: Acronym for resource directory. It contains many sub-directories.
• drawable-hdpi: It contains images of High dpi, i.e. the size of image is 72×72 px.
• drawable-ldpi: It contains images of low dpi, i.e. image size of 36×36 px.
• drawable-mdpi: It contains images of medium dpi, i.e. image size of 48×48 px.
• drawable-xhdpi: It contains images of extra high dpi, i.e. image size of 96×96 px.
These drawable directories are used to store images only. And, the category is created, so
that, a single application can support multiple screens. In a manner of saying, an
application can run on mobile device of small screen or it can run on tables and television
having large screen.
• layout: It contains the XML files responsible for generating a GUI for activities.
• menu: It contains XML files for menu options.
• values: It contains XML files to hold values, which are globally available for applications. It
may be string, array, or color.
• anim: It contains XML files that represents animation objects.
• xml: It contains XML files representing miscellaneous application component configuration.
• [Link]: It contains information about the application. The information
may include:
Package information,
Version Code and name,
Minimum SDK required for the application,
Target SDK version for the application,
Icon of project,
Information about Activities, services, content providers, and intent receivers,
Permission(s) it needs at user end,
7|GGNIMT
List of libraries to which the application is linked.
[Link]: This file helps in maintaining version control system. It stores the project
settings as mentioned in [Link].
• [Link]: The [Link] is an XML file which is used to store the colors for the [Link]
Android project contains 3 essential colours namely:
colorPrimary
colorPrimaryDark
colorAccent
These colors are used in some predefined resources of the android studio as well. These colors as
needed to be set opaque otherwise it could result in some exceptions to arise.
Below mentioned is the implementation of [Link] resource:
Note: It is also possible to define different user based colors for different types of resources.
• [Link]: The [Link] is used for defining the dimensions for different widgets to be
included in the Android project. It is a good coding practice to use [Link] to define a
dimension rather than just writing the dimension in the resource, due to the fact that if ever any
need arises to change the dimension, instead of making a change to all, only the [Link] can be
changed once and the change is reflected in all.
Below mentioned is the implementation of [Link] resource:
It is also possible to apply user-defined dimensions.
Note: Always remember the difference in using dp or sp. Generally use sp for font size and dp for
others.
• [Link]: One of the most important as well as widely used values file is the [Link] due to
its applicability in the Android project. Basic function of the [Link] is to define the strings in
one file so that it is easy to use same string in different positions in the android project plus it
makes the project looks less messy.
We can also define an array in this file as well.
Below mentioned is the implementation of [Link] resource:
Android studio gives a warning in layout xml if a string is used in that file, thus it is a good
practice to store all hardcoded strings in [Link] file.
• [Link]: Another important file in the values folder is the [Link] where all the themes of the
Android project are defined. The base theme is given by default having the option to customize or
make changes to the customized theme as well. Every theme has a parent attribute which defines
the base of the theme. There are a lot of options to choose from depending on the need of the
Android project.
8|GGNIMT
➢ Leveraging Android XML:
Using Android's XML vocabulary, you can quickly design UI layouts and the screen elements they
contain, in the same way you create web pages in HTML — with a series of nested elements.
Each layout file must contain exactly one root element, which must be a View or ViewGroup
object. Once you've defined the root element, you can add additional layout objects or widgets
as child elements to gradually build a View hierarchy that defines your layout.
Example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>
When you compile your app, each XML layout file is compiled into a View resource. You should
load the layout resource from your app code, in your [Link]() callback implementation.
Do so by calling setContentView(), passing it the reference to your layout resource in the form of:
[Link].layout_file_name. For example, if your XML layout is saved as main_layout.xml, you
would load it for your Activity.
Example:
fun onCreate(savedInstanceState: Bundle) {
[Link](savedInstanceState)
setContentView([Link].main_layout)
}
The onCreate() callback method in your Activity is called by the Android framework when
you’re activity is launched.
XML Methods:
1. getAttributeCount()
This method just Returns the number of attributes of the current start tag
9|GGNIMT
2. getAttributeName(int index)
This method returns the name of the attribute specified by the index value
3. getColumnNumber()
This method returns the Returns the current column number, starting from 0.
4. getDepth()
5. getLineNumber()
6. getNamespace()
This method returns the name space URI of the current element.
7. getPrefix()
8. getName()
9. getText()
10. isWhitespace()
This method checks whether the current TEXT event contains only whitespace characters
10 | G G N I M T
Practical 5
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Akash Calculator"
android:textStyle="bold" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:ems="10" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_marginTop="33dp"
android:layout_toRightOf="@+id/textView1"
11 | G G N I M T
android:text="Result"
android:textSize="30sp"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_centerHorizontal="true"
android:text="+" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_alignParentRight="true"
android:layout_marginRight="14dp"
android:text="-" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="36dp"
android:text="X" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button4"
android:layout_alignRight="@+id/button2"
android:text="/" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView2"
android:layout_marginTop="40dp"
android:text="Cls" />
</RelativeLayout>
12 | G G N I M T
Main Activity Coding
package [Link].calculator1;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
EditText t1;
EditText t2;
Button add;
Button sub;
Button mul;
Button div;
Button cls;
TextView res;
int a,b,c;
13 | G G N I M T
t1=(EditText)findViewById([Link].editText1);
t2=(EditText)findViewById([Link].editText2);
res=(TextView)findViewById([Link].textView2);
add=(Button)findViewById([Link].button2);
sub=(Button)findViewById([Link].button3);
mul=(Button)findViewById([Link].button4);
div=(Button)findViewById([Link].button5);
cls=(Button)findViewById([Link].button1);
[Link](new [Link]() {
[Link](color.background_light);
[Link]([Link](c));
}
});
[Link](new [Link]() {
[Link](color.holo_green_light);
[Link]([Link](c));
}
});
[Link](new [Link]() {
[Link](color.holo_green_light);
[Link]([Link](c));
}
});
[Link](new [Link]() {
14 | G G N I M T
public void onClick(View arg0)
{
a=[Link]([Link]().toString());
b=[Link]([Link]().toString());
c=a/b;
[Link](color.holo_green_light);
[Link]([Link](c));
}
});
[Link](new [Link]() {
[Link]("");
[Link]("");
[Link]("");
}
});
}
Output:
15 | G G N I M T
Practical 6
Applications based on Radio Buttons
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity
{
private RadioGroup radioGroup;
private RadioButton sound, vibration, silent; private Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
[Link](savedInstanceState);
setContentView([Link].activity_main);
radioGroup = (RadioGroup) findViewById([Link]);
[Link](new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
// find which radio button is selected
if(checkedId == [Link])
{
[Link](getApplicationContext(), "choice: Silent",
Toast.LENGTH_SHORT).show();
}
else if(checkedId == [Link])
{
[Link](getApplicationContext(), "choice: Sound",
Toast.LENGTH_SHORT).show();
}
Else
{
[Link](getApplicationContext(), "choice: Vibration",
Toast.LENGTH_SHORT).show();
}
}
});
sound = (RadioButton) findViewById([Link]);
16 | G G N I M T
vibration = (RadioButton) findViewById([Link]);
silent = (RadioButton) findViewById([Link]);
textView = (TextView) findViewById([Link].textView1);
button = (Button)findViewById([Link].button1);
[Link](new OnClickListener()
{
@Override
public void onClick(View v) {
int selectedId = [Link]();
// find which radioButton is checked by id
if(selectedId == [Link]())
{
[Link]("You chose 'Sound' option");
} else if(selectedId == [Link]())
{
[Link]("You chose 'Vibration' option");
}
else
{
[Link]("You chose 'Silent' option");
}
}
});
}}
Layout Coding
<RelativeLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<RadioGroup
android:id="@+id/myRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginLeft="27dp"
android:layout_marginTop="28dp" >
<RadioButton
android:id="@+id/sound"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Sound" />
<RadioButton
android:id="@+id/vibrate"
17 | G G N I M T
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vibration" />
<RadioButton
android:id="@+id/silent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Silent" />
</RadioGroup>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Choose one of the following modes"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/myRadioGroup"
android:layout_below="@+id/myRadioGroup"
android:layout_marginLeft="42dp"
android:layout_marginTop="53dp"
android:text="choose" />
</RelativeLayout>
Output:
18 | G G N I M T
Practical 7
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="77dp"
android:ems="10"
android:hint="Name" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="53dp"
android:hint="Password" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:hint="Name"
android:onClick="callCheck"
android:text="Login" />
</RelativeLayout>
19 | G G N I M T
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
name=(EditText)findViewById([Link].editText1);
pass=(EditText)findViewById([Link].editText2);
btn =(Button)findViewById([Link].button1);
}
public void callCheck(View v)
{
if(([Link]().toString()).equalsIgnoreCase("Admin")&&([Link]().toString()).equalsIgn
oreCase("123456"))
{
Intent i = new Intent(this,[Link]);
[Link]("UserName", [Link]().toString());
startActivity(i);
}
else
{
[Link](getBaseContext(), "either name or password is
incorrect",Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate([Link], menu);
return true;
}
}
20 | G G N I M T
Code of Second Page Activity:
[Link]
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="Male" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioButton1"
android:layout_centerVertical="true"
android:text="Female" />
</RelativeLayout>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
Output:
22 | G G N I M T
Practical 8
Applications based on Activities and services
Tic Tac Toe game in android
Layout Code
<RelativeLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="TIC TAC TOE"
android:textColor="#00008B"
android:textSize="15sp"
android:textStyle="italic" />
<Button
android:id="@+id/btn2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btn1"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@+id/btn1"
android:tag="2"
android:textSize="35sp"
android:text="" />
<Button
android:id="@+id/btn3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn0"
android:layout_below="@+id/btn0"
android:layout_marginTop="33dp"
android:tag="3"
android:textSize="35sp"
android:text="" />
23 | G G N I M T
<Button
android:id="@+id/btn4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn1"
android:layout_alignTop="@+id/btn3"
android:tag="4"
android:textSize="35sp"
android:text="" />
<Button
android:id="@+id/btn5"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn2"
android:layout_alignTop="@+id/btn4"
android:tag="5"
android:textSize="35sp"
android:text="" />
<Button
android:id="@+id/btn6"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/btn3"
android:layout_below="@+id/btn3"
android:layout_marginTop="36dp"
android:tag="6"
android:textSize="35sp"
android:text="" />
<Button
android:id="@+id/btn8"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn7"
android:layout_alignBottom="@+id/btn7"
android:layout_alignLeft="@+id/btn5"
android:tag="8"
android:textSize="35sp"
android:text="" />
<Button
android:id="@+id/btn7"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn6"
android:layout_alignBottom="@+id/btn6"
android:layout_alignLeft="@+id/btn4"
24 | G G N I M T
android:tag="7"
android:textSize="35sp"
android:text="" />
<Button
android:id="@+id/btn1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btn3"
android:layout_marginLeft="25dp"
android:layout_toRightOf="@+id/btn0"
android:tag="1"
android:textSize="35sp" />
<Button
android:id="@+id/btn0"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginLeft="27dp"
android:layout_marginTop="58dp"
android:tag="0"
android:textSize="35sp" />
</RelativeLayout>
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
25 | G G N I M T
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
tv=(TextView)findViewById([Link].textView1);
btn0=(Button)findViewById([Link].btn0);
btn1=(Button)findViewById([Link].btn1);
btn2=(Button)findViewById([Link].btn2);
btn3=(Button)findViewById([Link].btn3);
btn4=(Button)findViewById([Link].btn4);
btn5=(Button)findViewById([Link].btn5);
btn6=(Button)findViewById([Link].btn6);
btn7=(Button)findViewById([Link].btn7);
btn8=(Button)findViewById([Link].btn8);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
@Override
@Override
filledpos[clktag]=activePlayer;
if(activePlayer==p0)
{
[Link]("0");
26 | G G N I M T
[Link]([Link]);
activePlayer=px;
[Link]("X Turn");
}
else
[Link]("X");
[Link]([Link]);
activePlayer=p0;
[Link]("O Turn");
}
chkwin();
}
private void chkwin()
{
int[][] winpos={{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}};
}
}
if(count==9) {
showDialog("Match is drawn");
}
}
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
restartGame();
}
}).show();
{
activePlayer=p0;
filledpos=new int[]{-1,-1,-1,-1,-1,-1,-1,-1,-1};
[Link]("TIC TAC TOE");
[Link]("");
[Link]("");
[Link]("");
[Link]("");
[Link]("");
[Link]("");
[Link]("");
[Link]("");
[Link]("");
isGameActive=true;
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
}
}
29 | G G N I M T
Output:
30 | G G N I M T
Practical 9
Applications based on Animations
activity_main.xml
<RelativeLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/fade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="Fade In" />
<Button
android:id="@+id/rotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/fade"
android:layout_marginLeft="20dp"
android:text="Rotate" />
<Button
android:id="@+id/move"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/zoom"
android:layout_marginRight="20dp"
android:text="Move-Intent" />
<Button
android:id="@+id/zoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/rotate"
android:layout_alignLeft="@+id/move"
android:text="Zoom +/-" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
31 | G G N I M T
android:layout_marginTop="150dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold"
android:text="Fade In Text ..." />
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="200dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
3. [Link]
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="[Link]
<rotate xmlns:android="[Link]
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000" >
</rotate>
<rotate xmlns:android=[Link]
32 | G G N I M T
android:startOffset="1500"
android:fromDegrees="360"
android:toDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000" >
</rotate>
</set>
4. [Link]
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="[Link]
<scale xmlns:android="[Link]
android:fromXScale="0.5"
android:toXScale="2.5"
android:fromYScale="0.5"
android:toYScale="2.5"
android:duration="3000"
android:pivotX="50%"
android:pivotY="50%" >
</scale>
<scale xmlns:android="[Link]
android:startOffset="5000"
android:fromXScale="2.5"
android:toXScale="0.5"
android:fromYScale="2.5"
android:toYScale="0.5"
android:duration="3000"
android:pivotX="50%"
android:pivotY="50%" >
</scale>
</set>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity {
private Button fade;
private Button zoom;
private Button moveIntent;
private Button rotate;
private TextView mytext;
private ImageView myimage;
33 | G G N I M T
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
fadeAnimation(v);
}
});
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
zoomAnimation(v);
}
});
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
moveAnimation(v);
}
});
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
rotateAnimation(v);
}
});
}
private void fadeAnimation(View view) {
Animation animation =
[Link](getApplicationContext(), [Link]);
[Link](animation);
}
MoveActivity
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity {
private Button fade;
private Button zoom;
private Button moveIntent;
private Button rotate;
private TextView mytext;
private ImageView myimage;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
fadeAnimation(v);
}
35 | G G N I M T
});
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
zoomAnimation(v);
}
});
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
moveAnimation(v);
}
});
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
rotateAnimation(v);
}
});
}
private void fadeAnimation(View view) {
Animation animation = [Link](getApplicationContext(), [Link]);
[Link](animation);
}
36 | G G N I M T
Toast.LENGTH_SHORT).show();
[Link]();
}
}
Output:
37 | G G N I M T
Practical 10
<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_marginLeft="92dp"
android:layout_marginTop="62dp"
/>
</RelativeLayout>
38 | G G N I M T
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
39 | G G N I M T
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate([Link], menu);
return true;
}
}
Output:
40 | G G N I M T
Practical 11
Applications based on CheckBox
Layout_Activity Code:
<RelativeLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="51dp"
android:text="Pizza" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox1"
android:layout_below="@+id/checkBox1"
android:layout_marginTop="22dp"
android:text="Coffee" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox2"
android:layout_below="@+id/checkBox2"
android:layout_marginTop="39dp"
android:text="Burger" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox3"
android:layout_below="@+id/checkBox3"
android:layout_marginTop="32dp"
android:text="Order" />
</RelativeLayout>
41 | G G N I M T
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
int totalamount=0;
StringBuilder result=new StringBuilder();
[Link]("Selected Items:");
if([Link]()){
[Link]("\nPizza 100Rs");
totalamount+=100;
}
if([Link]()){
[Link]("\nCoffe 50Rs");
totalamount+=50;
}
if([Link]()){
[Link]("\nBurger 120Rs");
totalamount+=120;
}
[Link]("\nTotal: "+totalamount+"Rs");
//Displaying the message on the toast
[Link](getApplicationContext(), [Link](),
Toast.LENGTH_LONG).show();
}
});
42 | G G N I M T
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate([Link], menu);
return true;
}
Output:
43 | G G N I M T
Practical 12
Applications based on RatingBar
Layout_Activity Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent" android:layout_height="match_parent">
<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginTop="200dp"
android:numStars="5"
android:rating="3.5"/>
<Button
android:id="@+id/btnGet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/ratingBar1"
android:layout_below="@+id/ratingBar1"
android:layout_marginTop="30dp"
android:layout_marginLeft="60dp"
android:text="Get Rating"/>
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btnGet"
android:layout_below="@+id/btnGet"
android:layout_marginTop="20dp"
android:textSize="20dp"
android:textStyle="bold"/>
</RelativeLayout>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
44 | G G N I M T
[Link](savedInstanceState);
setContentView([Link].activity_main);
rBar = (RatingBar) findViewById([Link].ratingBar1);
tView = (TextView) findViewById([Link].textview1);
btn = (Button)findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
int noofstars = [Link]();
float getrating = [Link]();
[Link]("Rating: "+getrating+"/"+noofstars);
}
});
}
}
Output:
45 | G G N I M T
46 | G G N I M T