- we created a folder in res names menu
- we created a menu resource file and designed the menu.
- Infiltrate the menu to the activity that we want the menu in
@Override public boolean onCreateOptionsMenu (Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main_activity_menu, menu); return true; }
- we can access the items which is selected with following codes:
@Override public boolean onOptionsItemSelected (@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.menu_btn_setting: //Do Something Log.d(TAG, "onOptionsItemSelected: "); return true; } return super.onOptionsItemSelected(item); }
You can provide a dialog to request a user's choice, such as an alert that requires users to tap OK or Cancel. A dialog is a window that appears on top of the display or fills the display, interrupting the flow of activity.
//This function is going to open an alert dialog and ask for user choice:
new AlertDialog.Builder(this)
.setTitle("This is a dialog")
.setMessage("Yes Or No?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick (DialogInterface dialog, int which) {
Log.d(TAG, "onClick: Yes");
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick (DialogInterface dialog, int which) {
Log.d(TAG, "onClick: NO");
}
})
.show();Android provides ready-to-use dialogs, called pickers, for picking a time or a date. You can use them to ensure that your users pick a valid time or date that is formatted correctly and adjusted to the user's local time and date. Each picker provides controls for selecting each part of the time (hour, minute, AM/PM) or date (month, day, year). You can read all about setting up pickers in Pickers.
learn how to use a Fragment, which is a behavior or a portion of a UI within an Activity. It's like a mini-Activity within the main Activity, with its own lifecycle, and it's used for building a picker.
The best practice to show a picker is to use an instance of DialogFragment, which is a subclass of Fragment. A DialogFragment displays a dialog window floating on top of the Activity window.
- we created a blank fragment without xml file or factory methods and named it DatePickerFragment
- Visit: http://developer.android.com/guide/topics/ui/controls/pickers.html for more information about pickers.
- Created a DatePicker Dialog in the fragment Visit DatePickerFragment for the code
- Modify the main activity you need to add a method that creates an instance of FragmentManager to manage the Fragment and show the date picker.
- get the date inside the onDateSet() method inside the fragment.