title: Center aligning of title in action bar of activity and fragments
body: <p>My HomeActivity consists of Sliding Menu where each option in Sliding Menu opens a new Fragment with different title. I want that title in action bar of fragments and activity appear in center.I tried using custom layout for fragment-</p>
  
  <pre><code> final ActionBar ab = getActivity().getActionBar();
  
          ab.setHomeButtonEnabled(true);
          ab.setDisplayHomeAsUpEnabled(true);
          ab.setDisplayShowTitleEnabled(false);
         // actionBar.setDisplayShowHomeEnabled(false);
          ab.setCustomView(R.layout.action_bar);
          ab.setDisplayShowCustomEnabled(true);
  </code></pre>
  
  <p>actionbar.xml</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent" &gt;
  
      &lt;TextView
          android:id="@+id/title"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerHorizontal="true"
          android:layout_centerVertical="true"
          android:fontFamily="sans-serif"
          android:maxLines="1"
          android:text="title"
  
          android:textSize="20sp"
          android:gravity="center"/&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>This aligns the title in center but the sliding menu disappears.I want that all my fragments display title in center of action bar and sliding menu should not vanish.Here is my code for manifest,styles.xml,activity and one of the fragment and also xml file for Home Activity..</p>
  
  <p>Manifest</p>
  
  <pre><code> &lt;activity
              android:name=".ui.slidingmenu.Home"
              android:label="@string/app_name"
              android:theme="@style/MyTheme"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
  </code></pre>
  
  <p>Styles.xml</p>
  
  <pre><code>&lt;style name="MyTheme" parent="@android:style/Theme.Holo.Light"&gt;
      &lt;item name="android:actionBarStyle"&gt;@style/MyActionBar&lt;/item&gt;
      &lt;item name="android:actionBarTabStyle"&gt;@style/MyTheme.ActionBar.TabView&lt;/item&gt;
      &lt;item name="android:actionBarTabTextStyle"&gt;@style/MyTheme.ActionBar.TabText&lt;/item&gt;
  &lt;style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar"&gt;
          &lt;item name="android:windowFullscreen"&gt;true&lt;/item&gt;
          &lt;item name="android:windowContentOverlay"&gt;@null&lt;/item&gt;
          &lt;item name="android:background"&gt;#12ABEE&lt;/item&gt;
          &lt;item name="android:gravity"&gt;center&lt;/item&gt;
          &lt;item name="android:actionMenuTextColor"&gt;#ffffff&lt;/item&gt;
          &lt;item name="android:titleTextStyle"&gt;@style/myTheme.ActionBar.Text&lt;/item&gt;
      &lt;/style&gt;
   &lt;style name="myTheme.ActionBar.Text" parent="@android:style/TextAppearance"&gt;
          &lt;item name="android:textColor"&gt;#ffffff&lt;/item&gt;
          &lt;item name="android:textStyle"&gt;bold&lt;/item&gt;
          &lt;item name="android:gravity"&gt;center&lt;/item&gt;
  
      &lt;/style&gt;
  </code></pre>
  
  <p>HomeActivity</p>
  
  <pre><code>public class Home extends FragmentActivity {
      private DrawerLayout mDrawerLayout;
      private ListView mDrawerList;
      private ActionBarDrawerToggle mDrawerToggle;
      private CharSequence mDrawerTitle, mTitle;
      private String[] navMenuTitles;
      private TypedArray navMenuIcons;
      private ArrayList&lt;NavDrawerItem&gt; navDrawerItems;
      private NavDrawerListAdapter adapter;
      private String mName, mProfileimage;
      private LinearLayout linearLayout;
      private TextView nameTextView;
      private ImageView profileImageView;
  
      public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
          Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                  bitmap.getHeight(), Bitmap.Config.ARGB_8888);
          Canvas canvas = new Canvas(output);
  
          final int color = 0xff424242;
          final Paint paint = new Paint();
          final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
          final RectF rectF = new RectF(rect);
          final float roundPx = 40;
  
          paint.setAntiAlias(true);
          canvas.drawARGB(0, 0, 0, 0);
          paint.setColor(color);
          canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
  
          paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
          canvas.drawBitmap(bitmap, rect, rect, paint);
  
          return output;
      }
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.slidermenu);
          StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
          StrictMode.setThreadPolicy(policy);
          mTitle = mDrawerTitle = getTitle();
  
          navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
          navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
  
          linearLayout = (LinearLayout) findViewById(R.id.left_drawer_view);
          mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
          mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
  
          nameTextView = (TextView) findViewById(R.id.username);
          profileImageView = (ImageView) findViewById(R.id.drawer_profile_image);
          navDrawerItems = new ArrayList&lt;NavDrawerItem&gt;();
  
          SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(Home.this);
  
          mName = app_preferences.getString("name", "null");
          mProfileimage = app_preferences.getString("profileimage", "null");
  
          nameTextView.setText(mName);
  
          try {
              URL url = new URL(mProfileimage);
              Bitmap lProfileBitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
              lProfileBitmap = getRoundedCornerBitmap(lProfileBitmap);
              BitmapDrawable lProfileDrawable = new BitmapDrawable(lProfileBitmap);
              profileImageView.setBackgroundDrawable(lProfileDrawable);
          } catch (Exception e) {
              Log.d("error in image", e.toString());
          }
          navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
          navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
          navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
          navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
          navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
          navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));
          navDrawerItems.add(new NavDrawerItem(navMenuTitles[6], navMenuIcons.getResourceId(6, -1)));
          navDrawerItems.add(new NavDrawerItem(navMenuTitles[7], navMenuIcons.getResourceId(7, -1)));
          navDrawerItems.add(new NavDrawerItem(navMenuTitles[8], navMenuIcons.getResourceId(8, -1)));
          navDrawerItems.add(new NavDrawerItem(navMenuTitles[9], navMenuIcons.getResourceId(9, -1)));
          navDrawerItems.add(new NavDrawerItem(navMenuTitles[10], navMenuIcons.getResourceId(10, -1)));
          navDrawerItems.add(new NavDrawerItem(navMenuTitles[11], navMenuIcons.getResourceId(11, -1)));
  
          navMenuIcons.recycle();
  
          mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
  
          adapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerItems);
          mDrawerList.setAdapter(adapter);
  
          getActionBar().setDisplayHomeAsUpEnabled(true);
          getActionBar().setHomeButtonEnabled(true);
  
          mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                  R.drawable.ic_drawer,
                  R.string.app_name,
                  R.string.app_name
          ) {
              public void onDrawerClosed(View view) {
                  getActionBar().setTitle(mTitle);
                  invalidateOptionsMenu();
              }
  
              public void onDrawerOpened(View drawerView) {
                  getActionBar().setTitle(mDrawerTitle);
                  invalidateOptionsMenu();
              }
          };
          mDrawerLayout.setDrawerListener(mDrawerToggle);
  
  
          if (savedInstanceState == null) {
              displayView(0);
          }
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.main, menu);
          return super.onCreateOptionsMenu(menu);
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // toggle nav drawer on selecting action bar app icon/title
          if (mDrawerToggle.onOptionsItemSelected(item)) {
              return true;
          }
          switch (item.getItemId()) {
              case R.id.action_settings:
                  return true;
              default:
                  return super.onOptionsItemSelected(item);
          }
      }
  
      @Override
      public boolean onPrepareOptionsMenu(Menu menu) {
  
          boolean drawerOpen = mDrawerLayout.isDrawerOpen(this.linearLayout);
          menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
          return super.onPrepareOptionsMenu(menu);
      }
  
  
      private void displayView(int position) {
          // update the main content by replacing fragments
          Fragment fragment = null;
          switch (position) {
              case 0:
                  fragment = new HF();
                  break;
              case 1:
                  fragment = new NF();
                  break;
              case 2:
                  fragment = new FD();
                  break;
              case 3:
                  fragment = new FDFD();
                  break;
              case 4:
                  fragment = new VideosFragment();
                  break;
              case 5:
                  fragment = new DG();
                  break;
              case 6:
                  fragment = new DSF();
                  break;
              case 7:
                  fragment = new FDSF();
                  break;
              case 8:
                  fragment = new DFSD();
                  break;
              case 9:
                  fragment = new DFSF();
                  break;
              default:
                  break;
          }
  
          if (fragment != null) {
              FragmentManager fragmentManager = getFragmentManager();
              fragmentManager.beginTransaction()
                      .replace(R.id.frame_container, fragment).commit();
  
              mDrawerList.setItemChecked(position, true);
              mDrawerList.setSelection(position);
              setTitle(navMenuTitles[position]);
              mDrawerLayout.closeDrawer(this.linearLayout);
          } else {
              // error in creating fragment
              Log.e("MainActivity", "Error in creating fragment");
          }
      }
  
      @Override
      public void setTitle(CharSequence title) {
          mTitle = title;
          getActionBar().setTitle(mTitle);
      }
  
  
      @Override
      protected void onPostCreate(Bundle savedInstanceState) {
          super.onPostCreate(savedInstanceState);
  
          mDrawerToggle.syncState();
      }
  
      @Override
      public void onConfigurationChanged(Configuration newConfig) {
          super.onConfigurationChanged(newConfig);
  
          mDrawerToggle.onConfigurationChanged(newConfig);
      }
  
      private class SlideMenuClickListener implements
              ListView.OnItemClickListener {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position,
                                  long id) {
              displayView(position);
          }
      }
  
  }
  </code></pre>
  
  <p>VideosFragment</p>
  
  <pre><code>public class VideosFragment extends Fragment {
      private static final int CAMERA_CAPTURE_VIDEO_REQUEST_CODE = 200;
      ActionBar actionBar;
      private int countClick;
      private FragmentActivity myContext;
      private ViewPager viewPager;
  
  
      private String[] tabs = {"All Teams Videos", "Player Videos", "My Team Videos"};
  
      public VideosFragment() {
      }
  
      @Override
      public void onAttach(Activity activity) {
          myContext = (FragmentActivity) activity;
          super.onAttach(activity);
      }
  
      public void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
          setHasOptionsMenu(true);
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
  
          View rootView = inflater.inflate(R.layout.videos, container, false);
  
          actionBar = getActivity().getActionBar();
          actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
  
          return rootView;
      }
  
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle item selection
          switch (item.getItemId()) {
              case R.id.action_search2:
  
                  Intent i = new Intent();
                  i.setClass(myContext, LS.class);
                  startActivity(i);
                  return true;
  
              default:
                  return super.onOptionsItemSelected(item);
          }
  
      }
  
  }
  </code></pre>
  
  <p>slidermenu.xml</p>
  
  <pre><code>&lt;android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/drawer_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
  
      &lt;FrameLayout
          android:id="@+id/frame_container"
          android:layout_width="match_parent"
          android:layout_height="match_parent" /&gt;
  
      &lt;LinearLayout
          android:id="@+id/left_drawer_view"
          android:layout_width="240dp"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          android:background="@drawable/bgmenu"
          android:orientation="vertical"&gt;
  
          &lt;RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_marginTop="10dp"
              android:layout_weight="0"&gt;
  
  
              &lt;ImageView
                  android:id="@+id/drawer_profile_background"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_centerHorizontal="true"
                  android:scaleType="centerCrop"
                  android:src="@drawable/profileblock"&gt;
  
              &lt;/ImageView&gt;
  
              &lt;ImageView
                  android:id="@+id/drawer_profile_image"
                  android:layout_width="wrap_content"
                  android:layout_height="80dp"
                  android:layout_alignLeft="@+id/drawer_profile_background"
                  android:layout_alignRight="@+id/drawer_profile_background"
                  android:layout_centerHorizontal="true"
                  android:layout_marginLeft="4dp"
                  android:layout_marginRight="4dp"
                  android:layout_marginTop="4dp"
                  android:scaleType="centerCrop"&gt;&lt;/ImageView&gt;
  
              &lt;ImageView
                  android:id="@+id/settingicon"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignBottom="@+id/drawer_profile_background"
                  android:layout_marginLeft="-15dp"
                  android:layout_toRightOf="@+id/drawer_profile_background"
                  android:background="@drawable/settings" /&gt;
  
              &lt;TextView
                  android:id="@+id/username"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_below="@+id/drawer_profile_background"
                  android:layout_centerHorizontal="true"
                  android:layout_marginTop="5dp"
                  android:text="Name"
                  android:textAppearance="?android:attr/textAppearanceListItemSmall"
                  android:textColor="@android:color/white"&gt;&lt;/TextView&gt;
  
  
          &lt;/RelativeLayout&gt;
  
  
          &lt;ListView
              android:id="@+id/list_slidermenu"
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:layout_gravity="start"
              android:layout_marginTop="2dp"
              android:layout_weight="4"
              android:choiceMode="singleChoice"
              android:divider="@android:color/transparent"
              android:dividerHeight="2dp"
              android:listSelector="@drawable/menuselector" /&gt;
  
      &lt;/LinearLayout&gt;
  &lt;/android.support.v4.widget.DrawerLayout&gt;
  </code></pre>