Xamarin-Android, setting layout back not working - c#

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 :)

Related

How to Call this Method from OnCreate - Android C# Xamarin

I am wondering how I am meant to Call public static async Task CopyAssetAsync(Activity activity) from the OnCreate method.
public static async Task CopyAssetAsync(Activity activity)
{
{
var notesPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "NotesData.txt");
if (!File.Exists(notesPath))
{
try
{
UPDATE 1:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
ActionBar.Hide();
btnAdd = FindViewById<Button>(Resource.Id.btnAdd);
//Notes
notesList = FindViewById<ListView>(Resource.Id.lvNotes);
//Where I am trying to call the method...
CopyAssetAsync();
UPDATE 2:
You have to use await keyword while calling and make the OnCreate() method async :
protected override async void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
ActionBar.Hide();
btnAdd = FindViewById<Button>(Resource.Id.btnAdd);
//Notes
notesList = FindViewById<ListView>(Resource.Id.lvNotes);
//Where I am trying to call the method...
await CopyAssetAsync(this);
}

Update Recycler view by button in toolbar

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()

Can't reference to newly added layout in Xamarin

I wanna point out that i'm a total newbie in Xamarin.
My goal is to create simple app that after you push the button, it will jump to another layout. My problem is, that after i created a new layout(called layout1) and new activity, I can't reference to this layout in my MainActivity.
freshly created Activity:
namespace Layouts
{
[Activity(Label = "Activity1")]
public class Activity1 : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.layout1);
}
}
}
And my MainActivity:
namespace Layouts
{
[Activity(Label = "Layouts", MainLauncher = true, Icon = "#drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView (Resource.Layout.Main);
Button button1 = FindViewById<Button>(Resource.Id.button);
button1.Click += delegate
{
StartActivity(typeof(layout1)); //can't reference layout1 there
};
}
}
}
Any help will be great, thanks :)
StartActivity(typeof(Activity1));

nativeOnDraw failed - Xamarin Android Webview

Im using xamarin for android and i have looked into this issue but nothing seems to work.
Im trying to get a webview to work with a button but it keeps saying whenever i click on the button.
nativeOnDraw failed; clearing to background color.
I will include image of my layout and my code here.
MainActivity
namespace Application
{
[Activity(Label = "Application", MainLauncher = true, Icon = "#drawable/icon", Theme = "#android:style/Theme.NoTitleBar")]
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);
WebPage webPage = new WebPage();
Button btn_MySchedule = FindViewById<Button>(Resource.Id.btnSchedule);
Button btn_MyCareer = FindViewById<Button>(Resource.Id.btnCareer);
Button btn_MySocial = FindViewById<Button>(Resource.Id.btnSocial);
Button btn_Feedback = FindViewById<Button>(Resource.Id.btnFeedback);
Button btn_MyDetails = FindViewById<Button>(Resource.Id.btnDetails);
btn_MySchedule.Click += delegate
{
webPage.isSchedule = true;
//wPage.SetUrl("https://www.google.com");
StartActivity(typeof(WebPage));
};
}
}
}
WebPage
namespace Application
{
[Activity(Label = "WebPage", Theme = "#android:style/Theme.NoTitleBar")]
public class WebPage : Activity
{
public bool isSchedule;
private WebView _webView;
private string _url;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.WebPage);
_webView = FindViewById<WebView>(Resource.Id.webView);
new DisplayWebPage(_webView, GetUrl());
}
public string GetUrl()
{
return _url;
}
public void SetUrl(string sUrl)
{
_url = sUrl;
}
private void ButtonCLicked()
{
if (isSchedule == true)
{
SetUrl("https://www.google.com");
Toast.MakeText(this, "isSchedule is true", ToastLength.Short).Show();
}
}
}
}
I have found the issue. It was due to me calling an empty string via the GetUrl() methods.
The Solution I used is:
[Export("MyScheduleClicked")]
public void MyScheduleClicked(View v)
{
var activity2 = new Intent(this, typeof(WebPage));
activity2.PutExtra("MyURL", "https://www.google.com");
StartActivity(activity2);
}

Custom dialog collapsing when title is removed

My app is supposed to have a button in the main activity that when pressed should show a custom dialog box that will be used as a register form for users. The problem is when I remove the title bar of my custom dialog window collapses horizontally. Any advise on how to fix this? Is the idea of using a fragment as a registration form a good one? TIA
Here is the image to Illustrate
Here is what it looks like with the title bar
Here is the code for the Main Activity
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
mbtnfragment = FindViewById<Button>(Resource.Id.MyButton);
mbtnfragment.Click += mbtnfragment_Click;
}
void mbtnfragment_Click(object sender, EventArgs e)
{
FragmentTransaction transaction = FragmentManager.BeginTransaction();
create_dialog dialog = new create_dialog();
dialog.Show(transaction, "Fragment Dialog");
Console.WriteLine("Fragment created");
}
Code for the create_dialog class
class create_dialog: DialogFragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstancesState)
{
base.OnCreateView(inflater, container, savedInstancesState);
var view = inflater.Inflate(Resource.Layout.fragment_dialog, container, false);
return view;
}
public override void OnActivityCreated(Bundle savedInstanceState)
{
Dialog.Window.RequestFeature(WindowFeatures.NoTitle);
base.OnActivityCreated(savedInstanceState);
}
}
}
change the fragment_dialog layout's width from wrap_content to match_parent or a defined size
You can set a style without title to your custom dialog
#Override
public void onCreateDialog(Bundle savedInstanceState) {
setStyle(STYLE_NO_TITLE, 0);
}
Or you can set a theme in the second parameter like Theme.AppCompat.Dialog...

Categories