I'm trying to use Android ActionBar
in my app, and have an option that's hidden away in the overflow menu.
There's a lot of documentation out there, but it's confusing because most of it is only relevant to very old versions of Android, and when you try applying the same concepts, they don't work anymore or work differently.
This is in my Activity
layout
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:titleTextColor="@android:color/white"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
And this is in my Activity
's onCreate()
method
// sets up activity toolbar
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
myToolbar.showOverflowMenu();
myToolbar.setTitleTextColor(R.color.lightPrimaryText);
I've also tried inflating a menu xml file from the onCreateOptionsMenu()
, but that also didn't give me the results I wanted.
Define a
Menu
for yourToolbar
in the res/menu resource folder, for example:toolbar_menu.xml
Setting
app:showAsAction="never"
ensures that thisMenuItem
will not be shown in theToolbar
, but placed in the overflow menu instead.The theme of your
Activity
should be (or derive from) one of theNoActionBar
themes (Theme.AppCompat.NoActionBar
for example, orTheme.MaterialComponents.NoActionBar
if you're using Material Components).In your
Activity
, set up yourToolbar
:And override
onCreateOptionsMenu()
to inflate your previously defined menu resource:You can override
onOptionsItemSelected()
to define the onClick behaviour of yourMenuItem
(s):in manifest file declare
like this :
and add this to your style :
and call this in Activity onCreate() :
override this method in activity:
and declare your menu like this for overflow menu:
and for handle item selection call this
done :)
Simple do This copy this code on your MainActivet`
Now Make Directory file for menu name for this go on Android_Studio->app Folder->Right_Click->New->Directory-> Enter name menu now Create a xml file in there with menu2.xml name
and past this code on menu2.xml file
if any Query Please text me