Reuse action bar in all application actions

I'm new to android, and I was wondering if anyone could help me on how to reuse the action bar in all my android activities. As I understand it, I found out that we need to create the BaseActivity class and expand it in our activity, where we want to reuse it, and we also need to make an xml-layout and include it in our xml activity file. I ended up with a BaseActivity part. Now I'm a little confused about creating the xml part and turning it on. I know how to combine and enable the layout, but in the case of the Action Bar, what steps need to be taken. Any help would be appreciated.

This is my BaseMenuActivity:

public class BaseMenuActivity extends Activity{ ActionBar actionBar; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); actionBar = getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setDisplayShowCustomEnabled(true); actionBar.setIcon(R.drawable.ic_social_share); LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = inflator.inflate(R.layout.apptitle, null); actionBar.setDisplayShowTitleEnabled(false); actionBar.setCustomView(v); } } 

Manifesto for the same:

 <activity android:name="com.example.travelplanner.MenuActivity" android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden" android:uiOptions="splitActionBarWhenNarrow" android:label="WeTrip" android:theme="@style/MyTheme" > 

Part of Style.xml:

 <style name="MyTheme" parent="@android:style/Theme.Holo.Light"> <item name="android:actionBarStyle">@style/MyActionBar</item> </style> <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar"> <item name="android:background">#F0F1F1</item> <item name="android:backgroundSplit">#000000</item> </style> 

MenuActivity.java

 public class MenuActivity extends BaseMenuActivity implements OnItemClickListener{ ActionBar actionBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_menu); @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu, menu); SearchView searchView = (SearchView) menu.findItem(R.id.menu_action_search).getActionView(); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch(item.getItemId()){ case R.id.menu_action_search: {} case R.id.menu_action_locate: {} case R.id.menu_action_mail: {} case R.id.menu_action_call: {} } return super.onOptionsItemSelected(item); } } 
+6
source share
1 answer

Good. Your code looks good, but if you want to reuse the same ActionBar with the same icons and menus and, as a rule, the same functions in each action.

You can add the code:

  @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu, menu); SearchView searchView = (SearchView) menu.findItem(R.id.menu_action_search).getActionView(); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch(item.getItemId()){ case R.id.menu_action_search: {} case R.id.menu_action_locate: {} case R.id.menu_action_mail: {} case R.id.menu_action_call: {} } return super.onOptionsItemSelected(item); } 

in your BaseMenuActivity class, and your action bar will be populated the same for every action that extends from it.

Update:

To create a menu layout, you must create a menu folder in the res / menu folder. Then create an xml file inside the called: some_title.xml

A typical example of an XML menu file is as follows:

 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/menu_search" android:actionViewClass="com.actionbarsherlock.widget.SearchView" android:icon="@drawable/abs__ic_search" android:showAsAction="ifRoom|withText|collapseActionView" android:title="@string/menu_action_search"/> <item android:id="@+id/menu_sort" android:icon="@drawable/content_sort_icon" android:showAsAction="always" android:title="@string/menu_action_sort"> </item> </menu> 

and then inflate this file:

 @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.some_title, menu); SearchView searchView = (SearchView) menu.findItem(R.id.menu_action_search).getActionView(); return true; } 

For a more detailed introduction to this guide, it is very useful to use the ActionBar:

http://www.vogella.com/tutorials/AndroidActionBar/article.html

+8
source

Source: https://habr.com/ru/post/950388/


All Articles