0% found this document useful (0 votes)
18 views9 pages

LAB 11 Alert Box

Uploaded by

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

LAB 11 Alert Box

Uploaded by

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

Name :Aman Verma MET-Institute of Computer Science

Roll No:1266

EXPERIMENT 11
Alert Box

AIM: To design application using alert box


ASSIGNMENT
1. Write a program to implement alert box in android.
CODE:
OUTPUT:
2. Create an application which will display the following output:

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

<TextView
android:id="@+id/books"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select the Books"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:textSize="30dp"
Name :Aman Verma MET-Institute of Computer Science
Roll No:1266

android:layout_centerHorizontal="true"
android:textColor="#0FECDC"
android:textStyle="bold"
/>
<CheckBox
android:layout_marginTop="20dp"
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mathematics"
android:textSize="20dp"
android:layout_marginLeft="120dp"
android:layout_below="@+id/books"/>

<CheckBox
android:layout_below="@+id/checkBox"
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="120dp"
android:text="English"
android:textSize="20dp"/>

<CheckBox
android:layout_below="@+id/checkBox2"
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="120dp"
android:text="Science"
android:textSize="20dp"/>

<CheckBox
android:layout_below="@+id/checkBox3"
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="120dp"
android:text="Hindi"
android:textSize="20dp"/>
<Button
android:id="@+id/btnshow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_gravity="center"
Name :Aman Verma MET-Institute of Computer Science
Roll No:1266

android:layout_marginTop="15dp"
android:layout_below="@+id/checkBox4"
android:layout_centerHorizontal="true"
android:textColor="#fff"
android:background="#16D647"
android:textStyle="bold"
/>

</RelativeLayout>

SecondActivity.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">

<TextView
android:id="@+id/books"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Selected Books are"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:textSize="30dp"
android:layout_centerHorizontal="true"
android:textColor="#D60FEC"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="175dp"
android:layout_marginTop="25dp"
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:textColor="#F0512D"
android:layout_margin="7dp"
android:id="@+id/t2"
android:layout_below="@id/books"
/>
<Switch
android:layout_width="wrap_content"
Name :Aman Verma MET-Institute of Computer Science
Roll No:1266

android:layout_height="wrap_content"
android:text="Home Delivery"
android:layout_centerHorizontal="true"
android:id="@+id/s1"
android:textColor="#BA9113"
android:layout_marginTop="20dp"
android:layout_below="@+id/t2"
android:textSize="25dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="Proceed"

android:textColor="@color/white"
android:layout_centerHorizontal="true"

android:layout_marginTop="550dp"/>
</RelativeLayout>

MainActivity.java

package com.example.checkintent;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

public class MainActivity extends AppCompatActivity {


Button submit;
CheckBox ch1,ch2,ch3,ch4;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit=findViewById(R.id.btnshow);
ch1=findViewById(R.id.checkBox);
ch2=findViewById(R.id.checkBox2);
Name :Aman Verma MET-Institute of Computer Science
Roll No:1266

ch3=findViewById(R.id.checkBox3);
ch4=findViewById(R.id.checkBox4);

submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String msg = " ";
if(ch1.isChecked())
{
msg += "Mathematics\n";
}
if(ch2.isChecked())
{
msg += "English\n";
}
if(ch3.isChecked())
{
msg += "Science\n";
}
if(ch4.isChecked())
{
msg += "Hindi\n";
}
Intent i = new Intent(getApplicationContext(),Second.class);
i.putExtra("Msg",msg);
startActivity(i);

}
});

}
}

Second.java
package com.example.checkintent;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
Name :Aman Verma MET-Institute of Computer Science
Roll No:1266

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class Second extends AppCompatActivity {

TextView text1;
Switch s;
Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
text1=findViewById(R.id.t2);
s=findViewById(R.id.s1);
btn=findViewById(R.id.btn);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertBox();
}
});

String str = getIntent().getExtras().getString("Msg").toString();


text1.setText(str);
s.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
if(s.isChecked()) {
Toast.makeText(Second.this, "Books will be delivered on Home",
Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(Second.this, "Sorry!!No home delivery available",
Toast.LENGTH_SHORT).show();
}
}
});
}

private void AlertBox() {


Name :Aman Verma MET-Institute of Computer Science
Roll No:1266

AlertDialog.Builder builder = new AlertDialog.Builder(Second.this);


builder.setTitle("Alert");
builder.setMessage("Confirm Your Order ?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

Toast.makeText(Second.this, "Books will be delivered on Home",


Toast.LENGTH_SHORT).show();

}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
builder.create();
builder.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">

<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.CheckIntent"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Name :Aman Verma MET-Institute of Computer Science
Roll No:1266

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


</intent-filter>

<meta-data
android:name="android.app.lib_name"
android:value="" />

</activity>
<activity android:name=".Second"></activity>

</application>

</manifest>

OUTPUT:
Name :Aman Verma MET-Institute of Computer Science
Roll No:1266

You might also like