Search code examples
androidmenuandroid-actionbarmenuitem

Duplicating menu items everytime it open in overflow options menu


I am trying to add two menu items in action bar with xml code:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/search"
android:title="@string/search"
android:icon="@drawable/ic_action_search"
app:showAsAction="ifRoom"
/>
<item
android:id="@+id/share"
android:title="@string/share"
android:icon="@drawable/ic_action_share"
app:showAsAction="ifRoom"
/>

And my java code is :

    package com.erprakash.contacts;
    import android.content.ContentUris;
    import android.content.Context;
    import android.content.Intent;
    import android.database.Cursor;
    //import android.net.Uri;
    import android.provider.ContactsContract;
    //import android.support.v7.app.ActionBar;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.support.v7.widget.Toolbar;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Toast;

    import java.util.ArrayList;
    //import java.util.Comparator;

   public class MainActivity extends AppCompatActivity {

   private RecyclerView recyclerView;
   private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;

ArrayList<Contact> arrayList = new ArrayList<>();
int img_id = R.drawable.image1;
String name ;
String number ;
private int id;
private Contact contact;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   //Tool bar setting
    //toolbar end
    // Reading Contacts -----Working
    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE NOCASE ASC");
    if (phones != null) {
        while (phones.moveToNext())
        {
            name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            id = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO_FILE_ID));
            if(id == 0){
                contact = new Contact(number, name, img_id);
            }else {
                 contact = new Contact(number, name, id);
            }
            arrayList.add(contact);
        }
        phones.close();
    }



    /**
    name = getResources().getStringArray(R.array.person_name);
    number = getResources().getIntArray(R.array.mobile_number);
    int c = 0;
    for(String Name: name)
    {
        Contact contact = new Contact(number[c],Name,img_id[c]);
        c++;
        arrayList.add(contact);
    }
     **/
    recyclerView = (RecyclerView)findViewById(R.id.rvMainContennt);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);
    adapter = new ContactAdapter(arrayList);
    recyclerView.setAdapter(adapter);


}
//-------------Toolbar------------


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main,menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int res_id = item.getItemId();
    if(res_id == R.id.search)
    {
        Toast.makeText(this.getApplicationContext(),"You have seleted search option",Toast.LENGTH_SHORT).show();
    }
    return true;
}
@Override
public boolean onPrepareOptionsMenu(final Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return super.onCreateOptionsMenu(menu);
}
}

In preview the design appears correct : enter image description here

But in running state :enter image description here

And on clicking the option menu :enter image description here

Every time when i open the option menu the content gets duplicated and goes on , i have searched solution for this but i didn't get it anywhere . help me out....Thanks in advance


Solution

  • Remove onPrepareOptionsMenu().

    @Override
    public boolean onPrepareOptionsMenu(final Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }
    

    Update onCreateOptionsMenu() as below:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    

    If you want to show menu item always on Toolbar, then update menu_main.xml as below:

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <item
            android:id="@+id/search"
            android:title="@string/search"
            android:icon="@drawable/ic_action_search"
            app:showAsAction="always" />
    
        <item
            android:id="@+id/share"
            android:title="@string/share"
            android:icon="@drawable/ic_action_share"
            app:showAsAction="always" />
    </menu>