I'm new in Xamarin Studio, I'm trying to create a custom attribute in XAML, in a cross plataform solution, I´m using the xmlns:android="http://schemas.android.com/apk/res/android" reference, but when I use the prefix android:text="Hello world", and I run the solution it show me an error of "property 'text' was not found" at this line of code.
XAML Code:
<ContentPage.Content>
<Label Text="Hellow Word"
android:textStyle="Italic"> </Label>
</ContentPage.Content > </ContentPage>
Manifest Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0"
package="app.monodroid">
<uses-sdk android:minSdkVersion="15" />
<application android:label="Push_Money_N">
</application>
</manifest>
Error:
Thanks for any help, I have been trying for many days to solve this problem. I'm very new to Xamarin Studio, and I've worked with Android Studio before and in it I can use this custom attribute. Thanks.
Related
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?
EDIT: TLTR; This was a long and hard road. Turns out Visual Studio / Xamarin / Android / SOMETHING.. didn't like that I moved the downloaded .xml files into my drawable folder, and just renamed them to .axml so creating new .axml files and pasting the .xml layouts into there seems to have fixed it all.
.....
I think I have an issue in my layouts. The project won't build, but no errors are shown.
What I have noticed is that.. if I delete three blocks of code references my axml and layout ids, the project builds (see comments in first code section below).. also if I grab random axml and layout ids from my project (and not the ones I just imported from the online example) it also builds.
I think the issue might be:
one of the #dimens, #colors, etc is breaking my layouts, but I tried deleting them all from the .axml files with no luck.
my layout might be missing some code of declaration in the first Layout object aka RelativeLayout xmlns:android=
something I'm not catching is different in layouts in regular Android versus Xamarin android
NOTE:
All three blocks of code MUST be deleted / replaced
I have restarted my computer, restarted VS, deleted by obj/bin folders, etc.
I am recreating this project but with Xamarin Android C#: https://www.androidhive.info/2016/05/android-build-intro-slider-app/
I have the following code in my Activity OnCreate function: (NOTE that i point out which three blocks of code could be deleted in order for the build to work correctly)
class StartUpDialogs : Activity
{
...
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Making notification bar transparent
if ((int)Build.VERSION.SdkInt >= 21)
{
Window.DecorView.SystemUiVisibility = (StatusBarVisibility)(SystemUiFlags.LayoutStable | SystemUiFlags.LayoutFullscreen);
}
//REMOVING THE FOLLOWING THREE CODE BLOCKS ALLOWS IT TO BUILD
// Also replacing with Ids and Layouts not from this example work too
{
SetContentView(Resource.Layout.activity_welcome);
viewPager = FindViewById<ViewPager>(Resource.Id.view_pager);
dotsLayout = FindViewById<LinearLayout>(Resource.Id.layoutDots);
btnSkip = FindViewById<Button>(Resource.Id.btn_skip);
btnNext = FindViewById<Button>(Resource.Id.btn_next);
// layouts of all welcome sliders
// add few more layouts if you want
layouts = new int[]{
Resource.Layout.welcome_slide1,
Resource.Layout.welcome_slide2,
Resource.Layout.welcome_slide3,
Resource.Layout.welcome_slide4};
}
myViewPagerAdapter = new StartUpDialogAdapter(this, layouts);
viewPager.Adapter = (myViewPagerAdapter);
viewPager.PageSelected += OnPageChange;
btnSkip.Click += OnSkip;
btnNext.Click += GoNext;
}
...
}
Here are the layout files:
activity_welcome.axml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:showIn="#layout/activity_welcome">
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="#+id/layoutDots"
android:layout_width="match_parent"
android:layout_height="#dimen/dots_height"
android:layout_alignParentBottom="true"
android:layout_marginBottom="#dimen/dots_margin_bottom"
android:gravity="center"
android:orientation="horizontal"></LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:alpha=".5"
android:layout_above="#id/layoutDots"
android:background="#android:color/white" />
<Button
android:id="#+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#null"
android:text="#string/next"
android:textColor="#android:color/white" />
<Button
android:id="#+id/btn_skip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#null"
android:text="#string/skip"
android:textColor="#android:color/white" />
</RelativeLayout>
welcome_slide1.axml (2 - 4 are identical except with numbers switched out for 2, 3, 4)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/bg_screen1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:layout_width="#dimen/img_width_height"
android:layout_height="#dimen/img_width_height"
android:src="#drawable/ic_food" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/slide_1_title"
android:textColor="#android:color/white"
android:textSize="#dimen/slide_title"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:paddingLeft="#dimen/desc_padding"
android:paddingRight="#dimen/desc_padding"
android:text="#string/slide_1_desc"
android:textAlignment="center"
android:textColor="#android:color/white"
android:textSize="#dimen/slide_desc" />
</LinearLayout>
</RelativeLayout>
And my Resource files in case its them: (I had to move string-array into my strings.xml in order for them to be registered in my OnCreate function as the code owners project had it in colors.xml)
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<!-- Screens background color-->
<color name="bg_screen1">#f64c73</color>
<color name="bg_screen2">#20d2bb</color>
<color name="bg_screen3">#3395ff</color>
<color name="bg_screen4">#c873f4</color>
<!-- dots inactive colors -->
<color name="dot_dark_screen1">#d1395c</color>
<color name="dot_dark_screen2">#14a895</color>
<color name="dot_dark_screen3">#2278d4</color>
<color name="dot_dark_screen4">#a854d4</color>
<!-- dots active colors -->
<color name="dot_light_screen1">#f98da5</color>
<color name="dot_light_screen2">#8cf9eb</color>
<color name="dot_light_screen3">#93c6fd</color>
<color name="dot_light_screen4">#e4b5fc</color>
</resources>
dimens.xml
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="fab_margin">16dp</dimen>
<dimen name="dots_height">30dp</dimen>
<dimen name="dots_margin_bottom">20dp</dimen>
<dimen name="img_width_height">120dp</dimen>
<dimen name="slide_title">30dp</dimen>
<dimen name="slide_desc">16dp</dimen>
<dimen name="desc_padding">40dp</dimen>
</resources>
Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">XXX</string>
<string name="user_spinner_prompt">Select Account</string>
<string name="app_name">Intro Slider</string>
<string name="title_activity_welcome">Home Screen</string>
<string name="next">NEXT</string>
<string name="skip">SKIP</string>
<string name="start">GOT IT</string>
<string name="slide_1_title">Hello Food!</string>
<string name="slide_1_desc">The easiest way to order food from your favourite restaurant!</string>
<string name="slide_2_title">Movie Tickets</string>
<string name="slide_2_desc">Book movie tickets for your family and friends!</string>
<string name="slide_3_title">Great Discounts</string>
<string name="slide_3_desc">Best discounts on every single service we offer!</string>
<string name="slide_4_title">World Travel</string>
<string name="slide_4_desc">Book tickets of any transportation and travel the world!</string>
<string name="play_again_desc">To see the welcome slider again, goto Settings -> apps -> welcome slider -> clear data</string>
<string name="play_again">Play Again</string>
<string-array name="array_dot_active">
<item>#color/dot_light_screen1</item>
<item>#color/dot_light_screen2</item>
<item>#color/dot_light_screen3</item>
<item>#color/dot_light_screen4</item>
</string-array>
<string-array name="array_dot_inactive">
<item>#color/dot_dark_screen1</item>
<item>#color/dot_dark_screen2</item>
<item>#color/dot_dark_screen3</item>
<item>#color/dot_dark_screen4</item>
</string-array>
</resources>
try to check the axml which you copy from Java Project,set their BuildAction to AndroidResource like this:
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.
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.
I'm working on an Android Xamarin project for which I need to use android.support.v4.widget.NestedScrollView. I have a view which I am inflating via LayoutInflator.Inflate but the type returned is of type FrameLayout, not Android.Support.V4.Widget.NestedScrollView, which means I can't cast it to the correct type and access the ScrollView methods like SmoothScrollTo.
The ToString of the inflated view is android.support.v4.widget.NestedScrollView{27428a48 VFED.... .......D 1080,0-2160,1551} so I know at some level the correct view is being created.
Am I doing something wrong? Could this be a Xamarin bug?
Snippet of view XML as requested:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
..snip..
</android.support.v4.widget.NestedScrollView>
View inflation code:
view = (Android.Support.V4.Widget.NestedScrollView)mLayoutInflator.Inflate(Resource.Layout.view_registrationstep2, null);//InvalidCastException