Menus and Dialog
Menus and Dialog
Menus and Dialog
Practical 6
a) Creation of Menu
menus.xml
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>
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 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();
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);
}
}
b) Creation of Dialog
activity_main.xml
<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"
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>
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;
CharSequence[] items={"Android","Security","Cloud"};
boolean[] itemsChecked=new boolean[items.length];
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);