Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Simple Action Bar ( press 1 button and back button )

HalukmyHalukmy TRMember ✭✭

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?

Posts

  • HalukmyHalukmy TRMember ✭✭

    Thanks Sebastian

  • CheesebaronCheesebaron DKInsider, University mod

    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.

  • EricLuEricLu USMember

    Android.Resource.Id.Home

    why i can't find this id?

    where is the home button defined?

  • Sebastian, you are a top guy!

  • SebastianSeidel.9226SebastianSeidel.9226 DEInsider, University ✭✭✭✭

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

  • SanketJoshiSanketJoshi USMember ✭✭

    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);
    

    }
    }

  • SanketJoshiSanketJoshi USMember ✭✭

    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

  • SanketJoshiSanketJoshi USMember ✭✭

    @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

  • KhaledAlBashirKhaledAlBashir USMember

    Sebastian thank you Very Very much, you're Gooooood

  • ArvindrajaArvindraja INMember ✭✭✭

    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);
        }`
    
  • ArvindrajaArvindraja INMember ✭✭✭

    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); }

  • ArvindrajaArvindraja INMember ✭✭✭

    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);
        }`
    
  • BageraBagera USMember ✭✭
    edited March 2017

    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 :)

  • jigmothvsjigmothvs Member ✭✭

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

  • LearnEverythingLearnEverything USMember ✭✭✭

    this works

    public class SettingActivity : AppCompatActivity
        {
            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
                SetContentView(Resource.Layout.content_settings);
                SupportActionBar.SetDisplayHomeAsUpEnabled(true);
    
            }
        }
    
Sign In or Register to comment.