0% found this document useful (0 votes)
20 views46 pages

Mad Practical Answer Gemini

The document provides a series of programming tasks related to Android development, including steps to install the Android operating system, create user interfaces using various layouts (LinearLayout, AbsoluteLayout, TableLayout, FrameLayout), and implement a simple login functionality. Each task includes Java code for the MainActivity and corresponding XML layout files. Important notes on risks and best practices for Android development are also mentioned.

Uploaded by

ayaan08ff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views46 pages

Mad Practical Answer Gemini

The document provides a series of programming tasks related to Android development, including steps to install the Android operating system, create user interfaces using various layouts (LinearLayout, AbsoluteLayout, TableLayout, FrameLayout), and implement a simple login functionality. Each task includes Java code for the MainActivity and corresponding XML layout files. Important notes on risks and best practices for Android development are also mentioned.

Uploaded by

ayaan08ff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 46

Question 1:

1. List all the steps to install the Android operating system.

Installing the Android operating system (AOSP) on a device is a complex


process and typically not done by end-users. It's usually performed by
device manufacturers or advanced developers. Here's a general outline of
the steps involved:

1. Set up the Build Environment:

 Install Required Software: This includes a specific version of Java


Development Kit (JDK), Python, and other build tools.

 Install the Android SDK: The Android Software Development Kit (SDK)
provides the tools necessary for compiling and debugging Android.

 Download the AOSP Source Code: The entire source code for Android
is downloaded from the Android Open Source Project repository. This
requires a significant amount of storage space and a good internet
connection.

2. Prepare the Device:

 Unlock the Bootloader: The bootloader is responsible for starting the


operating system. Unlocking it allows you to install a custom OS. This step
varies depending on the device manufacturer and may void your
warranty.

 Enable USB Debugging: This allows your computer to communicate


with your Android device for development purposes.

3. Build the AOSP Code:

 Configure the Build: Select the target device and build options.

 Compile the Code: This process translates the source code into an
installable image. This can take several hours on a powerful computer.

4. Flash the Image to the Device:

 Put the Device in Fastboot Mode: This is a diagnostic mode that


allows you to flash images to the device's partitions.

 Flash the System Image: Use fastboot commands to flash the compiled
system image to the device. This replaces the existing operating system
with the new one.

5. Post-Installation Steps:
 Reboot the Device: Once the flashing is complete, reboot the device to
start the new Android OS.

 Initial Setup: Perform the initial setup steps, such as selecting a


language, connecting to Wi-Fi, and signing in to a Google account (if
Google Play Services are included).

Important Notes:

 These steps are a simplified overview. The exact process can vary
significantly depending on the specific device and Android version.

 Flashing a custom OS can be risky and may damage your device if not
done correctly. Proceed with caution and ensure you have the correct files
and instructions for your device.

 For most users, it's recommended to obtain Android updates through


official channels provided by the device manufacturer or carrier.
Activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="20dp">

<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student Name: John Doe"
android:textSize="18sp"
android:padding="8dp"/>

<TextView
android:id="@+id/txtMarks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Marks: 85"
android:textSize="18sp"
android:padding="8dp"/>
</LinearLayout>

MainActivity.java
package com.example.studentinfo

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}

Question 2:

2. Write a program to place Name, Age, and mobile number


linearly (Vertical) on the display screen using a linear layout.

MainActivity.java
package com.example.linearlayoutvertical;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView nameTextView = findViewById(R.id.nameTextView);


TextView ageTextView = findViewById(R.id.ageTextView);
TextView mobileTextView = findViewById(R.id.mobileTextView);

String name = "John Doe";


int age = 30;
String mobileNumber = "123-456-7890";

nameTextView.setText("Name: " + name);


ageTextView.setText("Age: " + age);
mobileTextView.setText("Mobile Number: " + mobileNumber);
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Name: " />

<TextView
android:id="@+id/ageTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Age: " />

<TextView
android:id="@+id/mobileTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Mobile Number: " />

</LinearLayout>

Question 3:

3. Write a program to place Name, Age, and mobile number


centrally on the display screen using Absolute Layout.

*Note:* AbsoluteLayout is deprecated in Android. It's recommended to use other


layouts like RelativeLayout or ConstraintLayout for more flexible and maintainable
layouts. However, I'll provide the code using AbsoluteLayout as requested, but I
strongly suggest you consider using ConstraintLayout in real-world applications.

MainActivity.java
package com.example.absolutelayoutdemo;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView nameTextView = findViewById(R.id.nameTextView);


TextView ageTextView = findViewById(R.id.ageTextView);
TextView mobileTextView = findViewById(R.id.mobileTextView);

