I have basket activity when I add products from list of products.
Code of adding attributes
add.Click += delegate {
var intent = new Intent (this, typeof(CartActivity));
intent.PutExtra ("title", (string)(firstitem ["post_title"]));
intent.PutExtra ("price", (string)(firstitem ["price"] + " грн"));
intent.PutExtra ("weight", (string)(firstitem ["weight"] + "г"));
StartActivity (intent);
};
Code of receiving attributes
productname.Text = Intent.GetStringExtra("title");
price.Text = Intent.GetStringExtra("price");
weight.Text = Intent.GetStringExtra("weight");
I tried OnPause
namespace MurakamiKiev
{
[Activity(Label = "Murakami", Icon = "#drawable/logo", Theme = "#android:style/Theme.Black.NoTitleBar", ScreenOrientation = ScreenOrientation.Portrait)]
public class CartActivity : Activity
{
protected override void OnPause()
{
base.OnPause();
But when I run activity I have black screen
Did you call SetContentView() in OnCreate()?
like this
namespace MurakamiKiev
{
[Activity(Label = "Murakami", Icon = "#drawable/logo", Theme = "#android:style/Theme.Black.NoTitleBar", ScreenOrientation = ScreenOrientation.Portrait)]
public class CartActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.YourLayout);
}
}
}
Related
I know that Android OS need paramterles constructor to recreate Activity and i could use bundle to pass some arguments if required as follows:
private void OpenOtherActivityWindow_Click(object sender, EventArgs e)
{
Intent nextActivity = new Intent(this, typeof(ThirdActivity));
Dog mydog = new Dog("mydogName");
Bundle bundle = new Bundle();
bundle.PutSerializable("mydoggy", mydog);
nextActivity.PutExtra("RowID", Convert.ToString(10));
nextActivity.PutExtras(bundle);
StartActivity(nextActivity);
}
[Activity(Label = "ThirdActivity")]
public class ThirdActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.third);
//Receive values if any from previous activity
if (!Intent.HasExtra("mydoggy")) return;
Dog tryme = (Dog)Intent.GetSerializableExtra("mydoggy");
if (!Intent.HasExtra("RowID")) return;
string text = Intent.GetStringExtra("RowID") ?? "0";
}
}
Nevertheless is it possible to create static method which would return intent for me from given parameters like?:
static Intent CreateIntent(Dog dog, int rowID)
If so could someone show me then how it could look like as opposite to whati show in my code please.
I don't know the details of your ThirdActivity, but I could achieve the similar function by creating a simple demo.
You can check the code here.
[Activity(Label = "MovieDetailActivity")]
public class MovieDetailActivity : Activity
{
public TextView textView;
public static MovieModel mMoviemodel;// define your model here
public static int mRowID; // define a int variable mRowID
public static Intent createIntent(Context context, MovieModel movie, int rowID)
{
Intent intent = new Intent(context, typeof(MovieDetailActivity));
//Pass parameters here
mMoviemodel = movie;
mRowID = rowID;
return intent;
}
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.detaillayout);
textView = FindViewById<TextView>(Resource.Id.info_textview);
textView.Text = "movie name:" + mMoviemodel.mMovieName + " text = " + mRowID;
}
}
Usage:
// pass your Object model
StartActivity( MovieDetailActivity.createIntent(this, movie,10));
I am trying to put together my existing Xamarin.Forms application with MvvmCross.Forms. Unfortunately I am not able to go through the initialization.
MainActivity.cs
[Activity(MainLauncher = true, Label = "Main Activity")]
public class MainActivity : FormsApplicationActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Forms.Init(this, bundle);
var app = new MvxFormsApp();
LoadApplication(app);
var presenter = (MvxFormsDroidPagePresenter) Mvx.Resolve<IMvxViewPresenter>(); // Exception
presenter.MvxFormsApp = app;
Mvx.Resolve<IMvxAppStart>().Start();
}
}
Setup.cs
public class Setup : MvxAndroidSetup
{
public Setup(Context applicationContext) : base(applicationContext)
{
}
protected override IMvxApplication CreateApp()
{
return new App();
}
protected override IMvxAndroidViewPresenter CreateViewPresenter()
{
var presenter = new MvxFormsDroidPagePresenter();
Mvx.RegisterSingleton<IMvxViewPresenter>(presenter);
return presenter;
}
}
I guess the problem is that the Setup is not called at all, not even the constructor. Am I right? What is wrong with the code?
The bootstrapping is done in the splash screen.
You have to remove MainLauncher = true from your MainActivity and add a splash screen like:
[Activity(MainLauncher = true
, Theme = "#style/Theme.Splash"
, NoHistory = true
, ScreenOrientation = ScreenOrientation.Portrait)]
public class SplashScreen
: MvxSplashScreenActivity
{
public SplashScreen()
: base(Resource.Layout.SplashScreen)
{
}
private bool _isInitializationComplete;
public override void InitializationComplete()
{
if (!_isInitializationComplete)
{
_isInitializationComplete = true;
StartActivity(typeof(MainActivity));
}
}
protected override void OnCreate(Android.OS.Bundle bundle)
{
Forms.Init(this, bundle);
Forms.ViewInitialized += (object sender, ViewInitializedEventArgs e) =>
{
if (!string.IsNullOrWhiteSpace(e.View.StyleId))
{
e.NativeView.ContentDescription = e.View.StyleId;
}
};
base.OnCreate(bundle);
}
}
If you need a working example, see our example app:
https://github.com/xabre/xamarin-bluetooth-le/tree/master/Source/BLE.Client
I'm currently working on a small game for android using Xamarin in Visual Studio, I have activities for the gameplay, and the game over screen. On the game over screen I want the users score to be displayed, which I get from an intent. This works perfectly the first time, but if the user chooses to restart the game, no matter what they score, the game over screen defaults their score to zero. How can I fix this so that the correct score is displayed everytime? Also I'm still fairly new to this so any help at all would be great!
Code in Game class the calls the game over screen
this.intent = new Intent(Android.App.Application.Context, typeof(GameOverAct));
this.intent.PutExtra("ThePoint", buttons.score);
this.intent.SetFlags(ActivityFlags.NewTask);
Android.App.Application.Context.StartActivity(intent);
Game Over Activity
public class GameOverAct : Microsoft.Xna.Framework.AndroidGameActivity
{
TextView score;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.layout1);
score = FindViewById<TextView>(Resource.Id.scoretext);
score.Text = "Score: " + getGameScore();
removescore();
Button restart = FindViewById<Button>(Resource.Id.restart);
restart.Click += (sender, e) =>
{
var intent = new Intent(this, typeof(Activity1));
StartActivity(intent);
};
Button mm = FindViewById<Button>(Resource.Id.mainmenu);
mm.Click += (sender, e) =>
{
var intent3 = new Intent(this, typeof(MainMenuAct));
StartActivity(intent3);
};
}
protected override void OnRestart()
{
base.OnRestart();
SetContentView(Resource.Layout.layout1);
score = FindViewById<TextView>(Resource.Id.scoretext);
score.Text = "Score: " + getGameScore();
removescore();
Button restart = FindViewById<Button>(Resource.Id.restart);
restart.Click += (sender, e) =>
{
var intent = new Intent(this, typeof(Activity1));
StartActivity(intent);
};
Button mm = FindViewById<Button>(Resource.Id.mainmenu);
mm.Click += (sender, e) =>
{
var intent3 = new Intent(this, typeof(MainMenuAct));
StartActivity(intent3);
};
}
public int getGameScore()
{
int a = Intent.GetIntExtra("ThePoint", 0);
return a;
}
public void removescore()
{
Intent.RemoveExtra("ThePoint");
}
}
Activity that runs the Game class
public class Activity1 : Microsoft.Xna.Framework.AndroidGameActivity
{
Game1 g;
//When the Game is played for the first time after launching the app, this method is called
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
//Gets a Game1 object, which is the class that builds the game
this.g = new Game1();
//sets it to view
SetContentView((View)g.Services.GetService(typeof(View)));
g.Run();
}
//If the player chooses to restart, this method is called
protected override void OnRestart()
{
base.OnRestart();
this.g = new Game1();
SetContentView((View)g.Services.GetService(typeof(View)));
g.Run();
}
}
I have these two codes (two activities)
MainActivity
namespace App16
{
[Activity(Label = "App16", 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);
TextView tv = FindViewById<TextView>(Resource.Id.textView1);
Button button = FindViewById<Button>(Resource.Id.button1);
var activity2 = new Intent(this, typeof(Activity1)).SetFlags(ActivityFlags.ReorderToFront);
string[] str = new string[2];
str[0] = "hello"; str[1] = "سلام";
activity2.PutExtra("MyData", str);
button.Click += delegate
{
StartActivity(activity2);
};
}
}
}
Activity
namespace App16
{
[Activity(Label = "Activity1")]
public class Activity1 : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.Main2);
string[] text = Intent.GetStringArrayExtra("MyData");
TextView tvE = FindViewById<TextView>(Resource.Id.textView3);
TextView tvP = FindViewById<TextView>(Resource.Id.textView2);
Typeface typeP = Typeface.CreateFromAsset(this.Assets, "fonts/blotus.ttf");
tvP.SetTypeface(typeP, TypefaceStyle.Normal);
if (text[0] == "hello")
{
tvE.Text = text[0];
tvP.Text = text[1];
}
}
}
}
These work very well but when I use these in another program, I have this error:
Java.Lang.RuntimeException: native typeface cannot be made
I do not know why?
I changed the letters of my font to uppercase letters and my problem was solved:
BLOTUS.TTF
How do I load a url from firstActivity to the webpageActivity?
I would like to be able to click a button with the url from firstActivity then pass it to the webpage activity and load the url.
Here is my code: FirstActivity
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
var scisnews = FindViewById<Button> (Resource.Id.scisnewsbtn);
string scisnewsurl = "http://cis.ulster.ac.uk/news-a-events-mainmenu-70";
//labinduction.Click += (sender, e) => {
// var LabInductionI = new Intent (this, typeof(LabInduction));
// StartActivity (LabInductionI);
//};
scisnews.Click += delegate {
var ScisNewsI = new Intent (this, typeof(WebPage));
ScisNewsI.PutExtra ("scisnews", scisnewsurl);
this.StartActivity (ScisNewsI);
};
}
public class HelloWebViewClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading (WebView view, string url)
{
view.LoadUrl ("http://cis.ulster.ac.uk/news-a-events-mainmenu-70");
return true;
}
}
}
Code: WebPageActivity
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Create your application here
SetContentView (Resource.Layout.WebPageLO);
web_view = FindViewById<WebView> (Resource.Id.webview);
web_view.Settings.JavaScriptEnabled = true;
web_view.Settings.BuiltInZoomControls = true;
web_view.SetWebViewClient (new HelloWebViewClient ());
}
}
public class HelloWebViewClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading (WebView view, string url)
{
view.LoadUrl ("http://cis.ulster.ac.uk/news-a-events-mainmenu-70");
return true;
}
}
public override bool OnKeyDown (Android.Views.Keycode keyCode, Android.Views.KeyEvent e)
{
if (keyCode == Keycode.Back && web_view.CanGoBack ())
{
web_view.GoBack ();
return true;
}
return base.OnKeyDown (keyCode, e);
}
}
You generally pass information from one activity to another by using the Intent class. It looks like you are trying to, but don't have the code pulling the value out.
Here is an example:
//In your first activity
var intent = new Intent(this, typeof(WebPage));
intent.PutExtra("url", "http://cis.ulster.ac.uk/news-a-events-mainmenu-70");
StartActivity(intent);
//Then in the second activity
string url = Intent.GetStringExtra("url");
//Then you can load the page like this
web_view.LoadUrl(url);
Does this help?