locked
How to make all activities(except MainActivity) have ActionBar with title and back button? RRS feed

  • Question

  • User337237 posted

    I have the following MainActivity:

    When I click on "Congreso" button the following activity appears:

    And when I click on "Programa" button the following activity appears:

    All activities except MainActivity have an action bar with a back button, a title of the activity aligned at the same place each time and the same color of the action bar. In my case I want the color to be blue. My question is, is there a way to set that behaviour on a single place in the application and it to apply to all the activities except for the MainActivity so that if some day I want to change the color of the action bar or the alignement of its title I don't have to go to all activities one by one and change it and so that I don't repeat code.

    P.S.: I don't know if what is on the screenshots is an action bar or it is a toolbar. If you give me a solution with a toolbar it is OK too.

    Saturday, December 7, 2019 7:30 AM

Answers

  • User382871 posted

    Create a custom Toolbar and replace the action bar with the toolbar in MainActivity. If the activity inherits from AppCompatActivity, the type of toolbar should be Android.Support.V7.Widget.Toolbar. toolbar.xml <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?attr/actionBarSize" android:background="#ff0000" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> activity_main.xml ```

    <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />
    

    MianActivity class public class MainActivity : AppCompatActivity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState);

        // Create your application here
        SetContentView(Resource.Layout.activity_main);
    
        Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
        SetSupportActionBar(toolbar);
    
        //if you set Toolbar for Activity not AppCompatActivity, the type of toolbar shoule be Toolbar and the method should be SetActionBar. 
    }
    

    } ```

    Tutorial: https://docs.microsoft.com/en-us/xamarin/android/user-interface/controls/tool-bar/replacing-the-action-bar

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, December 9, 2019 5:15 AM

All replies

  • User382871 posted

    Create a custom Toolbar and replace the action bar with the toolbar in MainActivity. If the activity inherits from AppCompatActivity, the type of toolbar should be Android.Support.V7.Widget.Toolbar. toolbar.xml <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?attr/actionBarSize" android:background="#ff0000" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> activity_main.xml ```

    <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />
    

    MianActivity class public class MainActivity : AppCompatActivity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState);

        // Create your application here
        SetContentView(Resource.Layout.activity_main);
    
        Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
        SetSupportActionBar(toolbar);
    
        //if you set Toolbar for Activity not AppCompatActivity, the type of toolbar shoule be Toolbar and the method should be SetActionBar. 
    }
    

    } ```

    Tutorial: https://docs.microsoft.com/en-us/xamarin/android/user-interface/controls/tool-bar/replacing-the-action-bar

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, December 9, 2019 5:15 AM
  • User337237 posted

    And how do I add arrows to the toolbars and make at clicking them to be led to the previous activity?

    Monday, December 9, 2019 11:44 AM
  • User382871 posted

    @SvetoslavHlebarov said: And how do I add arrows to the toolbars and make at clicking them to be led to the previous activity?

    When you use StartActivity to navigate to a new Activity, the arrow will appear automatically.

    Tuesday, December 10, 2019 2:18 AM
  • User337237 posted

    Where do I put StartActivity?

    Tuesday, December 10, 2019 6:41 AM
  • User382871 posted

    Check the Tutorial about Android Multiscreen. https://docs.microsoft.com/en-us/xamarin/android/get-started/hello-android-multiscreen/hello-android-multiscreen-quickstart?pivots=windows

    Tuesday, December 10, 2019 7:01 AM