Android Slip Answers
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) {
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;
}