I'm trying to create a basic toolbar that I can place clickable icons on. This will be inside a separate fragment to the starting activity page.
Fragment Code:
public void onCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetHasOptionsMenu(true);
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
//Inflates the new xml into the fragment
var view = inflater.Inflate(Resource.Layout.chord_progression, container, false);
mToolbar = view.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
((AppCompatActivity)this.Activity).SetSupportActionBar(mToolbar);
((AppCompatActivity)this.Activity).SupportActionBar.Title = "Progression Maker";
//Button that when pressed returns to the login screen
btnLogout = view.FindViewById<Button>(Resource.Id.btnLogout);
btnLogout.Click += (sender, EventArts) =>
{
//Drops back into main activity
var intent = new Intent(Activity, typeof(MainActivity));
StartActivity(intent);
};
//Calls the to play the chords selected by the user
btnPlay = view.FindViewById<Button>(Resource.Id.PlayChords);
btnPlay.Click += (sender, EventArgs) => PlayChords(sender, EventArgs);
//Calls this to generate random chords from the selected key
btnRandomise = view.FindViewById<Button>(Resource.Id.btnRandomise);
btnRandomise.Click += (sender, EventArgs) => randomiseChords(sender, EventArgs);
//Spinners to store tonality and key signatures
key_Spinner = view.FindViewById<Spinner>(Resource.Id.key_Spinner);
tonality_Spinner = view.FindViewById<Spinner>(Resource.Id.tonality_Spinner);
//Spinners that allow the user to select the individual chord they want to use
chord1_spinner = view.FindViewById<Spinner>(Resource.Id.chord1);
chord2_spinner = view.FindViewById<Spinner>(Resource.Id.chord2);
chord3_spinner = view.FindViewById<Spinner>(Resource.Id.chord3);
chord4_spinner = view.FindViewById<Spinner>(Resource.Id.chord4);
chord1_spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(generateChordOne);
chord2_spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(generateChordTwo);
chord3_spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(generateChordThree);
chord4_spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(generateChordFour);
//Calls procedure that changes the spinners content based on the selection of another
tonality_Spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(tonalitySpinner_ItemSelected);
key_Spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(keySpinner_ItemSelected);
//Brings up the save dialog
btnSave = view.FindViewById<ImageButton>(Resource.Id.SaveButton);
btnSave.Click += (sender, EventArgs) =>
{
//Ensures thet the user is unable to access save function if they have nothing to save
if (tonality_Spinner.SelectedItemPosition == 0)
{
Toast.MakeText(this.Activity, "Nothing to save!", ToastLength.Long).Show();
}
else if (UserID == "-1" && tonality_Spinner.SelectedItemPosition != 0)
{
Toast.MakeText(this.Activity, "You cannot save progressions as a guest!", ToastLength.Long).Show();
}
else if (UserID == "-1" && tonality_Spinner.SelectedItemPosition == 0)
{
Toast.MakeText(this.Activity, "You cannot save progressions as a guest! And there's nothing to save!", ToastLength.Long).Show();
}
else
{
//Gets the contents of the selected items of the spinners as strings
string chord1 = chord1_spinner.SelectedItem.ToString();
string chord2 = chord2_spinner.SelectedItem.ToString();
string chord3 = chord3_spinner.SelectedItem.ToString();
string chord4 = chord4_spinner.SelectedItem.ToString();
string tonality = tonality_Spinner.SelectedItem.ToString();
string keysig = key_Spinner.SelectedItem.ToString();
FragmentTransaction transaction = FragmentManager.BeginTransaction();
DialogSave Save_Dialog = new DialogSave(chord1, chord2, chord3, chord4, tonality, keysig, UserID);
Save_Dialog.Show(transaction, "dialog fragment");
}
};
btnLoad = view.FindViewById<ImageButton>(Resource.Id.LoadButton);
btnLoad.Click += (sender, EventArgs) =>
{
if (UserID == "-1")
{
Toast.MakeText(this.Activity, "Can't load as a guest!", ToastLength.Long).Show();
}
else
{
btnLoad_Click(sender, EventArgs);
}
};
//Brings up the dialog fragment for the help tab
btnHelp = view.FindViewById<ImageButton>(Resource.Id.HelpButton);
btnHelp.Click += (sender, EventArgs) =>
{
//Pull up dialog
FragmentTransaction transaction = FragmentManager.BeginTransaction();
DialogHelp Help_Dialog = new DialogHelp();
Help_Dialog.Show(transaction, "dialog fragment");
};
if (UserID == "-1")
{
btnLogout.Text = "Sign In";
}
return view;
}
public void OnCreateOptionsMenu(IMenu menu, MenuInflater inflater)
{
inflater.Inflate(Resource.Menu.toolbar_menu, menu);
base.OnCreateOptionsMenu(menu, inflater);
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
string textToShow;
if (item.ItemId == Resource.Id.menu_info)
textToShow = "Learn more about us affa";
else
textToShow = "overflooow";
return base.OnOptionsItemSelected(item);
}
Layout for fragment:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<android.support.v7.widget.Toolbar
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 = "?android:attr/actionBarSize"
android:background = "?android:attr/colorPrimary"
android:elevation = "4dp"
android:theme = "@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows = "true"/>
</LinearLayout>
Toolbar layout:
<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app ="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/menu_info"
android:icon ="@drawable/ic_help_icon"
app:showAsAction ="ifRoom"
android:title ="Info"/>
<item android:id ="@+id/menu_overflow"
app:showAsAction ="never"
android:title="Overflow"/>
</menu>
Style page:
<?xml version="1.0" encoding = "utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>
I have already made my main activity take from 'AppCompatActivity'. I also already have reference my theme in the Activity. I have also included 'SetHasOptionsMenu(true)' in the OnCreate. The problem is it sets the title just fine but it doesn't seem to be calling the OnCreateOptionsMenu. Any ideas?
Make sure to include this your main activity to allow your fragment to create the toolbar:
public override bool OnCreateOptionsMenu(IMenu menu)
{
MenuInflater.Inflate(Resource.Menu.toolbar_menu, menu);
return base.OnCreateOptionsMenu(menu);
}