<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>replace menus &#8211; MaXEster Technologies  | Technical Blog</title>
	<atom:link href="https://www.maxester.com/blog/tag/replace-menus/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.maxester.com/blog</link>
	<description>Tutorials, Examples and Implementation code for Developers Help</description>
	<lastBuildDate>Mon, 17 Feb 2025 09:17:48 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.22</generator>
	<item>
		<title>How to add fragment specific menus in Android</title>
		<link>https://www.maxester.com/blog/2019/12/05/how-to-set-fragment-specific-menus-in-toolbar-in-android/</link>
		<comments>https://www.maxester.com/blog/2019/12/05/how-to-set-fragment-specific-menus-in-toolbar-in-android/#respond</comments>
		<pubDate>Thu, 05 Dec 2019 12:46:56 +0000</pubDate>
		<dc:creator><![CDATA[Jony Chawla]]></dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[#fragment specific menus]]></category>
		<category><![CDATA[replace menus]]></category>
		<category><![CDATA[toolbar]]></category>

		<guid isPermaLink="false">https://www.maxester.com/blog/?p=779</guid>
		<description><![CDATA[<p>You can show new menus from fragment by onCreateOptionsMenu&#160;method First you need to setHasOptionsMenu(true)&#160;in onCreateView&#160;method&#160;as second you have to create menus.xml in res/menu/ folder And also you have to set menu in onCreateOptionsMenu&#160;method as If you want to replace menus&#8230;</p>
<p><a href="https://www.maxester.com/blog/2019/12/05/how-to-set-fragment-specific-menus-in-toolbar-in-android/" class="btn-more">Read More<span class="arrow-more">&#8594;</span></a></p>
<p>The post <a rel="nofollow" href="https://www.maxester.com/blog/2019/12/05/how-to-set-fragment-specific-menus-in-toolbar-in-android/">How to add fragment specific menus in Android</a> appeared first on <a rel="nofollow" href="https://www.maxester.com/blog">MaXEster Technologies  | Technical Blog</a>.</p>
]]></description>
				<content:encoded><![CDATA[
<p> You can show new menus from fragment by  <strong>onCreateOptionsMenu</strong>&nbsp;method</p>



<p>First you need to <strong>setHasOptionsMenu(true)</strong>&nbsp;in <strong>onCreateView</strong>&nbsp;method&nbsp;as</p>



<center>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Fads -->
<ins class="adsbygoogle fads"
     style="display:inline-block;"
     data-ad-client="ca-pub-3804472713147276"
     data-ad-slot="1267368188"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
</center>




<pre class="wp-block-code"><code> @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment,
                container, false);
        this.view = view;


        setHasOptionsMenu(true);


        return view;
    }</code></pre>



<p>

second you have to create <strong>menus.xml</strong> in <strong>res/menu/</strong> folder

</p>



<center>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Fads -->
<ins class="adsbygoogle fads"
     style="display:inline-block;"
     data-ad-client="ca-pub-3804472713147276"
     data-ad-slot="1267368188"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
</center>




<pre class="wp-block-code"><code>&lt;?xml version="1.0" encoding="utf-8"?>
&lt;menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    &lt;item
        android:id="@+id/settings"
        android:icon="@drawable/settings_icon"
        android:orderInCategory="100"
        android:title="@string/settings"
        app:showAsAction="always" />


&lt;/menu></code></pre>



<p>And also you have to set menu in <strong>onCreateOptionsMenu</strong>&nbsp;method as</p>



<pre class="wp-block-code"><code> @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        inflater.inflate(R.menu.menus, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }</code></pre>



<center>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Fads -->
<ins class="adsbygoogle fads"
     style="display:inline-block;"
     data-ad-client="ca-pub-3804472713147276"
     data-ad-slot="1267368188"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
</center>




<p>

If you want to replace menus with new menus then you just have to add <strong>menu.clear()</strong>&nbsp;&nbsp;before&nbsp;inflating&nbsp;new&nbsp;menus&nbsp;as

</p>



<pre class="wp-block-code"><code>@Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        menu.clear(); 
        inflater.inflate(R.menu.menus, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }</code></pre>



<p></p>
<p>The post <a rel="nofollow" href="https://www.maxester.com/blog/2019/12/05/how-to-set-fragment-specific-menus-in-toolbar-in-android/">How to add fragment specific menus in Android</a> appeared first on <a rel="nofollow" href="https://www.maxester.com/blog">MaXEster Technologies  | Technical Blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.maxester.com/blog/2019/12/05/how-to-set-fragment-specific-menus-in-toolbar-in-android/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
