How to change the default icon of OptionsMenu in android?

293 views Asked by At

I coded this in order to get a Filter icon at OptionsMenu.I was using an empty meny with drawable image and the onClick of that was showing a DialogFragment. But this doesn't work fine as an empty title item appears on the OptionsMenu clicking which my DialogFragment gets open. I appreciate any help. Thanks.

menu file for OptionsMenu :

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"  >
<item android:id="@+id/filter"
    android:orderInCategory="100"
    android:icon="@drawable/filter_icon"
    android:showAsAction="ifRoom|withText"
    android:title="hi" />
</menu>

This is myactivity :

public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.menu,menu);
   return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.

    String[] values = new String[]{ "Veg.",
            "Non Veg.",
            "Veg & Non veg."

    };
    int id = item.getItemId();
    if (id == R.id.filter) {
        RestaurantListingFragment restaurant_categories_dialog = new      RestaurantListingFragment();
        android.app.FragmentManager fm = getFragmentManager();

        Bundle args = new Bundle();
        args.putStringArray("restaurantCategories",values);

        restaurant_categories_dialog.setArguments(args);

        // Show DialogFragment
        restaurant_categories_dialog.show(fm, "hi");

        return true;
    }
    return super.onOptionsItemSelected(item);
}

This is my fragment class:

public class RestaurantListingFragment extends DialogFragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,   Bundle savedInstanceState) {
   View rootView = inflater.inflate(R.layout.fragment_dialog_restaurant_categories, container,false);
    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    String[] restaurantCategories = getArguments().getStringArray("restaurantCategories");

    if(restaurantCategories != null)
    {
        ListView lv=(ListView) rootView.findViewById(R.id.fd_lv);
        ArrayAdapter arrayAdapter =new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1,android.R.id.text1,restaurantCategories);
        lv.setAdapter(arrayAdapter);
    }
    return rootView;
 }
 }

Manifests :

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="cresol.demo.drestodemo">

 <application
    android:allowBackup="true"
    android:hardwareAccelerated="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:supportsRtl="true">
    <activity
        android:name=".Home"
        android:theme="@style/NoTitle">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".signup"
        android:theme="@style/NoTitle" />
    <activity
        android:name=".RestaurantListing"
        android:theme="@style/AppTheme" />
    <activity android:name=".DishListing"
        android:theme="@style/AppTheme"></activity>
 </application>

 </manifest>
1

There are 1 answers

0
Ibrahim Gharyali On BEST ANSWER

In your menu file change this line android:showAsAction="ifRoom|withText" with this one app:showAsAction="always"