I added a new layout file to my android project, I tried to reference the layout in my activity class like this SetContentView(Resource.Layout.newlayout), when I built my project, I got newlayout doesn't exist.
I got this fixed by changing the new layout build action from "BundleResource" to "AndroidResource" in the layout properties tab.
You need to simply build the solution once again to get the Layout.
Xamarin android will convert the resource files into int during build.
lemme know if you find any errors.
[Activity(Label = "Hello", MainLauncher = true, ScreenOrientation = ScreenOrientation.Portrait, Theme = "#android:style/Theme.Black.NoTitleBar")]
public class NewActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.newlayout);
}
}
newLayout.axml
Rebuild the solution and it should work.
Post errors too if not.
Related
Following this tutorial, after I created the project, there is already an error, and I haven't done anything yet:
The name 'Resource' does not exist in the current context
using Android.App;
using Android.Widget;
using Android.OS;
namespace Phoneword
{
[Activity(Label = "Phoneword", MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main); <-- Error here
}
}
}
Tried the answers here, but didn't work.
Checked this link, but as I said, I haven't done or add anything, so it does not work for me
Tried Clean, Rebuild, Build, Restart but still the same
Tried changing Build Action to None. Although the error disappeared, a new one appeared when debugging
The weird thing is I can start debugging even if there is an error. Sometimes it is okay, but after a minute it will produce the red underline, but the word Resource has a color
What would be the way/setup to completely remove this error?
I am working on an augment reality android app. I developed the app in unity using vuforia and then imported it in android studio as i wanted to design it in android studio. The activity name in generated project structure is UnityPlayerActivity which has no layout in the project. The activity is passed to UnityPlayerclass and the layout is generated. This is some code of my UnityPlayerActivity activity.
protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code
// Setup activity layout
#Override protected void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.RGBX_8888); // <--- This makes xperia play happy
mUnityPlayer = new UnityPlayer(this);
setContentView(mUnityPlayer);
mUnityPlayer.requestFocus();
}
as it uses that layout how can i design that activity by referencing it.
It is not possible to do this by referencing the object(Java does not support call by reference).
You need to define a new function in UnityPlayer class that generates a layout and returns the LayoutParams object.
Then use the Layout inflator. Something like this:
LayoutParams mParams=mUnityPlayer.layoutGenerator() //some function to generate LayoutParams
LayoutInflator.getLayoutInflator().inflate(LayoutParams);
Finally i found an easy solution to my problem. I displayed the unity scenes in a subview using a framelayout and now i can customize the UI easily. Check here and here to see more information.
I have used Ampiri integration in my app.
I copy paste all the code from their android sdk integration wizard for banner ads into my code. What bothers me is that Android Studio can not find that listener and view in the red letter.
So what is that I miss? Is there any problems on my code? If not what should I do to resolve this issue? Please help me.
Following is my code:
I’ve implemented the AdEventCallback using:
import com.ampiri.sdk.listeners.AdEventCallback;
and call it from main activity inside onCreate as describe in my code below:
public abstract class MainActivity extends AdCallbackActivity implements AdEventCallback {...}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout adView = (FrameLayout)view.findViewById(R.id.ad_view);
StandardAd standardAd = new StandardAd(this, adView,“MY_ADUNIT_ID",BannerSize.BANNER_SIZE_320x50, adListener);
standardAd.loadAd();
Please check my comments inline for each error.
view.findViewById(R.id.ad_view)
If the ad_view is in the current Activity layout set by setContentView, you don't need to add "view."
i.e.
findViewById(R.id.ad_view)
adListener
Please implement an event listener interface AdEventCallback, referencing to our integration page.
In addition, If you will have the AdEventCallback methods in your AdCallbackActivity.java file.
please try the code below.
StandardAd standardAd = new StandardAd(
this, adView, "3dfbb889-3bcd-4c34-82ae-8fcb539c3b25",
BannerSize.BANNER_SIZE_320x50, this);
If it doesn't work, please send me your MainActivity and AdCallbackActivity to suppport#ampiri.com.
This is my first Xamarin app. I have my environment set up in Visual Studio, and have a template project which I can run in the Android emulator.
Having dragged and dropped a couple of controls on to the designer surface, I find that when I run the application in the emulator, neither of the two controls that I have added (a button and a switch) display in the emulator.
I have tried:
Cleaning the solution
Rebuilding the solution
Manually deleting bin and object files
Unchecking 'Use Fast Deployment' in project Android Options
Uninstalling from the emulator
But with the same result each time.
[Project files removed]
Am I missing something?
Just downloaded your code and ran it. Uncommenting SetContentView (Resource.Layout.Main); it worked to me.
[Activity(Label = "Tgo.AndroidApp", MainLauncher = true, Icon = "#drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
}
}
Here a nice guide to get you started with Xamarin Android.
navigate to MainActivity.cs and uncomment this code
SetContentView (Resource.Layout.Main);
and rebuild the solution and debug.
Let me know it if helps!
In my case, I had to update my Main.xml file with the updates I put into the Main.axml and Rebuild.
In Xamarin, I have pasted the following code from a working activity into a blank new activity:
[Activity (Label = "SimpleOnePageViewPager", MainLauncher = true)]
public class MainActivity : FragmentActivity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.Main);
FragmentItem fragItem = new FragmentItem ();
List<Android.Support.V4.App.Fragment> fragments = new List<Android.Support.V4.App.Fragment>();
fragments.Add(fragItem);
ViewPagerAdapter pageAdapter = new ViewPagerAdapter(SupportFragmentManager, fragments);
var pager = FindViewById<ViewPager>(Resource.Id.MainViewPager);
pager.Adapter = pageAdapter;
}
}
This is the error that I am getting?
Error CS0103: The name 'Resource' does not exist in the current context
Can I please have some help with this code?
Thanks in advance
Make sure you added namespace using Project_Name;
I had this problem too, and solved with this steps:
Open MainActivity.cs of the Android project and put the line:
using AppName.Droid
This line is the name of namespace in the file Resource.designer.cs
You can change the MainActivity.cs Build Action property as 'none' instead of 'compile'. I Hope this will resolve your problem.
Thanks,
Imman
It was exactly the problem that I just solved, I think that this is a Xamarin Bug, when you add an existing xaml file it add it to the project without set it as embedded resource as it should be, and without set the custom tool command that must be set to: MSBuild:UpdateDesignTimeXaml
I met same problem today. I searched around, and also tried above solutions, but it did not work from my side.
I already resolved the problem by myself with bellow steps:
Update xamarin nuget packages
Clean and rebuild the project
P/S: hope it can help others if they meet same problem.
Happy coding!