Mobile App Development

A comprehensive Mobile App Development

1. What is XML?

XML = eXtensible Markup Language

👉 Used to design UI layout in Android apps.

👉 Stores data in structured format.

👉 Platform independent.

In Android:

2. Basic Structure of XML

<?xml version="1.0" encoding="utf-8"?> <root> <child>Content</child> </root>

Rules:

❌ Wrong:

<TextView text=Hello>

✅ Correct:

<TextView android:text="Hello"/>

3. XML in Android (Layout File)

Example: activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World"/> </LinearLayout>

4. Important Layout Types in Android

1. LinearLayout

Arranges views vertically or horizontally

<LinearLayout android:orientation="vertical">

Important Attributes:

2. RelativeLayout

Positions views relative to each other.

<RelativeLayout>

Example:

android:layout_below="@id/textView"

3. ConstraintLayout (Most Used Modern Layout)

Best for complex UI.

<androidx.constraintlayout.widget.ConstraintLayout>

Uses:

4. FrameLayout

Stacks views on top of each other.

5. Important UI Components

TextView

<TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello" android:textSize="18sp" android:textColor="#000000"/>

EditText

<EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter name"/>

Button

<Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Me"/>

ImageView

<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/image"/>

6. Width & Height Values

ValueMeaning
match_parentTake full space
wrap_contentTake required space
dpDensity pixel
spScalable pixel (for text)

Example:

android:layout_width="200dp" android:textSize="16sp"

7. ID Creation

android:id="@+id/myButton"

In Java:

Button btn = findViewById(R.id.myButton);

8. Strings Resource (Best Practice)

Instead of:

android:text="Hello"

Use:

android:text="@string/hello"

Inside res/values/strings.xml:

<string name="hello">Hello</string>

9. Colors Resource

Inside res/values/colors.xml:

<color name="black">#000000</color>

Use in layout:

android:textColor="@color/black"

10. Padding & Margin

android:padding="16dp" android:margin="8dp"

Specific sides:

11. Common Attributes

AttributePurpose
android:idUnique ID
android:layout_widthWidth
android:layout_heightHeight
android:textDisplay text
android:backgroundBackground color
android:gravityAlign inside view
android:layout_gravityAlign view inside parent

12. XML Comments

<!-- This is comment -->

13. XML Namespaces

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

For ConstraintLayout:

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

14. Drawable Usage

Inside res/drawable/

Example background:

android:background="@drawable/background"

15. Styles in XML

Inside styles.xml:

<style name="MyTextStyle"> <item name="android:textSize">20sp</item> </style>

Use:

style="@style/MyTextStyle"

16. Common Errors in XML ❌

Java

1. What is Java?

Java is:

In Android:

2. Basic Structure of Java Program

public class Main { public static void main(String[] args) { System.out.println("Hello World"); } }

In Android (Activity):

public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }

3. Data Types

Primitive Data Types

TypeSizeExample
int4 bytesint a = 10;
float4 bytesfloat f = 10.5f;
double8 bytesdouble d = 10.5;
char2 byteschar c = 'A';
boolean1 bitboolean b = true;

4. Variables

int age = 20; String name = "Bablu";

5. Operators

Arithmetic

Comparison

== != > < >= <=

Logical

&& || !

6. Conditional Statements

if-else

if(age > 18){ System.out.println("Adult"); }else{ System.out.println("Minor"); }

switch

switch(day){ case 1: System.out.println("Monday"); break; default: System.out.println("Invalid"); }

7. Loops

for loop

for(int i=0; i<5; i++){ System.out.println(i); }

while loop

while(i < 5){ i++; } \`\` ## 8. OOP Concepts (Very Important for Exam) ### 1. Class & Object ```java class Student { String name; int age; } Student s1 = new Student();

2. Constructor

class Student { String name; Student(String n){ name = n; } }

3. Encapsulation

class Student { private int age; public void setAge(int a){ age = a; } public int getAge(){ return age; } }

4. Inheritance

class Animal { void sound(){ System.out.println("Sound"); } } class Dog extends Animal { void bark(){ System.out.println("Bark"); } }

5. Polymorphism

Method Overloading:

int add(int a, int b){} double add(double a, double b){}

Method Overriding:

@Override void sound(){ System.out.println("Dog barks"); }

6. Abstraction

Using abstract class:

abstract class Shape { abstract void draw(); }

