Android Practical List
Android Practical List
Practical No.1:
Aim: Installing “Android Studio IDE” and “Android SDK”.
Practical No.2
Aim: Working with Linear Layout and UI components in Android.
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"
android:padding="16dp"
android:gravity="center">
<TextView
android:id="@+id/headingText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="@string/app_heading"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="@+id/helloText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="18sp"
android:padding="16dp" />
</LinearLayout>
MainActivity.java
package com.example.myfirstandroidapp;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
Output:
Practical No.3:
Aim: Working with Relative Layout and UI components in Android.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns: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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="126dp"
android:layout_marginTop="50dp"
android:text="Login Page"
android:textColor="#02628E"
android:textSize="30dp"
android:textStyle="bold" />
<EditText
android:id="@+id/email"
android:layout_centerInParent="true"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:hint="Email"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<EditText
android:id="@+id/Password"
android:layout_below="@id/email"
android:layout_centerInParent="true"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:hint="Password"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/Password"
android:layout_centerInParent="true"
android:layout_marginTop="30dp"
android:background="@color/design_default_color_on_primary"
android:text="Login"
android:textColor="#0F0E0E" />
</RelativeLayout>
MainActivity.java
package com.example.radiobutton;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
<TableRow
android:background="#D7CBCB"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Student ID" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Student Name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Marks" />
</TableRow>
<TableRow
android:background="#DAE8FC"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="A" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="10" />
</TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="B" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="15" />
</TableRow>
<TableRow android:background="#DAE8FC"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="D" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="25" />
</TableRow>
<TableRow android:background="#DAE8FC"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="E" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="25" />
</TableRow>
</TableLayout>
MainActivity.java
package com.example.tablelayout;
import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp">
<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />
<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female" />
</RadioGroup>
</LinearLayout>
</ScrollView>
package com.example.uicomponents;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing UI Components
EditText editTextName = findViewById(R.id.editTextName);
RadioGroup radioGroup = findViewById(R.id.radioGroup);
ToggleButton toggleButton = findViewById(R.id.toggleButton);
CheckBox checkBox = findViewById(R.id.checkBox);
RatingBar ratingBar = findViewById(R.id.ratingBar);
AutoCompleteTextView autoCompleteTextView = findViewById(R.id.autoCompleteTextView);
Button submitButton = findViewById(R.id.submitButton);
// Setting up AutoCompleteTextView with a list of countries
String[] countries = {"India", "USA", "Canada", "Germany", "France", "Australia", "Japan"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_dropdown_item_1line, countries);
autoCompleteTextView.setAdapter(adapter);
// Button Click Listener
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get data from UI components
String name = editTextName.getText().toString().trim();
// Name validation
if (name.isEmpty()) {
editTextName.setError("Name is required!");
editTextName.requestFocus();
return;
}
// Country validation
boolean isValidCountry = false;
for (String c : countries) {
if (c.equalsIgnoreCase(country)) {
isValidCountry = true;
break;
}
}
if (!isValidCountry) {
autoCompleteTextView.setError("Please select a valid country!");
autoCompleteTextView.requestFocus();
return;
}
// Display information in a Toast message
String result = "Name: " + name + "\nGender: " + gender +
"\nNotifications: " + (isNotificationOn ? "ON" : "OFF") +
"\nTerms Accepted: " + (isTermsAccepted ? "Yes" : "No") +
"\nRating: " + rating +
"\nCountry: " + country;
Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
}
});
}
}
Output:
Practical No.6
Aim: Create Android Application to demonstrate button clicked event.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="332dp"
android:layout_marginEnd="143dp"
android:onClick="ButtonClick"
android:text="Click Me" />
</RelativeLayout>
MainActivity.java
package com.example.buttonclick;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
Output:
Practical No.7
Aim: Create Android Application to demonstrate RadioButton checked event.
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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
tools:context=".MainActivity">
<LinearLayout
android:layout_marginTop="50dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="What does SDK stand for in software development?"
android:textSize="20sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/option1"
android:layout_marginTop="10dp"
android:layout_marginStart="20dp"
android:checked="true"
android:textSize="15sp"
android:text="Software Development Knowledge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:id="@+id/option2"
android:layout_marginTop="10dp"
android:layout_marginStart="20dp"
android:checked="true"
android:textSize="15sp"
android:text="System Development Kit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:id="@+id/option3"
android:layout_marginTop="10dp"
android:layout_marginStart="20dp"
android:checked="true"
android:textSize="15sp"
android:text="Software Development Kit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:id="@+id/option4"
android:layout_marginTop="10dp"
android:layout_marginStart="20dp"
android:checked="true"
android:textSize="15sp"
android:text="System Design Knowledge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RadioGroup>
<Button
android:id="@+id/btnSubmit"
android:layout_gravity="center"
android:text="Submit"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.radiobutton;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
Output:
Practical No.8
Aim: Create Android Application to demonstrate ToggelButton clicked event.
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:gravity="center_vertical">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="20dp"
android:textSize="24sp" />
<ToggleButton
android:id="@+id/button_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="onToggleClick" />
</LinearLayout>
MainActivity.java
package com.example.toggelbuttonevent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
ToggleButton togglebutton;
TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
<Button
android:id="@+id/btnAdd"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="+" />
<Button
android:id="@+id/btnSubtract"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="-" />
<Button
android:id="@+id/btnMultiply"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="×" />
<Button
android:id="@+id/btnDivide"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="÷" />
</LinearLayout>
</RelativeLayout>
MainActivity.java
package com.example.basiccalc;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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);
// Initialize UI components
num1 = findViewById(R.id.num1);
num2 = findViewById(R.id.num2);
btnAdd = findViewById(R.id.btnAdd);
btnSubtract = findViewById(R.id.btnSubtract);
btnMultiply = findViewById(R.id.btnMultiply);
btnDivide = findViewById(R.id.btnDivide);
resultText = findViewById(R.id.resultText);
// Set click listeners for buttons
btnAdd.setOnClickListener(v -> calculate('+'));
btnSubtract.setOnClickListener(v -> calculate('-'));
btnMultiply.setOnClickListener(v -> calculate('*'));
btnDivide.setOnClickListener(v -> calculate('/'));
}
<Button
android:id="@+id/buttonStyled"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Styled Button"
style="@style/CustomButtonStyle"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomButtonStyle" parent="Widget.MaterialComponents.Button">
<item name="android:backgroundTint">#FF5722</item> <!-- Orange -->
<item name="android:textColor">#FFFFFF</item> <!-- White text -->
<item name="android:padding">10dp</item>
</style>
</resources>
theme.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="Theme.MyFirstProj"
parent="Theme.MaterialComponents.Light.DarkActionBar">
<item name="colorPrimary">#6200EE</item>
<item name="colorPrimaryVariant">#3700B3</item>
<item name="colorOnPrimary">#FFFFFF</item>
<item name="colorSecondary">#03DAC5</item>
<item name="colorOnSecondary">#000000</item>
</style>
</resources>
Output: