I have a button in my mainactivity,after clicking it,it opens a new activity(Acitivity2).
And in that activity I am opening the browser.Here are the codes-
public class Activity2 : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
this.OpenBrowser();
}
public void OpenBrowser()
{
var uri = Android.Net.Uri.Parse("https://google.com");
var intent = new Intent(Intent.ActionView, uri);
intent.AddFlags(ActivityFlags.NoHistory)
.AddFlags(ActivityFlags.NewTask);
Application.Context.StartActivity(intent);
}
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
}
}
Now my question is after successful completion of opening the url in the browser,how to go back to the app,currently browser never closes.
Sorry it may be simple question,but i am really struggling with it
Is it even possible to access Views inside of a Fragment from the calling Activity. Or do i have to put all the needed Information into the Bundle when calling NewInstance or by using a static Method inside of my Fragment
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.ActivityNavigationLayout);
var button = FindViewById<Button>(Resource.Id.Button);
}
private void SetUpFragmentManager()
{
var frag = OverViewFragment.NewInstance();
var fm = this.FragmentManager.BeginTransaction();
fm.Replace(Resource.Id.content_frame, frag);
fm.Commit();
}
When Resource.Id.Button is part of Fragment inflated inside that Activity?
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.Inflate(Resource.Layout.Main, container, false);
return rootView;
}
i have problem with navigating, i can get to the second layout but i can not get back to the first(main) from the second. Even hardware/software back button not working. As far as i know on layout Profil does not work everything, when i try to make button what can change textview on that layout, it does not work too. IDs should be right.
Here is the first activity:
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Home);
Button btnProfil = FindViewById<Button>(Resource.Id.btnProfil);
btnProfil.Click += delegate
{
StartActivity(typeof(Activity1));
};
Here is the second activity:
public class Activity1 : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Profil);
Button btnBack = FindViewById<Button>(Resource.Id.btnBack);
btnBack.Click += delegate
{
StartActivity(typeof(MainActivity));
};
}
}
These formulations works same when they are in same script.
Button btnProfil = FindViewById<Button>(Resource.Id.btnProfil);
btnProfil.Click += delegate { SetContentView(Resource.Layout.Profil); };
Button btnBack = FindViewById<Button>(Resource.Id.btnBack);
btnBack.Click += delegate { SetContentView(Resource.Layout.Home); } ;
Thank you :)
I writing app for Android using Xamarin.
I have this code in Activity for OnCreate method.
protected override int LayoutResource
{
get { return Resource.Layout.Main; }
}
private RecyclerView recyclerView;
private ProgressBar activityIndicator;
private RecyclerView.LayoutManager layoutManager;
protected override async void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
recyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView);
activityIndicator = FindViewById<ProgressBar>(Resource.Id.activityIndicator);
activityIndicator.Visibility = Android.Views.ViewStates.Visible;
layoutManager = new LinearLayoutManager(this, LinearLayoutManager.Vertical, false);
recyclerView.SetLayoutManager(layoutManager);
var repository = new TestAppRepository();
var films = await repository.GetAllFilms();
var formsAdapter = new FormAdapter(films.results);
recyclerView.SetAdapter(formsAdapter);
activityIndicator.Visibility = Android.Views.ViewStates.Gone;
SupportActionBar.SetDisplayHomeAsUpEnabled(false);
SupportActionBar.SetHomeButtonEnabled(false);
I have Button in toolbar and need to refresh Recycler when I tap this button.
Here is calling of it for display
public override bool OnCreateOptionsMenu(IMenu menu)
{
MenuInflater.Inflate(Resource.Menu.home, menu);
return base.OnCreateOptionsMenu(menu);
}
How I need to write code to refresh Recycler?
Thank's for help
Use the button's setOnClickListener method and implement the onClick method of the new OnClickListener In the onClick method called the RecyclerView adapter's notifydatasetchanged()
How can I implement a simple "splash screen" on program startup? I am copying a SQLite DB and it can be a bit of a long process that is not UI "friendly" .
I would prefer not to use "java code".
TIA
I recently solved this problem in the following way.
In the main activity I passed a parameter via the intent to set the number of milliseconds for which the splash screen would remain visible.
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
Intent i=new Intent();
i.SetClass(this, typeof (Splash));
i.PutExtra("Milliseconds", 3000);
StartActivity(i);
}
Then, in the second activity which I named "Splash" I retrieved the value and set a second thread to end the activity when the time had elapsed.
[Activity(Label = "Daraize Tech")]
public class Splash : Activity
{
private int _milliseconds;
private DateTime _dt;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
_milliseconds = Intent.GetIntExtra("Milliseconds", 1000);
SetContentView(Resource.Layout.Splash);
_dt=DateTime.Now.AddMilliseconds(_milliseconds);
}
public override void OnAttachedToWindow()
{
base.OnAttachedToWindow();
new Thread(new ThreadStart(() =>
{
while (DateTime.Now < _dt)
Thread.Sleep(10);
RunOnUiThread( Finish );
}
)).Start();
}
}
Also see http://docs.xamarin.com/android/tutorials/Creating_a_Splash_Screen
Really great tutorial.
It only takes about 10 lines of code :)
In a Styles.xml:
<resources>
<style name="Theme.Splash" parent="android:Theme">
<item name="android:windowBackground">#drawable/splash</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
In your activity:
[Activity (MainLauncher = true, Theme = "#style/Theme.Splash", NoHistory = true)]
public class SplashActivity : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Create your async task here...
StartActivity (typeof (Activity1));
}
}
This worked for me:
Starting an new thread from the splash activity. You can wait a few seconds or load some data or something else.
[Activity(MainLauncher = true, NoHistory = true)]
public class Splashscreen : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView (Resource.Layout.splashscreen);
new Thread (new ThreadStart (() =>
{
//Load something here ...
Thread.Sleep(1500);
Intent main = new Intent (this, typeof(MainActivity));
this.StartActivity (main);
this.Finish ();
})).Start ();
}
}
This solution gets you the following:
Immediate display of the splash screen
Removal of splash screen the exact time the "main" activity is launched (main activity replaces the splash activity)
In OnCreate, SetContentView is called to get the splash screen up, and then the worker thread is kicked off, which runs the slow processing data initialization stuff.
This way, the spalsh screen is displayed without delay. The last statement in the worker thread kicks off the "main" app/activity, which will have its DB & data all ready for access. Calling StartActivity() from OnCreate (ie, after initializeDataWorker.Start()), will cause MainActivity to run before/while the DB is being created and/or data is being fetched, which is usually not desireable).
This solution lacks a way to remove the splash screen from the backstack. When I get around to implementing this functionality I'll update it.
namespace Mono.Droid
{
[Activity(
Label = "Splash Activity",
MainLauncher = true,
Theme = "#android:style/Theme.Black.NoTitleBar",
Icon = "#drawable/icon",
NoHistory = false)]
public class SplashActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.SplashLayout);
Thread initializeDataWorker = new Thread(new ThreadStart(InitializeData));
initializeDataWorker.Start();
}
private void InitializeData()
{
// create a DB
// get some data from web-service
// ...
StartActivity(typeof(MainActivity));
}
}
}