Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Lab Sheet 9 : AlertBox

1. Design an App that create an Alertbox contain the radio buttons for selection single choice. On
Clicking the option call a new activity displaying the message about the option selected.
Java code:package com.example.alertdialogradiobutton;

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

import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog.Builder builderSingle = new
AlertDialog.Builder(MainActivity.this);
builderSingle.setIcon(R.drawable.ic_launcher_foreground);
builderSingle.setTitle("Read the data");

final ArrayAdapter<String> arrayAdapter = new


ArrayAdapter<String>(MainActivity.this,
android.R.layout.select_dialog_singlechoice);
arrayAdapter.add("camera");
arrayAdapter.add("bluetooth");
arrayAdapter.add("printer");
arrayAdapter.add("Usb");

builderSingle.setNegativeButton("cancel", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});

builderSingle.setAdapter(arrayAdapter, new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String strName = arrayAdapter.getItem(which);
AlertDialog.Builder builderInner = new
AlertDialog.Builder(MainActivity.this);
builderInner.setMessage(strName);
builderInner.setTitle("Your Selected Item is");
builderInner.setPositiveButton("Ok", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int
which) {
dialog.dismiss();
}
});
builderInner.show();
}
});
builderSingle.show();
}
}

XML CODE:
<?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"/>

OUTPUT:
2. Create below app for event management. When ever the button clock is pressed, an alertBox
containing Timepicker should be loaded.On selecting the Time and clicked OK, the time should be
displayed in editbox. On Clicking No, the dialog should dismiss with out setting the time

JAVA CODE:
package com.example.alertnotification;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


TextView t1;
EditText et1;
Button b1;
TimePicker tp;
String time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1 = findViewById(R.id.t1);
et1 = findViewById(R.id.editTextTextPersonName);
b1 = findViewById(R.id.button);
tp=new TimePicker(MainActivity.this);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new
AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher_foreground);
builder.setTitle("Set Time");

tp.setOnTimeChangedListener(new
TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker timePicker, int
i, int i1) {
et1.setText(String.valueOf(i)
+":"+String.valueOf(i1));
//time = String.valueOf(i)
+":"+String.valueOf(i1);
}
});

builder.setView(tp);
builder.setPositiveButton("ok", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface,
int i) {

Toast.makeText(MainActivity.this,"Done",
Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("NO", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface,
int i) {
dialogInterface.cancel();
}
});
builder.create().show();
}
});
}
}

XML CODE:

<?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">

<TextView
android:id="@+id/t1"
android:layout_width="154dp"
android:layout_height="46dp"
android:text="EVENT"
android:textAlignment="center"
android:textSize="34sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.47"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.146" />

<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="100dp"
android:layout_marginTop="76dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/t1" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="95dp"
android:layout_marginEnd="51dp"
android:text="Time"
app:layout_constraintEnd_toEndOf="@+id/editTextTextPersonName"

app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName" />

</androidx.constraintlayout.widget.ConstraintLayout>

OUTPUT:

You might also like