I am trying to make a action bar. If main_menu.xml is the xml for the action bar, Where do I store the action bar's xml? NOTE: Not Duplicate! Yes, there are examples of action bars, but I am just wondering, ***where do I put the XML file?*
Hi @RedLight here is the tow way define action bar one is directly dynamically add action bar inside your activity or other way is If you want to customise your action bar then you can use toolbar and in action bar dynamically, following was the example you can see.
For the dynamic action bar you need to define theame of activity with action bar like following.
<activity
android:name=".ActivitySplash"
android:theme="@style/Theme.AppCompat.Light.DarkActionBar"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateAlwaysHidden"/>
And inside your activity :
getSupportActionBar().setTitle("Home"); // Define here your actionbar title
getSupportActionBar().setDisplayHomeAsUpEnabled(true); // If you want to create home or back button inside actionbar
And for second type define your toolbar inside your activity top and define them as not action bar like foollowing,
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_delete" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
Theme like
<activity
android:name=".ActivitySplash"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateAlwaysHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
And finally set toolbar to action bar dynamically
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
For the menu you have to create menu directory inside "res" directory and put inside that directory and following code in activity.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.search:
//your code here
return true;
default:
return super.onOptionsItemSelected(item);
}
}