I wanted to add a Search button to my Android ActionBar, to do this I followed this answer. The only thing that happens is that the "Search" button is added to the menu itself and not the ActionBar (see screenshot below).
This is my search.java (The Activity to show the search button in):
public class search extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.menu_search, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
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.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And this is my menu_search.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:title="search"
android:id="@+id/action_search"
android:icon="@drawable/searchicon"
android:orderInCategory="100"
android:showAsAction="always"/>
</menu>
Any help is apreciated! Thanks in advance!
0 comments:
Post a Comment