String name = "John Doe";


int age = 30;
String mobileNumber = "123-456-7890";

nameTextView.setText("Name: " + name);


ageTextView.setText("Age: " + age);
mobileTextView.setText("Mobile Number: " + mobileNumber);
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name: John Doe"
android:textSize="20sp"
android:layout_x="100dp"
android:layout_y="50dp" />

<TextView
android:id="@+id/ageTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age: 30"
android:textSize="20sp"
android:layout_x="100dp"
android:layout_y="100dp" />

<TextView
android:id="@+id/mobileTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mobile Number: 123-456-7890"
android:textSize="20sp"
android:layout_x="100dp"
android:layout_y="150dp" />

</AbsoluteLayout>

Question 4:

4. Write a program to display 10 students' basic information in a


table form using TableLayout.

MainActivity.java
package com.example.tablelayoutdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.view.Gravity;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TableLayout tableLayout = findViewById(R.id.tableLayout);

// Add header row


TableRow headerRow = new TableRow(this);
headerRow.setLayoutParams(new
TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));

String[] headers = {"Name", "Age", "Mobile"};


for (String header : headers) {
TextView headerTextView = new TextView(this);
headerTextView.setText(header);
headerTextView.setTextSize(18);
headerTextView.setGravity(Gravity.CENTER);
headerTextView.setPadding(8, 8, 8, 8);
headerRow.addView(headerTextView);
}
tableLayout.addView(headerRow);

// Add data rows


String[][] studentData = {
{"John Doe", "20", "123-456-7890"},
{"Jane Smith", "22", "987-654-3210"},
{"Peter Jones", "19", "555-123-4567"},
{"Mary Brown", "21", "111-222-3333"},
{"David Wilson", "23", "444-555-6666"},
{"Sarah Lee", "20", "777-888-9999"},
{"Michael Clark", "22", "222-333-4444"},
{"Jennifer Rodriguez", "19", "555-666-7777"},
{"Robert Williams", "21", "888-999-0000"},
{"Linda Garcia", "23", "333-444-5555"}
};

for (String[] student : studentData) {


TableRow dataRow = new TableRow(this);
dataRow.setLayoutParams(new
TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));

for (String data : student) {


TextView dataTextView = new TextView(this);
dataTextView.setText(data);
dataTextView.setTextSize(16);
dataTextView.setGravity(Gravity.CENTER);
dataTextView.setPadding(8, 8, 8, 8);
dataRow.addView(dataTextView);
}
tableLayout.addView(dataRow);
}
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TableLayout
android:id="@+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*">
</TableLayout>

</ScrollView>

Question 5:

5. Write a program to display all the data types in object-oriented


programming using Frame Layout.

MainActivity.java
package com.example.framelayoutdemo;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.graphics.Color;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get references to the TextViews


TextView intTextView = findViewById(R.id.intTextView);
TextView doubleTextView = findViewById(R.id.doubleTextView);
TextView stringTextView = findViewById(R.id.stringTextView);
TextView booleanTextView = findViewById(R.id.booleanTextView);
TextView charTextView = findViewById(R.id.charTextView);
// Set the text for each TextView with data type examples
intTextView.setText("Integer (int): 10");
intTextView.setBackgroundColor(Color.parseColor("#FFEB3B")); // Yellow

doubleTextView.setText("Double (double): 3.14159");


doubleTextView.setBackgroundColor(Color.parseColor("#4CAF50")); // Green

stringTextView.setText("String (String): \"Hello, World!\"");


stringTextView.setBackgroundColor(Color.parseColor("#3F51B5")); // Blue

booleanTextView.setText("Boolean (boolean): true");


booleanTextView.setBackgroundColor(Color.parseColor("#FF5252")); // Red

charTextView.setText("Character (char): 'A'");


charTextView.setBackgroundColor(Color.parseColor("#80CBC4")); //Teal
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/intTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:padding="10dp"
android:layout_marginTop="10dp"
android:gravity="center"/>

<TextView
android:id="@+id/doubleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:padding="10dp"
android:layout_marginTop="60dp"
android:gravity="center"/>

<TextView
android:id="@+id/stringTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:padding="10dp"
android:layout_marginTop="110dp"
android:gravity="center"/>

<TextView
android:id="@+id/booleanTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:padding="10dp"
android:layout_marginTop="160dp"
android:gravity="center"/>

<TextView
android:id="@+id/charTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:padding="10dp"
android:layout_marginTop="210dp"
android:gravity="center"/>

</FrameLayout>

Question 6:

6. Write a program to accept username and password from the


end-user using TextView and EditText.

