How to make Zebra Xing (Zxing) as subview in Xamarin Android - xamarin

In my Xamarin.Android app, I want to use ZXing to scan barcode. I want to display the scanner in the view of an activity.
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="5">
<Button
android:text="Scan with Default Overlay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/buttonScanDefaultView"
android:layout_weight="1" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/scanView"
android:layout_weight="2" />
</LinearLayout>
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
scannerFragment = new ZXingScannerFragment ();
scannerFragment.CustomOverlayView = CustomOverlayView;
scannerFragment.UseCustomOverlayView = UseCustomOverlayView;
scannerFragment.TopText = TopText;
scannerFragment.BottomText = BottomText;
this.FragmentManager.BeginTransaction ()
.Replace (Resource.Id.scanView, scannerFragment, "ZXINGFRAGMENT")
.Commit ();
}
I'm getting an error stating that I cannot convert support.v4.fragment into android.app.Fragment.
Can anyone advise what I'm doing wrong and how should I approach this to get the scanner view (of ZXing) in a layout of my current activity.

ZXingScannerFragment derives from Android.Support.V4.App.Fragment while the Activity.FragmentManager expects fragments derived from Android.App.Fragment.
Now, how to fix that:
Inherit your activity from any activity that works with Android.Support.V4. Easiest would to use Android.Support.V4.App.FragmentActivity from package Xamarin.Android.Support.v4 that is already installed by ZXing.Net.Mobile package as a dependency.
When you have correct activity, you can use this.SupportFragmentManager instead of this.FragmentManager to work with Support.V4-based fragments.
So, layout you have is good. Code should be updated to something like:
using Android.App;
using Android.Widget;
using Android.OS;
using ZXing.Mobile;
using Android.Support.V4.App;
namespace ZXingSample
{
[Activity(Label = "ZXing Sample", MainLauncher = true, Icon = "#mipmap/icon")]
public class MainActivity : FragmentActivity
{
int count = 1;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
var scannerFragment = new ZXingScannerFragment();
scannerFragment.UseCustomOverlayView = false;
scannerFragment.TopText = "Scan your code";
scannerFragment.BottomText = "Then proceed";
this.SupportFragmentManager.BeginTransaction()
.Replace(Resource.Id.scanView, scannerFragment, "ZXINGFRAGMENT")
.Commit();
}
}
}
Launching the app, you will see your scanner:

Related

Xamarin Android WebView not opening links in browser once SetWebViewClient is called

I have a simple webview that is loading some locally stored HTML. There are plain vanilla links in the HTML, like <a href="https://google.com"/>. When a user taps on one of these links, I want the device browser to open.
This is the default behaviour for an Android webview and seems to work fine, however as soon as I call the webView.SetWebViewClient(webViewClient) method, this default behaviour stops.
I've tried overriding the ShouldOverrideUrlLoading() method(s) in my implementation of the WebViewClient class to open the browser myself, but these methods are never getting called.
To summarise, like this, links in my HTML will open in the browser:
webView = new WebView(Activity);
//var webViewClient = new WebViewClient();
//webView.SetWebViewClient(webViewClient);
webView.LoadData(myHTML, "text/html", null);
But as soon as I uncomment those two lines, the links will open within the WebView.
I'm not sure why your ShouldOverrideUrlLoading method is not being called in your class as I don't have all your code to look at. So, I put together a fully working example where you can control how the links are opened--whether in your webview view or another Android browser activity. I hope it helps!
WebView.axml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/WebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent">
<WebView
android:id="#+id/webviewMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" />
</LinearLayout>
testwebview.html ("Assets/html/testwebview.html" in project)
<html>
<head>
<title>WebView Testing</title>
</head>
<body>
<h1>Select a link...</h1>
<br />
Google
<br />
Stack Overflow
</body>
</html>
WebViewCustomOverrideActivity.cs
using Android.App;
using Android.OS;
using Android.Webkit;
using Android.Content;
namespace XamdroidMaster.Activities {
[Activity(Label = "Custom WebView Testing", MainLauncher = true)]
public class WebViewCustomOverrideActivity : Activity {
protected override void OnCreate(Bundle savedInstanceState) {
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.WebView);
WebView wv = FindViewById<WebView>(Resource.Id.webviewMain);
MyWebViewClient myWebViewClient = new MyWebViewClient();
wv.SetWebViewClient(myWebViewClient);
wv.LoadUrl("file:///android_asset/html/testwebview.html"); // "Assets/html/testwebview.html" in project
}
}
public class MyWebViewClient : WebViewClient {
public override bool ShouldOverrideUrlLoading(WebView view, string url) {
if (url.ToLower().Contains("google.com")) {
// Let my WebView load the page
return false;
}
// For all other links, launch another Activity that handles URLs
var intent = new Intent(Intent.ActionView, Android.Net.Uri.Parse(url));
view.Context.StartActivity(intent);
return true;
}
}
}

