0% found this document useful (0 votes)
32 views

ANDROID PROGRAMMING NOTES

APJ notes

Uploaded by

khalog712
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

ANDROID PROGRAMMING NOTES

APJ notes

Uploaded by

khalog712
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

ANDROID PROGRAMMING NOTES

CONTENT
1.wrap content
2.match parent
3.manifest file
4.findview by id
5.id
6.full forms
7.activity
8.Activity cycle
9.intent
10.xml
11.layout
12.dx tools
13. Full forms

By:- Ansh Nimker


BASICS OF AP
Text view:- used to display simple text within the android application. We can add custom styling
to the text that we have to show. App user cannot edit this text but programmer can edit the
value inside a textview programmatically inside mainactivity.java.file
Image view :-used to display any type of image resource. We can use ImageView to tint an image so that a
drawable resource can be reused and overlays on background images can be created. It can also be used to
adjust an image's size and movement. Attributes- id, src, background, padding, scale type.
Edit text:- used to retrieve text data from user. Edit text refers to the widget that displays an empty textfield in
which a user can enter the required text and this text is further used inside our application attributes-
hint,textcolorhint ,text.
Button:- is a user interface that is used to perform some action when clicked or tapped. It is a
very common widget in Android and developers often use it. This article demonstrates how to
create a button in Android Studio, toggle button- two state button that can be checked or
unchecked & radio button-only one at a time can be clicked ( its type ). Another type of button is
image button(button with image on it) buttons works on click event, pressed event and touch event.
CheckBox is a type of two state button either checked or unchecked. There can be a lot of usage of
checkboxes. For example, it can be used to know the hobby of the user, activate/deactivate the specific action
etc. Android CheckBox class is the subclass of CompoundButton class. (baki koi jyda important nhi h or same
se hi h )
XML FILES IN AP

XML stands for Extensible Markup language.

• Like HTML, XML is too a markup language, but unlike, HTML wherein tags are used to
display data, XML use. tags to transport & store data ( to -describe data).

Unlike HTML, tags are not predefined. XML are have to create tags according to d. We will
our needs.

• XML is self-descriptive self-describing & structure. because nature, i.e. XML tags are they
describes both its content-and structure.

XML is well readable by both human & machine.

• Also, it is reliable & simple to develop.

• Lastly, XML is platform independent & language_ independent.


DIFFERENCE BETWEEN XML AND HTML
XML
HTML 1. Used to transport & store data,
1. Used to display data, focuses on how focuses on what data is
data looks 2. Provides framework to define markup
2. Is a markup language itself language
3. It is not case sensitive 3.is case sensitive
4. It is a presentation language 4. Neither presentation nor
5. It has its own predefined tags programming language
6. Its not necessary to use a closing tag 5. We can define tags based on our
7. Static because it used to display data needs
8. Does not preserve whitespaces 6.closing tags are mandatory
7.dynamic because it transport data
8. Prerevses whitespaces
Why we use XML in AP
We use XML to serve various purposes
- Designing our layouts (ie in layout,xml)
- To define components in app (manifest.xml)
- To define strings (strings.xml)
- To define styles (styles.xml)
- To add & provide graphics ( drawable.xml)
- To define color codes (color.xml)
- To define view dimensions (dimens.xml)
Layout xml files

Used to define actual user interface for our app


It holds all the elements (views) & tools that we want to use in our app

Eg- textview , button, imageview

Manifest xml
This xml file is used to define all the components of our application.It includes
the names of our app’s package, activities , receivers services & permission that our app needs and other
configurations.

Eg - defining internet permission in manifest.xml

Strings Xml
used to replace the
hard coded strings with a single string.We define all strings in this xml file & then access them in our app from
this file.This enhances the reusability of code.

Styles.xml-
used to define different
styles & looks for the user interface of app.We can define our custom theme & styles in this file
Color.xml
used
to define the color codes that we used in our app.
We simply define the color in this file & used them in our
app

Drawable.xml used to provide various graphics to the views of app.


When we need to create a custom UI , we use drawable
xml file

Dimensions xml
used to
define the dimensions of the view elements in a layout.
Suppose we need a button with 50dp height, then we define the value 50dp
in dimens.xml file and then we use it in out app from the file
ID
Every view object has an integer ID that uniquely identifies the view within the view hierarchy /
component tree.