MainActivity.java
package com.example.loginapp;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView usernameLabel = findViewById(R.id.usernameLabel);


TextView passwordLabel = findViewById(R.id.passwordLabel);
EditText usernameEditText = findViewById(R.id.usernameEditText);
EditText passwordEditText = findViewById(R.id.passwordEditText);
Button loginButton = findViewById(R.id.loginButton);
TextView resultTextView = findViewById(R.id.resultTextView);

loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();
if (username.equals("admin") && password.equals("password")) {
resultTextView.setText("Login successful!");
} else {
resultTextView.setText("Login failed. Invalid credentials.");
}
}
});
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">

<TextView
android:id="@+id/usernameLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Username:" />

<EditText
android:id="@+id/usernameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:layout_marginBottom="16dp"/>

<TextView
android:id="@+id/passwordLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Password:" />

<EditText
android:id="@+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:layout_marginBottom="16dp"/>

<Button
android:id="@+id/loginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginTop="16dp"
android:text=""
android:layout_gravity="center_horizontal"/>

</LinearLayout>

Question 7:

7. Write a program to accept and display the personal information


of the student.

MainActivity.java
package com.example.studentinfoapp;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

EditText nameEditText = findViewById(R.id.nameEditText);


EditText ageEditText = findViewById(R.id.ageEditText);
EditText addressEditText = findViewById(R.id.addressEditText);
Button displayButton = findViewById(R.id.displayButton);
TextView resultTextView = findViewById(R.id.resultTextView);

displayButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = nameEditText.getText().toString();
String age = ageEditText.getText().toString();
String address = addressEditText.getText().toString();

String result = "Name: " + name + "\n" +


"Age: " + age + "\n" +
"Address: " + address;
resultTextView.setText(result);
}
});
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Name:" />

<EditText
android:id="@+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:layout_marginBottom="16dp"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Age:" />

<EditText
android:id="@+id/ageEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:layout_marginBottom="16dp"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Address:" />

<EditText
android:id="@+id/addressEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPostalAddress"
android:layout_marginBottom="16dp"/>

<Button
android:id="@+id/displayButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display Information"
android:layout_gravity="center_horizontal"/>

<TextView
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginTop="16dp"
android:text="" />

</LinearLayout>

Question 8:

8. Write a program to create a first display screen of any search


engine using AutoCompleteTextView.

MainActivity.java
package com.example.searchengineapp;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.AutoCompleteTextView;
import android.widget.ArrayAdapter;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

AutoCompleteTextView autoCompleteTextView =
findViewById(R.id.autoCompleteTextView);

// Sample search queries


String[] suggestions = {
"Google", "Google Chrome", "Google Maps", "Google Translate",
"Bing", "Bing Maps", "Bing Translator",
"DuckDuckGo", "DuckDuckGo Privacy",
"Yahoo", "Yahoo Mail", "Yahoo Finance"
};

// Create an ArrayAdapter to bind the suggestions to the AutoCompleteTextView


ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_dropdown_item_1line, suggestions);
autoCompleteTextView.setAdapter(adapter);
autoCompleteTextView.setThreshold(1); // Start suggesting after 1 character is
typed
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Search:"/>

<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your search query"
android:textSize="18sp"
android:completionThreshold="1" />

</LinearLayout>

Question 9:

9. Write a program to display all the subjects of the sixth


semester using AutoCompleteTextView.

MainActivity.java
package com.example.sixthsemestersubjects;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.AutoCompleteTextView;
import android.widget.ArrayAdapter;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

AutoCompleteTextView autoCompleteTextView =
findViewById(R.id.autoCompleteTextView);

// Array of sixth-semester subjects


String[] subjects = {
"Software Engineering",
"Database Management Systems",
"Operating Systems",
"Computer Networks",
"Web Technology",
"Mobile Application Development",
"Data Mining",
"Artificial Intelligence",
"Information Security",
"Compiler Design"
};

// Create an ArrayAdapter
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_dropdown_item_1line, subjects);

// Set the adapter to the AutoCompleteTextView


autoCompleteTextView.setAdapter(adapter);
autoCompleteTextView.setThreshold(1); // Start suggesting after 1 character
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Select Subject:"/>

<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter subject name"
android:textSize="18sp" />

</LinearLayout>

Question 10:

10. Write a program to create a toggle button to display ON / OFF


Bluetooth on the display screen.

MainActivity.java
package com.example.bluetoothtoggle;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ToggleButton bluetoothToggle = findViewById(R.id.bluetoothToggle);


TextView statusTextView = findViewById(R.id.statusTextView);

bluetoothToggle.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
if (isChecked) {
statusTextView.setText("Bluetooth is ON");
} else {
statusTextView.setText("Bluetooth is OFF");
}
}
});
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">

<ToggleButton
android:id="@+id/bluetoothToggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="OFF"
android:textOn="ON" />

<TextView
android:id="@+id/statusTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Bluetooth is OFF"
android:layout_marginTop="16dp"/>

</LinearLayout>

Question 11:

11. Write a program to create a simple calculator.

MainActivity.java
package com.example.calculatorapp;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

private EditText number1EditText, number2EditText;


private TextView resultTextView;
private Button addButton, subtractButton, multiplyButton, divideButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get references to the UI elements


number1EditText = findViewById(R.id.number1EditText);
number2EditText = findViewById(R.id.number2EditText);
resultTextView = findViewById(R.id.resultTextView);
addButton = findViewById(R.id.addButton);
subtractButton = findViewById(R.id.subtractButton);
multiplyButton = findViewById(R.id.multiplyButton);
divideButton = findViewById(R.id.divideButton);

// Set click listeners for the buttons


addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate('+');
}
});

subtractButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate('-');
}
});

multiplyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate('*');
}
});

divideButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate('/');
}
});
}

private void calculate(char operation) {


String num1Str = number1EditText.getText().toString();
String num2Str = number2EditText.getText().toString();

if (num1Str.isEmpty() || num2Str.isEmpty()) {
resultTextView.setText("Please enter both numbers");
return;
}

double num1 = Double.parseDouble(num1Str);


double num2 = Double.parseDouble(num2Str);
double result = 0;

switch (operation) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 == 0) {
resultTextView.setText("Cannot divide by zero");
return;
}
result = num1 / num2;
break;
}
resultTextView.setText(String.valueOf(result));
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">

<EditText
android:id="@+id/number1EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:hint="Enter first number"
android:layout_marginBottom="8dp"/>

<EditText
android:id="@+id/number2EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:hint="Enter second number"
android:layout_marginBottom="16dp"/>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal">

<Button
android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:textSize="20sp"
android:layout_marginRight="8dp"/>

<Button
android:id="@+id/subtractButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:textSize="20sp"
android:layout_marginRight="8dp"/>

<Button
android:id="@+id/multiplyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*"
android:textSize="20sp"
android:layout_marginRight="8dp"/>

<Button
android:id="@+id/divideButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:textSize="20sp"/>
</LinearLayout>

<TextView
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Result: "
android:layout_marginTop="16dp"
android:layout_gravity="center_horizontal"/>

</LinearLayout>

Question 12:

12. Write a program to create a login form for a social networking


site.

MainActivity.java
package com.example.socialnetworkinglogin;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private EditText emailEditText, passwordEditText;


private Button loginButton;
private TextView forgotPasswordTextView, signUpTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get references to the UI elements


emailEditText = findViewById(R.id.emailEditText);
passwordEditText = findViewById(R.id.passwordEditText);
loginButton = findViewById(R.id.loginButton);
forgotPasswordTextView = findViewById(R.id.forgotPasswordTextView);
signUpTextView = findViewById(R.id.signUpTextView);

// Set click listener for the login button


loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = emailEditText.getText().toString();
String password = passwordEditText.getText().toString();
// Basic validation (you should use more robust validation)
if (email.isEmpty() || password.isEmpty()) {
Toast.makeText(MainActivity.this, "Please enter email and password",
Toast.LENGTH_SHORT).show();
} else if (email.equals("test@example.com") &&
password.equals("password")) {
Toast.makeText(MainActivity.this, "Login successful!",
Toast.LENGTH_SHORT).show();
// Here, you would typically start a new activity or fragment
} else {
Toast.makeText(MainActivity.this, "Invalid credentials",
Toast.LENGTH_SHORT).show();
}
}
});

// Set click listeners for "Forgot Password" and "Sign Up" (for demonstration)
forgotPasswordTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Forgot Password clicked",
Toast.LENGTH_SHORT).show();
// Here, you would typically start a new activity or fragment
}
});

signUpTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Sign Up clicked",
Toast.LENGTH_SHORT).show();
// Here, you would typically start a new activity or fragment.
}
});
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Login"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="16dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Email:" />

<EditText
android:id="@+id/emailEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="Enter your email"
android:layout_marginBottom="16dp"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Password:" />

<EditText
android:id="@+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="Enter your password"
android:layout_marginBottom="16dp"/>

<Button
android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="18sp"
android:layout_marginBottom="16dp"/>

<TextView
android:id="@+id/forgotPasswordTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="Forgot Password?"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="8dp"
android:textColor="#2196F3"
android:clickable="true"/>