my activity_main.axml in visual studio 2017 xamarin android app don't have any layout design showed

Hello and good day developers, I am here again to ask you some
question-related to my new build project in visual studio 2017 xamarin
the android app.used blank app..the issue is I can't see my layout design,
also when I drag and drop a button, textbox etc, but when I click the
centre of the white background there is a rectangular blueprint show
or blue border...
Note: this is a built-in code.
// activity_main.axml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
// MainActivity.cs
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
namespace MhylesOrdering
{
[Activity(Label = "#string/app_name", Theme = "#style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
}
}
}
// ScreeenShot
Now I added new layout which is the layout1.axml but the issue is
still there

Add Page on Navigate Drawer Menu and navigate to respective page on menu click Xamarin Android

I have made a sample with Navigate Drawer Menu from Left side. I have fixed 4 menu items as (Home, About Us, Enquiry, Contact Us).
Now I need to add pages to my layout, By default it should show Home Page with respective content. Then when I would click on About Us menu item then should display About Us Page with respective content...so on for other menu links too.
I didn't have an idea to how to add page and how should I make menu clickable and to track which menu item should have been clicked and which page should have to open and how.
I have searched a lot for this on google, on YouTube and on several tutorials, but didn't found any proper guidance. Therefore kindly help me regarding this please....
below it the code of MainActivity.cs
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Support.V7.App;
using Android.Support.V4.Widget;
using V7Toolbar = Android.Support.V7.Widget.Toolbar;
using Android.Support.Design.Widget;
namespace NavigationDrawerLayout
{
[Activity(Label = "J&K Tour and Travel", Theme = "#style/Theme.DesignDemo", MainLauncher = true, Icon = "#drawable/icon")]
public class MainActivity : AppCompatActivity
{
DrawerLayout drawerLayout;
NavigationView navigationView;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
// Create ActionBarDrawerToggle button and add it to the toolbar
var toolbar = FindViewById<V7Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
var drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, Resource.String.drawer_open, Resource.String.drawer_close);
drawerLayout.SetDrawerListener(drawerToggle);
drawerToggle.SyncState();
navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
setupDrawerContent(navigationView);
}
void setupDrawerContent(NavigationView navigationView)
{
navigationView.NavigationItemSelected += (sender, e) => {
//e.MenuItem.SetChecked(true);
drawerLayout.CloseDrawers();
};
}
public override bool OnCreateOptionsMenu(IMenu menu)
{
navigationView.InflateMenu(Resource.Menu.nav_menu);
return true;
}
}
}
Also i am sharing a link of .rar file of my whole sample project below. I have made this project using VS 2017 Community Edition using c#:
https://drive.google.com/open?id=0B584mT-OF6vJZzdFem4tMG9jV1U
It is recommended to use NavigationDrawer with Fragment to show different pages.
In your layout you can place a FrameLayout for the container of fragments for example:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<FrameLayout android:id="#+id/framelayout"
android:layout_height="match_parent"
android:layout_width="match_parent" />
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_gravity="start"
android:layout_height="match_parent"
android:id="#+id/nav_view"
app:menu="#menu/nav_menu"
app:headerLayout="#layout/drawer_header" />
</android.support.v4.widget.DrawerLayout>
Then in the OnCreate method first commit your home page like this:
FragmentTransaction transaction = this.FragmentManager.BeginTransaction();
HomeFragment home = new HomeFragment();
transaction.Add(Resource.Id.framelayout, home).Commit();
Then in the NavigationItemSelected event:
var naviview = FindViewById<NavigationView>(Resource.Id.nav_view);
naviview.NavigationItemSelected += (sender, e) =>
{
e.MenuItem.SetChecked(true);
FragmentTransaction transaction1 = this.FragmentManager.BeginTransaction();
switch (e.MenuItem.TitleFormatted.ToString())
{
case "Home":
HomeFragment home = new HomeFragment();
transaction1.Replace(Resource.Id.framelayout, home).AddToBackStack(null).Commit();
break;
case "About Us":
VideoFragment video = new VideoFragment();
transaction1.Replace(Resource.Id.framelayout, video).AddToBackStack(null).Commit();
break;
}
drawerLayout.CloseDrawers();
};
Since it is Fragment, then for example, your Home Page should be something like this:
public class HomeFragment : Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(Resource.Layout.homelayout, container, false);
return view;
}
}