When the app is compiled, this ID is referenced as aN integer, but typically in the layout xml file, it is
assigned as a string in the id attribute. -

<Button

android:id="@+id/ btn2" />

Here, @ symbol at the beginning of the string indicates the XML parser that it should parse & expand the
rest of the ID string & identify it as an ID resources

(+) symbol mears that this is a new resource. name that must be created & added to our resources

i.e.in R,java file.

We can retrieve any view or view group object in the .java file of an activity but using its id as shown:-

Button btn = (Button)findviewById(R.id.btn1);


LAYOUT PARAMETERS
Every view & view group object has certain xml attributes that define the layout orientations; such xml attributes
are called layout parameters

Most important ones are layput_width & layout_height

Below contents are often used to set width & height of view:

Wrap content - Tells the view to size itself to the minimum dimensions required by its 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

Match parent ;- tells the view to become as big as its parent viewgroup allows, Match_parent is an attribute
used to define the width or height of a view to be equal to the width or height of its parent view. This means that
the view will take up the same space as its parent. This is often used when creating a layout with multiple
views, as it ensures that the views are of equal size.
ACTIVITY AND ITS LIFE CYCLE
The Activity class is a crucial component of an Android app, and the way activities are launched and put together is a
fundamental part of the platform's application model. Unlike programming paradigms in which apps are launched with a main()
method, the Android system initiates code in an Activity instance by invoking specific callback methods that correspond to
specific stages of its lifecycle.

In Android, every activity goes through different. states such as - created started, resumed, pause stopped or
destroyed. All these states, collectively

Constitute (or form) the life cycle of an activity. For each state is an activity’s life cycle, we have Callback
methods that helps us to perform specific tasks to effectively run the android application. Ther. Callback
methods are given below-

1 onCreate()

2 on Start() One callback method for ead state in activity life cycle

3 on Resume()

4 onPause()

5 on Stop()

6 on Restart().
ACTIVITY CYCLE DIAGRAM
1. onCreate() method is called when the activity gets memory in the os. It takes one argument i.e an object of bundle
class.

2. onStart() method is invoked just after the activity is created . during the execution of this method , the activity is
started getting visible to user, however user interaction is not allowed in this state

3. onResume() activity is said to be resumed when it is actually visible to used and the user can interact with the activity.
(called at two occasions - after execution onStart() and onPause().)

4. onPause() when the content of the activity is no longer visible to the user & user cannot interact (user pressed home
button or back navigation button or when some notification or other dialogue box appears on screen)

5.onStop() is called when activity gets replaced by some other activity & goes into the background (recent items) activity
screen and data is hidden from user

6. onRestart() when a user resume the activity which was stopped earlier , the activity is in restart state. Invoked after
user restarts the activity that was present in recent items list of device. It is called after onStop() usually.

7 onDestroy() activity is in destroyed state when it is no longer running in the background. Eg; when user presses back
navigation button the activity might get deleted after completing the cycle of pause & stop

Or in case user presses home button , activity moves to recent app list, in this case if os needs resources used by these
activities os will destroy the activity.
Difference between onStart() , onCreate() & onResume()
Difference b/w on Start(), on Create() & onResume( ) :-

Basically onCreate() → create Necessary Object()


prepare Object For Display()

→on Restart() prepare Object For Display()

→ on Stark() Make the activity visible to user but do not begin/allow user interaction

→on Resume()→ Begin/allow user interaction

→ onPause() => Pause user interaction

→onstop() Hide the activity / make the activity invisible from user

→ on Destroy() Destroy the activity.


Thus, onCreate() method basically create necessary objects & prepare them for display.

But -