<TextView
android:id="@+id/signUpTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="Don't have an account? Sign Up"
android:layout_gravity="center_horizontal"
android:textColor="#2196F3"
android:clickable="true"/>
</LinearLayout>

Question 13:

13. Write a program to create a login form for a student


registration system.

MainActivity.java
package com.example.studentregistrationlogin;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private EditText nameEditText, emailEditText, passwordEditText;


private Button registerButton;
private TextView loginTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get references to the UI elements


nameEditText = findViewById(R.id.nameEditText);
emailEditText = findViewById(R.id.emailEditText);
passwordEditText = findViewById(R.id.passwordEditText);
registerButton = findViewById(R.id.registerButton);
loginTextView = findViewById(R.id.loginTextView);

// Set click listener for the register button


registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = nameEditText.getText().toString();
String email = emailEditText.getText().toString();
String password = passwordEditText.getText().toString();

// Basic validation (you should use more robust validation)


if (name.isEmpty() || email.isEmpty() || password.isEmpty()) {
Toast.makeText(MainActivity.this, "Please enter all details",
Toast.LENGTH_SHORT).show();
} else if (password.length() < 6) {
Toast.makeText(MainActivity.this, "Password must be at least 6
characters", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Registration successful!",
Toast.LENGTH_SHORT).show();
// Here, you would typically start a new activity or fragment for the
logged-in user
}
}
});

// Set click listener for the login text (for demonstration)


loginTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Login clicked",
Toast.LENGTH_SHORT).show();
// Here you would start the login activity
}
});
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Register"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="16dp"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Name:" />

<EditText
android:id="@+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="Enter your name"
android:layout_marginBottom="16dp"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Email:" />

<EditText
android:id="@+id/emailEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="Enter your email"
android:layout_marginBottom="16dp"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Password:" />

<EditText
android:id="@+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="Enter your password"
android:layout_marginBottom="16dp"/>

<Button
android:id="@+id/registerButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register"
android:textSize="18sp"
android:layout_marginBottom="16dp"/>

<TextView
android:id="@+id/loginTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="Already have an account? Login"
android:layout_gravity="center_horizontal"
android:textColor="#2196F3"
android:clickable="true"/>

</LinearLayout>

Question 14:

14. Write a program to show five checkboxes and toast selected


checkboxes.

MainActivity.java
package com.example.checkboxestoast;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private CheckBox checkBox1, checkBox2, checkBox3, checkBox4, checkBox5;


private Button showSelectedButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get references to the CheckBoxes and Button


checkBox1 = findViewById(R.id.checkBox1);
checkBox2 = findViewById(R.id.checkBox2);
checkBox3 = findViewById(R.id.checkBox3);
checkBox4 = findViewById(R.id.checkBox4);
checkBox5 = findViewById(R.id.checkBox5);
showSelectedButton = findViewById(R.id.showSelectedButton);

// Set click listener for the button


showSelectedButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<String> selectedCheckboxes = new ArrayList<>();

if (checkBox1.isChecked()) {
selectedCheckboxes.add(checkBox1.getText().toString());
}
if (checkBox2.isChecked()) {
selectedCheckboxes.add(checkBox2.getText().toString());
}
if (checkBox3.isChecked()) {
selectedCheckboxes.add(checkBox3.getText().toString());
}
if (checkBox4.isChecked()) {
selectedCheckboxes.add(checkBox4.getText().toString());
}
if (checkBox5.isChecked()) {
selectedCheckboxes.add(checkBox5.getText().toString());
}

if (selectedCheckboxes.isEmpty()) {
Toast.makeText(MainActivity.this, "No checkboxes selected",
Toast.LENGTH_SHORT).show();
} else {
String message = "Selected: " + String.join(", ", selectedCheckboxes);
Toast.makeText(MainActivity.this, message,
Toast.LENGTH_SHORT).show();
}
}
});
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">

<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checkbox 1"
android:textSize="18sp" />

<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checkbox 2"
android:textSize="18sp" />

<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checkbox 3"
android:textSize="18sp" />

<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checkbox 4"
android:textSize="18sp" />

<CheckBox
android:id="@+id/checkBox5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checkbox 5"
android:textSize="18sp" />

<Button
android:id="@+id/showSelectedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Selected"
android:layout_marginTop="16dp"
android:layout_gravity="center_horizontal" />

</LinearLayout>

Question 15:

15. Write a program to show the following output. First two radio
buttons are without using radio group and next two radio buttons
are using radio group. Note the changes between these two. Also
toast which radio button has been selected.