ProgressBar multiple displaying

I have an AsyncTask linked to a refresh Button (when I click on my refresh button my AsyncTask is called).
I have on my Layout a LinearLayout field for my ProgressBar :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#color/grey">
<Button android:id="#+id/refresh_p"
android:text="#string/refresh_promo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/custom_button1"/>
<LinearLayout android:id="#+id/linearlayoutProgressBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="#color/tabTransparent"
android:cacheColorHint="#00000000"/>
<!-- <ListView android:id="#+id/list_promo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>-->
</LinearLayout>
In my AsyncTask :
#Override
protected void onPreExecute()
{
super.onPreExecute();
pb = new ProgressBar(context);
LinearLayout ll = (LinearLayout) ((Activity) context).findViewById(R.id.linearlayoutProgressBar);
ll.addView(pb);
pb.setVisibility(View.VISIBLE);
}
#Override
protected void onPostExecute(ArrayList<HashMap<String, String>> promoList)
{
...
if (pb!=null)
{
pb.setVisibility(View.GONE);
((LinearLayout)pb.getParent()).removeView(pb);
}
}
The problem I have is when I make more than 2 clicks on my Refresh Button then Multiple ProgressBar are displaying into the screen.. I just want that the new ProgressBar replace the old at the same position ..
My guess is that you are probably starting your AsyncTask in a manner similar to this:
new RefreshTask().execute(params);
Instead of that, create an instance variable in your activity. Let's say this is named mTask and substitute the above code for the following call:
if(mTask != null && mTask.getState != AsyncTask.State.RUNNING){
mTask = new RefreshTask();
mTask.execute(params);
}
This way you will ensure that only one instance of your task is running at a given time and the user will have to wait until the refresh is finished before he can start a new refresh.
If you want the user to be able to cancel an existing refresh task and run a new one, you will have to cancel the old one before starting a new one:
if(mTask != null){
if(mTask.getStatus() == AsyncTask.Status.RUNNING){
mTask.cancel(true);
}
mTask = new RefreshTask();
mTask.execute(params);
}
This way the old task and it's ProgressBar will be properly replaced by the new one.

Not able to set the background to a ListView

I am trying to set background for my List view . I have taken a image from the drawable and set it as background to the layout file then it is giving the result as following screen shot
and when i just remove the background am getting the thing which i required but no background.
and screen shot is
Is there is any other Syntax to set background for ListView.
And my Code for layout is
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:text="#+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/label"
android:textColor="#FF0000"
android:textSize="30px"></TextView>
</LinearLayout>
And my java code and that is
String[] names = new String[] { "India", "Malaysia" };
// Use your own layout and point the adapter to the UI elements which
// contains the label
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.screen3,
R.id.label, names));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
if(keyword.equals("India"))
{
Intent ima1=new Intent(screen3.this,new21.class);
startActivity(ima1);
}
else
{
Intent ima2=new Intent(screen3.this,new22.class);
startActivity(ima2);
}
Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG)
.show();
}
And lastly i need is I want the background image with proper listview showing one listitem after another. Thanks in advance
}
Try this code, Instead of extending ListActivity you can set layout that contains List View and separate layout for contents of List View.
Java Code :
public class Table_Layout extends Activity {
ListView list_view;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.table_layout);
list_view=(ListView)findViewById(R.id.list_view);
String[] names = new String[] { "India", "Malaysia" };
list_view.setAdapter(new ArrayAdapter<String>(this, R.layout.screen3,R.id.label, names));
}
}
Main XML that contains List View :table_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#drawable/uploading">
<ListView android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="#+id/list_view"
/>
Then Contents of List View :screen3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView android:text="#+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/label"
android:textColor="#FF0000"
android:textSize="30px"></TextView>
</LinearLayout>
Set you Background for the table_layout.xml so that it will applied for entire List View as you expected.
All looks well with your xml file. Try implementing the below mentioned:-
Make an image of the size you want your listview's each row of. And set it as the parent LinearLayout's background. Set:- android:layout_width="fill_parent" android:layout_height="wrap_content".
Hope this helps !! :)

Resources