XAMARIN: How to Link [Android] Hardware Back Button to Webkit.WebView? - c#

I'm trying to link the hardware back button into my WebViews in Xamarin for Android. My WebViews are contained within OnCreate instances of TabHost (which is deprecated, but I'm using it anyway) I've got this inside my MainActivity : TabActivity class
public override void OnBackPressed()
{
base.OnBackPressed();
}
and here's an example of one of my Tab Activity Classes
[Activity]
public class SpeakersActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
//set the content view
SetContentView(Resource.Layout.subs);
//declare webview and tell our code where to find the XAML resource
WebView subWebView = FindViewById<WebView>(Resource.Id.webViewSubs);
//set the webview client
subWebView.SetWebViewClient(new WebViewClient());
//load the subscription url
subWebView.LoadUrl("https://www.bitchute.com/subscriptions/");
//enable javascript in our webview
subWebView.Settings.JavaScriptEnabled = true;
//zoom control on? This should perhaps be disabled for consistency?
//we will leave it on for now
subWebView.Settings.BuiltInZoomControls = true;
subWebView.Settings.SetSupportZoom(true);
//scrollbarsdisabled
// subWebView.ScrollBarStyle = ScrollbarStyles.OutsideOverlay;
subWebView.ScrollbarFadingEnabled = false;
}
}
and I've seen a lot information how to use this
subWebView.GoBack();
to goback in a webview, but the problem is that my WebViews are not within the scope of my hardware back button. The hardware back button is inside mainactivity class and my webviews are inside individual instances of the tab activities.
What's the best way to correct this issue? Thank you!

Thanks so much #SushiHangover !!!
I solved it like this:
[Activity]
public class SpeakersActivity : Activity
{
public override void OnBackPressed()
{
WebView subWebView = FindViewById<WebView>(Resource.Id.webViewSubs);
subWebView.GoBack();
}
}
EDIT: the way you do this with a ViewPager and Fragment is as follows:
//put this code in your MainActivity.cs
public override bool OnKeyDown(Android.Views.Keycode keyCode, KeyEvent e)
{
if (e.KeyCode == Android.Views.Keycode.Back)
{
switch (_viewPager.CurrentItem)
{
case 0:
_fm1.WebViewGoBack();
break;
case 1:
_fm2.WebViewGoBack();
break;
case 2:
_fm3.WebViewGoBack();
break;
case 3:
_fm4.WebViewGoBack();
break;
case 4:
_fm5.WebViewGoBack();
break;
}
}
return false;
}
then in a fragment instantiate WebView assign in OnCreate and create a method for going back inside Fragment :
protected static WebView _wv;
protected static View _view;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
_view = inflater.Inflate(Resource.Layout.TheFragmentLayout1, container, false);
_wv = _view.FindViewById<WebView>(Resource.Id.webView1);
//lots of code
}
public void WebViewGoBack()
{
if (_wv.CanGoBack())
_wv.GoBack();
}

Related

How to get view from main Activity

There is a class inheritor from the handler through which I control the graphic button.
To search for this button, I pass in the designer a link to the activation. But I want to pass the view there. How can I get a view from activity?
public class FlActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_fl);
toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
tabLayout = FindViewById<TabLayout>(Resource.Id.tabLayout);
viewPager = FindViewById<ViewPager>(Resource.Id.viewPager);
fpAdapter = new FpAdapter(SupportFragmentManager, null);
uiHandler = new UiHandler(this, MainLooper);
}
}
public class UiHandler : Handler
{
private Activity activity { get; }
public UiHandler(Activity a, Looper loader) : base(loader)
{
activity = a;
}
public override void HandleMessage(Message msg)
{
activity.FindViewById<ImageButton>(Resource.Id.imageButton1).SetImageResource(msg.Data.GetInt("ID", Resource.Drawable.bt_off));
}
}
If I change private Activity activity { get; } to private View view { get; }, how can I transfer the view from the main activity when creating a handler instance. What to replace this in creating a nadler object?
There are quite several ways to do this :
this.Window.DecorView.RootView;
or
this.Window.DecorView.FindViewById(Android.Resource.Id.Content);
or
this.FindViewById(Android.Resource.Id.Content);
or
this.FindViewById(Android.Resource.Id.Content).RootView;
or
((ViewGroup) this.FindViewById(Android.Resource.Id.Content)).GetChildAt(0);
Although, since it's probably a layout, not a single view, I recommend using ViewGroup instead of View (these methods returns View so you have to cast them to ViewGroup if you want)
=================
Credit to this answer

how to change navigation bar title icon on xamarin.forms ios?

i want to change navigation bar title icon on my xamarin.forms ios application.You can find requested point at the attached image.app_screenshot
You can achieve this by using Custom Renderers on Xamarin.Forms. Since each UIViewController has its own NavigationItem, you should do this in your particular page which you want to modify its LeftBarButtonItem. Here is my Renderer for you referring to:
[assembly: ExportRenderer(typeof(CustomPage), typeof(CustomPageRenderer))]
namespace UIBarButtomItemsForms.iOS
{
public class CustomPageRenderer : PageRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
var navigationItem = NavigationController.TopViewController.NavigationItem;
UIBarButtonItem leftItem = new UIBarButtonItem(UIImage.FromBundle("Image.png"), UIBarButtonItemStyle.Plain, (sender, args) =>
{
});
navigationItem.SetLeftBarButtonItem(leftItem, false);
}
}
}
Then on Forms you can use this CustomPage, please notice that you should make your MainPage wrapped in a NavigationPage: MainPage = new NavigationPage(new MainPage());
Take a look at Change the Back Button: https://developer.xamarin.com/recipes/ios/content_controls/navigation_controller/change_the_back_button/

