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 can follow these steps:
-
Create a new menu resource file by right-clicking on the "res" folder in the project structure, selecting "New", then "Android Resource Directory". Set the Resource Type as "menu" and click "OK". Next, right-click on the newly created "menu" folder, select "New", then "Menu Resource File". Enter a file name like "menu_main.xml".
-
Open the "menu_main.xml" file and add the following code:
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/action_about" android:title="About"/> </menu> -
- User
Do i have to add something in the activity_main?
- Assistant
No, you don't need to make any changes to the activity_main.xml layout file for adding the options menu and the "About" dialog. The modifications I provided are specific to the MainActivity Java class and the menu XML resource.
The activity_main.xml layout file seems to contain the UI elements for selecting a car type and a "Next" button, which are unrelated to the options menu and the "About" dialog. You can keep using the existing layout file as it is.
- User
The menu doesn't seem to be appearing in my app
- Assistant
If the menu is not appearing in your app, make sure to follow these steps to ensure it is properly implemented:
- Check that you have overridden the onCreateOptionsMenu() method in your MainActivity class and called getMenuInflater().inflate() to inflate the menu layout:
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; }- Verify that you have implemented the onOptionsItemSelected() method to handle item selection:
@Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.action_about) { showAboutDialog(); return true; } return super.onOptionsItemSelected(item); }