ANDROID PROGRAMMING NOTES
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
• 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.
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.
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
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
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
We can retrieve any view or view group object in the .java file of an activity but using its id as shown:-
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( ) :-
→ on Stark() Make the activity visible to user but do not begin/allow user interaction
→onstop() Hide the activity / make the activity invisible from user
But -
on Restart() simply prepare the (already existing objects. for display agair.
And→
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.
(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) 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
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 this layout, we specify the exact location of its. child views by using x &
y coordinates.
Screen resolutions (sizes) & aspect ratios. Due to this reason, absolute
layout is deprecated in Android
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.
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