E.A.O.S E.A.O.S - 3 months ago 33
Android Question

Support namespaces/references Java to C# equivalent in Android-Visual Studio

I am working on a application where I am translating code from Java to C# to create a custom MapView in offline mode on android.

I am using this tutorial that is written in Java and there are a few namespaces that I am unable to comprehend where the creator of this tutorial even got them from, especially one specific that is written as

import com.mapapp.mapapp.R;
.

To specify where I am at I am working on the activity.
I don't even know what to search for when trying to find information on it as it is written as if it is a class in the project but by looking at the
package com.mapapp.main;
it seems as if it should be a nuget package or a reference to a different project in the same solution. But the creator has never mentioned anything like that in the tutorial.

package com.mapapp.main;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Environment;
import android.view.Display;
import android.view.KeyEvent;

import com.mapapp.helpers.PointD;
import com.mapapp.mapapp.R;
import com.mapapp.tileManagement.TilesProvider;
import com.mapapp.views.MapView;
import com.mapapp.views.MapViewLocationListener;


What in this case would be the equivalent in C# for this namespace, and what is R?

Here is the part in the code that uses R:

void initViews()
{
// Creating the bitmap of the marker from the resources
Bitmap marker = BitmapFactory.decodeResource(getResources(), R.drawable.marker);

// Creating our database tilesProvider to pass it to our MapView
String path = Environment.getExternalStorageDirectory() + "/mapapp/world.sqlitedb";
tilesProvider = new TilesProvider(path);

// Creating the mapView and make sure it fills the screen
Display display = getWindowManager().getDefaultDisplay();
mapView = new MapView(this, display.getWidth(), display.getHeight(), tilesProvider, marker);

// If a location was saved while pausing the app then use it.
if (savedGpsLocation != null) mapView.setGpsLocation(savedGpsLocation);

// Update and draw the map view
mapView.refresh();
}

Answer

'R' is just a public class automatically generated by the compiler which contains static references to unique resource IDs.

When any resource is compiled (e.g. image, string, XML style etc) it is given a unique integer per-compile.

To make it easy to use, R.java contains static constant names for each resource.

Note that there is no guarantee that the ID will be the same between two compiles, hence why R.java is generated and is the only supported way to reference a resource.

Here's an example.

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int marker=0x7f020000;
    }
    public static final class id {
        public static final int b1=0x7f050001;
        public static final int text1=0x7f050000;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {

        public static final int app_name=0x7f040001;
        public static final int app_name1=0x7f040003;

        public static final int hello=0x7f040000;
        public static final int hello1=0x7f040002;
    }
}

'R.drawable.marker' will resolve to '0x7f020000'

You can read more here.