/res/menu/main_menu.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/itemShare" android:icon="@drawable/ic_share_white_24dp" android:title="@string/share" app:showAsAction="always" /> <item android:id="@+id/itemSearch" android:icon="@drawable/ic_share_white_24dp" android:title="@string/search" /> </menu>
MainActivity.java
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ActionBar actionBar=getSupportActionBar(); actionBar.setLogo(R.drawable.ic_android_white_24dp); actionBar.setDisplayUseLogoEnabled(true); actionBar.setDisplayShowHomeEnabled(true); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main_menu,menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.itemSearch: Toast.makeText(getBaseContext(),"Search",Toast.LENGTH_LONG).show(); break; case R.id.itemShare: Toast.makeText(getBaseContext(),"Share",Toast.LENGTH_LONG).show(); break; } return super.onOptionsItemSelected(item); } }
References
https://github.com/mhdr/AndroidSamples/tree/master/038