Q2
A. Develop an Android application that uses GUI components,
Font and Colors
android_main.xml
:
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:gravity="center"
android:text="Hello
World!"
android:textSize="25sp"
android:textStyle="bold" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="Change font
size"
android:textSize="25sp"
/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="Change
color"
android:textSize="25sp"
/>
</LinearLayout>
MainActivity.java
:
package com.example.labfile;
import ndroid.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int ch=1;
float font=30;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView t= (TextView)
findViewById(R.id.textView);
Button b1= (Button)
findViewById(R.id.button1);
b1.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
t.setTextSize(font);
font = font + 5;
if (font == 50)
font = 30;
}
});
Button b2= (Button)
findViewById(R.id.button2);
b2.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
switch (ch) {
case 1:
t.setTextColor(Color.RED);
break;
case 2:
t.setTextColor(Color.GREEN);
break;
case 3:
t.setTextColor(Color.BLUE);
break;
case 4:
t.setTextColor(Color.CYAN);
break;
case 5:
t.setTextColor(Color.YELLOW);
break;
case 6:
t.setTextColor(Color.MAGENTA);
break;
}
ch++;
if (ch == 7)
ch = 1;
}
});
}
}
Output
:
==========================================================================
Q2
B . Develop an Android application that uses Layout Managers and
event listeners. Subscription form and Login form
activity_main.xml :
<?xml version="1.0"
encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:text="Details
Form"
android:textSize="25sp"
android:gravity="center"/>
</LinearLayout>
<GridLayout
android:id="@+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="100dp"
android:layout_marginBottom="200dp"
android:columnCount="2"
android:rowCount="3">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="0"
android:layout_column="0"
android:text="Name"
android:textSize="20sp"
android:gravity="center"/>
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="0"
android:layout_column="1"
android:ems="10"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="1"
android:layout_column="0"
android:text="Reg.No"
android:textSize="20sp"
android:gravity="center"/>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="1"
android:layout_column="1"
android:inputType="number"
android:ems="10"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="2"
android:layout_column="0"
android:text="Dept"
android:textSize="20sp"
android:gravity="center"/>
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="2"
android:layout_column="1"
android:spinnerMode="dropdown"/>
</GridLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_marginBottom="150dp"
android:text="Submit"/>
</RelativeLayout>
MainActivity.java
:
package com.example.labfile;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class MainActivity extends AppCompatActivity {
EditText e1,e2;
Button bt;
Spinner s;
String []
dept_array={"CSE","ECE","IT","Mech","Civil"};
String name,reg,dept;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1= (EditText)
findViewById(R.id.editText);
e2= (EditText)
findViewById(R.id.editText2);
bt= (Button)
findViewById(R.id.button);
s= (Spinner) findViewById(R.id.spinner);
ArrayAdapter adapter= new
ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item,dept_array);
s.setAdapter(adapter);
bt.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
name=e1.getText().toString();
reg=e2.getText().toString();
dept=s.getSelectedItem().toString();
Intent i = new
Intent(MainActivity.this,SecondActivity.class);
i.putExtra("name_key", name);
i.putExtra("reg_key",reg);
i.putExtra("dept_key", dept);
startActivity(i);
}
});
}
}
activity_second.xml
:
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.labfile.SecondActivity"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="New Text"
android:textSize="30sp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="New Text"
android:textSize="30sp"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="New Text"
android:textSize="30sp"/>
</LinearLayout>
SecondActivity.java
:
package com.example.labfile;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
TextView t1,t2,t3;
String name,reg,dept;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
t1= (TextView)
findViewById(R.id.textView1);
t2= (TextView)
findViewById(R.id.textView2);
t3= (TextView)
findViewById(R.id.textView3);
Intent i = getIntent();
name=i.getStringExtra("name_key");
reg=i.getStringExtra("reg_key");
dept=i.getStringExtra("dept_key");
t1.setText(name);
t2.setText(reg);
t3.setText(dept);
}
}
Output :
==========================================================================
Q3.Create a fragment that has its own UI and enable your
activities to communicate with fragments.
Activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<!--
display two Button's and a FrameLayout to replace the Fragment's -->
<Button
android:id="@+id/firstFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/button_background_color"
android:text="First
Fragment"
android:textColor="@color/white"
android:textSize="20sp"
/>
<Button
android:id="@+id/secondFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@color/button_background_color"
android:text="Second
Fragment"
android:textColor="@color/white"
android:textSize="20sp"
/>
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
/>
</LinearLayout>
MainActivity.java
package
com.abhiandroid.fragmentexample;
import android.app.Fragment;
import
android.app.FragmentManager;
import
android.app.FragmentTransaction;
import
android.support.v7.app.AppCompatActivity;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
public
class MainActivity extends AppCompatActivity {
Button
firstFragment, secondFragment;
@Override
protected
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get
the reference of Button's
firstFragment
= (Button) findViewById(R.id.firstFragment);
secondFragment
= (Button) findViewById(R.id.secondFragment);
//
perform setOnClickListener event on First Button
firstFragment.setOnClickListener(new
View.OnClickListener() {
@Override
public
void onClick(View v) {
// load
First Fragment
loadFragment(new
FirstFragment());
}
});
//
perform setOnClickListener event on Second Button
secondFragment.setOnClickListener(new
View.OnClickListener() {
@Override
public
void onClick(View v) {
// load
Second Fragment
loadFragment(new
SecondFragment());
}
});
}
private
void loadFragment(Fragment fragment) {
// create
a FragmentManager
FragmentManager
fm = getFragmentManager();
// create
a FragmentTransaction to begin the transaction and replace the Fragment
FragmentTransaction
fragmentTransaction = fm.beginTransaction();
//
replace the FrameLayout with new Fragment
fragmentTransaction.replace(R.id.frameLayout,
fragment);
fragmentTransaction.commit();
// save the changes
}
}
FirstFragment.class
package
com.abhiandroid.fragmentexample;
import
android.app.Fragment;
import
android.os.Bundle;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.ViewGroup;
import android.widget.Button;
import
android.widget.Toast;
public
class FirstFragment extends Fragment {
View
view;
Button
firstButton;
@Override
public
View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle
savedInstanceState) {
// Inflate
the layout for this fragment
view =
inflater.inflate(R.layout.fragment_first, container, false);
// get
the reference of Button
firstButton
= (Button) view.findViewById(R.id.firstButton);
//
perform setOnClickListener on first Button
firstButton.setOnClickListener(new
View.OnClickListener() {
@Override
public
void onClick(View v) {
//
display a message by using a Toast
Toast.makeText(getActivity(),
"First Fragment", Toast.LENGTH_LONG).show();
}});
return
view;
}}
SecondFragment.class
package
com.abhiandroid.fragmentexample;
import
android.app.Fragment;
import
android.os.Bundle;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.ViewGroup;
import
android.widget.Button;
import
android.widget.Toast;
public
class SecondFragment extends Fragment {
View
view;
Button
secondButton;
@Override
public
View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle
savedInstanceState) {
//
Inflate the layout for this fragment
view =
inflater.inflate(R.layout.fragment_second, container, false);
// get
the reference of Button
secondButton
= (Button) view.findViewById(R.id.secondButton);
//
perform setOnClickListener on second Button
secondButton.setOnClickListener(new
View.OnClickListener() {
@Override
public
void onClick(View v) {
//
display a message by using a Toast
Toast.makeText(getActivity(),
"Second Fragment", Toast.LENGTH_LONG).show();
}});
return
view;
}}
fragment_first.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.abhiandroid.fragmentexample.FirstFragment">
<!--TextView
and Button displayed in First Fragment -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:text="This
is First Fragment"
android:textColor="@color/black"
android:textSize="25sp"
/>
<Button
android:id="@+id/firstButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@color/green"
android:text="First
Fragment"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold"
/>
</RelativeLayout>
SecondActivity.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.abhiandroid.fragmentexample.SecondFragment">
<!--TextView
and Button displayed in Second Fragment -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:text="This
is Second Fragment"
android:textColor="@color/black"
android:textSize="25sp"
/>
<Button
android:id="@+id/secondButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@color/green"
android:text="Second
Fragment"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold"
/>
</RelativeLayout>
Output :
==========================================================================
Q4. Write
an android code to make simple registration page using Intent
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">
<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:layout_marginTop="20dp"
android:layout_marginHorizontal="20dp"/>
<EditText
android:id="@+id/editTextEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:layout_below="@id/editTextUsername"
android:layout_marginTop="20dp"
android:layout_marginHorizontal="20dp"/>
<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/editTextEmail"
android:layout_marginTop="20dp"
android:layout_marginHorizontal="20dp"/>
<Button
android:id="@+id/btnRegister"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register"
android:layout_below="@id/editTextPassword"
android:layout_marginTop="20dp"
android:layout_marginHorizontal="20dp"/>
</RelativeLayout>
MainActivity.java
:
package
com.example.myapplication;// src/main/java/your_package_name/MainActivity.java
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 {
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the views from the layout
EditText editTextUsername =
findViewById(R.id.editTextUsername);
EditText editTextEmail = findViewById(R.id.editTextEmail);
EditText editTextPassword =
findViewById(R.id.editTextPassword);
Button btnRegister =
findViewById(R.id.btnRegister);
// Set click listener for the Register
button
btnRegister.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
// Get the entered values
String username =
editTextUsername.getText().toString().trim();
String email = editTextEmail.getText().toString().trim();
String password =
editTextPassword.getText().toString().trim();
// Validate and display a toast
with registration information
if (!username.isEmpty()
&& !email.isEmpty() && !password.isEmpty()) {
String registrationInfo =
"Username: " + username + "\nEmail: " + email +
"\nPassword: " + password;
showToast("Registration successful:\n" + registrationInfo);
} else {
showToast("Please fill
in all the fields.");
}
}
});
}
private void showToast(String message) {
Toast.makeText(this, message,
Toast.LENGTH_SHORT).show();
}
}
Output
:
==========================================================================
Q5 . Write an application to demonstrate Alert Dialog Box in
android.
activity_main.xml
:
<?xml
version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alert Dialog
Box"
android:textSize="25dp"
android:layout_marginBottom="50dp"/>
<Button
android:id="@+id/alertBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AlertBox"
android:textSize="25dp"/>
</LinearLayout>
MainActivity.java
:
package
com.example.alertbox;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button b;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button)
findViewById(R.id.alertBox);
b.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View
view) {
AlertDialog.Builder
builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Alert !");
builder.setMessage("Do you want to exit ?");
builder.setPositiveButton("Yes",null)
.setNegativeButton("No",null);
builder.show();
}
});
}
}
Output :
==========================================================================
Q6. Demonstrate Array
Adapter using List View to display list of fruits.
activity_main.xml:
<?xml version="1.0"
encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/listViewFruits"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@android:color/darker_gray"
android:dividerHeight="0.5dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
:
package
com.example.myapplication;// src/main/java/your_package_name/MainActivity.java
import
android.os.Bundle;
import
android.widget.ArrayAdapter;
import
android.widget.ListView;
import
androidx.appcompat.app.AppCompatActivity;
import
java.util.ArrayList;
import
java.util.Arrays;
public
class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> fruitsList =
new ArrayList<>(Arrays.asList(
"Apple",
"Banana", "Orange", "Grapes",
"Watermelon", "Mango", "Strawberry",
"Pineapple"));
// Create ArrayAdapter
ArrayAdapter<String> arrayAdapter
= new ArrayAdapter<>(
this,
android.R.layout.simple_list_item_1,
fruitsList);
ListView listViewFruits =
findViewById(R.id.listViewFruits);
listViewFruits.setAdapter(arrayAdapter);
}
}
Output :
==========================================================================
Q7.Write
an application to produce Notification
activity_main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/showNotificationButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Show
Notification" />
</RelativeLayout>
MainActivity.java
:
package com.example.a8;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
public class MainActivity extends AppCompatActivity {
private final String channelId =
"notification_channel";
private final int notificationId = 1;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createNotificationChannel();
Button showNotificationButton =
findViewById(R.id.showNotificationButton);
showNotificationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View
view) {
showNotification();
}
});
}
private void
createNotificationChannel() {
if (Build.VERSION.SDK_INT >=
Build.VERSION_CODES.O) {
String name = "My
Notification Channel";
String descriptionText = "Channel
description";
int importance =
NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel =
new NotificationChannel(channelId, name, importance);
channel.setDescription(descriptionText);
NotificationManager
notificationManager =
(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
}
}
private void showNotification() {
NotificationCompat.Builder
builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Notification Example")
.setContentText("This
is an Notification Example.")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat.from(this).notify(notificationId,
builder.build());
}
}
Output:-
==========================================================================
Q8. Create an application which demonstrate radio
button, check button, and Spinner.
Activity_main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/radioButtonOption1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option
1" />
<RadioButton
android:id="@+id/radioButtonOption2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option
2" />
</RadioGroup>
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check me"
/>
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"/>
</LinearLayout>
MainActivity.java
package com.example.a9;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
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 radioGroup =
findViewById(R.id.radioGroup);
CheckBox checkBox =
findViewById(R.id.checkBox);
Spinner spinner =
findViewById(R.id.spinner);
// Set a listener for RadioGroup
radioGroup.setOnCheckedChangeListener(new
RadioGroup.OnCheckedChangeListener() {
@Override
public void
onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton radioButton =
findViewById(checkedId);
if (radioButton != null) {
showToast("Selected: " + radioButton.getText());
}
}
});
// Set a listener for CheckBox
checkBox.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
if (checkBox.isChecked())
{
showToast("CheckBox checked");
} else {
showToast("CheckBox unchecked");
}
}
});
// Populate the Spinner with data
ArrayAdapter<CharSequence>
adapter = ArrayAdapter.createFromResource(
this,
R.array.spinner_options,
android.R.layout.simple_spinner_item
);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
// Set a listener for Spinner
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void
onItemSelected(AdapterView<?> parentView, View selectedItemView, int
position, long id) {
String selectedOption =
(String) parentView.getItemAtPosition(position);
showToast("Selected
Spinner Option: " + selectedOption);
}
@Override
public void
onNothingSelected(AdapterView<?> parentView) {
// Do nothing here
}
});
}
private void showToast(String
message) {
Toast.makeText(this, message,
Toast.LENGTH_LONG).show();
}
}
strings.xml
<resources>
<string-array
name="spinner_options">
<item>Option A</item>
<item>Option B</item>
<item>Option C</item>
</string-array>
</resources>
Output:-
==========================================================================
Q9. Design screen to send email (compose email) using
linear layout.
Activity_main.xml
<?xml
version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Design screen
to send email (compose email) using linear layout.\n" />
<EditText
android:id="@+id/recipientEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Recipient"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/subjectEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Subject"
/>
<MultiAutoCompleteTextView
android:id="@+id/bodyEditText"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:hint="Email
Body"
android:inputType="textMultiLine" />
<Button
android:id="@+id/sendButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send
Email" />
</LinearLayout>
MainActivity.java
package
com.example.composeemail;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText recipientEditText,
subjectEditText;
private MultiAutoCompleteTextView
bodyEditText;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recipientEditText =
findViewById(R.id.recipientEditText);
subjectEditText =
findViewById(R.id.subjectEditText);
bodyEditText =
findViewById(R.id.bodyEditText);
Button sendButton =
findViewById(R.id.sendButton);
sendButton.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View
view) {
sendEmail();
}
});
}
private void sendEmail() {
String recipient =
recipientEditText.getText().toString();
String subject =
subjectEditText.getText().toString();
String body =
bodyEditText.getText().toString();
// You can implement the email
sending logic here
String message = "Email
Sent!\nRecipient: " + recipient + "\nSubject: " + subject +
"\nBody: " + body;
Toast.makeText(this, message,
Toast.LENGTH_SHORT).show();
}
}
Output :|
==========================================================================
Q10.
Create application to demonstrate date and time picker.
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">
<!--
Button to show Date Picker Dialog -->
<Button
android:id="@+id/btnShowDatePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Date Picker"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>
<!--
Button to show Time Picker Dialog -->
<Button
android:id="@+id/btnShowTimePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Time Picker"
android:layout_below="@id/btnShowDatePicker"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>
</RelativeLayout>
MainActivity.java
:
package
com.example.myapplication;// src/main/java/your_package_name/MainActivity.java
import
android.app.DatePickerDialog;
import
android.app.TimePickerDialog;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
import
android.widget.DatePicker;
import android.widget.TimePicker;
import
android.widget.Toast;
import
androidx.appcompat.app.AppCompatActivity;
import
java.util.Calendar;
public
class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnShowDatePicker =
findViewById(R.id.btnShowDatePicker);
Button btnShowTimePicker =
findViewById(R.id.btnShowTimePicker);
btnShowDatePicker.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
showDatePickerDialog();
}
});
btnShowTimePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showTimePickerDialog();
}
});
}
private void showDatePickerDialog() {
// Get the current date
Calendar calendar =
Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month =
calendar.get(Calendar.MONTH);
int day =
calendar.get(Calendar.DAY_OF_MONTH);
// Create a Date Picker Dialog
DatePickerDialog datePickerDialog = new
DatePickerDialog(
this,
new
DatePickerDialog.OnDateSetListener() {
@Override
public void
onDateSet(DatePicker datePicker, int selectedYear, int selectedMonth, int
selectedDay) {
String date =
selectedYear + "-" + (selectedMonth + 1) + "-" +
selectedDay;
showToast("Selected Date: " + date);
}
},
year, month, day);
// Show the Date Picker Dialog
datePickerDialog.show();
}
private void showTimePickerDialog() {
Calendar calendar =
Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute =
calendar.get(Calendar.MINUTE);
// Create a Time Picker Dialog
TimePickerDialog timePickerDialog = new
TimePickerDialog(
this,
new TimePickerDialog.OnTimeSetListener()
{
@Override
public void
onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
String time =
selectedHour + ":" + selectedMinute;
showToast("Selected Time:
" + time);
}
},
hour, minute, true); // true
for 24-hour format
// Show the Time Picker Dialog
timePickerDialog.show();
}
private void showToast(String message) {
Toast.makeText(this, message,
Toast.LENGTH_SHORT).show();
}
}
Output :
==========================================================================
Q11 . Design
screen for payment board and display message on button click.
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="443dp"
android:layout_height="247dp"
android:layout_marginTop="2dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/paypal" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Card Number
:"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.175"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.414"
tools:ignore="TouchTargetSizeCheck"
/>
<EditText
android:id="@+id/editText"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_marginStart="49dp"
android:layout_marginTop="14dp"
android:layout_marginEnd="62dp"
android:hint="Enter card
number"
android:inputType="number"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
tools:ignore="TouchTargetSizeCheck" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginEnd="250dp"
android:text="Expiry Date
:"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
<EditText
android:id="@+id/editText2"
android:layout_width="170dp"
android:layout_height="39dp"
android:layout_marginStart="17dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="62dp"
android:hint="Enter expiry
date"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/editText"
tools:ignore="TouchTargetSizeCheck" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="CVV :"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.135"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<EditText
android:id="@+id/editText3"
android:layout_width="170dp"
android:layout_height="39dp"
android:layout_marginTop="20dp"
android:hint="Enter
CVV"
android:inputType="number"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.563"
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toBottomOf="@+id/editText2"
tools:ignore="TouchTargetSizeCheck" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="49dp"
android:layout_marginTop="35dp"
android:layout_marginEnd="67dp"
android:text="Name :"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@+id/editText4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
<EditText
android:id="@+id/editText4"
android:layout_width="170dp"
android:layout_height="40dp"
android:layout_marginTop="23dp"
android:layout_marginEnd="62dp"
android:layout_marginBottom="173dp"
android:hint="Enter card
owner name"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText3"
tools:ignore="TouchTargetSizeCheck" />
<Button
android:id="@+id/button"
android:layout_width="121dp"
android:layout_height="48dp"
android:layout_marginBottom="96dp"
android:text="Add Card"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="TouchTargetSizeCheck" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java:
package com.example.labq11;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText cNo, oName;
String cardNo, ownerName;
Button b;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cNo = (EditText)
findViewById(R.id.editText);
oName = (EditText) findViewById(R.id.editText4);
b = (Button)
findViewById(R.id.button);
b.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View
view) {
submit(view);
}
});
}
public void submit(View v){
cardNo =
cNo.getText().toString();
ownerName =
oName.getText().toString();
Toast.makeText(MainActivity.this,"Card "+cardNo +"\n
added Successful",Toast.LENGTH_SHORT).show();
}
}
Output :
==========================================================================
Q12. Write a program to demonstrate Options Menu in
android for a restaurant and display appropriate message related to the dish
user has opted from the menu on text field.
activity_main.xml.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<Spinner
android:id="@+id/menuSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginBottom="16dp"
android:entries="@array/menu_options"
/>
<Button
android:id="@+id/displayButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/menuSpinner"
android:layout_marginTop="16dp"
android:text="Display
Dish"
android:onClick="displaySelectedDish" />
<TextView
android:id="@+id/resultText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/displayButton"
android:layout_marginTop="16dp"
android:text=""
android:textSize="18sp"
/>
</RelativeLayout>
Array.xml:
<?xml
version="1.0" encoding="utf-8"?>
<resources>
<string-array
name="menu_options">
<item>Pizza</item>
<item>Burger</item>
<item>Pasta</item>
<!-- Add more items as needed
-->
</string-array>
</resources>
MainActivity.java:
package com.example.a12practical;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.a12practical.R;
public class MainActivity extends AppCompatActivity {
private Spinner menuSpinner;
private TextView resultText;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
menuSpinner =
findViewById(R.id.menuSpinner);
resultText =
findViewById(R.id.resultText);
ArrayAdapter<CharSequence>
adapter = ArrayAdapter.createFromResource(
this,
R.array.menu_options,
android.R.layout.simple_spinner_item
);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
menuSpinner.setAdapter(adapter);
}
public void displaySelectedDish(View
view) {
String selectedDish = menuSpinner.getSelectedItem().toString();
String message = "You
selected: " + selectedDish;
resultText.setText(message);
0 Comments