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

Android Slip Answers

The document contains multiple Android application examples demonstrating various functionalities, including activity lifecycle management, date selection using DatePicker, calculating factorials, and playing audio in the background. Each example includes XML layout files and corresponding Java code for the main activity and other components. The applications showcase user interactions, data handling, and service management in Android development.

Uploaded by

amit2648yt
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 views78 pages

Android Slip Answers

The document contains multiple Android application examples demonstrating various functionalities, including activity lifecycle management, date selection using DatePicker, calculating factorials, and playing audio in the background. Each example includes XML layout files and corresponding Java code for the main activity and other components. The applications showcase user interactions, data handling, and service management in Android development.

Uploaded by

amit2648yt
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/ 78

Android slip Answers

Slip 1:
Q1. Create a Simple Application which shows the Life Cycle of 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="20sp"
android:textStyle="bold"/>
</LinearLayout>
MainActivity.java
package com.example.slip1q1;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
String tag="Events";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(tag, "in on create event");
}
public void onStart()
{
super.onStart();
Log.d(tag,"in the onStart() event");
}
public void onRestart()
{
super.onRestart();
Log.d(tag,"in the onRestart () event");
}
public void onResume()
{
super.onResume();
Log.d(tag,"in the onResume() event");
}
public void onPause()
{
super.onPause();
Log.d(tag,"in the onPause() event");
}
public void onStop()
{
super.onStop();
Log.d(tag,"in the onStop() event");
}
public void onDestroy()
{
super.onDestroy();
Log.d(tag,"in the onDestroy() event");
}
}
Q2. Create an Android Application that demonstrate DatePicker and
DatePickerDailog.
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/dateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginBottom="16dp" />
<Button
android:id="@+id/showDatePickerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Select Date" />
</LinearLayout>
MainActivity.java
package com.example.slip110;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private TextView dateTextView;
private Button showDatePickerButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dateTextView = findViewById(R.id.dateTextView);
showDatePickerButton = findViewById(R.id.showDatePickerButton);
showDatePickerButton.setOnClickListener(v -> {
Calendar calendar = Calendar.getInstance();
new DatePickerDialog(this, (view, year, month, dayOfMonth) ->
dateTextView.setText("date: " + dayOfMonth + "/" + (month + 1) +
"/" + year),
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH)
).show();
});
}
}
Slip 2:
Q1. Create a Simple Application, which reads a positive number from
the user and display its factorial value in another activity.
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:gravity="center"
android:padding="16dp">
<EditText
android:id="@+id/inputNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter number"
android:inputType="number"/>
<Button
android:id="@+id/calculateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate"
android:layout_marginTop="10dp"/>
</LinearLayout>
activity_result.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:gravity="center">
<TextView
android:id="@+id/resultText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>
</LinearLayout>
MainActivity.java
package com.example.slip2q1;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText inputNumber = findViewById(R.id.inputNumber);
Button calculateButton = findViewById(R.id.calculateButton);
calculateButton.setOnClickListener(v -> {
int num = Integer.parseInt(inputNumber.getText().toString());
long fact = 1;
for (int i = 1; i <= num; i++) fact *= i;
Intent intent = new Intent(this, ResultActivity.class);
intent.putExtra("result", fact);
startActivity(intent);
});
}
}
ResultActivity.java
package com.example.slip2q1;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class ResultActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
long result = getIntent().getLongExtra("result", 1);
((TextView) findViewById(R.id.resultText)).setText("Factorial: " + result);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Slip2q1"
tools:targetApi="31">
<activity android:name=".ResultActivity" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Q2. Create an Android application that plays an audio(song) in the
background. Audio will not be stopped even if you switch to another
activity. To stop the audio, you need to stop the service
Create drectory(raw) Add song file into res/raw/song.mp3
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">
<Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Audio" />
<Button
android:id="@+id/stopButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop Audio"
android:layout_marginTop="16dp" />
<Button
android:id="@+id/nextActivityButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to Next Activity"
android:layout_marginTop="16dp" />
</LinearLayout>
MainActivity.java
package com.example.slip2q2audio;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startButton = findViewById(R.id.startButton);
Button stopButton = findViewById(R.id.stopButton);
Button nextActivityButton = findViewById(R.id.nextActivityButton);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, AudioService.class);
startService(intent);
}
});
stopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, AudioService.class);
stopService(intent);
}
});
nextActivityButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, AnotherActivity.class);
startActivity(intent);
}
});
}
}
AnotherActivity.java
package com.example.slip2q2audio;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class AnotherActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
}
}
AudioService.java
package com.example.slip2q2audio;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
public class AudioService extends Service {
private MediaPlayer mediaPlayer;
@Override
public void onCreate() {
super.onCreate();
mediaPlayer = MediaPlayer.create(this, R.raw.song);
mediaPlayer.setLooping(true);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mediaPlayer.start();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
activity_another.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:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Another Activity"
android:textSize="24sp" />
</LinearLayout
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.slip2q2audio">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Slip2q2audio">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AnotherActivity" />
<service android:name=".AudioService" />
</application>
</manifest>
Slip 3:
Q1. Create an Android Application that will change color of the College
Name on click of Push Button and change the font size, font style of
text view using xml.
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/collegeNameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Haribhai V. Desai, Pune"
android:textSize="24sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_marginBottom="20dp"/>
<Button
android:id="@+id/changeTextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Text Appearance" />
</LinearLayout>
MainActivity.java
package com.example.slip3q1;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
public TextView collegeNameTextView;
public Button changeTextButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
collegeNameTextView = findViewById(R.id.collegeNameTextView);
changeTextButton = findViewById(R.id.changeTextButton);
changeTextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
collegeNameTextView.setTextColor(Color.BLUE);
collegeNameTextView.setTextSize(30);
collegeNameTextView.setTypeface(null, Typeface.BOLD_ITALIC);
}
});
}
}
Q2. Create an Android Application to find the factorial of a number and
Display the Result on Alert Box.
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:gravity="center"
android:padding="20dp">
<EditText
android:id="@+id/etNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter a number"
android:inputType="number" />
<Button
android:id="@+id/btnCalculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Find Factorial"
android:layout_gravity="center" />
</LinearLayout>
MainActivity.java
package com.example.slip3q2b;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
EditText etNumber;
Button btnCalculate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etNumber = findViewById(R.id.etNumber);
btnCalculate = findViewById(R.id.btnCalculate);
btnCalculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculateFactorial();
}
});
}
private void calculateFactorial() {
String input = etNumber.getText().toString();
if (input.isEmpty()) {
showAlert("Error", "Please enter a number");
return;
}
int number = Integer.parseInt(input);
long factorial = 1;
for (int i = 1; i <= number; i++) {
factorial *= i;
}
showAlert("Factorial Result", "Factorial of " + number + " is " + factorial);
}
private void showAlert(String title, String message) {
new AlertDialog.Builder(this)
.setTitle(title)
.setMessage(message)
.setPositiveButton("OK", null)
.show();
}
}
Slip 4:
Q1. Create a Simple Application, that performs Arithmetic Operations.
(Use constraint
layout)
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<EditText
android:id="@+id/num1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter first number"
android:inputType="numberDecimal"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="50dp"/>
<EditText
android:id="@+id/num2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter second number"
android:inputType="numberDecimal"
app:layout_constraintTop_toBottomOf="@id/num1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
app:layout_constraintTop_toBottomOf="@id/num2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
app:layout_constraintTop_toBottomOf="@id/add"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/mul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="×"
app:layout_constraintTop_toBottomOf="@id/sub"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/div"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="÷"
app:layout_constraintTop_toBottomOf="@id/mul"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result: "
android:textSize="20sp"
app:layout_constraintTop_toBottomOf="@id/div"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="20dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.slip4q1;
import android.os.Bundle;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
EditText num1, num2;
TextView result;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1 = findViewById(R.id.num1);
num2 = findViewById(R.id.num2);
result = findViewById(R.id.result);
findViewById(R.id.add).setOnClickListener(v -> calculate('+'));
findViewById(R.id.sub).setOnClickListener(v -> calculate('-'));
findViewById(R.id.mul).setOnClickListener(v -> calculate('*'));
findViewById(R.id.div).setOnClickListener(v -> calculate('/'));
}
private void calculate(char op) {
double n1 = Double.parseDouble(num1.getText().toString());
double n2 = Double.parseDouble(num2.getText().toString());
double res = (op == '+') ? n1 + n2 : (op == '-') ? n1 - n2 : (op == '*') ? n1 *
n2 : (n2 != 0 ? n1 / n2 : 0);
result.setText("Result: " + res);
}
}
Q2. Create an android Application for performing the following
operation on the table
Customer (id, name, address, phno). (use SQLite database)
i) Insert New Customer Details.
ii) Show All the Customer Details on Toast Message.
Slip 5:
Q1. Create an Android Application to accept two numbers and find
power and Average.
Display the result on the next activity on Button click.
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:gravity="center"
android:padding="20dp">
<EditText android:id="@+id/etNum1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter first number"
android:inputType="numberDecimal" />
<EditText android:id="@+id/etNum2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter second number"
android:inputType="numberDecimal" />
<Button android:id="@+id/btnCalculate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Calculate" />
</LinearLayout>
activity_result.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:gravity="center"
android:padding="20dp">
<TextView android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Results will be shown here" />
</LinearLayout>
MainActivity.java
package com.example.slip5q1;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
EditText etNum1, etNum2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etNum1 = findViewById(R.id.etNum1);
etNum2 = findViewById(R.id.etNum2);
Button btnCalculate = findViewById(R.id.btnCalculate);
btnCalculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double num1 = Double.parseDouble(etNum1.getText().toString());
double num2 = Double.parseDouble(etNum2.getText().toString());
double power = Math.pow(num1, num2);
double average = (num1 + num2) / 2;
Intent intent = new Intent(MainActivity.this, ResultActivity.class);
intent.putExtra("POWER", power);
intent.putExtra("AVERAGE", average);
startActivity(intent);
}
});
}
}
ResultActivity.java
package com.example.slip5q1;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class ResultActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
TextView tvResult = findViewById(R.id.tvResult);
double power = getIntent().getDoubleExtra("POWER", 0);
double average = getIntent().getDoubleExtra("AVERAGE", 0);
tvResult.setText("Power: " + power + "\nAverage: " + average);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.slip5q1">
<application
android:allowBackup="true"
android:theme="@style/Theme.AppCompat.Light"
android:supportsRtl="true"
android:label="Power and Average App">
<activity android:name=".ResultActivity" />
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Q2. Create an Android application that creates a custom Alert Dialog
containing Friends Name and on Click of Friend Name Button greet
accordingly.
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:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/btnShowFriends"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Friends" />
</LinearLayout>
MainActivity.java
package com.example.slip5q2option;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private String[] friends = {"Amit", "sakshi", "kirti", "Sumit"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnShowFriends = findViewById(R.id.btnShowFriends);
btnShowFriends.setOnClickListener(v -> showCustomDialog());
}
private void showCustomDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogView = inflater.inflate(R.layout.activity_second, null);
builder.setView(dialogView);
LinearLayout friendsContainer =
dialogView.findViewById(R.id.friendsContainer);
for (String friend : friends) {
Button friendButton = new Button(this);
friendButton.setText(friend);
friendButton.setOnClickListener(v -> showGreeting(friend));
friendsContainer.addView(friendButton);
}
AlertDialog dialog = builder.create();
dialog.show();
}
private void showGreeting(String friendName) {
new AlertDialog.Builder(this)
.setTitle("Greeting")
.setMessage("Hello, " + friendName + "! Have a great day 😊")
.setPositiveButton("OK", null)
.show();
}
}
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select a Friend"
android:textSize="18sp"
android:textStyle="bold"
android:paddingBottom="10dp" />
<LinearLayout
android:id="@+id/friendsContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"/>
</LinearLayout>

Slip 6:
Q1. Create a Simple Application Which Send ―Hello! message from one
activity to another with help of Button (Use Intent).
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:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Message" />
</LinearLayout>
activity_second.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:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message will appear here"
android:textSize="20sp" />
</LinearLayout>
MainActivity.java
package com.example.slip6q1;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendButton = findViewById(R.id.sendButton);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("message", "Hello!");
startActivity(intent);
}
});
}
}
SecondActivity.java
package com.example.slip6q1;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView textView = findViewById(R.id.textView);
String message = getIntent().getStringExtra("message");
textView.setText(message);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.slip6q1">
<application
android:allowBackup="true"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"/>
</application>
</manifest>
Q2. Create an Android Application that Demonstrates List View and
Onclick of List Display the Toast.
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="20dp">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity.java
package com.example.slip6q2;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = findViewById(R.id.listView);
String[] items = {"Apple", "Banana", "Cherry", "Date", "Mango", "Orange"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String selectedItem = items[position];
Toast.makeText(MainActivity.this, "Clicked: " + selectedItem,
Toast.LENGTH_SHORT).show();
}
});
}
}
Slip 7:
Q1. Create an Android Application that Demonstrate Radio Button.
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="20dp"
android:gravity="center">
<RadioGroup
android:id="@+id/genderGroup"
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>
</LinearLayout>
MainActivity.java
package com.example.slip7q1;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioGroup genderGroup = findViewById(R.id.genderGroup);
genderGroup.setOnCheckedChangeListener(new
RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton selectedRadioButton = findViewById(checkedId);
if (selectedRadioButton != null) {
String gender = selectedRadioButton.getText().toString();
Toast.makeText(MainActivity.this, "Selected: " + gender,
Toast.LENGTH_SHORT).show();
}
}
});
}
}
Q2.Create an Android application to demonstrate phone call using
Implicit Intent.
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:gravity="center"
android:padding="16dp">
<EditText
android:id="@+id/etPhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Phone Number"
android:inputType="phone" />
<Button
android:id="@+id/btnCall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Make Call" />
</LinearLayout>
MainActivity.java
package com.example.slip7q2;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.pm.PackageManager;
public class MainActivity extends AppCompatActivity {
private EditText etPhoneNumber;
private Button btnCall;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etPhoneNumber = findViewById(R.id.etPhoneNumber);
btnCall = findViewById(R.id.btnCall);
btnCall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
makePhoneCall();
}
});
}
private void makePhoneCall() {
String phoneNumber = etPhoneNumber.getText().toString();
if (phoneNumber.isEmpty()) {
Toast.makeText(this, "Please enter a phone number",
Toast.LENGTH_SHORT).show();
} else {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions(this, new String[]


{Manifest.permission.CALL_PHONE}, 1);
} else {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + phoneNumber));
startActivity(intent);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
makePhoneCall();
} else {
Toast.makeText(this, "Permission denied to make phone calls",
Toast.LENGTH_SHORT).show();
}
}
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.slip7q2">
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:allowBackup="true"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Slip 8:
Q1. Create an Android App with Login Screen. On successful login, gives
message go to next Activity (Without Using Database& use Table
Layout).
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:gravity="center">
<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="text"
android:layout_marginBottom="8dp"/>
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:layout_below="@id/editTextUsername"
android:layout_marginBottom="16dp"/>
<Button
android:id="@+id/buttonLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editTextPassword"
android:text="Login"
android:layout_marginTop="8dp"/>
</RelativeLayout>
activity_next.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<TextView
android:id="@+id/textViewMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="welcome to the next activity"
android:layout_centerInParent="true"/>
</RelativeLayout>
MainActivity.java
package com.example.slip8q1;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText editTextUsername, editTextPassword;
private Button buttonLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextUsername = findViewById(R.id.editTextUsername);
editTextPassword = findViewById(R.id.editTextPassword);
buttonLogin = findViewById(R.id.buttonLogin);
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = editTextUsername.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
if (!username.isEmpty() && !password.isEmpty()) {
if (username.equals("user") && password.equals("pass")) {
Toast.makeText(MainActivity.this, "Login Successful! Go to next
activity.", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,
com.example.slip8q1.NextActivity.class);
startActivity(intent);
} else {
Toast.makeText(MainActivity.this, "invalid username and
password.", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
NextActivity.java
package com.example.slip8q1;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class NextActivity extends AppCompatActivity{
private TextView textViewMessage;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
textViewMessage = findViewById(R.id.textViewMessage);
textViewMessage.setText("welcome to the next activity!");
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Slip8q1"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
<activity android:name=".NextActivity"/>
</application>
</manifest>
Q2. Create application to send email with attachment.
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:gravity="center"
android:padding="16dp">
<EditText
android:id="@+id/emailEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/subjectEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Subject"
android:inputType="text" />
<EditText
android:id="@+id/messageEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Message"
android:inputType="textMultiLine"
android:minLines="3" />
<Button
android:id="@+id/attachButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Attach File" />
<Button
android:id="@+id/sendEmailButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send Email" />
</LinearLayout>
MainActivity.java
package com.example.slip8q2;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText emailEditText, subjectEditText, messageEditText;
private Button attachButton, sendEmailButton;
private Uri fileUri = null;
private final ActivityResultLauncher<Intent> filePickerLauncher =
registerForActivityResult(new
ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == RESULT_OK && result.getData() !=
null) {
fileUri = result.getData().getData();
Toast.makeText(this, "File Selected: " +
fileUri.getLastPathSegment(), Toast.LENGTH_SHORT).show();
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
emailEditText = findViewById(R.id.emailEditText);
subjectEditText = findViewById(R.id.subjectEditText);
messageEditText = findViewById(R.id.messageEditText);
attachButton = findViewById(R.id.attachButton);
sendEmailButton = findViewById(R.id.sendEmailButton);
attachButton.setOnClickListener(v -> selectFile());
sendEmailButton.setOnClickListener(v -> sendEmail());
}
private void selectFile() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
filePickerLauncher.launch(intent);
}
private void sendEmail() {
String email = emailEditText.getText().toString().trim();
String subject = subjectEditText.getText().toString().trim();
String message = messageEditText.getText().toString().trim();
if (email.isEmpty() || subject.isEmpty() || message.isEmpty()) {
Toast.makeText(this, "Please fill all fields", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(this, "Email sent", Toast.LENGTH_LONG).show();
}
}
Slip 9:
Q1. Write an Android application to accept two numbers from the user,
and display them, but reject input if both numbers are greater than 10
and asks for two new numbers.
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:gravity="center"
android:padding="20dp">
<EditText
android:id="@+id/num1EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Number 1"
android:inputType="number" />
<EditText
android:id="@+id/num2EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Number 2"
android:inputType="number"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/validateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Validate"
android:layout_marginTop="20dp"/>
</LinearLayout>
MainActivity.java
package com.example.slip9q1;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
EditText num1EditText, num2EditText;
Button validateButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1EditText = findViewById(R.id.num1EditText);
num2EditText = findViewById(R.id.num2EditText);
validateButton = findViewById(R.id.validateButton);
validateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String num1String = num1EditText.getText().toString();
String num2String = num2EditText.getText().toString();
if (num1String.isEmpty() || num2String.isEmpty()) {
Toast.makeText(MainActivity.this, "Please enter both numbers",
Toast.LENGTH_SHORT).show();
return;
}
int num1 = Integer.parseInt(num1String);
int num2 = Integer.parseInt(num2String);
if (num1 > 10 && num2 > 10) {
Toast.makeText(MainActivity.this, "Both numbers are greater than
10. Please enter new numbers.", Toast.LENGTH_LONG).show();
num1EditText.setText("");
num2EditText.setText("");
} else {
Toast.makeText(MainActivity.this, "Number 1: " + num1 + "\
nNumber 2: " + num2, Toast.LENGTH_LONG).show();
}
}
});
}
}
Q2. Create table Company (id, name, address, phno). Create Application
for Performing the
following operation on the table.
a) Insert New Company details.
b) Show All Company details
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">
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Company Name" />
<EditText
android:id="@+id/address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Company Address" />
<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone Number"
android:inputType="phone" />
<Button
android:id="@+id/insertButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Insert Company"
android:layout_marginTop="16dp" />
<Button
android:id="@+id/showButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show All Companies"
android:layout_marginTop="16dp" />
</LinearLayout>
activity_show_companies.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/companiesList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Companies List"
android:textSize="18sp"
android:gravity="center" />
</LinearLayout>
MainActivity.java
package com.example.slip9q2;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText name, address, phone;
private Button insertButton, showButton;
private DatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = findViewById(R.id.name);
address = findViewById(R.id.address);
phone = findViewById(R.id.phone);
insertButton = findViewById(R.id.insertButton);
showButton = findViewById(R.id.showButton);
dbHelper = new DatabaseHelper(this);
insertButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String companyName = name.getText().toString();
String companyAddress = address.getText().toString();
String companyPhone = phone.getText().toString();
if (companyName.isEmpty() || companyAddress.isEmpty() ||
companyPhone.isEmpty()) {
Toast.makeText(MainActivity.this, "Please fill all fields",
Toast.LENGTH_SHORT).show();
} else {
boolean isInserted = dbHelper.insertCompany(companyName,
companyAddress, companyPhone);
if (isInserted) {
Toast.makeText(MainActivity.this, "Company inserted
successfully", Toast.LENGTH_SHORT).show();
name.setText("");
address.setText("");
phone.setText("");
} else {
Toast.makeText(MainActivity.this, "Failed to insert company",
Toast.LENGTH_SHORT).show();
}
}
}
});
showButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,
ShowCompaniesActivity.class);
startActivity(intent);
}
});
}
}
ShowCompaniesActivity.java
package com.example.slip9q2;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class ShowCompaniesActivity extends AppCompatActivity {
private TextView companiesList;
private DatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_companies);
companiesList = findViewById(R.id.companiesList);
dbHelper = new DatabaseHelper(this);
displayCompanies();
}
private void displayCompanies() {
Cursor cursor = dbHelper.getAllCompanies();
StringBuilder data = new StringBuilder();
if (cursor.getCount() == 0) {
data.append("No companies found");
} else {
while (cursor.moveToNext()) {
data.append("ID: ").append(cursor.getInt(0)).append("\n");
data.append("Name: ").append(cursor.getString(1)).append("\n");
data.append("Address: ").append(cursor.getString(2)).append("\n");
data.append("Phone: ").append(cursor.getString(3)).append("\n\n");
}
}
companiesList.setText(data.toString());
}
}
DatabaseHelper.java
package com.example.slip9q2;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "CompanyDB";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "Company";
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_ADDRESS = "address";
private static final String COLUMN_PHONE = "phno";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTableQuery = "CREATE TABLE " + TABLE_NAME + " ("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_NAME + " TEXT, "
+ COLUMN_ADDRESS + " TEXT, "
+ COLUMN_PHONE + " TEXT)";
db.execSQL(createTableQuery);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean insertCompany(String name, String address, String phone) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, name);
values.put(COLUMN_ADDRESS, address);
values.put(COLUMN_PHONE, phone);
long result = db.insert(TABLE_NAME, null, values);
return result != -1;
}
public Cursor getAllCompanies() {
SQLiteDatabase db = this.getReadableDatabase();
return db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.slip9q2">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light">
<activity android:name=".ShowCompaniesActivity" />
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Slip 10:
Q1. Create an Android Application that Demonstrate Switch and Toggle
Button.
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:gravity="center"
android:padding="20dp">
<Switch
android:id="@+id/switchBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Switch"/>
<ToggleButton
android:id="@+id/toggleBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="ON"
android:textOff="OFF"/>
<TextView
android:id="@+id/statusText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Status: OFF"
android:textSize="18sp"
android:paddingTop="10dp"/>
</LinearLayout>
MainActivity.java
package com.example.slip10q1;
import android.os.Bundle;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.ToggleButton;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Switch switchBtn = findViewById(R.id.switchBtn);
ToggleButton toggleBtn = findViewById(R.id.toggleBtn);
TextView statusText = findViewById(R.id.statusText);
switchBtn.setOnCheckedChangeListener((btn, isChecked) ->
statusText.setText("Switch: " + (isChecked ? "ON" : "OFF")));
toggleBtn.setOnCheckedChangeListener((btn, isChecked) ->
statusText.setText("Toggle: " + (isChecked ? "ON" : "OFF")));
}
}
Q2. Demonstrate Array Adapter using List View to display list of fruits.
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="10dp">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity.java
package com.example.slip10q2;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = findViewById(R.id.listView);
String[] fruits = {"Apple", "Banana", "Cherry", "Grapes", "Mango", "Orange",
"Pineapple"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, fruits);
listView.setAdapter(adapter);
listView.setOnItemClickListener((parent, view, position, id) ->
Toast.makeText(this, "Selected: " + fruits[position],
Toast.LENGTH_SHORT).show());
}
}
Slip 11:
Q.1 Create android application to change Font Size, Color and Font
Family of String.
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:gravity="center"
android:padding="20dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hiii Amit"
android:textSize="20sp"
android:textColor="#000000"
android:fontFamily="sans-serif"
android:padding="20dp"/>
<Spinner
android:id="@+id/colorSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Spinner
android:id="@+id/fontSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<SeekBar
android:id="@+id/sizeSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="50"/>
</LinearLayout>
MainActivity.java
package com.example.slip11q1;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.SeekBar;
import android.widget.Spinner;
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);
TextView textView = findViewById(R.id.textView);
Spinner colorSpinner = findViewById(R.id.colorSpinner);
Spinner fontSpinner = findViewById(R.id.fontSpinner);
SeekBar sizeSeekBar = findViewById(R.id.sizeSeekBar);
String[] colors = {"Black", "Red", "Blue", "Green"};
String[] fonts = {"sans-serif", "serif", "monospace"};
ArrayAdapter<String> colorAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_dropdown_item, colors);
ArrayAdapter<String> fontAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_dropdown_item, fonts);
colorSpinner.setAdapter(colorAdapter);
fontSpinner.setAdapter(fontAdapter);
colorSpinner.setOnItemSelectedListener(new
android.widget.AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(android.widget.AdapterView<?> parent,
android.view.View view, int position, long id) {
switch (position) {
case 0: textView.setTextColor(Color.BLACK); break;
case 1: textView.setTextColor(Color.RED); break;
case 2: textView.setTextColor(Color.BLUE); break;
case 3: textView.setTextColor(Color.GREEN); break;
}
}
@Override public void onNothingSelected(android.widget.AdapterView<?
> parent) {}
});
fontSpinner.setOnItemSelectedListener(new
android.widget.AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(android.widget.AdapterView<?> parent,
android.view.View view, int position, long id) {
textView.setTypeface(android.graphics.Typeface.create(fonts[position],
android.graphics.Typeface.NORMAL));
}
@Override public void onNothingSelected(android.widget.AdapterView<?
> parent) {}
});
sizeSeekBar.setOnSeekBarChangeListener(new
SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean
fromUser) {
textView.setTextSize(progress + 10);
}
@Override public void onStartTrackingTouch(SeekBar seekBar) {}
@Override public void onStopTrackingTouch(SeekBar seekBar) {}
});
}
}
Q.2 Create First Activity to accept information like Student First Name,
Middle Name, Last Name, Date of birth, Address, Email ID and display
all information on Second Activity when user click on the Submit
button.
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:gravity="center"
android:padding="20dp">
<EditText
android:id="@+id/firstName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="First Name" />
<EditText
android:id="@+id/middleName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Middle Name" />
<EditText
android:id="@+id/lastName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Last Name" />
<EditText
android:id="@+id/dob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Date of Birth" />
<EditText
android:id="@+id/address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Address" />
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email ID" />
<Button
android:id="@+id/submitBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
activity_second.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:gravity="center"
android:padding="20dp">
<TextView
android:id="@+id/displayInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Student Information"
android:padding="10dp" />
</LinearLayout>
MainActivity.java
package com.example.slip11q2;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText firstName = findViewById(R.id.firstName);
EditText middleName = findViewById(R.id.middleName);
EditText lastName = findViewById(R.id.lastName);
EditText dob = findViewById(R.id.dob);
EditText address = findViewById(R.id.address);
EditText email = findViewById(R.id.email);
Button submitBtn = findViewById(R.id.submitBtn);
submitBtn.setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("firstName", firstName.getText().toString());
intent.putExtra("middleName", middleName.getText().toString());
intent.putExtra("lastName", lastName.getText().toString());
intent.putExtra("dob", dob.getText().toString());
intent.putExtra("address", address.getText().toString());
intent.putExtra("email", email.getText().toString());
startActivity(intent);
});
}
}
SecondActivity.java
package com.example.slip11q2;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView displayInfo = findViewById(R.id.displayInfo);
String firstName = getIntent().getStringExtra("firstName");
String middleName = getIntent().getStringExtra("middleName");
String lastName = getIntent().getStringExtra("lastName");
String dob = getIntent().getStringExtra("dob");
String address = getIntent().getStringExtra("address");
String email = getIntent().getStringExtra("email");
String studentDetails = "Name: " + firstName + " " + middleName + " " +
lastName +
"\nDate of Birth: " + dob +
"\nAddress: " + address +
"\nEmail: " + email;
displayInfo.setText(studentDetails);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Slip11q2"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity" />
</application>
</manifest>
Slip 12:
Q1.Create a Simple Application Which Send ―Hi‖ message from one
activity to another with help of Button (Use Intent).
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:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Message" />
</LinearLayout>
activity_second.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:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message will appear here"
android:textSize="20sp" />
</LinearLayout>
MainActivity.java
package com.example.slip6q1;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendButton = findViewById(R.id.sendButton);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("message", "Hello!");
startActivity(intent);
}
});
}
}
SecondActivity.java
package com.example.slip6q1;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView textView = findViewById(R.id.textView);
String message = getIntent().getStringExtra("message");
textView.setText(message);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.slip6q1">
<application
android:allowBackup="true"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"/>
</application>
</manifest>
Q.2 Create an application to demonstrate date and time picker.
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">
<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:calendarViewShown="false"
android:datePickerMode="spinner"
android:layout_marginBottom="16dp"/>
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timePickerMode="clock"
android:layout_marginBottom="16dp"/>
<Button
android:id="@+id/showButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Date Time"
android:layout_gravity="center"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Selected Date and Time will appear here"
android:textSize="18sp"
android:layout_marginBottom="16dp"/>
</LinearLayout>
MainActivity.java
package com.example.slip12q2b;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
TextView textView;
DatePicker datePicker;
TimePicker timePicker;
Button showButton;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
datePicker = findViewById(R.id.datePicker);
timePicker = findViewById(R.id.timePicker);
showButton = findViewById(R.id.showButton);
showButton.setOnClickListener(v -> {
int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth() + 1;
int year = datePicker.getYear();
int hour = timePicker.getHour();
int minute = timePicker.getMinute();
String selectedDateTime = "Selected Date: " + day + "/" + month + "/" +
year +
"\nSelected Time: " + hour + ":" + String.format("%02d", minute);
textView.setText(selectedDateTime);
});
}
}
Slip 13:
Q1. Create following Vertical Scroll View Creation in Android.
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="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical"
android:padding="30dp">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:text="Button 1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:text="Button 2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:text="Button 3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:text="Button 4" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:text="Button 5" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:text="Button 6" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:text="Button 7" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:text="Button 8" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:text="Button 9" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:text="Button 10" />
</LinearLayout>
</ScrollView>
MainActivity.java
package com.example.slip13;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Q2. Write an application to accept a teacher name from user and
display the names of students along
with subjects to whom they are teaching.
Create table Student (sno , s_name,s_class,s_addr)
Teacher (tno, t_name, qualification, experience)
Student-Teacher has Many to Many relationship.
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:gravity="center"
android:padding="16dp">
<EditText
android:id="@+id/teacherNameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Teacher Name"
android:inputType="text" />
<Button
android:id="@+id/showStudentsButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Students" />
<TextView
android:id="@+id/tvStudentDetails"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:textSize="16sp"
android:text="Student details will appear here..."
android:gravity="start"/>
</LinearLayout>
MainActivity.java
package com.example.slip13q2stud;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
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 {
private EditText teacherNameEditText;
private Button showStudentsButton;
private TextView tvStudentDetails;
private DatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
teacherNameEditText = findViewById(R.id.teacherNameEditText);
showStudentsButton = findViewById(R.id.showStudentsButton);
tvStudentDetails = findViewById(R.id.tvStudentDetails);
dbHelper = new DatabaseHelper(this);
insertSampleData();
showStudentsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showStudents();
}
});
}
private void insertSampleData() {
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT COUNT(*) FROM Teacher", null);
cursor.moveToFirst();
int count = cursor.getInt(0);
cursor.close();
if (count > 0) {
return;
}
dbHelper.insertTeacher("Mr. Sharma", "M.Sc. Mathematics", 10);
dbHelper.insertTeacher("Ms. Verma", "M.A. English", 8);
dbHelper.insertStudent("Rahul", "10th", "Delhi");
dbHelper.insertStudent("Priya", "12th", "Mumbai");
dbHelper.insertStudent("Karan", "10th", "Pune");
dbHelper.insertStudentTeacher(1, 1, "Mathematics");
dbHelper.insertStudentTeacher(1, 3, "Mathematics");
dbHelper.insertStudentTeacher(2, 2, "English");
}
private void showStudents() {
String teacherName = teacherNameEditText.getText().toString().trim();
if (teacherName.isEmpty()) {
Toast.makeText(this, "Please enter a teacher name",
Toast.LENGTH_SHORT).show();
return;
}
Cursor cursor = dbHelper.getStudentsByTeacherName(teacherName);
StringBuilder studentDetails = new StringBuilder();
if (cursor.moveToFirst()) {
do {
String studentName = cursor.getString(0);
String subject = cursor.getString(1);
studentDetails.append("Student: ").append(studentName)
.append(" | Subject: ").append(subject).append("\n");
} while (cursor.moveToNext());
} else {
studentDetails.append("No students found for ").append(teacherName);
}
cursor.close();
tvStudentDetails.setText(studentDetails.toString());
}
@Override
protected void onDestroy() {
dbHelper.close();
super.onDestroy();
}
}
DatabaseHelper.java
package com.example.slip13q2stud;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "SchoolDB.db";
private static final int DATABASE_VERSION = 1;
public static final String TABLE_STUDENT = "Student";
public static final String COLUMN_SNO = "sno";
public static final String COLUMN_SNAME = "s_name";
public static final String COLUMN_SCLASS = "s_class";
public static final String COLUMN_SADDR = "s_addr";
public static final String TABLE_TEACHER = "Teacher";
public static final String COLUMN_TNO = "tno";
public static final String COLUMN_TNAME = "t_name";
public static final String COLUMN_QUALIFICATION = "qualification";
public static final String COLUMN_EXPERIENCE = "experience";
public static final String TABLE_STUDENT_TEACHER = "StudentTeacher";
public static final String COLUMN_SUBJECT = "subject";
private static final String CREATE_STUDENT_TABLE = "CREATE TABLE " +
TABLE_STUDENT + " (" +
COLUMN_SNO + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_SNAME + " TEXT, " +
COLUMN_SCLASS + " TEXT, " +
COLUMN_SADDR + " TEXT)";
private static final String CREATE_TEACHER_TABLE = "CREATE TABLE " +
TABLE_TEACHER + " (" +
COLUMN_TNO + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_TNAME + " TEXT, " +
COLUMN_QUALIFICATION + " TEXT, " +
COLUMN_EXPERIENCE + " INTEGER)";
private static final String CREATE_STUDENT_TEACHER_TABLE = "CREATE
TABLE " + TABLE_STUDENT_TEACHER + " (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_TNO + " INTEGER, " +
COLUMN_SNO + " INTEGER, " +
COLUMN_SUBJECT + " TEXT, " +
"FOREIGN KEY(" + COLUMN_TNO + ") REFERENCES " + TABLE_TEACHER
+ "(" + COLUMN_TNO + "), " +
"FOREIGN KEY(" + COLUMN_SNO + ") REFERENCES " + TABLE_STUDENT
+ "(" + COLUMN_SNO + "))";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void insertTeacher(String name, String qualification, int experience) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_TNAME, name);
values.put(COLUMN_QUALIFICATION, qualification);
values.put(COLUMN_EXPERIENCE, experience);
db.insert(TABLE_TEACHER, null, values);
db.close();
}
public void insertStudent(String name, String sClass, String address) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_SNAME, name);
values.put(COLUMN_SCLASS, sClass);
values.put(COLUMN_SADDR, address);
db.insert(TABLE_STUDENT, null, values);
db.close();
}
public void insertStudentTeacher(int tno, int sno, String subject) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_TNO, tno);
values.put(COLUMN_SNO, sno);
values.put(COLUMN_SUBJECT, subject);
db.insert(TABLE_STUDENT_TEACHER, null, values);
db.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_STUDENT_TABLE);
db.execSQL(CREATE_TEACHER_TABLE);
db.execSQL(CREATE_STUDENT_TEACHER_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_STUDENT_TEACHER);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TEACHER);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_STUDENT);
onCreate(db);
}
public Cursor getStudentsByTeacherName(String teacherName) {
SQLiteDatabase db = this.getReadableDatabase();
return db.rawQuery("SELECT S." + COLUMN_SNAME + ", ST." +
COLUMN_SUBJECT +
" FROM " + TABLE_STUDENT_TEACHER + " ST " +
" JOIN " + TABLE_STUDENT + " S ON ST." + COLUMN_SNO + " = S." +
COLUMN_SNO +
" JOIN " + TABLE_TEACHER + " T ON ST." + COLUMN_TNO + " = T." +
COLUMN_TNO +
" WHERE T." + COLUMN_TNAME + " = ?", new String[]{teacherName});
}
}
Slip 14:
Q1. Create a Simple Application which shows Life Cycle of Activity {Use
log}.
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="20sp"
android:textStyle="bold"/>
</LinearLayout>
MainActivity.java
package com.example.slip1q1;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
String tag="Events";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(tag, "in on create event");
}
public void onStart()
{
super.onStart();
Log.d(tag,"in the onStart() event");
}
public void onRestart()
{
super.onRestart();
Log.d(tag,"in the onRestart () event");
}
public void onResume()
{
super.onResume();
Log.d(tag,"in the onResume() event");
}
public void onPause()
{
super.onPause();
Log.d(tag,"in the onPause() event");
}
public void onStop()
{
super.onStop();
Log.d(tag,"in the onStop() event");
}
public void onDestroy()
{
super.onDestroy();
Log.d(tag,"in the onDestroy() event");
}
}
Q2. Create the following layout which is changing android spinner text
size with styles.
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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Coffee?"
android:textSize="20sp"
android:textStyle="bold" />
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:entries="@array/coffee_options"
android:spinnerMode="dropdown"
style="@style/SpinnerTextStyle" />
</LinearLayout>
MainActivity.java
package com.example.slip14q2;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
styles.xml file location (app/res/values/styles.xml)
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="SpinnerTextStyle">
<item name="android:textSize">40sp</item>
<item name="android:textStyle">italic</item>
</style>
</resources>
strings.xml file location (app/res/values/strings.xml)
<resources>
<string name="app_name">slip14q2</string>
<string-array name="coffee_options">
<item>Filter</item>
<item>Latte</item>
<item>Cappuccino</item>
<item>Espresso</item>
<item>Filter Cappuccino</item>
<item>Mocha</item>
<item>Skinny</item>
<item>Espresso Corretto</item>
</string-array>
</resources>
Slip 15:
Q1. Design following-add a border to an Android Layout.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="35sp"
android:textColor="@color/white"/>
</LinearLayout>
MainActivity.java
package com.example.slip15q1;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.widget.LinearLayout;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout rootLayout = findViewById(R.id.rootLayout);
GradientDrawable borderDrawable = new GradientDrawable();
borderDrawable.setStroke(65, Color.WHITE);
borderDrawable.setCornerRadius(75f);
borderDrawable.setColor(Color.BLUE);
rootLayout.setBackground(borderDrawable);
}
}
Q2. Create simple application with Login Screen. On successful login,
gives message go to next Activity (Without Using Database).
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="text"
android:layout_marginBottom="8dp"/>
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:layout_below="@id/editTextUsernamex"
android:layout_marginBottom="16dp"/>
<Button
android:id="@+id/buttonLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editTextPassword"
android:text="Login"
android:layout_marginTop="8dp"/>
</RelativeLayout>
activity_next.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<TextView
android:id="@+id/textViewMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="welcome to the next activity"
android:layout_centerInParent="true"/>
</RelativeLayout>
MainActivity.java
package com.example.login;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText editTextUsername, editTextPassword;
private Button buttonLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextUsername = findViewById(R.id.editTextUsername);
editTextPassword = findViewById(R.id.editTextPassword);
buttonLogin = findViewById(R.id.buttonLogin);
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = editTextUsername.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
if (!username.isEmpty() && !password.isEmpty()) {
if (username.equals("user") && password.equals("pass")) {
Toast.makeText(MainActivity.this, "Login Successful! Go to next
activity.", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,
com.example.login.NextActivity.class);
startActivity(intent);
} else {
Toast.makeText(MainActivity.this, "invalid username and
password.", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
NextActivity.java
package com.example.login;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class NextActivity extends AppCompatActivity{
private TextView textViewMessage;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
textViewMessage = findViewById(R.id.textViewMessage);
textViewMessage.setText("welcome to the next activity!");
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Login"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NextActivity"/>
</application>
</manifest>
Slip 16:
Q1. Create an Android App, it reads the Students Details (Name,
Surname, Class, Gender, Hobbies, Marks) and display the all information
in another activity in table format on click of Submit button.
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:gravity="center"
android:padding="16dp">
<EditText
android:id="@+id/edtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name" />
<EditText
android:id="@+id/edtSurname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Surname" />
<EditText
android:id="@+id/edtClass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Class" />
<EditText
android:id="@+id/edtGender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Gender" />
<EditText
android:id="@+id/edtHobbies"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Hobbies" />
<EditText
android:id="@+id/edtMarks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Marks" />
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
activity_display.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">
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1" />
</LinearLayout>
MainActivity.java
package com.example.slip3q2b;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
EditText edtName, edtSurname, edtClass, edtGender, edtHobbies, edtMarks;
Button btnSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtName = findViewById(R.id.edtName);
edtSurname = findViewById(R.id.edtSurname);
edtClass = findViewById(R.id.edtClass);
edtGender = findViewById(R.id.edtGender);
edtHobbies = findViewById(R.id.edtHobbies);
edtMarks = findViewById(R.id.edtMarks);
btnSubmit = findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = edtName.getText().toString();
String surname = edtSurname.getText().toString();
String studentClass = edtClass.getText().toString();
String gender = edtGender.getText().toString();
String hobbies = edtHobbies.getText().toString();
String marks = edtMarks.getText().toString();
Intent intent = new Intent(MainActivity.this, DisplayActivity.class);
intent.putExtra("NAME", name);
intent.putExtra("SURNAME", surname);
intent.putExtra("CLASS", studentClass);
intent.putExtra("GENDER", gender);
intent.putExtra("HOBBIES", hobbies);
intent.putExtra("MARKS", marks);
startActivity(intent);
}
});
}
}
DisplayActivity.java
package com.example.slip3q2b;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class DisplayActivity extends AppCompatActivity {
TableLayout tableLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
tableLayout = findViewById(R.id.tableLayout);
String name = getIntent().getStringExtra("NAME");
String surname = getIntent().getStringExtra("SURNAME");
String studentClass = getIntent().getStringExtra("CLASS");
String gender = getIntent().getStringExtra("GENDER");
String hobbies = getIntent().getStringExtra("HOBBIES");
String marks = getIntent().getStringExtra("MARKS");
addRow("Name:", name);
addRow("Surname:", surname);
addRow("Class:", studentClass);
addRow("Gender:", gender);
addRow("Hobbies:", hobbies);
addRow("Marks:", marks);
}
private void addRow(String label, String value) {
TableRow row = new TableRow(this);
TextView labelView = new TextView(this);
labelView.setText(label);
row.addView(labelView);
TextView valueView = new TextView(this);
valueView.setText(value);
row.addView(valueView);
tableLayout.addView(row);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.slip3q2b">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Slip3q2b">
<activity android:name=".DisplayActivity"
android:exported="false" />
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Q2. Create an Android Application that Demonstrate Time Picker and
display Selected Time on Text View.
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:gravity="center"
android:padding="20dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Selected Time"
android:textSize="20sp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick Time"
android:layout_marginTop="20dp" />
</LinearLayout>
MainActivity.java
package com.example.slip16q2;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
TextView textView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
button = findViewById(R.id.button);

button.setOnClickListener(v -> showTimePicker());


}
private void showTimePicker() {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog = new TimePickerDialog(this,
(TimePicker view, int hourOfDay, int minuteOfHour) -> {
String selectedTime = hourOfDay + ":" + String.format("%02d",
minuteOfHour);
textView.setText("Selected time "+selectedTime);
}, hour, minute, true);
timePickerDialog.show();
}
}
Slip 17:
Q1. Write an android code to make phone call using Intent.
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="20dp"
android:gravity="center">
<EditText
android:id="@+id/phoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Phone Number"
android:inputType="phone"/>
<Button
android:id="@+id/callButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Make Call"
android:layout_marginTop="20dp"/>
</LinearLayout>
MainActivity.java
package com.example.slip17q1;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
public class MainActivity extends AppCompatActivity {
private static final int CALL_PERMISSION_CODE = 1;
EditText phoneNumber;
Button callButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

phoneNumber = findViewById(R.id.phoneNumber);
callButton = findViewById(R.id.callButton);

callButton.setOnClickListener(v -> {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED)
{
makeCall();
} else {
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.CALL_PHONE}, CALL_PERMISSION_CODE);
}
});
}
private void makeCall() {
String number = phoneNumber.getText().toString().trim();
if (number.isEmpty()) {
Toast.makeText(this, "Enter a phone number",
Toast.LENGTH_SHORT).show();
return;
}

Intent intent = new Intent(Intent.ACTION_CALL);


intent.setData(Uri.parse("tel:" + number));
startActivity(intent);
}
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == CALL_PERMISSION_CODE) {
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
makeCall();
} else {
Toast.makeText(this, "Permission Denied",
Toast.LENGTH_SHORT).show();
}
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-feature
android:name="android.hardware.telephony"
android:required="false" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Slip17q1"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>
</manifest>
Q2. Construct an Android Application to accept a number and calculate
Factorial and Sum of Digits of a given number using Context Menu.
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">
<EditText
android:id="@+id/inputNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter a number"
android:inputType="number"
android:gravity="center"
android:textSize="18sp" />
<TextView
android:id="@+id/resultText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Long press here for options"
android:textSize="18sp"
android:padding="16dp"
android:gravity="center" />
</LinearLayout>
MainActivity.java
package com.example.slip17q2;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText inputNumber;
private TextView resultText;
private int number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputNumber = findViewById(R.id.inputNumber);
resultText = findViewById(R.id.resultText);
registerForContextMenu(resultText);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Choose Action");
menu.add(0, v.getId(), 0, "Calculate Factorial");
menu.add(0, v.getId(), 0, "Calculate Sum of Digits");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
String input = inputNumber.getText().toString();
if (input.isEmpty()) {
Toast.makeText(this, "Please enter a number",
Toast.LENGTH_SHORT).show();
return true;
}
number = Integer.parseInt(input);
if (item.getTitle().equals("Calculate Factorial")) {
long factorial = calculateFactorial(number);
resultText.setText("Factorial: " + factorial);
return true;
} else if (item.getTitle().equals("Calculate Sum of Digits")) {
int sum = calculateSumOfDigits(number);
resultText.setText("Sum of Digits: " + sum);
return true;
} else {
return false;
}
}
private long calculateFactorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
long result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
private int calculateSumOfDigits(int n) {
int sum = 0;
while (n != 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
}
Slip 18:
Q1. Create an Android Application that Demonstrate Alert Dialog Box.
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:gravity="center"
android:orientation="vertical"
android:padding="20dp">
<Button
android:id="@+id/btn_show_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exit"
android:padding="10dp" />
</LinearLayout>
MainActivity.java
package com.example.slip18q1;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnShowDialog = findViewById(R.id.btn_show_dialog);
btnShowDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showAlertDialog();
}
});
}
private void showAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Exit App");
builder.setMessage("Are you sure you want to close the app?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
Q2Create an Android Application to accept two numbers and find power
and Average. Display the result on the next activity using Context
Menu.
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:gravity="center"
android:padding="20dp">
<EditText
android:id="@+id/num1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter First Number"
android:inputType="number" />
<EditText
android:id="@+id/num2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Second Number"
android:inputType="number" />
<Button
android:id="@+id/openMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Long press to Select Operation"
android:longClickable="true" />
</LinearLayout>
activity_result.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:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/resultText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result"
android:textSize="24sp" />
</LinearLayout>
MainActivity.java
package com.example.slip18q2;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
EditText num1, num2;
Button openMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1 = findViewById(R.id.num1);
num2 = findViewById(R.id.num2);
openMenu = findViewById(R.id.openMenu);
registerForContextMenu(openMenu);
openMenu.setOnLongClickListener(v -> {
openContextMenu(v);
return true;
});
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select Operation");
menu.add(0, v.getId(), 0, "Power");
menu.add(0, v.getId(), 0, "Average");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
int number1 = Integer.parseInt(num1.getText().toString());
int number2 = Integer.parseInt(num2.getText().toString());
double result = 0;
if (item.getTitle().equals("Power")) {
result = Math.pow(number1, number2);
} else if (item.getTitle().equals("Average")) {
result = (number1 + number2) / 2.0;
}
Intent intent = new Intent(MainActivity.this, ResultActivity.class);
intent.putExtra("result", result);
startActivity(intent);
return true;
}
}
ResultActivity.java
package com.example.slip18q2;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class ResultActivity extends AppCompatActivity {
TextView resultText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
resultText = findViewById(R.id.resultText);
double result = getIntent().getDoubleExtra("result", 0);
resultText.setText("Result: " + result);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.slip18q2">
<uses-permission
android:name="android.permission.POST_NOTIFICATIONS" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Slip18q2"
tools:targetApi="31">
<activity android:name=".ResultActivity" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Slip 19:
Q1. Create an Android Application that on/off the bulb using Toggle
Button.
Add bulb_off, bulb_on img in res->drawable->
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:gravity="center"
android:padding="16dp">
<ImageView
android:id="@+id/bulbImage"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/bulb_off"/>
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Turn OFF"
android:textOff="Turn ON"
android:layout_marginTop="20dp"/>
</LinearLayout>
MainActivity.java
package com.example.slip19q1;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.ToggleButton;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
ImageView bulbImage;
ToggleButton toggleButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bulbImage = findViewById(R.id.bulbImage);
toggleButton = findViewById(R.id.toggleButton);
toggleButton.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked) {
bulbImage.setImageResource(R.drawable.bulb_on);
} else {
bulbImage.setImageResource(R.drawable.bulb_off);
}
});
}
}
Q2.Design Following Screens using Table Layout. Display the entered
text on next activity
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:padding="20dp"
android:orientation="vertical"
android:gravity="center"
android:background="#FFFFFF">
<TextView
android:id="@+id/titleText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="250dp"
android:gravity="center"
android:paddingTop="40dp"
android:paddingBottom="20dp"
android:text="Membership Form"
android:textColor="#800080"
android:textSize="34sp"
android:textStyle="bold" />
<EditText
android:id="@+id/fullName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-220dp"
android:hint="Full Name"
android:padding="10dp"
android:textSize="16sp" />
<RadioGroup
android:id="@+id/genderGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gender: " />
<RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="M" />
<RadioButton
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="F" />
<RadioButton
android:id="@+id/other"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Other" />
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/currentWeight"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Current Weight"
android:layout_marginTop="40dp"
android:padding="10dp"
android:textSize="16sp" />
<EditText
android:id="@+id/height"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Height"
android:layout_marginTop="40dp"
android:padding="10dp"
android:textSize="16sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<EditText
android:id="@+id/goalWeight"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Goal Weight"
android:layout_marginTop="35dp"
android:padding="10dp"
android:textSize="16sp"/>
<EditText
android:id="@+id/age"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Age"
android:layout_marginTop="35dp"
android:padding="10dp"
android:textSize="16sp"/>
</LinearLayout>
<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone"
android:layout_marginTop="35dp"
android:inputType="phone"
android:padding="10dp"
android:textSize="16sp"/>
<EditText
android:id="@+id/address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Address"
android:layout_marginTop="35dp"
android:padding="10dp"
android:textSize="16sp"/>
<CheckBox
android:id="@+id/termsCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I have read, understood, and accepted membership rules"
android:layout_marginTop="35dp"/>
<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_margin="10dp"
android:background="#800080"
android:padding="10dp"
android:text="SUBMIT"
android:textColor="#FFFFFF"
android:textSize="10sp" />
</LinearLayout>
activity_result.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="20dp"
android:gravity="center">
<TextView
android:id="@+id/resultText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:gravity="left"
android:padding="20dp"
android:text="Results will appear here..." />
</LinearLayout>
MainActivity.java
package com.example.slip19q2;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText fullName, currentWeight, goalWeight, height, age, phone,
address;
private RadioGroup genderGroup;
private CheckBox termsCheckBox;
private Button submitButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fullName = findViewById(R.id.fullName);
currentWeight = findViewById(R.id.currentWeight);
goalWeight = findViewById(R.id.goalWeight);
height = findViewById(R.id.height);
age = findViewById(R.id.age);
phone = findViewById(R.id.phone);
address = findViewById(R.id.address);
genderGroup = findViewById(R.id.genderGroup);
termsCheckBox = findViewById(R.id.termsCheckBox);
submitButton = findViewById(R.id.submitButton);
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendDataToNextActivity();
}
});
}
private void sendDataToNextActivity() {
String name = fullName.getText().toString().trim();
String weight = currentWeight.getText().toString().trim();
String goal = goalWeight.getText().toString().trim();
String heightValue = height.getText().toString().trim();
String ageValue = age.getText().toString().trim();
String phoneValue = phone.getText().toString().trim();
String addressValue = address.getText().toString().trim();
int selectedGenderId = genderGroup.getCheckedRadioButtonId();
RadioButton selectedGender = findViewById(selectedGenderId);
String gender = selectedGender != null ?
selectedGender.getText().toString() : "Not Selected";
if (name.isEmpty() || phoneValue.isEmpty() || !termsCheckBox.isChecked())
{
Toast.makeText(this, "Please fill in all fields and accept terms",
Toast.LENGTH_SHORT).show();
return;
}
Intent intent = new Intent(MainActivity.this, ResultActivity.class);
intent.putExtra("name", name);
intent.putExtra("weight", weight);
intent.putExtra("goal", goal);
intent.putExtra("height", heightValue);
intent.putExtra("age", ageValue);
intent.putExtra("phone", phoneValue);
intent.putExtra("address", addressValue);
intent.putExtra("gender", gender);
startActivity(intent);
}
}
ResultActivity.java
package com.example.slip19q2;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class ResultActivity extends AppCompatActivity {
private TextView resultText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
resultText = findViewById(R.id.resultText);
Intent intent = getIntent();
String name = intent.getStringExtra("name");
String weight = intent.getStringExtra("weight");
String goal = intent.getStringExtra("goal");
String height = intent.getStringExtra("height");
String age = intent.getStringExtra("age");
String phone = intent.getStringExtra("phone");
String address = intent.getStringExtra("address");
String gender = intent.getStringExtra("gender");
String displayMessage = "Membership Details:\n\n"
+ "Full Name: " + name + "\n"
+ "Gender: " + gender + "\n"
+ "Current Weight: " + weight + "\n"
+ "Goal Weight: " + goal + "\n"
+ "Height: " + height + "\n"
+ "Age: " + age + "\n"
+ "Phone: " + phone + "\n"
+ "Address: " + address;
resultText.setText(displayMessage);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Slip19q2"
tools:targetApi="31">
<activity android:name=".ResultActivity"
android:exported="true" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Slip 20:
Q1. Create Android Program to Change the Image on the Screen.
Add image1, image2 picture in res->drawable->
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="16dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="700dp"
android:layout_height="700dp"
android:src="@drawable/image1"/>
<Button
android:id="@+id/changeImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Image"
android:layout_marginTop="20dp"/>
</LinearLayout>
MainActivity.java
package com.example.slip20q1;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
boolean isImage1 = true;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = findViewById(R.id.imageView);
Button changeImageButton = findViewById(R.id.changeImageButton);
changeImageButton.setOnClickListener(v -> {
if (isImage1) {
imageView.setImageResource(R.drawable.image2);
} else {
imageView.setImageResource(R.drawable.image1);
}
isImage1 = !isImage1;
});
}
}
Q2. Demonstrate Array Adapter using List View to display list of
Country.
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="20dp">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity.java
package com.example.slip20q2;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
ListView listView;
String[] countries = {"India", "USA", "Canada", "Australia", "Germany",
"France", "Japan", "China", "Brazil", "South Africa"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, countries);
listView.setAdapter(adapter);
listView.setOnItemClickListener((AdapterView<?> parent, View view, int
position, long id) ->
Toast.makeText(MainActivity.this, "Selected: " + countries[position],
Toast.LENGTH_SHORT).show()
);
}
}

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