on Restart() simply prepare the (already existing objects. for display agair.

And→

onStart() simply make the activity content visible to uren.

onResume() actually stave off the actual weer inter- Cinched

action.
INTENT & ITS TYPES

• In android, intent is an object which is used for passing the information amongst the
activities of an application, or from One app to another.

• An intent is basically an abstract description of an operation to be performed.

• It can be used (1) with startActivity (Intent) to launch an activity,

(2) broadcast-Intent to send a broadcast message to any interested broadcast


receivers, or

(3) start Service (Intent) or bind Service (Intent) to Communicate with a background
service.
Intent structure:-
An
intent generally consists of two different portions of information, namely→
(1) Primary attributes (action , data)

(2) Secondary attributes (type, category , extras, component)

Each one is discussed below-


(1) action:- The general
action to be performed, such as

• ACTION-VIEW • ACTION-EDIT • ACTION-MAIN etc

(2) Data: - this specifies the data on which the action has to be performed such as a person record in the
contacts database.
(3) Category -
This attribute gives additional information about the action to execute
(4) Type:- specifies an explicit type of the intent data, normally the type is inferred from the data
itself . (5) Component - specifies an explicit name of a component class to use for the intent ,
normally this is determined by looking at the other information in the intent (like action data/type and
categories ) and a matching component that can handle it
TYPES OF INTENT

There are two types of intent namely:


1. Explicit intent:- These intents specifies the component name which provides the exact
class to be run . Often these intents does not include any other information , since this type
of intents are mostly used to navigate b/w different activities of an app , in a similar way we
can also start a service to download a file in the background.

2. Implicit intent:- They do not specify a component , instead they must include enough
information for the system to determine which of the available components is best to run for
the intent
These intent objects just specify the action which has to be performed & the data on which
the action will be performed.
The decision with implicit intents, about which component will run the action is taken by a
process called intent resolution that revolves around matching an intent against all the intent
filters descriptions in the androidmanifest.xml (action, type, category - these attributes
together determine the component that will execute the action specified by an implicit intent)
INTENT FILTER AND INTENT RESOLUTION

Intent filters enable your app to participate in various Android features such as handling implicit intents, deep linking,
handling share actions, and receiving broadcast intents. It helps in creating a seamless and integrated user
experience across different apps and system components. (Jb koi image share krne k liye share button pr click krte
hai to intent filter sb apps ko filter krta hai then select krta hai ki kis kis app k paas image share krne ka action
possible h)

By using intent filters, your app can take advantage of inter-component communication and allow other apps to
invoke specific functionalities provided by your app. For example, if your app can handle the “ACTION_SEND” intent
action and the “text/plain” data type, other apps can send text content to your app using this intent. Similarly, when a
specific action or data type matches the intent filter of your component, the system will launch your component to
handle the incoming intent.

In Android, intent resolution refers to the process of determining which component (such as an
activity, service, or broadcast receiver) should handle a given intent. When an intent is sent, the
Android system must decide which app or component should respond to it based on the intent's
action, data, category, and other attributes. This process involves matching the intent against intent
filters declared by apps in their manifest files. The resolution process ensures that the appropriate
component is launched to fulfill the user's request. If multiple components are capable of handling
the intent, the system allows the user to choose which one to use.
LAYOUTS IN AP

A layout defines the structure of or a user interface is an app, such as an activity. All elements in the layout are
built using a hierarchy of View & View Group object,
wherein - (a) A View usually draws something the user can see & interact with.
(b) A ViewGroup is an invisible container that defines the layout streduse for itx child View & View Group objectx,
as shown below
The View objects are usually called "widgets" & can be one of the sub classes, such as Button or TextView. On the other hand, the View Group
objects are → usually called "layouts" which can be of following types, each defining a different layout structure:-
1) Linear Layout (2) Relative Layout
3) Table Layout (4) Frame layout
5) Absolute layout. (6) Constraint layout
Linear layout:- is the simplest layout in android for layout designing. In linear layout , all the child elements are displayed in a linear fashion ,
either horizontally or vertically, depending upon the orientation attribute defined in the linear layout . orientation and gravity are its main
attributes . orientation represent the direction in which the child elements will be aligned in the activity. Default value is vertical. Gravity is an
optional attribute which is used to control the alignment of the whole layout.
Relative layout:- Relative Layout in a very flexible layout especially for custom layout designing. It is because, it gives us the flexibility to position
our child view/component based on sibling components , position relative to the Container or another container
• And, since, it allows us to position our child components anywhere inside an activity, it is considered as most flexible layout & most used layout
after the Linear Layout is android.
• In Relative Layout, we use "above/below/left/right" to arrange the components position in relation to other component/container.
Attributes of relative layout:-
Above:- position the bottom edge of the view above the given action view/component ID where the ID must be reference of another resource id
Align bottom- used to make the bottom edge of the view component match the bottom edge of the given action view ID where ID is a reference to
another resource
There are several other align attributes unko khud pd lena pdf se

