Search code examples
androidandroid-toolbar

actionbar icon in android


I am trying to add filter icon on the actionbar , I added a menu file named filer.xml , then I inflated the menu in the java file of that activity, but There is no use of that , I can not see my icon in the xml layout of that page.

filter.xml

      <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".travels">

<!-- "Mark Favorite", should appear as action button if possible -->
<item
    android:id="@+id/action_filter"
    android:icon="@drawable/ic_sort_black_24dp"
    android:title="@string/bus_filter"
    app:showAsAction="always"/>



 </menu>

travels.java

                      public class travels extends AppCompatActivity implements View.OnClickListener {

    //  private int mYear, mMonth, mDay, mHour, mMinute;
    EditText From_edittext, To_Editext, departure_date_edtxt, return_date_edtxt, time1_edtxt, time2_edtxt;
   private int mYear, mMonth, mDay, mHour, mMinute;
   Button continue_btn;
   Switch switchbtn;
    private static final LatLngBounds BOUNDS_MOUNTAIN_VIEW = new LatLngBounds(
        new LatLng(37.398160, -122.180831), new LatLng(37.430610, -121.972090));

    private static final int PLACE_PICKER_REQUEST1 = 1;
    private static final int PLACE_PICKER_REQUEST2 = 2;



    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_journey);



       @Override
   public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.filter,menu);
    return super.onCreateOptionsMenu(menu);
    /*MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.filter, menu);
    return true;*/

}

}

Solution

  • Wrongly placed onCreateOptionsMenu, it should be like

    public class travels extends AppCompatActivity implements View.OnClickListener {
    
     protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_journey);
     }
    
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
     getMenuInflater().inflate(R.menu.filter,menu);
     return super.onCreateOptionsMenu(menu);
     }
    }