Custom theme in Android / Xamarin project - center Action bar text title

I have a custom theme in an Android / Xamarin project. What I can not do is:

  • set the title to the middle (and remove the icon)
  • add a menu button with a menu option (e.g. button settings)

I tried using the settings -> gravity to center property, but that didn't work.

Preview

<?xml version="1.0" encoding="utf-8"?> <resources> <!-- the theme applied to the application or activity --> <style name="AgrippaTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar"> <item name="android:actionBarStyle">@style/MyActionBar</item> </style> <!-- ActionBar styles --> <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse"> <item name="android:background">@color/main_orange_color</item> <item name="android:titleTextStyle">@style/AgrippaTheme.TitleTextStyle</item> </style> <!-- ActionBar TitleTextStyle styles --> <style name="AgrippaTheme.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title"> <item name="android:textColor">@color/main_black_color</item> <item name="android:gravity">center_vertical|center_horizontal</item> </style> </resources> 
+7
android xamarin
source share
2 answers

You will have to do this programmatically using a static method inside a static class

 public static void SetActionbarText(Activity activity, string text) { // Setting custom view enable activity.ActionBar.SetHomeButtonEnabled(false); activity.ActionBar.SetIcon(Android.Resource.Color.Transparent); activity.ActionBar.SetDisplayShowCustomEnabled(true); activity.ActionBar.Title = ""; LinearLayout linearLayout = new LinearLayout(activity); linearLayout.SetGravity(GravityFlags.CenterVertical); LinearLayout.LayoutParams textViewParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.MatchParent); textViewParameters.RightMargin = (int)(40 * activity.Resources.DisplayMetrics.Density); TextView modelTitle = new TextView(activity); modelTitle.Text = text; modelTitle.Gravity = GravityFlags.Center; linearLayout.AddView(modelTitle,textViewParameters); ActionBar.LayoutParams actionbarParams = new ActionBar.LayoutParams(ActionBar.LayoutParams.MatchParent,ActionBar.LayoutParams.MatchParent); activity.ActionBar.SetCustomView(linearLayout, actionbarParams); } 

Please note that you need to play with the correct field size. This field should be equal to the width of the house icon (it is, but it is invisible).

You can add the icon to the right by adding it to Menu.xml, and inflate this XML file in the OnCreateOptionsMenu method of your activity.

+10
source share

You can also do this using xml menu and style files. For example, to add a settings button, you can simply add an item to the menu file that defines the menu of the user interface shown in the picture. There are several predefined icons, such as the one I used (ic_menu_preferences), but you can use a different one or use an image instead. So in, say, agrippa_menu.xml:

  <?xml version="1.0" encoding="utf-8"?> 

[...]

 <item android:id="@+id/menu_item_settings" android:title="@string/settings" android:icon="@android:drawable/ic_menu_preferences" android:showAsAction="ifRoom|withText" app:showAsAction="ifRoom|withText"/> 

[...]

To disable the icon, you can add this line to the style that defines this user interface, or create a new one if you have not already created it. So in styles.xml:

 <style name="AgrippaTheme" parent="android:Theme.Holo.Light"> <!--pre v11--> <item name="logo">@color/transparent</item> <!--post v11--> <item name="android:logo">@color/transparent</item> <!-- Customize your theme here. --> </style> 

Finally, referring to the heading in the middle, I did not find any other way, besides defining a custom xml action bar, and programmatically adjust it as it says here: Center Align the heading in the action bar using styles in android. :. If someone knows how to do this using only styles, would be welcome.

+3
source share

All Articles