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

Expt.

No: Page No:


Date:

Experiment-3
Writing Android applications, Understanding anatomy of an Android
application

Aim: Writing Android applications, Understanding anatomy of an Android application.

Description:
Start a new Project:

1. Click “Start a new Android Studio project” and enter a name for your app in the
“Application name:” field.

2. Fill the above blanks


3. Click next

4. On the next dialog make sure “Phone and Tablet” is selected and that the “Minimum
SDK” is set to API 15: Android 4.0.3. Make sure that “Wear” and “TV” are not checked

5. On the “Add an activity to Mobile” dialog,use the default of “Blank Activity” and click
“Next.” On the “Customize the Activity” dialog use all the default values and click
“Finish.”

ADITYAGROUP OF EDUCATIONAL INSTITUTIONS, SURAMPALEM(A) Roll No: 22A91A614


Expt.No: Page No:
Date:

6. The project tree holds all the different files and resources that are needed to build your
Android app. If you are familiar with writing simple programs in Java, C, Python, etc
you might think that everything will be contained in just one or possibly two files.

7. Under “java” you will find the Java code for the app. It will be under a sub folder
called something like “com.example.user.myfirstapp”, which is the reverse of the
company domain name you entered earlier, plus the name of the app. Under that
folder you will find MainActivity.java. This is the entry point into your app and for
our example app this is the only Java file that we will need.

ADITYAGROUP OF EDUCATIONAL INSTITUTIONS, SURAMPALEM(A) Roll No: 22A91A614


Expt.No: Page No:
Date:

To create this sample app we will need to modify MainActivity.java, activity_main.xml,


and strings.xml.

Writing the app

1. For our example app we will add a Button with the label “Tap Me!”, we will change
the default “Hello world!” label to “Tap me if you dare!” plus change its position so
that it is in the center. And finally, we will add some code to display a “toast” when
the button is tapped!

2. Let’s start by altering the text of the label and changing its alignment. First find
“activity_main.xml” in the project tree and double-click it. Remember,
“activity_main.xml” is the file which holds the User Interface definition. At the
bottom of the code window there are two tabs, “Design” and “Text.” Make sure you
are using the “Design” tab.

3. Now click in the text “Hello world!” that is shown on the rendering of the phone. If it
is too small use the zoom button (the plus sign in a magnifying glass) to enlarge the
rendering of the phone.

4. In the “properties” window just to the right of the phone image, scroll down until you
find “layout:centerInParent.” Click the space next to it and select “horizontal.” The
“Hello world!” text will now jump to the horizontal center.

ADITYAGROUP OF EDUCATIONAL INSTITUTIONS, SURAMPALEM(A) Roll No: 22A91A614


Expt.No: Page No:
Date:

5. Now to change the text. The string “Hello world!” is held in the file “strings.xml”
under res->values. If you double-click on the file you will see a few lines of XML that
defines the the strings used by the app. Find this line:

Code:

<string name="hello_world">Hello world!<string>

Java Code:

