Toolbar in Xamarin Android not showing icons - c#

I'm trying to create a toolbar to save items in my application. But my toolbar is not showing properly.
It is showing like this
I want to display like this
In my Fragment
public override void OnCreateOptionsMenu(IMenu menu, MenuInflater inflater)
{
inflater.Inflate(Resource.Menu.menu_RefuelingToolbar, menu);
}
menu_RefuelingToolbar.xml
<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".RefuelingFragment">
<item android:id="#+id/button_Save"
android:icon="#drawable/ic_done_white_24dp"
android:showAsAction="always"
android:title="Settings" />
</menu>

If you extend AppCompatActivity instead of Activity like this :
public class MainActivity : AppCompatActivity {...}//instead of Activity
In this case, the toolbar cant showing picture, the reason is that :
When using the appcompat library, menu resources should refer to the showAsAction in the app: namespace, not the android: namespace.
Similarly, when not using the appcompat library, you should be using the android:showAsAction attribute.
I think the problem is that you are mixing Framework Activity and AppCompat menu.
You should use AppCompatActivity with AppCompat Action bar and app:showAsAction; or Activity with android:showAsAction.
EDIT : Modify your code in menu_MGradeToolbar.xml like this :
<?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/button_MGradeSave"
android:icon="#drawable/ic_done_white_24dp"
app:showAsAction="ifRoom"
android:title="Save" />
</menu>
Here is the result.

Related

Android Xamarin grey out menu item in c#

I am trying to grey out my menu item text when it is disabled but am having trouble getting it to work. My menu.xml file is;
<?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">
<group
android:checkableBehavior="single">
<item
android:id="#+id/item1"
android:checked="false"
android:icon="#drawable/icon_item1"
android:title="item1"
app:actionLayout="#layout/item1Counter"/>
<item
android:id="#+id/about_us"
android:checked="false"
android:title="About Us" />
</group>
</menu>
I disable the menu item above by the following:
var menu1 = menuList.Menu.GetItem(0);
menu1.SetEnabled(false);
//Here is where I want to grey out the icon
Anyone know how I could do that. Also, item1Counter is the item action layout which also has text that will be greyed out. I want everything in item to be greyed out when after I disable it. Anyone know how I can do that?

How to fix "Android.Views.InflateException: Binary XML file line #18: Error inflating class (not found)LinearLayout" in Xamarin Android?

I'm trying to build an app which will have a Settings page. I searched for the best way to build a Preference page and now I'm trying to build it using PreferenceFragmentCompat. I found some tutorials on the web and tried to put it together, but the problem happens when I try to run the app. The app builds without errors, but when it deploys I get an error
Android.Views.InflateException: Binary XML file line #18: Error inflating class (not found)LinearLayout
which happens in AddPreferencesFromResource() function. It doesn't matter if I define RelativeLayout or LinearLayout in Main.axml, the error is always the same.
I tried cleaning and rebuilding project, restarting VS and finally I managed to get it to work using FragmentManager and PreferenceFragment in another app instead of SupportFragmentManager and PreferenceFragmentCompat, but I would prefer for it to work with latter options.
This is the code from a simple empty app where I reproduced the problem:
MainActivity.cs
using Android.App;
using Android.OS;
using Android.Support.V7.App;
namespace App1
{
[Activity(Label = "App1", MainLauncher = true, Icon = "#drawable/icon", Theme = "#style/MyTheme")]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
Android.Support.V4.App.FragmentTransaction transaction = SupportFragmentManager.BeginTransaction();
transaction.Replace(Resource.Id.pref_container, new SettingsFragment()).Commit();
}
}
}
SettingsFragment.cs
using Android.OS;
using Android.Support.V7.Preferences;
namespace App1
{
class SettingsFragment : PreferenceFragmentCompat
{
public override void OnCreatePreferences(Bundle savedInstanceState, string rootKey)
{
AddPreferencesFromResource(Resource.Layout.Preference);
}
}
}
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">
<FrameLayout
android:id="#+id/pref_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Preference.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="https://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="checkbox"
android:summary="Tap to check if on or off"
android:title="Checkbox Preference" />
<RingtonePreference
android:key="ringtone"
android:showDefault="true"
android:showSilent="true"
android:summary="Pick a ringtone you like"
android:title="Ringtone Preference" />
<EditTextPreference
android:dialogTitle="Enter a text"
android:key="text"
android:summary="Click to show a text entry dialog"
android:title="EditText Preference" />
<SwitchPreference
android:key="switch"
android:title="Switch Preference"
android:summary="Click to switch on or off"
android:defaultValue="true"/>
</PreferenceScreen>
Styles.xml
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="preferenceTheme">#style/PreferenceThemeOverlay.v14.Material</item>
</style>
</resources>
I want to crate Settings page using PreferenceFragmentCompat. Is there anything I'm missing?

Vector (XML) drawable on splash screen on old Android versions

I'm struggling with creating a splash screen using (XML-based) vector drawables on old Android versions (API 16) in Xamarin.Android. I've read a lot of similar questions, but I can't get it to work no matter what I try.
I currently have the following, which works fine on recent Android versions (e.g. API 25):
drawable/itlogotext.xml:
<vector xmlns:android="http://schemas.android.com/apk/res/android" [...]>
[...]
</vector>
drawable/splash_screen.xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="#3498DB" />
</item>
<item
android:drawable="#drawable/itlogotext"
android:gravity="center" />
</layer-list>
values/styles.xml:
<resources>
[...]
<style name="MyTheme.Splash" parent ="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#drawable/splash_screen</item>
<item name="colorPrimaryDark">#1B6698</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
SplashActivity.cs:
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Support.V7.App;
using JetBrains.Annotations;
[Activity(Theme = "#style/MyTheme.Splash", MainLauncher = true, NoHistory = true,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
ScreenOrientation = ScreenOrientation.Portrait)]
public sealed class SplashActivity : AppCompatActivity
{
protected override void OnResume()
{
base.OnResume();
this.StartActivity(new Intent(Application.Context, typeof(MainActivity)));
}
}
I have the latest support libraries (26.1.0.1, including Xamarin.Android.Support.v4 and Xamarin.Android.Support.v7.AppCompat, the latter depends on Xamarin.Android.Support.Vector.Drawable).
Unfortunately, when I try to run the app on old Android versions (e.g. API 16), I simply get an exception saying android.content.res.Resources$NotFoundException: File res/drawable/splash_screen.xml from drawable resource ID #0x7f020125, with this line at the bottom of the trace: Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #1: invalid drawable tag vector. Removing the <item> with the drawable from splash_screen.xml fixes the crash, but of course there's no logo in the splash screen anymore.
I have also tried adding AppCompatDelegate.CompatVectorFromResourcesEnabled = true in constructor, static constructor, and OnCreate, but no matter where I put it, the result is the splash screen not being shown at all (i.e. the Android app drawer is visible until the app's main screen is shown).
How can I get XML vector drawables on splash screens to work on earlier Android versions?
You could refer to :
https://bugzilla.xamarin.com/show_bug.cgi?id=41489
As #Jon Dick said, it should failed in < API Level 19. In API Level 21, vector support was added to Android, so it works on API Level 21+ with no changes, since the support library would just switch to the native implementation at that point.
Here is an solution : Custom an Android Application, add the AppCompatDelegate.CompatVectorFromResourcesEnabled = true like this :
[Application]
public class MainApplication : Application
{
public static Context AppContext;
public MainApplication()
{
}
public MainApplication(IntPtr handle, JniHandleOwnership transer) : base(handle, transer)
{
}
public override void OnCreate()
{
base.OnCreate();
AppCompatDelegate.CompatVectorFromResourcesEnabled = true;
}
}
Update :
You could use a picture as a background when Android device version is < 21.
Step 1 :
Create a values-21 folder in Resource folder, create a styles.xml :
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<!--
Base application theme for API 21+. This theme completely replaces
MyTheme.Splash from BOTH res/values/styles.xml on API 21+ devices.
-->
<style name="MyTheme.Splash" parent ="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#drawable/splash_screen</item>
<item name="colorPrimaryDark">#1B6698</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
A Resource/values-v21/ directory contains resources that will be used when the device that is running your app is on API Level 21 or higher. If the device is running on an older version of Android, the Resource/values-v21/ directory will be ignored.
Step 2 :
In your Reources\values\styles.xml, modify the MyTheme.Splash's windowNoTitle with a different background.
Reources\values\styles.xml :
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="MyTheme.Splash" parent ="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#drawable/splash_screen_low_version</item>
<item name="colorPrimaryDark">#1B6698</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
splash_screen_low_version.xml :
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item>
<color android:color="#3498DB" />
</item>
<item
android:drawable="#drawable/yourlogo"
android:gravity="center"/>
</layer-list>
Please note that, yourlogo is a .png file.

BottomNavigationBarXF design issue

i created a blank forms app and implemented the library BottomNavigationBarXF with nuget.
but the design is not working or maybe a part, i see it like a normal toolbar:
https://storage.googleapis.com/material-design/publish/material_v_11/assets/0Bzhp5Z4wHba3ckpYOVVyMFpJSzg/components_tabs_usage_desktop7.png
After the installation of the library i implement this:
<?xml version="1.0" encoding="utf-8" ?>
<xf:BottomBarPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xf="clr-namespace:BottomBar.XamarinForms;assembly=BottomBar.XamarinForms"
xmlns:example="clr-namespace:BottomBarXFExampleXaml;assembly=BottomBarXFExampleXaml"
x:Class="BottomBarXFExampleXaml.BarPage">
<example:TabPage Title="Favorites" Icon="ic_favorites.png" />
<example:TabPage Title="Friends" Icon="ic_friends.png" />
<example:TabPage Title="Nearby" Icon="ic_nearby.png" />
<example:TabPage Title="Recents" Icon="ic_recents.png" />
<example:TabPage Title="Restaurants" Icon="ic_restaurants.png"/>
</xf:BottomBarPage>
public App()
{
InitializeComponent();
MainPage = new BarPage();
}
Must i implement something else, maybe a design file or should all be in the library?
Thanks
I solved the problem. The library was only implemented to the forms project not to the droid project.

Progress Bar in Theme

I have been having problems with incorporating a Progress Bar into my splash screen. I have an XML file called splash_setup,
<?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"
android:background="#color/background">
<bitmap
android:src="#drawable/splash"
android:tileMode="disabled"
android:gravity="top|center_horizontal" />
<ProgressBar
style="#android:style/Widget.ProgressBar.Small"
android:gravity="top|center_horizontal" />
</LinearLayout>
This file is used as background for 'splashTheme' (in Styles.XML) as following,
?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="splashTheme" parent="android:Theme">
<item name="android:windowBackground">#drawable/splash_setup</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
The splashTheme is used in splashActivity (the splash executes database initialisation and then finished),
[Activity(Label = "#string/appName", Theme = "#style/splashTheme",
MainLauncher = true, NoHistory = true)]
So to my problem... the splash_setup code runs fine without tags, but when they are in I get the following error,
android.content.res.Resources$NotFoundException: File res/drawable/splash_setup.xml from drawable resource ID #0x7f020002
Could anyone point out what I am doing wrong? Thanks!
What you have is a layout not a drawable. android:windowBackground attribute in style allows only drawable which may include shapes and/or layer lists. If you really wish to show a progressbar, then you need an activity.

Categories