Radio Button Android Part 1 :- Check and Unchecked

Description From https://developer.android.com/guide/topics/ui/controls/radiobutton.html

Radio Buttons


Radio buttons allow the user to select one option from a set. You should use radio buttons for optional sets that are mutually exclusive if you think that the user needs to see all available options side-by-side. If it's not necessary to show all options side-by-side, use a spinner instead.

To create each radio button option, create a RadioButton in your layout. However, because radio buttons are mutually exclusive, you must group them together inside a RadioGroup. By grouping them together, the system ensures that only one radio button can be selected at a time.

1. Make a   XML Layout


<?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"    tools:context="com.avadhesh.radiobutton.MainActivity">


    <RadioButton        android:id="@+id/radioButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"        android:text="RadioButton" />
</RelativeLayout>
2. Coding
Within the Activity that hosts this layout, the following method handles the click event for  radio buttons: by which you can Check and Unchecked the radio button.
@Overridepublic void onClick(View v) {

    switch (v.getId())
    {
        case R.id.radioButton:
            if(radioButton.isSelected())
            {
                radioButton.setSelected(false);
                radioButton.setChecked(false);
            }else {
                radioButton.setSelected(true);
                radioButton.setChecked(true);
            }

            break;
    }

}

3. Output

 When radio button is not check.

When radio button is check.





Android Bluetooth Example Part 2 Android Developer Bluetooth Bluetooth Android Studio

public class Connect_Bluetooth extends Activity {

  private ProgressDialog mProgressDlg;
  private static final int REQUEST_ENABLE_BT = 1;
  private TextView onBtn;
  private TextView offBtn;
  private TextView listBtn;
  private TextView findBtn;
  private TextView text;
  private BluetoothAdapter myBluetoothAdapter;
  private Set<BluetoothDevice> pairedDevices;
  private ListView myListView;
/*    private ArrayAdapter<String> BTArrayAdapter;*/

  private DeviceListAdapter mAdapter;
  private ArrayList<BluetoothDevice> mDeviceList ;
 
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main_band);
      text = (TextView) findViewById(R.id.text);
      onBtn = (TextView)findViewById(R.id.turnOn);
      offBtn = (TextView)findViewById(R.id.turnOff);
      listBtn = (TextView)findViewById(R.id.paired);
      findBtn = (TextView)findViewById(R.id.search);
      myListView = (ListView)findViewById(R.id.listView1);
      mDeviceList = new ArrayList<BluetoothDevice>();
      mAdapter      = new DeviceListAdapter(this);
      mProgressDlg      = new ProgressDialog(this);
      mProgressDlg.setMessage("Scanning...");
      myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
      mProgressDlg.setCancelable(false);
      mProgressDlg.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
              dialog.dismiss();

              myBluetoothAdapter.cancelDiscovery();
          }
      });

      if (myBluetoothAdapter == null) {
          showUnsupported();
      } else {
          listBtn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Set<BluetoothDevice> pairedDevices = myBluetoothAdapter.getBondedDevices();

                  if (pairedDevices == null || pairedDevices.size() == 0) {
                      showToast("No Paired Devices Found");
                  } else {


                      if (pairedDevices == null || pairedDevices.size() == 0) {
                          showToast("No Paired Devices Found");
                      } else {
                          ArrayList<BluetoothDevice> list = new ArrayList<BluetoothDevice>();

                          list.addAll(pairedDevices);
                          mAdapter.setData(mDeviceList);
                          myListView.setAdapter(mAdapter);

                      }

                  }
              }
          });

          findBtn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View arg0) {
                  if (myBluetoothAdapter.isDiscovering()) {
                      // the button is pressed when it discovers, so cancel the discovery
                      myBluetoothAdapter.cancelDiscovery();
                  }
                  else {
                     // BTArrayAdapter.clear();
                      myBluetoothAdapter.startDiscovery();

                      registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
                  }
              }
          });

          onBtn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {

                  Intent turnOnIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                  startActivityForResult(turnOnIntent, REQUEST_ENABLE_BT);



              }
          });

          offBtn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {

                      myBluetoothAdapter.disable();

                      showDisabled();

              }
          });


          if (myBluetoothAdapter.isEnabled()) {
              showEnabled();
          } else {
              showDisabled();
          }
      }

      mAdapter.setListener(new DeviceListAdapter.OnPairButtonClickListener() {
          @Override
          public void onPairButtonClick(int position) {
              BluetoothDevice device = mDeviceList.get(position);

              if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
                  unpairDevice(device);
              } else {
                  showToast("Pairing...");

                  pairDevice(device);
              }
          }
      });

      IntentFilter filter = new IntentFilter();

      filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
      filter.addAction(BluetoothDevice.ACTION_FOUND);
      filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
      filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

      registerReceiver(mReceiver, filter);

  }
  private void pairDevice(BluetoothDevice device) {
      try {
          Method method = device.getClass().getMethod("createBond", (Class[]) null);
          method.invoke(device, (Object[]) null);
      } catch (Exception e) {
          e.printStackTrace();
      }
  }

  private void unpairDevice(BluetoothDevice device) {
      try {
          Method method = device.getClass().getMethod("removeBond", (Class[]) null);
          method.invoke(device, (Object[]) null);

      } catch (Exception e) {
          e.printStackTrace();
      }
  }


  @Override
  public void onPause() {
      if (myBluetoothAdapter != null) {
          if (myBluetoothAdapter.isDiscovering()) {
              myBluetoothAdapter.cancelDiscovery();
          }
      }

      super.onPause();
  }

  @Override
  public void onDestroy() {
      unregisterReceiver(mReceiver);

      super.onDestroy();
  }

  private void showEnabled() {
      text.setText("Bluetooth is On");
      text.setTextColor(Color.BLUE);
      offBtn.setEnabled(true);
      onBtn.setEnabled(false);
 
  }

  private void showDisabled() {
      text.setText("Bluetooth is Off");
      text.setTextColor(Color.RED);
      offBtn.setEnabled(false);
      onBtn.setEnabled(true);
  }

  private void showUnsupported() {
      text.setText("Bluetooth is unsupported by this device");
  }


  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      // TODO Auto-generated method stub
      if(requestCode == REQUEST_ENABLE_BT){
          if(myBluetoothAdapter.isEnabled()) {
              showEnabled();
          } else {
              text.setText("Status: Disabled");
          }
      }
  }
  private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
      public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();

          if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
              final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);

              if (state == BluetoothAdapter.STATE_ON) {
                  showToast("Enabled");

                  showEnabled();
              }
          } else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
              mDeviceList = new ArrayList<BluetoothDevice>();

              mProgressDlg.show();
          } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
              mProgressDlg.dismiss();


          } else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
              BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

              mDeviceList.add(device);

              showToast("Found device " + device.getName());
              mAdapter.setData(mDeviceList);
              myListView.setAdapter(mAdapter);
         
          }
      }
  };
  private void showToast(String message) {
      Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
  }

}