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. The menu should rely on a context menu
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 the item "About" and a customized dialog using a context menu in your car rental app, you can follow the steps below:
- User
for some reason menu_Main is not appearing on my program when I run it. How do it fix it?
- Assistant
If the
menu_Mainis not appearing when you run your program, there are a few things you can check to fix the issue:-
Ensure that you have created the
menu_Main.xmlfile in the correct directory:- Open your project's
resdirectory. - Navigate to
res/menudirectory. - Check if the
menu_Main.xmlfile exists in this directory. If not, create a new XML file namedmenu_Main.xml.
- Open your project's
-
Verify that the file contains the correct menu items:
-
- User
can you revise the file to use the toolbar?
- Assistant
Certainly! If you want to use a Toolbar for your options menu instead of the default ActionBar, you can follow the revised steps below:
- Update your activity_main.xml layout file to include a Toolbar. Replace the relevant part of the code with the following:
xml