1.
RelativeLayout - Movement of Child with respect toParent
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Submit"
android:backgroundTint="#FA9FFF"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
2. RelativeLayout - Movement of Child w.r.t Another Child
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/child1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Submit"
android:backgroundTint="#FA9FFF"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/child2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_above="@id/child1" />
</RelativeLayout>
3. LinearLayout Movement
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity">
<Button
android:id="@+id/child1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Submit"
android:backgroundTint="#FA9FFF" />
<Button
android:id="@+id/child2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click" />
</LinearLayout>
4. Toast Experiment (UI + Java)
📄 XML Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity">
<EditText
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter your name" />
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Text"
android:backgroundTint="#009688" />
</LinearLayout>
💻 Java Code
package com.example.day1android;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editText = findViewById(R.id.username);
Button button = findViewById(R.id.submit);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = editText.getText().toString();
Toast.makeText(MainActivity.this, username, Toast.LENGTH_LONG).show();
}
});
}
}
5. Background Color and Text Color in RelativeLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:background="#E91E63"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/myname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Zarrin"
android:layout_centerInParent="true"
android:textSize="30sp"
android:textColor="#FFEB3B" />
</RelativeLayout>
6. Login Form (UI + Java)
📄 XML Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:id="@+id/heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LOGIN"
android:textSize="35dp"
android:gravity="center"
android:layout_marginBottom="40dp"
android:textColor="#673AB7" />
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Username"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:inputType="textPassword"
android:hint="Enter Password" />
<Button
android:id="@+id/login"
android:layout_marginTop="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login" />
</LinearLayout>
💻 Java Code
package com.example.day1android;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editText1 = findViewById(R.id.username);
EditText editText2 = findViewById(R.id.password);
Button button1 = findViewById(R.id.login);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = editText1.getText().toString();
String password = editText2.getText().toString();
if (username.equals("Zarrin123") && password.equals("Zarrin@123")) {
Toast.makeText(MainActivity.this, "Successfully Logged in", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Invalid Credentials", Toast.LENGTH_LONG).show();
}
}
});
}
}
1. Login with Second Activity
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:id="@+id/heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:text="LOGIN"
android:textSize="35dp"
android:gravity="center"
android:layout_marginBottom="40dp"
android:textColor="#673AB7" />
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter User Name"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:inputType="textPassword"
android:hint="Enter Password" />
<Button
android:id="@+id/login"
android:layout_marginTop="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"/>
</LinearLayout>
🔹 MainActivity.java
package com.example.day1android;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
String fixUsername ="admin";
String fixPassword ="admin@123";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
validateUser();
}
public void validateUser() {
EditText editText1 = findViewById(R.id.username);
EditText editText2 = findViewById(R.id.password);
Button button1 = findViewById(R.id.login);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String currUser = editText1.getText().toString();
String currPass = editText2.getText().toString();
if (currUser.equals(fixUsername) && currPass.equals(fixPassword)) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("USERNAME", currUser);
startActivity(intent);
} else {
Toast.makeText(MainActivity.this, "Invalid Credentials", Toast.LENGTH_LONG).show();
}
}
});
}
}
🔹 activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:gravity="center"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">
<TextView
android:id="@+id/welcome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Welcome"
android:gravity="center"
android:textColor="#E91E63"
android:textSize="30dp"/>
<TextView
android:id="@+id/usernameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="username"
android:textSize="20sp"/>
</LinearLayout>
🔹 SecondActivity.java
package com.example.day1android;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView userNameTextView = findViewById(R.id.usernameTextView);
String currUsername = getIntent().getStringExtra("USERNAME");
userNameTextView.setText(currUsername);
}
}
2. Login with Button Disabled & Email Validation
🔹 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:id="@+id/heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:text="LOGIN"
android:textSize="35dp"
android:gravity="center"
android:layout_marginBottom="40dp"
android:textColor="#673AB7"/>
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Your Email"
android:inputType="text"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:inputType="textPassword"
android:hint="Enter Password"/>
<Button
android:id="@+id/login"
android:layout_marginTop="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="Login"/>
</LinearLayout>
🔹 MainActivity.java
package com.example.day1android;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
String fixPassword = "admin@123";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
validateUser();
}
public void validateUser() {
EditText editText1 = findViewById(R.id.username);
EditText editText2 = findViewById(R.id.password);
Button button1 = findViewById(R.id.login);
editText1.addTextChangedListener(new TextWatcher() {
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override public void onTextChanged(CharSequence s, int start, int before, int count) {
boolean isMatched = Patterns.EMAIL_ADDRESS.matcher(s).matches();
button1.setEnabled(isMatched);
}
@Override public void afterTextChanged(Editable s) {}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
String currUser = editText1.getText().toString();
String currPass = editText2.getText().toString();
if (currUser.equals(fixUsername) && currPass.equals(fixPassword)) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("USERNAME", currUser);
startActivity(intent);
} else {
Toast.makeText(MainActivity.this, "Invalid Credentials", Toast.LENGTH_LONG).show();
}
}
});
}
}
🔹 activity_second.xml (same as above)
<LinearLayout ...>
<TextView android:id="@+id/welcome" ... />
<TextView android:id="@+id/usernameTextView" ... />
</LinearLayout>
🔹 SecondActivity.java (same as above)
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView userNameTextView = findViewById(R.id.usernameTextView);
String currUsername = getIntent().getStringExtra("USERNAME");
userNameTextView.setText(currUsername);
}
}
ScrollView with for loop
1. Dynamic TextView Generator
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:padding="20dp">
<EditText
android:id="@+id/numberEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter number greater than zero"
android:inputType="number"/>
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Submit"/>
<ScrollView
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>
MainActivity.java
package com.example.androidday2;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
collectNumber();
}
public void collectNumber() {
EditText numberEditText = findViewById(R.id.numberEditText);
Button submit = findViewById(R.id.submit);
LinearLayout linearLayout = findViewById(R.id.linearlayout);
submit.setOnClickListener(v -> {
String input = numberEditText.getText().toString();
if (!input.isEmpty()) {
int number = Integer.parseInt(input);
if (number > 0) {
linearLayout.removeAllViews(); // Clear previous views
for (int i = 1; i <= number; i++) {
TextView textView = new TextView(MainActivity.this);
textView.setText("Hello " + i);
textView.setTextSize(20);
linearLayout.addView(textView);
}
} else {
Toast.makeText(MainActivity.this, "Enter number > 0", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(MainActivity.this, "Please enter a number", Toast.LENGTH_SHORT).show();
}
});
}
}
✅ 2. Spinner With Images
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:padding="20dp">
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/imageView"
android:layout_marginTop="30dp"
android:layout_width="200dp"
android:layout_height="200dp"/>
</LinearLayout>
MainActivity.java
package com.example.androidday2;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewSpinnerAndImage();
}
public void viewSpinnerAndImage() {
Spinner spinner = findViewById(R.id.spinner);
ImageView imageView = findViewById(R.id.imageView);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this,
R.array.Flowers,
android.R.layout.simple_spinner_dropdown_item
);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedItem = parent.getItemAtPosition(position).toString().toLowerCase();
switch (selectedItem) {
case "flower1":
imageView.setImageResource(R.drawable.lily);
break;
case "flower2":
imageView.setImageResource(R.drawable.daff);
break;
case "flower3":
imageView.setImageResource(R.drawable.tulip);
break;
default:
imageView.setImageDrawable(null);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
imageView.setImageDrawable(null);
}
});
}
}
res/values/strings.xml
<resources>
<string name="app_name">AndroidDay2</string>
<string-array name="Flowers">
<item>Flower1</item>
<item>Flower2</item>
<item>Flower3</item>
</string-array>
</resources>
🖼 Make sure you have these image files saved under res/drawable/:
• lily.png or lily.jpg
• daff.png or daff.jpg
• tulip.png or tulip.jpg