University Of
Education Lahore
Submitted To:Prof.Sheraz Ahmad
Class:BSIT POST ADP
Name:Zain Ali
Roll No : mtf24002743
Mobile Application development
What is fragment and Intent?
A fragment is like a small piece of a screen in Android.
It lives inside an activity (an activity is a full screen).
A fragment has its own layout, its own logic, and its own life cycle.You use fragments to
divide your app's UI into smaller, more manageable sections.
Example:
In whatsapp:
Left side: list of chats (Fragment 1)
Right side: open conversation (Fragment 2)
Both parts can be fragments inside a single activity.
Fragment 1.Kotlin
Fragment1.Xml
Fragment2.kotlin
Fragment2.xml
MainActivity.kotlin fragment
Intent:
An intent is like a messenger between different parts of your app.
It is used to start a new activity, start a service, or send information between
activities/fragments.
You use intents when you want to move from one screen to another or pass data.
Example:
In a login app :
After login, you want to open the home screen.
You create an Intent to move from LoginActivity ➔ HomeActivity.
Q 2 :How to create Fragment?
Open res folder -> right click layout file ->add blank fragment.
Name to fragment1.
Change Layout to relative layout.
Same for fragment2.
Implement Fragment class method for Fragment1
In Fragment1.kotlinfile:
Fragment1:Fragment()
class Fragment1:Fragment(R.layout.fragment1) {
}
In main Activity :
lateinit var binding:ActivityMainBinding
Q3What is Relative Laout?
RelativeLayout is a ViewGroup (a type of layout) where you can position child views relative
to each other or relative to the parent.
To the left, right, above, or below another view.
Align views to the parent’s top, bottom, left, or right.
Code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
android:layout_centerInParent="true" />
</RelativeLayout>
Q4 What is Android and androidx library?
Android Library
In Android development, a library is a collection of ready-made code, layouts, functions, etc.,
that you can reuse in your project.
Android libraries were originally provided by Google with the Android platform itself.
Examples include:
android.app.Activity
android.widget.Button
android.view.View
etc.
Androidx Library
AndroidX is the newer, improved version of Android libraries.
Google created AndroidX to fix problems and make updating easier.
Instead of waiting for a full Android OS update, now you can update libraries independently
(faster updates, bug fixes, new features).
AndroidX libraries are separate from the Android OS itself.
AndroidX reorganized the old libraries and added lots of new features. :
Old: android.support.v7.app.AppCompatActivity
New: androidx.appcompat.app.AppCompatActivity
Q5 What is Static View Binding And Dynamic View Binding?
Static View Binding :
The binding is done directly and early (at compile time).
You know exactly which layout you will bind in the code.
No layouts are changed at runtime.
ActivityMainBinding binding;
Code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
binding.myButton.setText("Static Binding Example");
}
Dynamic View Binding
he layout to bind is decided at runtime.
You might inflate different layouts dynamically or handle multiple view types.
Useful for complex apps like RecyclerView Adapters or Fragments where layouts can change.
Code
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
if (viewType == 1) {
ItemOneBinding binding = ItemOneBinding.inflate(inflater, parent, false);
return new OneViewHolder(binding);
} else {
ItemTwoBinding binding = ItemTwoBinding.inflate(inflater, parent, false);
return new TwoViewHolder(binding);
}
}