Add an options menu with item “About” and a customized dialog (not Alert Dialog) for…
- User
Add an options menu with item “About” and a customized dialog (not Alert Dialog) for “About” into car rental app.
Please do so with the following android studio code in Java:
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
RadioButton sedan = findViewById(R.id.sedan); RadioButton suv = findViewById(R.id.suv); RadioButton mv = findViewById(R.id.mv); Button button = findViewById(R.id.next);
SharedPreferences sharedPreferences = getSharedPreferences(“car_types”, Context.MODE_PRIVATE);
button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String selectedCarType = “”;
if (sedan.isChecked()) { selectedCarType = “Sedan”; } else if (suv.isChecked()) { selectedCarType = “SUV”; } else if (mv.isChecked()) { selectedCarType = “Minivan”; }
//save preferences SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString(“selected_car”, selectedCarType); editor.apply();
Intent intent = new Intent(MainActivity.this, SavedPreference.class); startActivity(intent); } }); } }
- Assistant
To add an options menu with an "About" item and a customized dialog, you need to make the following modifications to your code:
- Create a new menu layout file named
menu_main.xmlin theres/menudirectory. Add the "About" item to the menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/action_about" android:title="About" /> </menu>- Modify the
onCreateOptionsMenumethod in yourMainActivityto inflate the menu layout:
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; }- Add a listener for the "About" menu item in the
onOptionsItemSelectedmethod:
@Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_about) { showAboutDialog(); return true; } return super.onOptionsItemSelected(item); }- Create a new method named
showAboutDialogto display your customized dialog. You can use theDialogclass to create a custom dialog with your desired layout:
private void showAboutDialog() { Dialog dialog = new Dialog(MainActivity.this); dialog.setContentView(R.layout.dialog_about); dialog.setCancelable(true); dialog.setTitle("About"); // Set dialog contents and customize the layout dialog.show(); }- Create a new layout file named
dialog_about.xmlin theres/layoutdirectory. Customize the layout according to your requirements:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white" android:orientation="vertical" android:padding="16dp"> <!-- Add your dialog contents here --> </LinearLayout>With these modifications, you should have an options menu with an "About" item that displays a customized dialog when clicked.
- Create a new menu layout file named
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.