Toolbar title set center align.

SanjayTejaniSanjayTejani USMember ✭✭✭
edited January 2016 in Xamarin.Forms

how can i title will be set center align in toolbar using Android layout.

Tagged:

Best Answer

Answers

  • SanjayTejaniSanjayTejani USMember ✭✭✭

    @YkshLeo

    Thanks for your comment, but i relay sorry to inform you i forgot to notice in my first post i am used xamarin cross plate-from not xamarin android native layout, we used XAML form for custom layout. is that possible your code will work in cross plate-from??

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    @SanjayTejani You will need to do it in your Android project. It should be pretty simple.

    1. In YourApp.Droid/Resources/, add an Android Layout file named toolbar.axml.

    2. Put the XML that @YkshLeo provided into toolbar.axml.

    3. Make sure you are using Xamarin.Forms 2.0.0 or higher, and your MainActivity inherits from FormsAppCompatActivity.

    4. In OnCreate of your MainActivity, set the ToolbarResource property prior to calling base.OnCreate, like so:

      public class MainActivity : FormsAppCompatActivity
      {
          protected override void OnCreate(Bundle bundle)
          {
              ToolbarResource = Resource.Layout.toolbar_top;
      
              base.OnCreate(bundle);
      
              global::Xamarin.Forms.Forms.Init(this, bundle);
      
              LoadApplication(new App());
          }
      }
      
  • SanjayTejaniSanjayTejani USMember ✭✭✭

    @JoeManke Thank you so much for guide will definitively implement as you say and @YkshLeo also.

    before implement your code i just attach bellow image so you can easily understood what i am getting issue.

  • `public class MainActivity : FormsAppCompatActivity
    {
    protected override void OnCreate(Bundle bundle)
    {
    ToolbarResource = Resource.Layout.toolbar_top;

        base.OnCreate(bundle);
    
        global::Xamarin.Forms.Forms.Init(this, bundle);
    
        LoadApplication(new App());
    }
    

    }`

    If I am using above code, then NavigationPage.SetTitleIcon (myContentPage, s) is not working for Android but it is working iOS.

    How set icon to ToolBar? In Android, We just use getSupportedToolBar().setLogo(Drawable).

  • Vamsi.KatakamVamsi.Katakam USMember ✭✭

    @SanjayTejani

    Toolbar title set center align.?
    is have any solution?

  • DougHillDougHill USMember

    Is it possible to then set the text of the TextView if using Xamarin forms?

  • MattiaBaldingerMattiaBaldinger CHUniversity
    edited January 2017

    Does anybody have a solution for this in combination with forms? I tried to use it in combination with a custom renderer, but i wasn't able to find the toolbar, it was always null..?

                protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
                {
                    base.OnElementChanged(e);
    
                    if(Element!=null)
                    {
                        var page = (ContentPage)Element;
                        e.NewElement.fin
    
                        var toolbarTop = (Toolbar)FindViewById(Resource.Id.toolbar_top);
                        if (toolbarTop != null)
                        {
                            var mTitle = (TextView)toolbarTop.FindViewById(Resource.Id.toolbar_title);
    
                            mTitle.Text = page.Title;
                        }
                    }
                }
    
  • MannyVSMannyVS USMember ✭✭

    @YkshLeo said:
    @SanjayTejani,

    To use a custom title in your Toolbar all you need to do is remember is that Toolbar is just a fancy ViewGroup so you can add a custom title like so:

    <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_top"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/action_bar_bkgnd"
    app:theme="@style/ToolBarTheme" >

       <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Toolbar Title"
          android:layout_gravity="center"
          android:id="@+id/toolbar_title" />
    

    </android.support.v7.widget.Toolbar>

    This means that you can style the TextView however you would like because it's just a regular TextView. So in your activity you can access the title like so:

    var toolbarTop = (Toolbar) FindViewById(Resource.Id.toolbar_top);
    var mTitle = (TextView) toolbarTop.FindViewById(Resource.Id.toolbar_title);
    

    @YkshLeo
    This didn't work for me. I followed the same what you have mentioned above.

    i see this line of error : Resource. Id does not contain a definition of toolbar_title.

  • MannyVSMannyVS USMember ✭✭

    Is there any other way to set the title to center on android.
    am working on xamarin.forms app.

    Appreciate your response.

  • YkshLeoYkshLeo USMember ✭✭✭✭

    @MannyManny : Refer this SO to solve your error.

  • MannyVSMannyVS USMember ✭✭
    edited April 2017

    I tried this one already. It didn't help me.

    Let me try one more time and see if am making any mistake in the code.

    Thanks for quick response.

  • pallavboharapallavbohara USMember ✭✭
    edited April 2017

    @JoeManke
    @YkshLeo
    The above solution you mentioned sets Toolbar same title for all the pages i need different for each page.

    Please suggest something.

  • YkshLeoYkshLeo USMember ✭✭✭✭

    @pallavbohara Try something like :

    var toolbarTop = (Toolbar) FindViewById(Resource.Id.toolbar_top);
    var mTitle = (TextView) toolbarTop.FindViewById(Resource.Id.toolbar_title);
    mTitle.Text = "Your Title";
    
  • pallavboharapallavbohara USMember ✭✭

    I am coding in Xamarin.Forms, where i suppose to put this code PCL or Project.Droid

  • Pierre-ChristopheDusPierre-ChristopheDus FRUniversity ✭✭✭

    @pallavbohara Did you found any solution with Xamarin.Forms?

  • pallavbohara.0061pallavbohara.0061 USMember ✭✭
    edited August 2017

    @Pierre-ChristopheDus No... I settled for default left-alignment of text. There is one temporary solution...you can create your own toolbar from stacklayout for every page. This solution will also help you in getting soft back button event if required.
    Cheers!

  • Pierre-ChristopheDusPierre-ChristopheDus FRUniversity ✭✭✭

    @pallavbohara
    Thanks for your return.
    I've tested a similar solution, but it didn't satisfied me, cause the animation during the navigation isn't the same than with the default navigation bar...
    I tried to do something with a renderer, but I meet a problem on the OnViewAdded() method: I can't retrieve the current Xamarin.Form's page. I've opened a new topic if you would have an idea ;-)

  • HansiHansi Member ✭✭

    Any updates how to place the title in the middle with Xamarin forms?

  • Naveen9453Naveen9453 USMember ✭✭
    edited March 2018

    @YkshLeo said:
    @pallavbohara Try something like :

    var toolbarTop = (Toolbar) FindViewById(Resource.Id.toolbar_top);
    var mTitle = (TextView) toolbarTop.FindViewById(Resource.Id.toolbar_title);
    mTitle.Text = "Your Title";
    

    I am working On Xamarin.Forms project where should I have to put these lines in android or in shared part. How to access mTitle TextView from shared part to set the title.

  • Dinesh_PhDinesh_Ph INMember ✭✭

    How to achieve this in xamarin forms? Having different title in different pages?

Sign In or Register to comment.