MainActivity.java
package com.example.radiobuttonsapp;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private RadioButton radioButton1, radioButton2, radioButton3, radioButton4;


private RadioGroup radioGroup;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get references to the RadioButtons


radioButton1 = findViewById(R.id.radioButton1);
radioButton2 = findViewById(R.id.radioButton2);
radioButton3 = findViewById(R.id.radioButton3);
radioButton4 = findViewById(R.id.radioButton4);
radioGroup = findViewById(R.id.radioGroup);

// Set click listeners for all RadioButtons


radioButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showToast(((RadioButton) v).getText().toString());
}
});

radioButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showToast(((RadioButton) v).getText().toString());
}
});
radioButton3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showToast(((RadioButton) v).getText().toString());
}
});

radioButton4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showToast(((RadioButton) v).getText().toString());
}
});

// Clear the RadioGroup to ensure only one is selected initially


radioGroup.clearCheck();
radioGroup.setOnCheckedChangeListener(new
RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId != -1) { // This condition prevents the toast when the
RadioGroup is cleared
RadioButton checkedRadioButton = (RadioButton)
findViewById(checkedId);
showToast(checkedRadioButton.getText().toString());
}
}
});
}

private void showToast(String text) {


Toast.makeText(this, text + " selected", Toast.LENGTH_SHORT).show();
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Without RadioGroup:"
android:layout_marginBottom="8dp"/>

<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 1"
android:textSize="18sp" />

<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 2"
android:textSize="18sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="With RadioGroup:"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"/>

<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 3"
android:textSize="18sp" />

<RadioButton
android:id="@+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 4"
android:textSize="18sp" />

</RadioGroup>

</LinearLayout>

Question 16:

16. Write a program to display a circular progress bar.

MainActivity.java
package com.example.circularprogressbar;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {


private ProgressBar progressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

progressBar = findViewById(R.id.progressBar);
// You can set the progress programmatically if needed
// progressBar.setProgress(50);
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">

<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true" />

</LinearLayout>

Question 17:

17. Write a program to show the following output. Use


appropriate view for the same.

Based on the image, the output consists of a screen with a title


"ProgressBar", a button labeled "DOWNLOAD FILE", and a progress bar
that appears and updates when the button is clicked.

MainActivity.java
package com.example.progressbardemo;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.os.Handler;
import android.os.Message;
public class MainActivity extends AppCompatActivity {

private ProgressBar progressBar;


private Button downloadButton;
private TextView progressTextView;
private int progress = 0;
private Handler handler;
private final int PROGRESS_UPDATE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get references to the ProgressBar and Button


progressBar = findViewById(R.id.progressBar);
downloadButton = findViewById(R.id.downloadButton);
progressTextView = findViewById(R.id.progressTextView);
progressBar.setVisibility(View.INVISIBLE);
progressTextView.setVisibility(View.INVISIBLE);

// Set click listener for the button


downloadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Simulate a download process
progressBar.setVisibility(View.VISIBLE);
progressTextView.setVisibility(View.VISIBLE);
progress = 0;
progressBar.setProgress(progress);
// Start the download simulation
startDownload();
}
});

// Handler to update the progress bar


handler = new Handler(getMainLooper()) {
@Override
public void handleMessage(Message msg) {
if (msg.what == PROGRESS_UPDATE) {
progressBar.setProgress(progress);
progressTextView.setText(progress + "%");
if (progress < 100) {
// Simulate progress increment
progress += 5;
// Send message again after a delay
sendEmptyMessageDelayed(PROGRESS_UPDATE, 200); // Update
every 200ms
} else {
// Download complete
progressTextView.setText("Download Complete!");
}
}
}
};
}

private void startDownload() {


// Start the progress updates
handler.sendEmptyMessage(PROGRESS_UPDATE);
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">

<Button
android:id="@+id/downloadButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DOWNLOAD FILE"
android:layout_marginBottom="16dp" />

<ProgressBar
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Horizontal"
android:indeterminate="false"
android:max="100"
android:visibility="invisible" />

<TextView
android:id="@+id/progressTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="0%"
android:layout_marginTop="8dp"
android:visibility="invisible"/>

</LinearLayout>

Question 18:

18. Write a program to display an ImageView and a button named


as “Change Image”. Once you click on the button, another image
should get displayed.

MainActivity.java
package com.example.changeimageapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

private ImageView imageView;


private Button changeImageButton;
private int currentImage = 1; // 1 for image1, 2 for image2

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get references to the ImageView and Button


imageView = findViewById(R.id.imageView);
changeImageButton = findViewById(R.id.changeImageButton);

// Set click listener for the button


changeImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (currentImage == 1) {
imageView.setImageResource(R.drawable.image2); // Change to
image2.png
currentImage = 2;
} else {
imageView.setImageResource(R.drawable.image1); // Change back to
image1.png
currentImage = 1;
}
}
});
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image1" // Initial image (image1.png)
android:layout_marginBottom="16dp"/>

<Button
android:id="@+id/changeImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Image" />

</LinearLayout>

Question 19:

19. Write a program to display 15 buttons using GridView.

MainActivity.java
package com.example.gridviewbuttons;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ArrayAdapter;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private GridView gridView;


private String[] buttonLabels = new String[15];

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Initialize the button labels


for (int i = 0; i < 15; i++) {
buttonLabels[i] = "Button " + (i + 1);
}

// Get reference to the GridView


gridView = findViewById(R.id.gridView);

// Create an ArrayAdapter to populate the GridView


ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, buttonLabels);

// Set the adapter to the GridView


gridView.setAdapter(adapter);

// Set item click listener for the GridView


gridView.setOnItemClickListener((parent, view, position, id) -> {
String buttonText = ((Button) view).getText().toString();
Toast.makeText(MainActivity.this, "Clicked: " + buttonText,
Toast.LENGTH_SHORT).show();
});
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">

<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3" // You can change the number of columns
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"/>

</LinearLayout>

Question 20:

20. Write a program to display a TextView with vertical scroll


view.

MainActivity.java
package com.example.textviewscrollview;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView textView = findViewById(R.id.longTextView);


String longText = "This is a very long text string. It is designed to demonstrate
how a ScrollView works with a TextView. The text will be too long to fit on the
screen, so a vertical scrollbar should appear, allowing the user to scroll through the
entire text. This is important for displaying large amounts of text, such as terms of
service, legal disclaimers, or lengthy articles. The ScrollView provides a smooth and
efficient way to navigate through the content. You can add as much text as you
want, and the ScrollView will automatically adjust to accommodate it. Remember to
set the layout_height of the TextView to wrap_content, and the ScrollView's
layout_height to match_parent. Also, ensure that the TextView's width is set to
match_parent. This example uses a LinearLayout as the parent layout, but you can
use other layouts as well. The key is to wrap the TextView in a ScrollView. This is
the end of the long text.";
textView.setText(longText);
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/longTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="" />

</ScrollView>

</LinearLayout>

Question 21:

21. Write a program to show the following output. Use


appropriate view for the same.

Based on the image, the output is a list of programming languages. A


ListView is the most appropriate view for this.

MainActivity.java
package com.example.listviewdemo;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.view.View;
import android.widget.AdapterView;

public class MainActivity extends AppCompatActivity {

private ListView listView;


private String[] languages = {
"Android", "Java", "Php", "Hadoop", "Sap", "Python", "Ajax", "C++", "Ruby",
"Java", "Rails", ".Net"
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get reference to the ListView


listView = findViewById(R.id.listView);

// Create an ArrayAdapter to populate the ListView


ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, languages);

// Set the adapter to the ListView


listView.setAdapter(adapter);

// Set item click listener for the ListView


listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long
id) {
String selectedLanguage = languages[position];
Toast.makeText(MainActivity.this, "Clicked: " + selectedLanguage,
Toast.LENGTH_SHORT).show();
}
});
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>
Question 22:

22. Write a program to display the following toast message.

Based on the image, the toast message to be displayed is "You have got
mail!".

MainActivity.java
package com.example.toastmessageapp;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private Button showToastButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get reference to the button


showToastButton = findViewById(R.id.showToastButton);

// Set click listener for the button


showToastButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Display the toast message
Toast.makeText(MainActivity.this, "You have got mail!",
Toast.LENGTH_LONG).show();
}
});
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">

<Button
android:id="@+id/showToastButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Toast" />

</LinearLayout>

Question 23:

23. Write a program to display three checkboxes and one button


named “Order“ as shown below. Once you click on the button it
should toast different selected checkboxes along with items
individual and total price ?

MainActivity.java
package com.example.orderapp;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private CheckBox pizzaCheckBox, burgerCheckBox, coffeeCheckBox;


private Button orderButton;

// Define item prices


private final int PIZZA_PRICE = 10;
private final int BURGER_PRICE = 5;
private final int COFFEE_PRICE = 3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get references to the CheckBoxes and Button


pizzaCheckBox = findViewById(R.id.pizzaCheckBox);
burgerCheckBox = findViewById(R.id.burgerCheckBox);
coffeeCheckBox = findViewById(R.id.coffeeCheckBox);
orderButton = findViewById(R.id.orderButton);

// Set click listener for the order button


orderButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<String> selectedItems = new ArrayList<>();
int totalPrice = 0;

if (pizzaCheckBox.isChecked()) {
selectedItems.add("Pizza");
totalPrice += PIZZA_PRICE;
}
if (burgerCheckBox.isChecked()) {
selectedItems.add("Burger");
totalPrice += BURGER_PRICE;
}
if (coffeeCheckBox.isChecked()) {
selectedItems.add("Coffee");
totalPrice += COFFEE_PRICE;
}

if (selectedItems.isEmpty()) {
Toast.makeText(MainActivity.this, "No items selected",
Toast.LENGTH_SHORT).show();
} else {
StringBuilder message = new StringBuilder("Selected Items:\n");
for (String item : selectedItems) {
message.append(item).append("\n");
}
message.append("Total Price: $").append(totalPrice);
Toast.makeText(MainActivity.this, message.toString(),
Toast.LENGTH_LONG).show();
}
}
});
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">

<CheckBox
android:id="@+id/pizzaCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pizza ($10)"
android:textSize="18sp" />

<CheckBox
android:id="@+id/burgerCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Burger ($5)"
android:textSize="18sp" />

<CheckBox
android:id="@+id/coffeeCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Coffee ($3)"
android:textSize="18sp" />

<Button
android:id="@+id/orderButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Order"
android:layout_marginTop="16dp"
android:layout_gravity="center_horizontal" />

</LinearLayout>

Question 24:

24. Write a program to display the following output. Use


TimePicker with SpinnerMode.

MainActivity.java
package com.example.timepickerspinner;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TimePicker;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

private TimePicker timePicker;


private TextView timeTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get references to the TimePicker and TextView


timePicker = findViewById(R.id.timePicker);
timeTextView = findViewById(R.id.timeTextView);

// Set TimePicker to use spinner mode (if available)


if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M)
{
timePicker.setHour(10);
timePicker.setMinute(30);
timePicker.setMinuteIncrement(5);
} else {
//For versions less than M, we can set current time.
timePicker.setCurrentHour(10);
timePicker.setCurrentMinute(30);
}

// Set a listener for the TimePicker


timePicker.setOnTimeChangedListener(new
TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// Display the selected time
String time = "Selected Time: " + hourOfDay + ":" + minute;
timeTextView.setText(time);
Log.d("TimePickerActivity", "Time changed: " + time);
// Optional: Show a Toast message
// Toast.makeText(MainActivity.this, time, Toast.LENGTH_SHORT).show();
}
});
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">

<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timePickerMode="spinner" />

<TextView
android:id="@+id/timeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Selected Time: "
android:layout_marginTop="16dp"/>

</LinearLayout>

Question 25:

25. Write a program to display the following output. Select and


display date and time on click of “select date”, “select time”
buttons respectively.

MainActivity.java
package com.example.datetimepickerdialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Calendar;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.widget.DatePicker;
import android.widget.TimePicker;

public class MainActivity extends AppCompatActivity {

private Button selectDateButton, selectTimeButton;


private TextView dateTextView, timeTextView;
private int year, month, day, hour, minute;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get references to the buttons and TextViews


selectDateButton = findViewById(R.id.selectDateButton);
selectTimeButton = findViewById(R.id.selectTimeButton);
dateTextView = findViewById(R.id.dateTextView);
timeTextView = findViewById(R.id.timeTextView);

// Set click listener for the "Select Date" button


selectDateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get current date
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);

// Create a DatePickerDialog
DatePickerDialog datePickerDialog = new
DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// Display the selected date
String date = "Selected Date: " + dayOfMonth + "-" +
(monthOfYear + 1) + "-" + year;
dateTextView.setText(date);
}
}, year, month, day);
datePickerDialog.show();
}
});

// Set click listener for the "Select Time" button


selectTimeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get current time
final Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);

// Create a TimePickerDialog
TimePickerDialog timePickerDialog = new
TimePickerDialog(MainActivity.this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
{
// Display the selected time
String time = "Selected Time: " + hourOfDay + ":" + minute;
timeTextView.setText(time);
}
}, hour, minute,
android.text.format.DateFormat.is24HourFormat(MainActivity.this));
timePickerDialog.show();
}
});
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">

<Button
android:id="@+id/selectDateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Date"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="16dp"/>

<TextView
android:id="@+id/dateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Selected Date: "
android:layout_gravity="center_horizontal"
android:layout_marginBottom="32dp"/>

<Button
android:id="@+id/selectTimeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Time"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="16dp"/>

<TextView
android:id="@+id/timeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Selected Time: "
android:layout_gravity="center_horizontal"/>

</LinearLayout>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy