0% found this document useful (0 votes)
15 views36 pages

Andr Unit 1 Notes

The document provides an overview of Android, including its definition, features, history, and the development environment Android Studio. It explains the structure of Android projects, the functionality of the ActionBar, and the use of Activities and Intents. Additionally, it covers the creation of a new Android project and provides examples of implicit and explicit intents in Android applications.

Uploaded by

nayanbiradar14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views36 pages

Andr Unit 1 Notes

The document provides an overview of Android, including its definition, features, history, and the development environment Android Studio. It explains the structure of Android projects, the functionality of the ActionBar, and the use of Activities and Intents. Additionally, it covers the creation of a new Android project and provides examples of implicit and explicit intents in Android applications.

Uploaded by

nayanbiradar14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Unit 1

What is Android: Android, Obtaining the required tools,


1.1 creating first android app, understanding the components
of screen, adapting display orientation
Action bar, Activities and Intents, Activity Lifecycle and
Saving State, Basic Views: TextView, Button,
1.2
Introduction to ImageButton, EditText, CheckBox, ToggleButton,
I Android RadioButton, and RadioGroup Views
ProgressBar View, AutoCompleteTextView, TimePicker
View, DatePicker View, ListView View,Spinner View
1.3

What is Android
Android is a software package and linux based operating system for mobile devices such as tablet
computers and smartphones.

It is developed by Google and later the OHA (Open Handset Alliance). Java language is mainly used to
write the android code even though other languages can be used.

The goal of android project is to create a successful real-world product that improves the mobile
experience for end users

Features of Android
After learning what is android, let's see the features of android. The important features of android are
given below:

1) It is open-source.

2) Anyone can customize the Android Platform.

3) There are a lot of mobile applications that can be chosen by the consumer.

4) It provides many interesting features like weather details, opening screen, live RSS (Really Simple
Syndication) feeds etc.

It provides support for messaging services(SMS and MMS), web browser, storage (SQLite), connectivity
(GSM, CDMA, Blue Tooth, Wi-Fi etc.), media, handset layout etc.
History of Android
The history and versions of android are interesting to know. The code names of android ranges from A to
J currently, such as Aestro, Blender, Cupcake, Donut, Eclair, Froyo, Gingerbread, Honeycomb, Ice
Cream Sandwitch, Jelly Bean, KitKat and Lollipop. Let's understand the android history in a sequence.

1) Initially, Andy Rubin founded Android Incorporation in Palo Alto, California, United States in
October, 2003.

2) In 17th August 2005, Google acquired android Incorporation. Since then, it is in the subsidiary of
Google Incorporation.

3) The key employees of Android Incorporation are Andy Rubin, Rich Miner, Chris White and Nick
Sears.

4) Originally intended for camera but shifted to smart phones later because of low market for camera
only.

5) Android is the nick name of Andy Rubin given by coworkers because of his love to robots.

6) In 2007, Google announces the development of android OS.

7) In 2008, HTC launched the first android mobile.

Android Studio
Android Studio is the official Integrated Development Environment (IDE) for android application
development. Android Studio provides more features that enhance our productivity while building
Android apps.

Android Studio was announced on 16th May 2013 at the Google I/O conference as an official IDE for
Android app development. It started its early access preview from version 0.1 in May 2013. The first
stable built version was released in December 2014, starts from version 1.0.

Since 7th May 2019, Kotlin is Google's preferred language for Android application development. Besides
this, other programming languages are supported by Android Studio.
Features of Android Studio

● It has a flexible Gradle-based build system.

● It has a fast and feature-rich emulator for app testing.

● Android Studio has a consolidated environment where we can develop for all Android devices.

● Apply changes to the resource code of our running app without restarting the app.

● Android Studio provides extensive testing tools and frameworks.

● It supports C++ and NDK.

● It provides build-in supports for Google Cloud Platform. It makes it easy to integrate Google
Cloud Messaging and App Engine.
Android Studio Project Structure
The Android Studio project contains one or more modules with resource files and source code files. These
include different types of modules

● Android app modules

● Library modules

● Google App Engine modules


By default, Android Studio displays our project files in the Android project view, as shown in the above
image. This view is formed by modules to provide quick access to our project's key source files.

These build files are visible to the top-level under Gradle Scripts. And the app module contains the
following folders:

● manifests: It contains the AndroidManifest.xml file.

● java: It contains the source code of Java files, including the JUnit test code.

● res: It contains all non-code resources, UI strings, XML layouts, and bitmap images.

We will see the actual file structure of the project by selecting the Project from the Project dropdown.

Android Studio User Interface


The Android Studio main window contains the several logical areas which are shown in the below figure:
1. The toolbar provides us a wide range of actions, which includes running apps and launching
Android tools.

2. The navigation bar helps in navigating our project and open files for editing. It gives a compact
view of structure visible in the Project window.

3. The editor window is a space where we can create and modify our code. On the basis of the
current file type, the editor can change. While viewing a layout file, the editor displays the Layout
Editor.

4. The tool window bar runs around the outside the IDE window and contains buttons that allow as
to expand and collapse individual tool windows.

5. The tool windows provide us access specific tasks like search, project management, version
control, and more. We can able expand and collapse them.

6. The status bar displays the status of our project and IDE itself, as well as any messages or
warnings.
We are willing to organize the main window to give ourselves more screen space by moving or hiding
toolbars and tool windows. We can also use keyboard shortcuts to access most of the IDE features.

Gradle build system


Gradle build used as the foundation of the build system in Android Studio. It uses more Android-specific
capabilities provided by the Android plugin for Gradle. This build system runs independently from the
command line and integrated tool from the Android Studio menu. We can use build features for the
following purpose:

● Configure, customize, and extend the build process.

● We can create multiple APKs from our app, with different features using the same project and
modules.

● Reuse resource and code across source sets.

The Main Activity File


The main activity code is a Java file MainActivity.java. This is the actual application file which ultimately
gets converted to a Dalvik executable and runs your application. Following is the default code generated
by the application wizard for Hello World! application −

File: MainActivity.java

package com.example.helloworld;

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);
}
}
Here, R.layout.activity_main refers to the activity_main.xml file located in the res/layout folder. The
onCreate() method is one of many methods that are figured when an activity is loaded.

File: activity_main.xml
Android studio auto generates code for activity_main.xml file. You may edit this file according to your
requirement.

<?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="com.example.helloworld">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Hello World!"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintRight_toRightOf="parent"

app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

match_parent
match_parent is used for referring to the parent size either through layout_width or layout_height
attributes. If we set layout_height as match_parent then the view's height will be as same as the parent's
height. The parent is the outer layout in which the view is present.

Wrap_content

wrap_content is used to wrap the view according to its actual size. It can be used with
layout_height as well as layout_width. If we set layout_height as wrap_content then the height
will be the same as the content size and if we set layout_width as wrap_content then the width
will be the same as the content size. The size will be defined on the basis of content size, if the
image size is small then the view will wrap only that image according to its size.
1) Create the New Android project

For creating the new android studio project:

1) Select Start a new Android Studio project


2) Provide the following information: Application name, Company domain, Project location and Package
name of application and click next.
3) Select the API level of application and click next.
4) Select the Activity type (Empty Activity).
5) Provide the Activity Name and click finish.
After finishing the Activity configuration, Android Studio auto generates the activity class and other
required configuration files.

Now an android project has been created. You can explore the android project and see the simple
program, it looks like this:
Action bar

In Android applications, ActionBar is the element present at the top of the activity screen. It is a salient feature of a
mobile application that has a consistent presence over all its activities. It provides a visual structure to the app and
contains some of the frequently used elements for the users. Android ActionBar was launched by Google in 2013 with the
release of Android 3.0(API 11). Before that, the name of this top most visual element was AppBar. AppBar contains only
the name of the application or current activity. It was not very much useful for the users and developers also have
negligible option to customize it.

Google announced a support library along with the introduction of ActionBar. This library is a part of AppCompat and
its purpose is to provide backward compatibility for older versions of Android and to support tabbed interfaces. All
applications that use the default theme provided by the Android(Theme.AppCompat.Light.DarkActionBar), contains an
ActionBar by default. However, developers can customize it in several ways depending upon their needs. Components
included in the ActionBar are:

● App Icon: Display the branding logo/icon of the application.

● View Controls: Section that displays the name of the application or current activity. Developers can

also include spinner or tabbed navigation for switching between views.

● Action Button: Contains some important actions/elements of the app that may be required to the users

frequently.

● Action Overflow: Include other actions that will be displayed as a menu.

Designing a Custom ActionBar

The following example demonstrates the steps involved in creating a custom ActionBar for the MainActivity of an
application. All important aspects of visual elements like icon, title, subtitle, action buttons, and overflow menu will be
covered.

Default ActionBar

As mentioned earlier, every android app contains an ActionBar by default. This pre-included ActionBar display title for
the current activity that is managed by the AncdroidManifest.xml file. The string value of the application’s title is
provided by @string/app_name resource present under the application nodes.

<application

…..

…..

android:label=”@string/app_name”

…..
</application>

Output:

Advantages of ActionBar

● Provides a customized area to design the identity of an app

● Specify the location of the user in the app by displaying the title of the current Activity.

● Provides access to important and frequently used actions

● Support tabs and a drop-down list for view switching and navigation.

Disadvantages of ActionBar

● All features of the ActionaBar are not introduced at once but were introduced with the release of

different API levels such as API 15, 17, and 19.

● The ActionBar behaves differently when it runs on different API levels.

● The features that were introduced with a particular API does not provide backward compatibility.

Note: To resolve all the problems related to ActionBar, Google introduced ToolBar along with the release of API 21(Android
Lollipop).

Activities and Intents

Android Intent is the message that is passed between components such as activities, content providers, broadcast
receivers, services etc.

It is generally used with startActivity() method to invoke activity, broadcast receivers etc.

The dictionary meaning of intent is intention or purpose. So, it can be described as the intention to do action.
The LabeledIntent is the subclass of android.content.Intent class.

Android intents are mainly used to:

● Start the service

● Launch an activity

● Display a web page

● Display a list of contacts

● Broadcast a message

● Dial a phone call etc.

Types of Android Intents

There are two types of intents in android: implicit and explicit.

1) Implicit Intent

Implicit Intent doesn't specifiy the component. In such case, intent provides information of available components
provided by the system that is to be invoked.

For example, you may write the following code to view the webpage.

1. Intent intent=new Intent(Intent.ACTION_VIEW);

2. intent.setData(Uri.parse("http://www.javatpoint.com"));

3. startActivity(intent);

2) Explicit Intent

Explicit Intent specifies the component. In such case, intent provides the external class to be invoked.

1. Intent i = new Intent(getApplicationContext(), ActivityTwo.class);

2. startActivity(i);

To get the full code of explicit intent, visit the next page.
Android Implicit Intent Example

Let's see the simple example of implicit intent that displays a web page.

activity_main.xml

File: activity_main.xml

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

2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

3. xmlns:app="http://schemas.android.com/apk/res-auto"

4. xmlns:tools="http://schemas.android.com/tools"

5. android:layout_width="match_parent"

6. android:layout_height="match_parent"

7. tools:context="example.com.implicitintent.MainActivity">

8.

9. <EditText

10. android:id="@+id/editText"

11. android:layout_width="wrap_content"

12. android:layout_height="wrap_content"

13. android:layout_marginEnd="8dp"

14. android:layout_marginStart="8dp"

15. android:layout_marginTop="60dp"

16. android:ems="10"

17. app:layout_constraintEnd_toEndOf="parent"
18. app:layout_constraintHorizontal_bias="0.575"

19. app:layout_constraintStart_toStartOf="parent"

20. app:layout_constraintTop_toTopOf="parent" />

21.

22. <Button

23. android:id="@+id/button"

24. android:layout_width="wrap_content"

25. android:layout_height="wrap_content"

26. android:layout_marginRight="8dp"

27. android:layout_marginLeft="156dp"

28. android:layout_marginTop="172dp"

29. android:text="Visit"

30. app:layout_constraintEnd_toEndOf="parent"

31. app:layout_constraintHorizontal_bias="0.0"

32. app:layout_constraintStart_toStartOf="parent"

33. app:layout_constraintTop_toBottomOf="@+id/editText" />

34. </android.support.constraint.ConstraintLayout>

Activity class

File: MainActivity.java

1. package example.com.implicitintent;
2.

3. import android.content.Intent;

4. import android.net.Uri;

5. import android.support.v7.app.AppCompatActivity;

6. import android.os.Bundle;

7. import android.view.View;

8. import android.widget.Button;

9. import android.widget.EditText;

10.

11. public class MainActivity extends AppCompatActivity {

12.

13. Button button;

14. EditText editText;

15.

16. @Override

17. protected void onCreate(Bundle savedInstanceState) {

18. super.onCreate(savedInstanceState);

19. setContentView(R.layout.activity_main);

20.

21. button = findViewById(R.id.button);

22. editText = findViewById(R.id.editText);


23.

24. button.setOnClickListener(new View.OnClickListener() {

25. @Override

26. public void onClick(View view) {

27. String url=editText.getText().toString();

28. Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(url));

29. startActivity(intent);

30. }

31. }); } }

