almin causevic
almin causevic

Reputation: 23

Xamarin Android menu icons not showing up

Can you help me? I can not see any icons. Is this due to my icon size maybe or my xamarin configuration or is it really the code? I tried to copy 1 on 1 the xamarin toolbar guideline into my code, nothing changed. Unfortunately I can not post images of my screen as a beginner :/. There should be an "X" icon in the right corner for cancelling or at least the three dots, but nothing is showing.

Code:

Main.axml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />
    <Space
        android:id="@+id/s0"



android:layout_below="@id/toolbar"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_margin="5dp" />
<Button
    android:layout_margin="10dp"
    android:id="@+id/button1"
    android:layout_below="@id/s0"
    android:layout_height="20dp"
    android:drawablePadding="10dip"
    android:textSize="12sp"
    android:gravity="start|center_vertical"
    android:layout_width="fill_parent"
    android:text="@string/rate"
    android:drawableLeft="@drawable/ic_action_star_icon"
    android:background="@android:color/transparent" />
<View
    android:layout_margin="10dp"
    android:id="@+id/v1"
    android:layout_below="@id/button1"
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="#d3d3d3" />
<Button
    android:layout_margin="10dp"
    android:id="@+id/button2"
    android:layout_below="@id/v1"
    android:textSize="12sp"
    android:drawablePadding="10dip"
    android:gravity="start|center_vertical"
    android:layout_alignParentLeft="true"
    android:layout_height="20dp"
    android:layout_width="fill_parent"
    android:text="@string/empfehlen"
    android:drawableLeft="@drawable/ic_action_google_plus_one"
    android:background="@android:color/transparent" />
<View
    android:layout_margin="10dp"
    android:id="@+id/v2"
    android:layout_below="@id/button2"
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="#d3d3d3" />
<Button
    android:layout_margin="10dp"
    android:id="@+id/button3"
    android:layout_below="@id/v2"
    android:textSize="12sp"
    android:drawablePadding="10dip"
    android:gravity="start|center_vertical"
    android:layout_alignParentLeft="true"
    android:layout_height="20dp"
    android:layout_width="fill_parent"
    android:text="@string/gefaellt"
    android:drawableLeft="@drawable/ic_action_like"
    android:background="@android:color/transparent" />
<View
    android:layout_margin="10dp"
    android:id="@+id/v3"
    android:layout_below="@id/button3"
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="#d3d3d3" />
<Button
    android:layout_margin="10dp"
    android:id="@+id/button4"
    android:drawablePadding="10dip"
    android:layout_below="@id/v3"
    android:textSize="12sp"
    android:gravity="start|center_vertical"
    android:layout_alignParentLeft="true"
    android:layout_height="20dp"
    android:layout_width="fill_parent"
    android:text="@string/feedback"
    android:drawableLeft="@drawable/ic_action_unnamed"
    android:background="@android:color/transparent" />
<View
    android:layout_margin="10dp"
    android:id="@+id/v4"
    android:layout_below="@id/button4"
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="#d3d3d3" />
<Button
    android:layout_margin="10dp"
    android:id="@+id/button5"
    android:layout_below="@id/v4"
    android:drawablePadding="10dip"
    android:textSize="12sp"
    android:gravity="start|center_vertical"
    android:layout_alignParentLeft="true"
    android:layout_height="20dp"
    android:layout_width="fill_parent"
    android:paddingLeft="22dip"
    android:text="@string/impr"
    android:background="@android:color/transparent" />
<View
    android:layout_margin="10dp"
    android:id="@+id/v5"
    android:layout_below="@id/button5"
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="#d3d3d3" />
<Button
    android:layout_margin="10dp"
    android:id="@+id/button6"
    android:layout_below="@id/v5"
    android:drawablePadding="10dip"
    android:textSize="12sp"
    android:gravity="start|center_vertical"
    android:layout_alignParentLeft="true"
    android:paddingLeft="22dip"
    android:layout_height="20dp"
    android:layout_width="fill_parent"
    android:text="@string/schutz"
    android:background="@android:color/transparent" />
<View
    android:layout_margin="10dp"
    android:id="@+id/v6"
    android:layout_below="@id/button6"
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="#d3d3d3" />
<Button
    android:layout_margin="10dp"
    android:id="@+id/button7"
    android:layout_below="@id/v6"
    android:drawablePadding="10dip"
    android:textSize="12sp"
    android:gravity="start|center_vertical"
    android:layout_alignParentLeft="true"
    android:paddingLeft="22dip"
    android:layout_height="20dp"
    android:layout_width="fill_parent"
    android:text="@string/nutz"
    android:background="@android:color/transparent" />
<View
    android:layout_margin="10dp"
    android:id="@+id/v7"
    android:layout_below="@id/button7"
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="#d3d3d3" />
<Space
    android:id="@+id/s1"
    android:layout_below="@id/v7"
    android:layout_width="fill_parent"
    android:layout_height="10dip" />
<Button
    android:id="@+id/button8"
    android:layout_below="@id/s1"
    android:textSize="12sp"
    android:layout_height="20dp"
    android:layout_width="fill_parent"
    android:text="@string/ab"
    android:background="@android:color/transparent"
    android:textColor="@color/red"
    android:gravity="start|center_vertical"
    android:layout_alignParentLeft="true"
    android:paddingLeft="22dp"
    android:layout_margin="10dp" />

</RelativeLayout>

top_menus.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:local="http://schemas.android.com/apk/res-auto">
      <item
           android:id="@+id/x"
           android:title="prille"
           android:icon="@drawable/ic_action_star_icon"
           local:showAsAction="always" />
    </menu>

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:id="@+id/app_bar_layout"
     android:layout_width="match_parent"
     android:layout_height="wrap_content">

  <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="?attr/colorPrimary" >

    <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="More"
       android:textColor="#000000"
       android:layout_gravity="center"
       android:id="@+id/toolbar_title" />

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

MainActivitiy.cs

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Support.V7.App;
using Toolbar = Android.Support.V7.Widget.Toolbar;
using static Android.Resource;
using static App2.Resource;
using System;

namespace App2
{
    [Activity(Label = "More", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : AppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

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

            //ActionBar.Title = "My Toolbar";

            SetContentView(Resource.Layout.Main);
        }

        public override bool OnCreateOptionsMenu(Android.Views.IMenu menu)
        {
            MenuInflater.Inflate(Resource.Menu.top_menus, menu);
            return base.OnCreateOptionsMenu(menu);
        }
    }
}

Besides this does not work for me: //ActionBar.Title = "My Toolbar"; Always gives me the message it is pointing to a null reference object, but the toolbar is definitely set and shown in my application!

So I did it with a TextView inside the toolbar.

Please help me!! Thank you a lot

Upvotes: 2

Views: 3563

Answers (1)

Maxime Esprit
Maxime Esprit

Reputation: 1015

You should NOT use ToolBar. The AppCompatActivity already provide suitable Action Bar for top right menu. Also for your title.

Take a look here to get a full nice tutorial.

Use this code :

MainActivity.cs

using Android.App;
using Android.OS;
using Android.Support.V7.App;

namespace App1
{
    [Activity(Label = "More", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : AppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.Main);

            SupportActionBar.Title = "My Toolbar";
        }

        public override bool OnCreateOptionsMenu(Android.Views.IMenu menu)
        {
            MenuInflater.Inflate(Resource.Menu.top_menus, menu);
            return base.OnCreateOptionsMenu(menu);
        }
    }
}

Main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/MyButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/Hello" />
</LinearLayout>

top_menus.xml

<?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/x"
       android:title="prille"
       android:icon="@drawable/appicon_cloche"
          app:showAsAction="always"/>
</menu>

As explained here, in your manifest, you must add this code in your application tag :

android:theme="@style/Theme.AppCompat.Light"

Here is the result given :

enter image description here

Upvotes: 2

Related Questions