public void onButtonTap(View v) {

Toast myToast =
Toast.makeText(getApplicationContext(), "Ouch!",
Toast.LENGTH_LONG);

myToast.show();

6. The word “View” in “(View v)” will likely be in red with a message bubble displayed
near it. This is Android Studio telling you that you have used a new construct (View)
without importing it in the import section, at the top of the Java code. This is easy to
fix. Click on the word “View” and then press ALT+ENTER, Android Studio will fix it
for you! If the word “Toast” is in red, then do exactly the same thing again. Click on
the word Toast and then press ALT+ENTER.

ADITYAGROUP OF EDUCATIONAL INSTITUTIONS, SURAMPALEM(A) Roll No: 22A91A614


Expt.No: Page No:
Date:

7. Now back in the designer for “activity_main.xml”, click on the button and scroll
down through the properties list until you find “onClick”. Click on the box to the right
and a list of functions will appear. Click on “onButtonTap”, the function we just
added.

8. So now the “onButtonTap()” function will be called whenever the button is tapped.
When it is called it creates a Toast called myToast that will display the message
“Ouch!”. To show the Toast we just call myToast.show().

Anatomy Of Android Anatomy:

1. Activities in Android

1. Those who know Object-oriented languages can understand the concept of encapsulating
the elements of application functionalities and then used as objects to creating and
manipulating the application. Since Android applications developed in Java and Kotlin. So
Android architecture also has the concept of reusable components to the higher level.

2. Android application is developed by one or more components that work together, these
components are activities. Activity is a single and independent module that is correlated to the
application’s single screen and its functionalities.

2. Fragment in Android

As I describe above activity is a single user interface with a layout design file and a java class
for dynamic functionalities. One way to make Android apps by using the activities as required
screens. But it is good to make a different section in the activity layout. So that it is easy to
categorize user interface and it reduces the coupling and increases the separation of the
concern in the app coding. The fragment represents a separate part of a user interface or
activity layout or any other layout. So we can use fragments as a separate section in an activity
layout. An activity can become just a container of the different fragments with different
layouts. It is preferable to create a single activity that switches between different fragments as
a different screen in the app.

3. Intent in Android

ADITYAGROUP OF EDUCATIONAL INSTITUTIONS, SURAMPALEM(A) Roll No: 22A91A614


Expt.No: Page No:
Date:

The intent is a built-in class from android SDK which is used to launch another activity from
one activity and using intent we can implement a flow of activities that can make a complete
application. Intent can contain the data about the operation to be performed and data which is
passing from one activity to another activity.
There are two types of intent:
1.Explicit Intent
2.Implicit Intent

1. Explicit Intent

It will specify which application or package name fully satisfies the intent. For example, if you
want to open a specific application or you want to open another application.

2.Implicit Intent

It will not specify the name of any component but instead of it, it will use general action to
perform. For example, you want to show the specific location on the map so you can use
implicit intent with a specific action, and which app qualifies for it will be selected.

ADITYAGROUP OF EDUCATIONAL INSTITUTIONS, SURAMPALEM(A) Roll No: 22A91A614


Expt.No: Page No:
Date:

Experiment-4: To develop a simple Android application that uses GUI


components, font and colors.
Aim: To develop a Simple Android Application that uses GUI components, Font and Colors.
Description:
Creating a New project:

1. Open Android Studio and then click on File -> New -> New project.

2. Then type the Application name as “ex.no.1″ and click Next.

3. Then select the Minimum SDK as shown below and click Next.

ADITYAGROUP OF EDUCATIONAL INSTITUTIONS, SURAMPALEM(A) Roll No: 22A91A614


Expt.No: Page No:
Date:

4. Then select the Empty Activity and click Next.

5. Finally click Finish.

6. It will take some time to build and load the project.


7. After completion it will look as given below.

ADITYAGROUP OF EDUCATIONAL INSTITUTIONS, SURAMPALEM(A) Roll No: 22A91A614


Expt.No: Page No:
Date:

Designing layout for the Android Application:

1. Click on app -> res -> layout -> activity_main.xml.

2. Now click on Text as shown below.

3. Then delete the code which is there and type the code as given below.
Code for Activity_main.xml:

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

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

ADITYAGROUP OF EDUCATIONAL INSTITUTIONS, SURAMPALEM(A) Roll No: 22A91A614


Expt.No: Page No:
Date:

android:layout_height="match_parent">

<TextView

android:id="@+id/textView"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_margin="30dp"

android:gravity="center"

android:text="Hello World!"

android:textSize="25sp"

android:textStyle="bold" />

<Button

android:id="@+id/button1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_margin="20dp"

android:gravity="center"

android:text="Change font size"

android:textSize="25sp" />

<Button

android:id="@+id/button2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_margin="20dp"

android:gravity="center"

android:text="Change color"

android:textSize="25sp" />

ADITYAGROUP OF EDUCATIONAL INSTITUTIONS, SURAMPALEM(A) Roll No: 22A91A614


Expt.No: Page No:
Date:

</LinearLayout>

4. Now click on Design and your application will look as given below.

5. So now the designing part is completed.


Java Coding for the Android Application:

1. Click on app -> java -> com.example.exno1 -> MainActivity.

2. Then delete the code which is there and type the code as given below.
Code for MainActivity.java:

package com.example.exno1;

import android.graphics.Color;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

ADITYAGROUP OF EDUCATIONAL INSTITUTIONS, SURAMPALEM(A) Roll No: 22A91A614


Expt.No: Page No:
Date:

public class MainActivity extends AppCompatActivity

int ch=1;

float font=30;

@Override

protected void onCreate(Bundle savedInstanceState)

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

final TextView t= (TextView)


findViewById(R.id.textView);

Button b1= (Button) findViewById(R.id.button1);

b1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

t.setTextSize(font);

font = font + 5;

if (font == 50)

font = 30;

});

Button b2= (Button) findViewById(R.id.button2);

b2.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

switch (ch) {

ADITYAGROUP OF EDUCATIONAL INSTITUTIONS, SURAMPALEM(A) Roll No: 22A91A614


Expt.No: Page No:
Date:

case 1:

t.setTextColor(Color.RED);

break;

case 2:

t.setTextColor(Color.GREEN);

break;

case 3:

t.setTextColor(Color.BLUE);

break;

case 4:

t.setTextColor(Color.CYAN);

break;

case 5:

t.setTextColor(Color.YELLOW);

break;

case 6:

t.setTextColor(Color.MAGENTA);

break;

ch++;

if (ch == 7)

ch = 1;

});

ADITYAGROUP OF EDUCATIONAL INSTITUTIONS, SURAMPALEM(A) Roll No: 22A91A614


Expt.No: Page No:
Date:

3. So now the Coding part is also completed.


4. Now run the application to see the output.
Output:

ADITYAGROUP OF EDUCATIONAL INSTITUTIONS, SURAMPALEM(A) Roll No: 22A91A614

You might also like