Reputation: 13
I have set up an activity with some buttons. One button is used to start a bluetooth connection. So the activity is started and at some point the user clicks on "Start Bluetooth", then the bluetooth connection is established and after that is done I want to enable another button for further options.
My question: How can I change a button from Enabled = False to Enabled = True outside the onCreate() function of the activity? My Code: in the method onActivityResult() I want to enable the button buttonConnect. My solution does not seem to work.
I could only find onClickListeners, but that's not what I need...
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Android.Bluetooth;
using Android.Util;
using Android.Content;
using System.Reflection;
namespace firstTry3
{
[Activity(Label = "@string/app_name")]
public class bluetoothConnectionActivity : AppCompatActivity
{
BluetoothAdapter mBluetoothAdapter;
const int REQUEST_ENABLE_BT = 1; //necessary to start bluetooth, must be greater than 0
string tag = "blueApp";
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_bluetoothConnection);
Button buttonBluetoothOn = FindViewById<Button>(Resource.Id.bluetoothOn);
Button buttonConnect = FindViewById<Button>(Resource.Id.connectButton);
Button buttonDissconnect = FindViewById<Button>(Resource.Id.disconnectButton);
mBluetoothAdapter = BluetoothAdapter.DefaultAdapter;
buttonBluetoothOn.Click += (sender, e) =>
{
if (!mBluetoothAdapter.IsEnabled)
{
Log.Info(tag, MethodBase.GetCurrentMethod().Name + ": Device does not have bluetooth capabilities");
}
enableBluetooth();
};
}
public void enableBluetooth() {
if(mBluetoothAdapter == null)
{
Log.Warn(tag, MethodBase.GetCurrentMethod().Name + ": Device does not have bluetooth capabilities");
}
if (!mBluetoothAdapter.IsEnabled)
{
Intent enableBTIntent = new Intent(BluetoothAdapter.ActionRequestEnable);
StartActivityForResult(enableBTIntent, REQUEST_ENABLE_BT);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == constants.RESULT_OK)
{
Log.Info(tag, MethodBase.GetCurrentMethod().Name + ": Bluetooth has been activated");
Button buttonConnect = FindViewById<Button>(Resource.Id.connectButton);
buttonConnect.Enabled = true;
}
if (resultCode == constants.RESULT_CANCELLED){
Log.Info(tag, MethodBase.GetCurrentMethod().Name + ": Bluetooth has NOT been activated");
}
else
{
Log.Error(tag, MethodBase.GetCurrentMethod().Name + ": invalid resultCode");
}
}
}
}
Upvotes: 0
Views: 331
Reputation: 17432
Declare your button
at activity
level & initialize it in OnCreate
than you can use it anywhere in the current activity
. Like this
public class bluetoothConnectionActivity : AppCompatActivity
{
Button buttonConnect;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.activity_bluetoothConnection);
buttonConnect = FindViewById<Button>(Resource.Id.connectButton);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == constants.RESULT_OK)
{
Log.Info(tag, MethodBase.GetCurrentMethod().Name + ": Bluetooth has been activated");
buttonConnect.Enabled = true;
}
}
}
Upvotes: 1