Android Practical’s 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;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Optionally, set a dynamic message in the TextView
TextView helloText = findViewById(R.id.helloText);
helloText.setText("Hello, World! Welcome to Android Development!");
}
}
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;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
RadioButton radioButton1 = findViewById(R.id.option1);
RadioButton radioButton2 = findViewById(R.id.option2);
RadioButton radioButton3 = findViewById(R.id.option3);
Button button = findViewById(R.id.btnSubmit);
button.setOnClickListener((new View.OnClickListener() {
@Override
public void onClick(View v) {
String selectedAns = "Nothing selected";
if(radioButton1.isChecked()) {
selectedAns = radioButton1.getText().toString();
}
else if (radioButton2.isChecked()) {
selectedAns = radioButton2.getText().toString();
}
else if (radioButton3.isChecked()){
selectedAns = radioButton3.getText().toString();
}
if (selectedAns.equals("Software Development Kit")){
Toast.makeText(MainActivity.this, "Correct Answer", Toast.LENGTH_SHORT).show();
}else
Toast.makeText(MainActivity.this, "Wrong Answer", Toast.LENGTH_SHORT).show();
}
}));
}
}
Output:
Practical No.4
Aim: Working with Table Layout and UI components in Android
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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"
android:layout_marginTop="100dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<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>
<TableRow android:background="#DAE8FC" android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<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;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
}
}
Output:
Practical No.5
Aim: Working with UI components (TextView, EditText, RadioButton,
ToggelButton, CheckBox, RatingBar,AutocompleteTextView)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp">
<!-- TextView -->
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter your name:"
android:textSize="18sp" />
<!-- EditText -->
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name"
android:inputType="textPersonName" />
<!-- RadioGroup -->
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<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>
<!-- ToggleButton -->
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Notifications ON"
android:textOff="Notifications OFF" />
<!-- CheckBox -->
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/accept_terms_conditions" />
<!-- RatingBar -->
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0"
android:isIndicator="false" />
<!-- AutoCompleteTextView -->
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Country" />
<!-- Button -->
<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</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;
}
// Gender selection validation
int selectedRadioId = radioGroup.getCheckedRadioButtonId();
String gender;
if (selectedRadioId == -1) {
Toast.makeText(MainActivity.this, "Please select a gender", Toast.LENGTH_SHORT).show();
return;
} else {
gender = selectedRadioId == R.id.radioMale ? "Male" : "Female";
}
boolean isNotificationOn = toggleButton.isChecked();
boolean isTermsAccepted = checkBox.isChecked();
float rating = ratingBar.getRating();
String country = autoCompleteTextView.getText().toString().trim();
// 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;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
// Initialize button inside onCreate()
Button myButton = findViewById(R.id.my_button);
// Set click listener
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Button Clicked!", Toast.LENGTH_SHORT).show();
}
});
// Handle system insets
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
}
public void ButtonClick(View view) {
}
}
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;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
RadioButton radioButton1 = findViewById(R.id.option1);
RadioButton radioButton2 = findViewById(R.id.option2);
RadioButton radioButton3 = findViewById(R.id.option3);
Button button = findViewById(R.id.btnSubmit);
button.setOnClickListener((new View.OnClickListener() {
@Override
public void onClick(View v) {
String selectedAns = "Nothing selected";
if(radioButton1.isChecked()) {
selectedAns = radioButton1.getText().toString();
}
else if (radioButton2.isChecked()) {
selectedAns = radioButton2.getText().toString();
}else if (radioButton3.isChecked()){
selectedAns = radioButton3.getText().toString();
}if (selectedAns.equals("Software Development Kit")){
Toast.makeText(MainActivity.this, "Correct Answer",
Toast.LENGTH_SHORT).show();
}else
Toast.makeText(MainActivity.this, "Wrong Answer", Toast.LENGTH_SHORT).show();
}
}));
}
}
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);
// Initialize the toggle button and text view
togglebutton = (ToggleButton) findViewById(R.id.button_toggle);
textview = (TextView) findViewById(R.id.textView1);
}
// Method is called when the toggle button is clicked
public void onToggleClick(View view) {
if (togglebutton.isChecked()) {
textview.setText("Toggle is ON");
} else {
textview.setText("Toggle is OFF");
}
}
}
Output:
Practical No.9
Aim:
Create
Android Application to demonstrate basic
calculator activity_main.xml
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:padding="20dp">
<!-- First Number Input -->
<EditText
android:id="@+id/num1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter first number"
android:inputType="numberDecimal" />
<!-- Second Number Input -->
<EditText
android:id="@+id/num2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter second number"
android:inputType="numberDecimal"
android:layout_below="@id/num1"
android:layout_marginTop="10dp" />
<!-- Buttons for Operations -->
<LinearLayout
android:id="@+id/buttonLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/num2"
android:layout_marginTop="20dp">
<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>
<!-- Result Display -->
<TextView
android:id="@+id/resultText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/buttonLayout"
android:layout_marginTop="20dp"
android:text="Result: "
android:textSize="20sp"
android:textStyle="bold" />
</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 {
EditText num1, num2;
Button btnAdd, btnSubtract, btnMultiply, btnDivide;
TextView resultText;
@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('/'));
}
private void calculate(char operator) {
// Get numbers from input fields
String num1Str = num1.getText().toString();
String num2Str = num2.getText().toString();
// Validate input
if (num1Str.isEmpty() || num2Str.isEmpty()) {
Toast.makeText(this, "Please enter both numbers", Toast.LENGTH_SHORT).show();
return;
}
double number1 = Double.parseDouble(num1Str);
double number2 = Double.parseDouble(num2Str);
double result = 0;
// Perform calculation based on operator
switch (operator) {
case '+':
result = number1 + number2;
break;
case '-':
result = number1 - number2;
break;
case '*':
result = number1 * number2;
break;
case '/':
if (number2 == 0) {
Toast.makeText(this, "Cannot divide by zero", Toast.LENGTH_SHORT).show();
return;
}
result = number1 / number2;
break;
}
// Display result
resultText.setText("Result: " + result);
}
}
Output:
Practical No.10
Aim: Design Android Application component using style sheet.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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/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: