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

Andoroid Lecture

Uploaded by

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

Andoroid Lecture

Uploaded by

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

Android Lecture 2

MCA SEM 1
Dr. Manoj Devare
Faculty Incharge
What is View and ViewGroup in Android
Programming
• This class represents the basic building block for user interface
components.
• A View occupies a rectangular area on the screen and is responsible
for drawing and event handling.
• View is the base class for widgets, which are used to create interactive
UI components (buttons, text fields, etc.).
• The ViewGroup subclass is the base class for layouts, which are
invisible containers that hold other Views (or other ViewGroups) and
define their layout properties.
Button Event
Button b1=(Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View view) {
TextView t1=(TextView)findViewById(R.id.text1);

t1.setText("You clicked on Button");


}
});
LinearLayout
• LinearLayout is the most basic layout in android studio, that aligns all
the children sequentially either in a horizontal manner or a vertical
manner.
• By specifying the android:orientation attribute. If one
applies android:orientation=”vertical” then elements will be
arranged one after another in a vertical manner.
• If you apply android:orientation=”horizontal” then elements will be
arranged one after another in a horizontal manner.
RelativeLayout
• Android RelativeLayout is a ViewGroup subclass, used to specify the
position of child View elements relative to each other like (A to the
right of B) or relative to the parent (fix to the top of the parent).

• Instead of using LinearLayout, we have to use RelativeLayout to


design the user interface and keep our hierarchy flat because it
improves the performance of the application.
TableLayout
• Android TableLayout is a ViewGroup subclass which is used to
display the child View elements in rows and columns.
• It will arrange all the children elements into rows and columns and
does not display any border lines in between rows, columns or cells.
• The working of TableLayout is almost similar to HTML table and it
contains as many columns as row with the most cells.
Absolute Layout
• An Absolute Layout lets
you specify exact
locations (x/y
coordinates) of its
children.
• Absolute layouts are less
flexible and harder to
maintain than other types
of layouts without
absolute positioning.
FrameLayout

• Frame Layout is designed to block out an area on the


screen to display a single item.

• Generally, FrameLayout should be used to hold a single


child view, because it can be difficult to organize child
views in a way that's scalable to different screen sizes
without the children overlapping each other.
List View
• Android ListView is a view which groups several items
and display them in vertical scrollable list.

• The list items are automatically inserted to the list using


an Adapter that pulls content from a source such as an
array or database.
GridView

• Android GridView shows items in two-dimensional


scrolling grid (rows & columns) and the grid items are
not necessarily predetermined but they automatically
inserted to the layout using a ListAdapter
Android Resources
• Bitmaps
• Colors
• Layout Definitions
• User Interface Strings
• Animation Instructions

• Maintained separately in various sub-directories


under res/ directory of the project.
Android R.java file
• Android R.java is an auto-generated file by aapt (Android Asset
Packaging Tool) that contains resource IDs for all the resources of res/
directory.
• If you create any component in the activity_main.xml file, id for the
corresponding component is automatically created in this file.
• This id can be used in the activity source file to perform any action on
the component.
• It includes a lot of static nested classes such as menu, id, layout, attr,
drawable, string etc.
Bundle
• Android Bundles are generally used for passing data from one activity
to another.
• Basically here concept of key-value pair is used where the data that
one wants to pass is the value of the map, which can be later retrieved
by using the key.
• Bundles are used with intent and values are sent and retrieved in the
same fashion, as it is done in the case of Intent.
• It depends on the user what type of values the user wants to pass, but
bundles can hold all types of values (int, String, boolean, char) and
pass them to the new activity.
The following are the major types that are
passed/retrieved to/from a Bundle
• putInt(String key, int value),
• getInt(String key, int value)
• putString(String key, String value),
• getString(String key, String value)
• putStringArray(String key, String[] value),
• getStringArray(String key, String[] value)
• putChar(String key, char value),
• getChar(String key, char value)
• putBoolean(String key, boolean value),
• getBoolean(String key, boolean value)
// creating a intent
Intent intent = new Intent(this, SecondActivity.class);

// creating a bundle object


Bundle bundle = new Bundle();

// storing the string value in the bundle


// which is mapped to key
bundle.putString("key1", "GFG :- Main Activity");

// passing the bundle into the intent


intent.putExtras(bundle);

// starting the intent


startActivity(intent);
Activity
• If you have worked with C, C++ or Java programming
language then you must have seen that your program
starts from main() function.
• Very similar way, Android system initiates its program
with in an Activity starting with a call
on onCreate() callback method.
• There is a sequence of callback methods that start up
an activity and a sequence of callback methods that
tear down an activity.
• The Activity class defines the following call backs i.e.
events.
• You don't need to implement all the callbacks methods.
• However, it's important that you understand each one
and implement those that ensure your app behaves the
way users expect.
Callbacks
• Callbacks is a mechanism in Object Oriented Programming that allows
an application to handle subscribed events, arising at runtime, through
a listener interface.
• The subscribers will need to provide a concrete implementation of the
interface abstract methods.
• A callback is a function passed into another function as an argument,
which is then invoked inside the outer function to complete some kind
of routine or action.

You might also like