Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assigning the view in Xamarin application #46

Open
CodeAddiction opened this issue May 28, 2020 · 0 comments
Open

Assigning the view in Xamarin application #46

CodeAddiction opened this issue May 28, 2020 · 0 comments

Comments

@CodeAddiction
Copy link

I have the pin view created, assigned an Id, and is present in resource.cs

The problem is that I am trying to reference the view in my settings activity so I can use the onDataEntered method to get the values, but my reference variable is always null. Even though using breakpoints tells me that the view I have referenced is not null.

`using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Gms.Ads;
using Android.Preferences;
using Com.Goodiebag.Pinview;
using static Com.Goodiebag.Pinview.Pinview;

namespace JourneyAssistant
{
[Activity(Label = "Settings")]
public class SettingsActivity : Activity, IPinViewEventListener
{
//Variable declarations
AdView mAdView;
ISharedPreferences preferences;
ISharedPreferencesEditor editor;
Switch hideAddress;
Button saveBTN;
EditText streetName;
EditText area;
EditText town;
EditText county;
EditText postcode;
Pinview pinView;
string pinViewPin;
public string userPin;
bool hideAddressStatus;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.activity_settings);

        //Initialise Xamarin Essentials API
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);

        pinView = new Pinview(this);

        streetName = FindViewById<EditText>(Resource.Id.streetName);
        area = FindViewById<EditText>(Resource.Id.area);
        town = FindViewById<EditText>(Resource.Id.town);
        county = FindViewById<EditText>(Resource.Id.county);
        postcode = FindViewById<EditText>(Resource.Id.postcode);
        saveBTN = FindViewById<Button>(Resource.Id.saveBTN);
        mAdView = FindViewById<AdView>(Resource.Id.adView);
        hideAddress = FindViewById<Switch>(Resource.Id.hideAddress);
        pinView = FindViewById<Pinview>(Resource.Id.pinEntry);

        EditText[] editTexts = new EditText[] { streetName, area, town, county, postcode };

        MobileAds.Initialize(this, "ca-app-pub-5407142743467927~1824688848");
        AdRequest adRequest = new AdRequest.Builder().Build();
        mAdView.LoadAd(adRequest);

        preferences = PreferenceManager.GetDefaultSharedPreferences(this);
        hideAddressStatus = preferences.GetBoolean("hideAddress", false);
        userPin = preferences.GetString("userPin", "Pin not set");
        
        if (hideAddressStatus == true)
        {
            pinView.SetPinViewEventListener(this);
            hideAddress.Checked = true;
            streetName.Text = preferences.GetString("streetName", "");
            area.Text = preferences.GetString("area", "");
            town.Text = preferences.GetString("town", "");
            county.Text = preferences.GetString("county", "");
            postcode.Text = preferences.GetString("postcode", "");
            streetName.Visibility = ViewStates.Invisible;
            area.Visibility = ViewStates.Invisible;
            town.Visibility = ViewStates.Invisible;
            county.Visibility = ViewStates.Invisible;
            postcode.Visibility = ViewStates.Invisible;
        }
        else
        {
            pinView.SetPinViewEventListener(this);
            hideAddress.Checked = false;
            streetName.Text = preferences.GetString("streetName", "");
            area.Text = preferences.GetString("area", "");
            town.Text = preferences.GetString("town", "");
            county.Text = preferences.GetString("county", "");
            postcode.Text = preferences.GetString("postcode", "");
            streetName.Visibility = ViewStates.Visible;
            area.Visibility = ViewStates.Visible;
            town.Visibility = ViewStates.Visible;
            county.Visibility = ViewStates.Visible;
            postcode.Visibility = ViewStates.Visible;

        };

        hideAddress.CheckedChange += (s, e) =>
        {
            if (e.IsChecked)
            {
                if (pinViewPin == "")
                {
                    Toast.MakeText(this, "Please set a PIN",ToastLength.Short).Show();
                }
                hideAddressStatus = true;
                streetName.Visibility = ViewStates.Invisible;
                area.Visibility = ViewStates.Invisible;
                town.Visibility = ViewStates.Invisible;
                county.Visibility = ViewStates.Invisible;
                postcode.Visibility = ViewStates.Invisible;
            }
            else
            {
                hideAddressStatus = false;
                streetName.Visibility = ViewStates.Visible;
                area.Visibility = ViewStates.Visible;
                town.Visibility = ViewStates.Visible;
                county.Visibility = ViewStates.Visible;
                postcode.Visibility = ViewStates.Visible;
            }
        };

        saveBTN.Click += delegate
        {
            foreach (EditText e in editTexts)
            {
                if (e.Text == "")
                {
                    Toast.MakeText(this, "All address fields are required", ToastLength.Short).Show();
                    break;
                }
                    
                
            }
            saveSettings();

        };

    }

    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
    {
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    public void saveSettings()
    {
        editor = preferences.Edit();
        editor.PutString("streetName", streetName.Text);
        editor.PutString("area", area.Text);
        editor.PutString("town", town.Text);
        editor.PutString("county", county.Text);
        editor.PutString("postcode", postcode.Text);
        editor.PutBoolean("hideAddress", hideAddressStatus);
        editor.Commit();
        Toast.MakeText(this, "Settings saved", ToastLength.Short).Show();
    }

    public void OnDataEntered(Pinview pinView, bool fromUser)
    {
        Toast.MakeText(this, "" + pinView.Value, ToastLength.Short).Show();
    }
}

}`

Does anybody know whats going on please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant