Xamarin Android application not finding resources

I have a very small application that (for now) only gets the devices screen width and height. However, when I try to run it on an actual device I get three errors:

  • 'Resource Id' does not contain a definition for 'screenHeightDp'
  • 'Resource Layout' does not contain a definition for 'activity_main'
  • 'Resource Id' does not contain a definition for 'screenWidthDp'

Here is my main_activity.axml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:text="Screen Width:"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/screenWidthDp" />
    <TextView
        android:text="Screen Height:"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/screenHeightDp" />
</LinearLayout>

And here is my MainActivity.cs

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

namespace WallpapersApp
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            int deviceWidthPixels = Resources.DisplayMetrics.WidthPixels;
            int deviceHeightPixels = Resources.DisplayMetrics.HeightPixels;
            base.OnCreate(savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);
            TextView deviceWidthText = FindViewById<TextView>(Resource.Id.screenWidthDp);
            TextView deviceHeightText = FindViewById<TextView>(Resource.Id.screenHeightDp);

            deviceWidthText.Text = "Screen width: " + deviceWidthPixels.ToString();
            deviceHeightText.Text = "Screen height: " + deviceHeightPixels.ToString();
        }
    }
}

So I am not sure what is going wrong. Is there a difference between "Resources" and "Resource"? Do I need to define 'screenHeightDp', 'activity_main', and 'screenWidthDp' in Resources/strings xml?
Thanks.

Tagged:

Answers

  • CharwakaCharwaka INMember ✭✭✭✭✭

    No Need to Define, Just Add Namespace before WallpapersApp.Resource.Id.screenWidthDp

  • jezhjezh Member, Xamarin Team Xamurai

    No, you don't need to define 'screenHeightDp', 'activity_main', and 'screenWidthDp' in Resources/strings xml.

    Just try to use the following code instead:

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout 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:orientation="vertical"
    android:layout_height="match_parent">
     <TextView
        android:text="Screen Width:"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/screenWidthDp" />
    <TextView
        android:text="Screen Height:"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/screenHeightDp" />
    

Sign In or Register to comment.