Xamarin how to open another layout by clicking on new resource layout

I am new with Xamarin, but I am training by creating parking application. Now I got issue by trying to access to another layout.
This is my MainActivity.cs
[Activity(Label = "CustomActionBarParking", MainLauncher = true, Icon = "#drawable/icon", Theme ="#style/CustomActionBarTheme")]
public class MainActivity : Activity
{
private LinearLayout mBarZone;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
ActionBar.SetDisplayShowCustomEnabled(true);
SetContentView(Resource.Layout.action_bar);
mBarZone = FindViewById<LinearLayout>(Resource.Id.linearLayout2);
mBarZone.Click += (object sender, EventArgs args) =>
{
SetContentView(Resource.Layout.zones_list);
};
}}}
Here I am accessing from my menu, by clicking on "Zones" action bar. And open "zones list" layout.
From here I want to access to another Layout: vehicle_not_parked by clicking on blue zone action bar button. But I don't know where I have to initialize it, because when I initialized that in MainAcitivy class on OnCreate method, then I got error, that my object is nullable. Then I create ZonesActivity.cs which looks like this:
[Activity(Label = "CustomActionBarParking")]
public class ZonesActivity : Activity
{
private LinearLayout mBlueZone;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.zones_list);
mBlueZone = FindViewById<LinearLayout>(Resource.Id.linearLayout2);
mBlueZone.Click += (object sender, EventArgs args) =>
{
SetContentView(Resource.Layout.vehicle_not_parked);
};
}}}
But when I tryed to call this class in Main Activity class I have to deal with Bundle savedInstanceState property. I am not really know how I can from one view -> 2nd view and then -> 3rd view.
If I understand you correctly, your swapping out the layouts in the button click event? I think it would be best to start a new activity
mBarZone.Click += delegate {
StartActivity(typeof(ZonesActivity));
};
Docs on starting a new activity

Xamarin Toast Message error (C#)

I want to display a toast Message. If I'd do this in onCreate() it'd work fine. But I want to do it like this and I get an error:
Java.Lang.NullPointerException: Attempt to invoke virtual method
'android.content.res.Resources android.content.Context.getResources()'
on a null object reference
What should I do?
public void textToast(string textToDisplay) {
Toast.MakeText(this,
textToDisplay, ToastLength.Long).Show();
}
class SampleTabFragment : Fragment
{
Button add;
MainActivity main = new MainActivity();
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
var view = inflater.Inflate(Resource.Layout.Tab, container, false);
add = view.FindViewById<Button>(Resource.Id.add);
add.Click += Click;
return view;
}
void Click(object sender, EventArgs eventArgs)
{
main.textToast( "I like Toast!");
}
}
The Java.Lang.NullPointerException is triggered because you are manually creating and using an instance of MainActivity.
Instead of using a custom instance of MainActivity to display your toast message in Click, simplify your code to use the fragments existing activity reference:
public void textToast(string textToDisplay) {
Toast.MakeText(this,
textToDisplay, ToastLength.Long).Show();
}
class SampleTabFragment : Fragment
{
Button add;
// Remove manual creation code
// MainActivity main = new MainActivity();
// ...
void Click(object sender, EventArgs eventArgs)
{
(Activity as MainActivity).textToast( "I like Toast!");
}
}
This code assumes that the owning activity is always an instance of MainActivity.
See:
Fragment getActivity()
How to use the method of an Activity in a DialogFragment?
If I understand correctly your question, I think a good solution may be this one:
public void makeToast(Context ctx, string str)
{
Toast.MakeText(ctx, str, ToastLength.Long).Show();
}
And when you use it in every fragment you have, you can call it just writing:
makeToast(this.Activity, "test!");
Works for me, let me know :)

Navigate back to calling Fragment from another activity

I have a MainActivity which has 3 fragments. 2 of these fragments have lists in them and on click of a list item I go to another activity (ie. NewActivity). I have an ActionBar implemented on the NewActivity and want to go back to the calling fragment from this activity.
I have the following code:
MainActivity.cs
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
SupportActionBar.SetDisplayShowHomeEnabled(true);
var ft = SupportFragmentManager.BeginTransaction();
ft.Add(Resource.Id.fragment_container, homeFragment, "FRAG_HOME").SetBreadCrumbShortTitle("FRAG_HOME");
ft.Add(Resource.Id.fragment_container, FragmentList1, "FRAG_LIST1").SetBreadCrumbShortTitle("FRAG_LIST1");
ft.Hide(FragmentList1);
ft.Add(Resource.Id.fragment_container, FragmentList2, "FRAG_LIST2").SetBreadCrumbShortTitle("FRAG_LIST2");
ft.AddToBackStack(null);
ft.Hide(FragmentList2);
ft.Commit();
FragmentList1.cs
void LvLatestArticles_ItemClick (object sender, AdapterView.ItemClickEventArgs e)
{
Intent intent = new Intent(Activity, typeof(NewActivity));
StartActivity(intent);
}
NewActivity.cs
public override bool OnOptionsItemSelected(IMenuItem item)
{
switch(item.ItemId)
{
case Android.Resource.Id.Home:
SupportFragmentManager.PopBackStackImmediate();
break;
}
return base.OnOptionsItemSelected(item);
}
In the above code, I override the Home button functionality on ActionBar. But it simply does not do anything. How do I go forward with this? Any help would be appreciated
Try calling OnBackPressed() on the Home button. Something like so :-
public override bool OnOptionsItemSelected(IMenuItem item){
switch(item.ItemId){
case Android.Resource.Id.Home:
base.OnBackPressed();
break;
}
return base.OnOptionsItemSelected(item);
}

Categories

Resources