Android Option menu should remain even when we click on the peference menu below it
-
28-10-2019 - |
Question
I have created an option menu in android. I have written code for the menu to pop up when the activity starts. But the problem is that the option menu will disappear when i click on the preference menu beneath it. I want he option menu to be persisting on my menu page even when i click on the activity page or the preference options on the page. Please Please help.
Here is my xml code:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/icon"
android:icon="@drawable/icon" />
<item android:id="@+id/text"
android:title="Text" />
<item android:id="@+id/icontext"
android:title="Icon and text"
android:icon="@drawable/icon" />
</menu>
Java code:
public void onAttachedToWindow()
{
super.onAttachedToWindow();
openOptionsMenu();
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
Solution
You have several options if you want to have persistent buttons at the bottom of your screen.
You can put a LinearLayout
at the bottom or use a RelativeLayout
with alignParentBottom=true
.
Anyway, OptionsMenu
should only be shown on options key press, and closed on optionsItem press or key back pressed. Any other implementation is against Android's user experience.
Here is (part of) a prefs page i created with different options in a scrollview and the save button at the bottom :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/titleTextView"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:background="@drawable/background_blue_gradient"
android:gravity="center"
android:text="Preferences"
android:textColor="@color/white"
android:textSize="16dp"
android:textStyle="bold" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/preferencesSaveButton"
android:layout_below="@id/titleTextView" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/titleTextView"
android:paddingBottom="0dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:weightSum="2" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1.2"
android:text="Activate Notifications" />
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2" />
<ToggleButton
android:id="@+id/allowAlertsToggleButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.6"
android:textOff="No"
android:textOn="Yes" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="0dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:weightSum="2" >
<TextView
android:id="@+id/alertDelayTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.8"
android:text="Notifications Delay" />
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2" />
<Spinner
android:id="@+id/alertDelaySpinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:entries="@array/alert_intervals" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="0dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" >
<CheckBox
android:id="@+id/allowAutostartCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
<View
android:layout_width="20dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/allowAutostartTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Auto start on boot" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<Button
android:id="@id/preferencesSaveButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:padding="15dp"
android:text="Save"
android:textStyle="bold" />
</RelativeLayout>
OTHER TIPS
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
Use this code in oncreate method.
You should use a regular view : a horizontal LinearLayout with and populate it with Buttons instead of using MenuOptions.
If this is on HoneyComb or ICS you can use following flag in Menu.xml this will show Menu Options and actions in the Action Bar
<item android:id="@[+][package:]id/resource_name"
android:title="string"
android:titleCondensed="string"
android:icon="@[package:]drawable/drawable_resource_name"
android:onClick="method name"
android:showAsAction= "always"
..
/>