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()
Related
if I do a PushAsync in the constructor on my MainPage, the ToolbarItems from the MainPage are also displayed on the second page. I already tried to switch the toolbar items in the OnAppearing and OnDisappearing triggers on and off. Unfortunately, this is useless. I test on android and I use Visual Studio.
protected override void OnDisappearing()
{
this.ToolbarItems.Clear();
}
protected override void OnAppearing()
{
var edit = new ToolbarItem
{
Icon = "edit.png",
Command = new Command(this.startEditMode),
};
this.ToolbarItems.Add(edit);
var addpage = new ToolbarItem
{
Icon = "connect.png",
Command = new Command(this.startWebsiteConnector),
};
this.ToolbarItems.Add(addpage);
}
I'm making app in Xamarin with MVVMCross. This is my code:
Android View (iOS is almost the same):
var button = (Button)FindViewById(Resource.Id.button3);
var set = this.CreateBindingSet<MenuView, MenuViewModel>();
set.Bind(button).To(vm => vm.CommandNavigateToSecondPage);
set.Apply();
Core ViewModel:
public ICommand CommandNavigateToSecondPage
{
get
{
return new MvxCommand((() =>
{
ShowViewModel<SecondPageViewModel>();
}));
}
}
I want to have a default back button that will navigate me to previous page. I made same navigation with simple function in core and there was back button. Like this:
public void Navigate()
{
ShowViewModel<SecondPageViewModel>();
}
MVVM is all about binding this is why I want to make it in this way.
To display default home/back button in android
[Activity (Label = "MyView")]
public class MyView : MvxActivity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView(Resource.Layout.ThirdNativeView);
ActionBar.SetDisplayHomeAsUpEnabled(true);
}
/// handle back navigation here
public override bool OnOptionsItemSelected(IMenuItem item)
{
switch (item.ItemId)
{
case Android.Resource.Id.Home:
//Execute viewModel command here
this.Finish();
return true;
default:
return base.OnOptionsItemSelected(item);
}
}
}
more info on executing command here
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 :)
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...
I am creating a web wrapper app, and am wanting to only show certain elements in an html page. I have the current code below, and I know I need to use javascript possibly but am not sure where to go to from here.
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);
var webView = FindViewById<WebView> (Resource.Id.webView);
webView.Settings.JavaScriptEnabled = true;
// Use subclassed WebViewClient to intercept hybrid native calls
webView.SetWebViewClient (new WebViewClient ());
webView.LoadUrl ("http://bobhoil.com/");
}
public override bool OnCreateOptionsMenu(IMenu menu)
{
base.OnCreateOptionsMenu (menu);
MenuInflater inflater = this.MenuInflater;
inflater.Inflate (Resource.Menu.menu, menu);
return true;
}
}
Thanks
You can use something like this:
StringBuilder sb = new StringBuilder();
sb.Append("your javascript");
webView.LoadUrl("javascript:" + sb.ToString());