locked
Simple Action Bar ( press 1 button and back button ) RRS feed

  • Question

  • User5953 posted

    i looked all action bar samples, but its some a little bit complicated i guess.

    i dont know much fragmented style so i think with action bar its more like monotouch style,

    is there a simple like press back or options img on action bar?

    when i check samples its shows me only message box but i get stuck at what if when i press back or other button process?

    public override bool OnCreateOptionsMenu (IMenu menu)
        {}
    

    menu. image or something?

    Wednesday, January 29, 2014 9:46 AM

All replies

  • User1341 posted

    @haluk This is possible. Just place this in your OnCreate method:

    ActionBar.SetHomeButtonEnabled(true);
    ActionBar.SetDisplayHomeAsUpEnabled(true);
    

    Then override the OnOptionsItemSelected method like this.

    public override bool OnOptionsItemSelected(IMenuItem item)
    {
       switch (item.ItemId)
       {
          case Android.Resource.Id.Home:
             Finish();
             return true;
    
          default:
             return base.OnOptionsItemSelected(item);
       }
    }
    
    Wednesday, January 29, 2014 1:47 PM
  • User5953 posted

    Thanks Sebastian

    Wednesday, January 29, 2014 2:06 PM
  • User209 posted

    If you are using Xamarin.Android.Support.v7 the Android.Resource.Id.Home should be Resource.Id.Home.

    Also you only need ActionBar.SetDisplayHomeAsUpEnabled(true); to show the Home button, no need for the ButtonEnabled call.

    Wednesday, January 29, 2014 8:56 PM
  • User64099 posted

    Android.Resource.Id.Home

    why i can't find this id?

    where is the home button defined?

    Monday, August 4, 2014 8:56 AM
  • User79851 posted

    Try use "global::Android.Resource.Id.Home".

    Thursday, October 23, 2014 4:59 PM
  • User178351 posted

    Sebastian, you are a top guy!

    Wednesday, February 3, 2016 3:25 PM
  • User1341 posted

    @HarriYerkess Thank you. I wish some more people would recognize that. ;-)

    Wednesday, February 3, 2016 3:27 PM
  • User188010 posted

    Hey Sebastian I am using code you have suggested , But I am facing some challenges I am passing Username by intent.putextra to every activity but issue is that when user get back from current activity by pressing back button provided on ActionBar the username is not present on previous activity , if user go back by pressing back button provided down by default username is present in the previous activity,

    can you help me Sebastian?

    ActionBar.SetHomeButtonEnabled(true); ActionBar.SetDisplayHomeAsUpEnabled(true);

    public override bool OnOptionsItemSelected(IMenuItem item) { switch (item.ItemId) { case Android.Resource.Id.Home: Finish(); return true;

      default:
         return base.OnOptionsItemSelected(item);
    

    } }

    Friday, February 12, 2016 9:44 AM
  • User188010 posted

    Hey Sebastian I am using above code you suggested but I am facing some challenges that I am passing UserName using intent.putExtra on every activity but using above code we can add back button on actionbar to activity and it works perfect but if we jump back to the previous activity using actionbar button username is not present but is user go back by pressing back button provided down by default username is present

    Friday, February 12, 2016 9:52 AM
  • User188010 posted

    @SanketJoshi said: Hey Sebastian I am using above code you suggested but I am facing some challenges that I am passing UserName using intent.putExtra on every activity but using above code we can add back button on actionbar to activity and it works perfect but if we jump back to the previous activity using actionbar button username is not present but is user go back by pressing back button provided down by default username is present

    Friday, February 12, 2016 9:53 AM
  • User213783 posted

    Sebastian thank you Very Very much, you're Gooooood

    Wednesday, May 18, 2016 8:17 PM
  • User218392 posted

    toolbar = FindViewById<V7Toolbar>(Resource.Id.toolbar); SetSupportActionBar(toolbar); //SupportActionBar.SetDisplayShowTitleEnabled(false); SupportActionBar.SetDisplayHomeAsUpEnabled(true);

    if i'm using SetSupportActionBar than how should i handle up button click event, i'm not getting fire this event

    ` public override bool OnOptionsItemSelected(IMenuItem item) {

            //dLayout.
            if (item.ItemId == Android.Resource.Id.Home)
            {
                Finish();
                StartActivity(typeof(SISActivity));
            }
            //Finish();
            return base.OnOptionsItemSelected(item);
        }`
    
    Monday, October 31, 2016 5:05 PM
  • User218392 posted

    toolbar = FindViewById<V7Toolbar>(Resource.Id.toolbar); SetSupportActionBar(toolbar); //SupportActionBar.SetDisplayShowTitleEnabled(false); SupportActionBar.SetDisplayHomeAsUpEnabled(true);

    if i'm using SetSupportActionBar than how should i handle up button click event, i'm not getting fire the event this is my code for OnOptionsItemSelected

    public override bool OnOptionsItemSelected(IMenuItem item) { if (item.ItemId == Android.Resource.Id.Home) { Finish(); StartActivity(typeof(SISActivity)); } return base.OnOptionsItemSelected(item); }

    Monday, October 31, 2016 5:05 PM
  • User218392 posted

    toolbar = FindViewById<V7Toolbar>(Resource.Id.toolbar); SetSupportActionBar(toolbar); //SupportActionBar.SetDisplayShowTitleEnabled(false); SupportActionBar.SetDisplayHomeAsUpEnabled(true);

    if i'm using SetSupportActionBar than how should i handle up button click event, i'm not getting fire this event

    ` public override bool OnOptionsItemSelected(IMenuItem item) {

            //dLayout.
            if (item.ItemId == Android.Resource.Id.Home)
            {
                Finish();
                StartActivity(typeof(SISActivity));
            }
            //Finish();
            return base.OnOptionsItemSelected(item);
        }`
    
    Monday, October 31, 2016 5:05 PM
  • User299676 posted

    I am using Xamarin.Android.Support.v7 and I did it like this:

    in OnCreate method

    Toolbar toolbar = FindViewById<Toolbar>(Resource.Id.toolbarID);

    SetSupportActionBar(toolbar);

    SupportActionBar.SetDisplayHomeAsUpEnabled(true);

    Then I created this method:

    public override bool OnOptionsItemSelected(IMenuItem item) { switch (item.ItemId) { case Android.Resource.Id.Home: Finish(); return true;

              default:
                   ` return base.OnOptionsItemSelected(item);`
          ` }`
        `}`
    

    Hope this will help some one :)

    Tuesday, March 21, 2017 6:54 AM
  • User366616 posted

    Thanks Sebastian, I spend hours to find back arrow. This helped me.

    Thursday, April 12, 2018 7:23 PM
  • User342099 posted

    this works

    ~~~~ public class SettingActivity : AppCompatActivity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.content_settings); SupportActionBar.SetDisplayHomeAsUpEnabled(true);

        }
    }
    

    ~~~~

    Thursday, August 8, 2019 11:43 AM