Table layout:- is used to arange a group of views into rows & columns. Table layout container does not display a border line for its row , columns or
cells
It is not necessary to have the same no of columns in each rows as shown below
A table can leave the cells empty but cells can’t spam the columns as they can in html
Each row of the table has zero or more cells & each cells can hold only one view like
image view, text view, button etc
Attributes of table layout
ID- used to uniquely identify a tablelayout in an activity
StretchColumns- This attribute is defined & used in tablelayout element to change the
default width of a column
Frame layout- one of the simplest layout to organize view controls. They are designed
to block area on the screen wherein we can add multiple child elements to the
Framelayout and can control each one’s position using the “android:layout_gravity”
attribute of each child element
Id is one of its attributes
Absolute Layout-:-

• In android, absolute layout is used to design the custom layouts.

•In this layout, we specify the exact location of its. child views by using x &
y coordinates.

• Absolute layout is harder to maintain for different mobile screen sizes


than other types of lay out became we set the exact location of a child.
view. This positioning is based on x (top) & y (left) coordinate This
positioning is not useful in world of various

Screen resolutions (sizes) & aspect ratios. Due to this reason, absolute
layout is deprecated in Android

Id, layout_X(used to specify x coordinate of child view), layout_y(used to


specify y coordinate of child view) are its attributes.
Constraint layout is a viewGroup that allows the app developer to create large & complex layout with a flat view
hierarchy, and also allows to position & size the widgets in a very flexible way.
It was created to reduce the nesting of views & improve the performance of layout files.
• Although Constraint Layout is very similar to Relative Layout in a way by aligning the views according to
relationships b/w sibling view's & parent layout; yet Constraint Layout is a lot more flexible & works much better with
the Layout editor editor of Android studio.
It was released at Google I/O 2016.
• Since it came into existence, il-has become the most- widely used layout, that is compatible with Android 2.3. (API
level 9) & higher.

Advantages of Constraint Layout over other layout :- • Constraint-Layout has dual power of both Relative & as well as.
Linear layout, i.e. →We can set/align views relative to sibling view/ parent view position, (like in relative layout)
We can also set weights for dynamic VI (only possible in linear layout.)
Constraint layout provides baseline handle to align the content of one view with another view & resize handles to
resize the view in dps.

• A very powerful feature a chain. With view & which -"grouping of elements" by forming a chain with Constraint
Layout, we can form a group of views which as a whole, can be placed in a desired way without adding any other
layout.

• This way, constraint layout provides flat view hierarchy. wherein there is no need for nested layouts. This also
provides better performance, than other layouts.

• Next important feature is that Constraint Layout respects & provides the functionality to handle the GONE views so
that layout do not break if some view is set to GONE through java code
Difference between constraint layout and relative layout
* What is Constraint?
•To define a view's position in Constraint Layout, we must add at least one the view, horizontal & one vertical
constraint for wherein, a constraint represents a connection or alignment to another view, an invisible guideline.
Hence, the a constraint defines the view's position either along the vertical axis or the horizontal axis, &
therefore, each view must have atleast one constraint for each axis; however, oftenly more are necessary.

* How to add a constraint?

• To add a constraint, do the following-

1) Drag a view from the Palette into the widow, & Consequently, it displays a rectangular box with square
resizing handles on each corner & circular constraint handles on each side -

Resize handle

Constraint handle
2)Select the view by clicking on it.

3) Next, click a constraint handle & drag it to an available anchor point i.e. edge of another view / edge of.
the layout. Then click create a connection + to the view inspector at the top of Attributes window.

4) When the constraint is created, the editor giver it a default margin to separate the two views.

To remove constraint select the view and click on constraint handle remove all the constraint by pressing
delete constraints
XD tools and some full forms
it takes all the files in a program when program after the process of gradle built

Some important full forms

Xml - Extensible Markup Language

Html- hypertext markup language

Apk- android application package

Sdk- software development kit

Avd- Android virtual device

Adb - Android Debug Bridge

Api- Application programming interface

GUI- Graphical user interface

You might also like