9. Strings in Java

String name = "Bablu"; name.length(); name.toUpperCase(); name.equals("Bablu");

10. Arrays

int[] arr = {1,2,3,4}; for(int i : arr){ System.out.println(i); }

11. Exception Handling

try{ int a = 10/0; }catch(Exception e){ e.printStackTrace(); }

12. Android Specific Java Concepts

1. findViewById()

Button btn = findViewById(R.id.button);

2. OnClickListener

btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this,"Clicked",Toast.LENGTH_SHORT).show(); } });

3. Intent (Activity Switching)

Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivity(intent);

4. Toast Message

Toast.makeText(this,"Hello",Toast.LENGTH_SHORT).show();

13. Activity Lifecycle (Very Important)

Methods:

14. Access Modifiers

ModifierAccess
publicEverywhere
privateSame class
protectedSame package + subclass
defaultSame package

15. Static Keyword

static int count = 0;

Bundle, Intent & Fragment Cheat Sheet (Android)

1. Intent in Android

Intent = Message object used to start activities, services, or send data.

Types of Intent

TypeUse
ExplicitStart specific activity
ImplicitRequest system to perform action (call, web, etc.)

Explicit Intent Example

Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivity(intent);

Implicit Intent Example

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://example.com")); startActivity(intent);

Sending Data via Intent

Intent intent = new Intent(MainActivity.this, SecondActivity.class); intent.putExtra("name", "Bablu"); intent.putExtra("age", 22); startActivity(intent);

🔹 Receiving Data in SecondActivity

String name = getIntent().getStringExtra("name"); int age = getIntent().getIntExtra("age", 0);

2. Bundle in Android

Bundle = Collection of key-value pairs. Used for passing data between Activities/Fragments.

Using Bundle with Intent

Intent intent = new Intent(MainActivity.this, SecondActivity.class); Bundle bundle = new Bundle(); bundle.putString("name", "Bablu"); bundle.putInt("age", 22); intent.putExtras(bundle); startActivity(intent);

Receiving Bundle

Bundle bundle = getIntent().getExtras(); if(bundle != null){ String name = bundle.getString("name"); int age = bundle.getInt("age"); }

Using Bundle with Fragment

Bundle bundle = new Bundle(); bundle.putString("name", "Bablu"); MyFragment fragment = new MyFragment(); fragment.setArguments(bundle);

In Fragment:

Bundle bundle = getArguments(); if(bundle != null){ String name = bundle.getString("name"); }

3. Fragment in Android

Fragment = Reusable UI component inside Activity.

Adding Fragment in XML (Static)

activity_main.xml:

<FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent"/>

MainActivity.java:

getSupportFragmentManager().beginTransaction() .add(R.id.fragment_container, new MyFragment()) .commit();

🔹 Replacing Fragment Dynamically

getSupportFragmentManager().beginTransaction() .replace(R.id.fragment_container, new AnotherFragment()) .addToBackStack(null) .commit();

Fragment Lifecycle (Important for Exam)

MethodWhen Called
onAttach()Fragment attached to Activity
onCreate()Initialize fragment
onCreateView()Inflate fragment layout
onViewCreated()View is ready
onStart()Fragment visible
onResume()Fragment active
onPause()Fragment paused
onStop()Fragment stopped
onDestroyView()View destroyed
onDestroy()Fragment destroyed
onDetach()Fragment detached from Activity

Fragment Layout Example

fragment_my.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello Fragment"/> </LinearLayout>

MyFragment.java:

public class MyFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_my, container, false); } }

Passing Data to Fragment (Bundle Example)

Bundle bundle = new Bundle(); bundle.putString("username", "Bablu"); MyFragment fragment = new MyFragment(); fragment.setArguments(bundle); getSupportFragmentManager().beginTransaction() .replace(R.id.fragment_container, fragment) .commit();

In Fragment:

@Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); Bundle bundle = getArguments(); if(bundle != null){ String username = bundle.getString("username"); TextView tv = view.findViewById(R.id.tvName); tv.setText(username); } }

Quick Differences

FeatureIntentBundleFragment
PurposeStart activity / actionPass dataReusable UI inside activity
Can hold dataYes (via extras)YesYes
LifecycleActivity lifecycleNoOwn fragment lifecycle
UsageBetween activitiesBetween activities/fragmentsWithin activity