32. Output:

Android Explicit Intent Example

Android Explicit intent specifies the component to be invoked from activity. In other words, we can call another activity
in android by explicit intent.

We can also pass the information from one activity to another using explicit intent.

Here, we are going to see an example to call one activity from another and vice-versa.

Android calling one activity from another activity example

Let's see the simple example of android explicit example that calls one activity from another and vice versa.

activity_main.xml

File: activity_main.xml

1. <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout

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

3. xmlns:app="http://schemas.android.com/apk/res-auto"
4. xmlns:tools="http://schemas.android.com/tools"

5. android:layout_width="match_parent"

6. android:layout_height="match_parent"

7. tools:context="examplet.com.explicitintent.FirstActivity">

8.

9. <TextView

10. android:layout_width="wrap_content"

11. android:layout_height="wrap_content"

12. android:layout_marginEnd="8dp"

13. android:layout_marginStart="8dp"

14. android:layout_marginTop="8dp"

15. android:text="First Activity"

16. app:layout_constraintBottom_toBottomOf="parent"

17. app:layout_constraintEnd_toEndOf="parent"

18. app:layout_constraintHorizontal_bias="0.454"

19. app:layout_constraintLeft_toLeftOf="parent"

20. app:layout_constraintRight_toRightOf="parent"

21. app:layout_constraintStart_toStartOf="parent"

22. app:layout_constraintTop_toTopOf="parent"

23. app:layout_constraintVertical_bias="0.06" />

24.
25. <Button

26. android:id="@+id/button"

27. android:layout_width="wrap_content"

28. android:layout_height="wrap_content"

29. android:layout_marginEnd="8dp"

30. android:layout_marginStart="8dp"

31. android:layout_marginTop="392dp"

32. android:onClick="callSecondActivity"

33. android:text="Call second activity"

34. app:layout_constraintEnd_toEndOf="parent"

35. app:layout_constraintStart_toStartOf="parent"

36. app:layout_constraintTop_toTopOf="parent" />

37.

38. </android.support.constraint.ConstraintLayout>

ActivityOne class

File: MainActivityOne.java

1. package example.com.explicitintent;

2.

3. import android.content.Intent;

4. import android.support.v7.app.AppCompatActivity;

5. import android.os.Bundle;
6. import android.view.View;

7.

8. public class FirstActivity extends AppCompatActivity {

9.

10. @Override

11. protected void onCreate(Bundle savedInstanceState) {

12. super.onCreate(savedInstanceState);

13. setContentView(R.layout.activity_first);

14. }

15. public void callSecondActivity(View view){

16. Intent i = new Intent(getApplicationContext(), SecondActivity.class);

17. i.putExtra("Value1", "Android By Javatpoint");

18. i.putExtra("Value2", "Simple Tutorial");

19. // Set the request code to any code you like, you can identify the

20. // callback via this code

21. startActivity(i);

22. }

23.

24. }

activitytwo_main.xml

File: activitytwo_main.xml
1. <?xml version="1.0" encoding="utf-8"?>

2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

3. xmlns:app="http://schemas.android.com/apk/res-auto"

4. xmlns:tools="http://schemas.android.com/tools"

5. android:layout_width="match_parent"

6. android:layout_height="match_parent"

7. tools:context="example.com.explicitintent.SecondActivity">

8.

9. <TextView

10. android:layout_width="wrap_content"

11. android:layout_height="wrap_content"

12. android:layout_marginEnd="8dp"

13. android:layout_marginStart="8dp"

14. android:layout_marginTop="8dp"

15. android:text="Second Activity"

16. app:layout_constraintBottom_toBottomOf="parent"

17. app:layout_constraintEnd_toEndOf="parent"

18. app:layout_constraintHorizontal_bias="0.454"

19. app:layout_constraintLeft_toLeftOf="parent"

20. app:layout_constraintRight_toRightOf="parent"

21. app:layout_constraintStart_toStartOf="parent"
22. app:layout_constraintTop_toTopOf="parent"

23. app:layout_constraintVertical_bias="0.06" />

24.

25. <Button

26. android:id="@+id/button"

27. android:layout_width="wrap_content"

28. android:layout_height="wrap_content"

29. android:layout_marginEnd="8dp"

30. android:layout_marginStart="8dp"

31. android:layout_marginTop="392dp"

32. android:onClick="callFirstActivity"

33. android:text="Call first activity"

34. app:layout_constraintEnd_toEndOf="parent"

35. app:layout_constraintStart_toStartOf="parent"

36. app:layout_constraintTop_toTopOf="parent" />

37. </android.support.constraint.ConstraintLayout>

ActivityTwo class

File: MainActivityTwo.java

1. package example.com.explicitintent;

2.

3. import android.content.Intent;
4. import android.support.v7.app.AppCompatActivity;

5. import android.os.Bundle;

6. import android.view.View;

7. import android.widget.Toast;

8.

9. public class SecondActivity extends AppCompatActivity {

10.

11. @Override

12. protected void onCreate(Bundle savedInstanceState) {

13. super.onCreate(savedInstanceState);

14. setContentView(R.layout.activity_second);

15. Bundle extras = getIntent().getExtras();

16. String value1 = extras.getString("Value1");

17. String value2 = extras.getString("Value2");

18. Toast.makeText(getApplicationContext(),"Values are:\n First value: "+value1+

19. "\n Second Value: "+value2, Toast.LENGTH_LONG).show();

20. }

21. public void callFirstActivity(View view){

22. Intent i = new Intent(getApplicationContext(), FirstActivity.class);

23. startActivity(i);

24. } } Output:
Android Activity Lifecycle

Android Activity Lifecycle is controlled by 7 methods of android.app.Activity class. The android Activity is the subclass of
ContextThemeWrapper class.

An activity is the single screen in android. It is like window or frame of Java.

By the help of activity, you can place all your UI components or widgets in a single screen.

The 7 lifecycle method of Activity describes how activity will behave at different states.

Android Activity Lifecycle methods

Let's see the 7 lifecycle methods of android activity.

Method Description

onCreate called when activity is first created.

onStart called when activity is becoming visible to the user.

onResume called when activity will start interacting with the user.

onPause called when activity is not visible to the user.

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

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

onDestroy called before the activity is destroyed.

File: activity_main.xml
1. <?xml version="1.0" encoding="utf-8"?>

2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

3. xmlns:app="http://schemas.android.com/apk/res-auto"

4. xmlns:tools="http://schemas.android.com/tools"

5. android:layout_width="match_parent"

6. android:layout_height="match_parent"

7. tools:context="example.com.activitylifecycle.MainActivity">

8.

9. <TextView

10. android:layout_width="wrap_content"

11. android:layout_height="wrap_content"

12. android:text="Hello World!"

13. app:layout_constraintBottom_toBottomOf="parent"

14. app:layout_constraintLeft_toLeftOf="parent"

15. app:layout_constraintRight_toRightOf="parent"

16. app:layout_constraintTop_toTopOf="parent" />

17.

18. </android.support.constraint.ConstraintLayout>

Android Activity Lifecycle Example

It provides the details about the invocation of life cycle methods of activity. In this example, we are displaying the content
on the logcat.

File: MainActivity.java
1. package example.com.activitylifecycle;

2.

3. import android.app.Activity;

4. import android.os.Bundle;

5. import android.util.Log;

6.

7. public class MainActivity extends Activity {

8.

9. @Override

10. protected void onCreate(Bundle savedInstanceState) {

11. super.onCreate(savedInstanceState);

12. setContentView(R.layout.activity_main);

13. Log.d("lifecycle","onCreate invoked");

14. }

15. @Override

16. protected void onStart() {

17. super.onStart();

18. Log.d("lifecycle","onStart invoked");

19. }

20. @Override

21. protected void onResume() {


22. super.onResume();

23. Log.d("lifecycle","onResume invoked");

24. }

25. @Override

26. protected void onPause() {

27. super.onPause();

28. Log.d("lifecycle","onPause invoked");

29. }

30. @Override

31. protected void onStop() {

32. super.onStop();

33. Log.d("lifecycle","onStop invoked");

34. }

35. @Override

36. protected void onRestart() {

37. super.onRestart();

38. Log.d("lifecycle","onRestart invoked");

39. }

40. @Override

41. protected void onDestroy() {

42. super.onDestroy();
43. Log.d("lifecycle","onDestroy invoked");

44. } }

Button,

ImageButton,
Android RadioButton
RadioButton is a two states button which is either checked or unchecked. If a single radio
button is unchecked, we can click it to make checked radio button. Once a radio button is
checked, it cannot be marked as unchecked by user.

RadioButton is generally used with RadioGroup. RadioGroup contains several radio buttons,
marking one radio button as checked makes all other radio buttons as unchecked.

You might also like