Menus and Dialog

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

T.Y.BSc(I.

T) SEM-VI Maharashtra College Advanced Mobile Programming

Practical 6

Programming menus and dialog

a) Creation of Menu

 Create a new project and go to,


 Project > App -> src -> res(Right-click on res and select) -> New ->
Android Resource File -> Filename(menus) and Resource Type
Menu
 Now go to, menus.xml file and make the following changes to code
file,

menus.xml

<?xml version="1.0" encoding="utf-8"?>


<menu xmlns:android="http://schemas.android.com/apk/res/android">

android:layout_width="wrap_content"
android:layout_height="wrap_content"
<item android:id="@+id/newb" android:title="File" />
<item android:id="@+id/search" android:title="Open" />
<item android:id="@+id/save" android:title="Save"/>
<item android:id="@+id/Share" android:title="Save As" />
<item android:id="@+id/delete" android:title="Delete" />
<item android:id="@+id/Exit" android:title="Close" />

</menu>

Prof. Ansari Mohd. Shahid(7977-079-345 / 9821-77-1054) Page 1


T.Y.BSc(I.T) SEM-VI Maharashtra College Advanced Mobile Programming

Now go to MainActivity.java file and make the following changes to


code,

MainActivity.java

package MaharashtraCollege.example.profshahidansari;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {

public boolean onCreateOptionsMenu(Menu menu)


{
MenuInflater mi=getMenuInflater();
mi.inflate(R.menu.menus,menu);
return true;
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}
public boolean onOptionsItemSelected(MenuItem item)
{

switch (item.getItemId())
{
case R.id.newb:
Toast.makeText(this,"File Selected",Toast.LENGTH_SHORT).show();
return true;
case R.id.Share:
Toast.makeText(this,"Open Selected",Toast.LENGTH_SHORT).show();
return true;
case R.id.delete:
Toast.makeText(this,"Save Selected",Toast.LENGTH_SHORT).show();

Prof. Ansari Mohd. Shahid(7977-079-345 / 9821-77-1054) Page 2


T.Y.BSc(I.T) SEM-VI Maharashtra College Advanced Mobile Programming

return true;
case R.id.save:
Toast.makeText(this,"Save As Selected",Toast.LENGTH_SHORT).show();
return true;
case R.id.search:
Toast.makeText(this,"Delete Selected",Toast.LENGTH_SHORT).show();
return true;
case R.id.Exit: Toast.makeText(this,"Exit Selected",Toast.LENGTH_SHORT).show();
return true;
default:
Toast.makeText(this,"Default",Toast.LENGTH_SHORT).show();
return super.onOptionsItemSelected(item);
}
}

Prof. Ansari Mohd. Shahid(7977-079-345 / 9821-77-1054) Page 3


T.Y.BSc(I.T) SEM-VI Maharashtra College Advanced Mobile Programming

Output of Toast Message

Prof. Ansari Mohd. Shahid(7977-079-345 / 9821-77-1054) Page 4


T.Y.BSc(I.T) SEM-VI Maharashtra College Advanced Mobile Programming

b) Creation of Dialog

Create a new project and go to, activity_main.xml, and make the


following additions to the code

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout 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:orientation="vertical"
android:stretchColumns="1">

<Button android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/button_dialog"
android:onClick="onClickDialog"
android:text="Click to display an alert dialog"/>

<Button android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/button_progressdialog"
android:onClick="onClickProgressDialog"
android:text="Click to display a progressdialog"/>

<Button android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/button_datedialog"
android:onClick="onClickDateDialog"
android:text="Click to display a DatePicker dialog"/>

<Button android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/button_timedialog"
android:onClick="onClickTimeDialog"
android:text="Click to display a TImePicker dialog"/>

<TextView
android:id="@+id/textView_date"
android:layout_width="398dp"
android:layout_height="wrap_content"

Prof. Ansari Mohd. Shahid(7977-079-345 / 9821-77-1054) Page 5


T.Y.BSc(I.T) SEM-VI Maharashtra College Advanced Mobile Programming

android:text="TextView1"
android:textSize="50dp" />

<TextView
android:id="@+id/textView_time"
android:layout_width="400dp"
android:layout_height="wrap_content"
android:text="TextView2"
android:textSize="50dp" />

</LinearLayout>

Go to file MainActivity.java and make the following changes to the


code .

Main_Activity.java

package MaharashtraCollege.example.profshahidansari;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.os.Bundle;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

CharSequence[] items={"Android","Security","Cloud"};
boolean[] itemsChecked=new boolean[items.length];

Prof. Ansari Mohd. Shahid(7977-079-345 / 9821-77-1054) Page 6


T.Y.BSc(I.T) SEM-VI Maharashtra College Advanced Mobile Programming

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

public void onClickDialog(View v)


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

builder.setTitle("This is a dialog with a simple Text");


builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

@Override public void onClick(DialogInterface dialog, int id)


{
Toast.makeText(getBaseContext(), "OK CLicked", Toast.LENGTH_SHORT).show();
} });
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
@Override public void onClick(DialogInterface dialog, int id)
{
Toast.makeText(getBaseContext(), "Cancel CLicked", Toast.LENGTH_SHORT).show();
} });
builder.setMultiChoiceItems(items, itemsChecked, new
DialogInterface.OnMultiChoiceClickListener()
{
@Override public void onClick(DialogInterface dialog, int id, boolean isChecked)
{
Toast.makeText(getBaseContext(), items[id] + (isChecked ? "checked!" : "unchecked!"),
Toast.LENGTH_SHORT).show();
} });
AlertDialog dialog = builder.create(); builder.show();
}

Prof. Ansari Mohd. Shahid(7977-079-345 / 9821-77-1054) Page 7


T.Y.BSc(I.T) SEM-VI Maharashtra College Advanced Mobile Programming

Prof. Ansari Mohd. Shahid(7977-079-345 / 9821-77-1054) Page 8

You might also like