Why is RadioGroup.check() and RadioButton.setChecked() failing? - android

I have a RadioGroup with 2 RadioButtons. When clicked, some condition is validated, and, based on the outcome, the click should happen, otherwise revert.
However, it seems I can't set the check programmatically.
Here is my code:
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(this, "onCheckedChanged", Toast.LENGTH_SHORT).show();
switch (buttonView.getId()){
case R.id.radio_adresse:
if(radioAdresse.isChecked()){
getEditTextPlz().setEnabled(true);
getEditTextOrt().setEnabled(true);
getEditTextStrasse().setEnabled(true);
} else {
getEditTextPlz().setEnabled(false);
getEditTextOrt().setEnabled(false);
getEditTextStrasse().setEnabled(false);
}
break;
case R.id.radio_aktueller_standort:
if(isLocationAvailable()){
Toast.makeText(this, "Location available", Toast.LENGTH_SHORT).show();
if(radioAktuellerStandort.isChecked()){
getEditTextPlz().setEnabled(false);
getEditTextOrt().setEnabled(false);
getEditTextStrasse().setEnabled(false);
} else {
getEditTextPlz().setEnabled(true);
getEditTextOrt().setEnabled(true);
getEditTextStrasse().setEnabled(true);
}
} else {
if(isChecked){
} else {
}
Toast.makeText(this, "Location NOT available", Toast.LENGTH_SHORT).show();
searchModeRadioGroup.clearCheck();
searchModeRadioGroup.check(R.id.radio_adresse);
radioAdresse.setSelected(true);
radioAktuellerStandort.setSelected(false);
}
break;
}
}
Here is the XML:
<RadioGroup android:layout_width="fill_parent"
android:id="#+id/search_mode_radio_group"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_column="0"
android:layout_span="2">
<RadioButton android:id="#+id/radio_adresse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/adresse"
android:checked="true"/>
<RadioButton android:id="#+id/radio_aktueller_standort"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/aktueller_standort"
android:checked="false"
android:layout_gravity="right" />
</RadioGroup>
As you can see at the end of my event handler, I tried several methods, but none works...
Any suggestions?

I used the onClick event instead of onCheckedChanged event, and it worked.

Related

Assign a unique integer value such as(10, 20, 30, …) to each Radio Button in a Radio Group and return that value for Printing in Android

Trying to assign each Radio Buttons in a Radio Group, with a unique Integer value or id such as 10,20,30...so-on, and return the particular value/id for further actions such as printing that id or sending it to the backend.
For eg. : for RadioButton radio_visa it should return a value of 20, for radio_mastercard, it should return 30, and so-on.
No idea how to approach.
Thanks for help in advance.
My XML file:
<RadioGroup
android:id="#+id/radioGroupCard"
android:layout_width="151dp"
android:layout_height="130dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.165"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.384">
<RadioButton
android:id="#+id/radio_visa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClickedCards"
android:text="VISA" />
<RadioButton
android:id="#+id/radio_mastercard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClickedCards"
android:text="Master Card" />
<RadioButton
android:id="#+id/radio_rupay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClickedCards"
android:text="Rupay" />
<RadioButton
android:id="#+id/radio_amex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClickedCards"
android:text="AMEX" />
</RadioGroup>
My Activity file:
public void onRadioButtonClickedCards(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.radio_visa:
if (checked)
// actions
{
IVCardType.setImageResource(R.drawable.visa);
}
break;
case R.id.radio_mastercard:
if (checked)
// actions
{
IVCardType.setImageResource(R.drawable.mastercard);
}
break;
case R.id.radio_rupay:
if (checked)
// actions
{
IVCardType.setImageResource(R.drawable.amex);
}
break;
case R.id.radio_amex:
if (checked)
// actions
{
IVCardType.setImageResource(R.drawable.rupay);
}
break;
}
}
You can use the tag property of a RadioButton for that.
android:tag="30"
Actually, any widget have this property. Then just get it by
((RadioButton) view).getTag()

How to use if else ladder in android while dealing with radio buttons

The following is the code that I am currently using in the OnClick method for using a radio button.
switch(view.getId()) {
case R.id.op1_radio_button:
if (checked) {
score=0;
break;
}
I am looking for an if-else version of the code so that it would be easier for me to use radio buttons.
Try this
int id = view.getId();
if(id == R.id.op1_radio_button){
//do something
}else if(id == R.id.button2){
//do something else
}
you should use RadioGroup to manage RadioButton selection like this in your XML,
<RadioGroup
android:checkedButton="#id/rbtn_male"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rbtn_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Male" />
<RadioButton
android:id="#+id/rbtn_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Female" />
</RadioGroup>
now, to manage the click on RadioButton in Your Activity add this,
RadioButton rbMale = findViewById(R.id.rbtn_male);
RadioButton rbFemale = findViewById(R.id.rbtn_female);
rbMale.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
score = 1;
}
});
rbFemale.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
score = 0;
}
});
I hope this is the best answer for your case.

Android how to handle multiple switches

So I'm rather new to the android development and quickly ran into a problem, I have a view where I want to enable / disable both the bluetooth and wifi of the device, though both controlled over a seperate switch.
So here is what my view is like:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_connect"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="nl.neverdull.payleven.safeport.Connect">
<Switch
android:text="Bluetooth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="#+id/bluetooth_switch"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
tools:text="bluetooth"
android:contentDescription="Status of bluetooth"
android:textOn="#string/bluetooth_on"
android:textOff="#string/bluetooth_off" />
<Switch
android:text="Wifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="25dp"
android:id="#+id/wifi_switch"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
tools:text="wifi" />
</RelativeLayout>
Now I already have it working for the bluetooth, which I do as follow
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connect);
blueToothStatus();
wifiStatus();
}
public void blueToothStatus() {
Switch s = (Switch) findViewById(R.id.bluetooth_switch);
if (s != null) {
s.setOnCheckedChangeListener(this);
}
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Toast.makeText(this, "Bluetooth " + (isChecked ? "enabled" : "disabled"),
Toast.LENGTH_SHORT).show();
if(isChecked) {
mBluetoothAdapter.enable();
} else {
mBluetoothAdapter.disable();
}
}
Now when I try to do the same for the wifi it will ofcourse end up using the same onCheckedChanged function, so what my ideal solution would be it how to have the wifi use it's own onCheckedChanged function?
As of now I have basically the exact same code for the WIFI and Bluetooth, but it won't work since both end up going to the same function, which makes my wifi switch change the bluetooth status.
public void wifiStatus() {
Switch s = (Switch) findViewById(R.id.wifi_switch);
s.setOnCheckedChangeListener(this);//This goes to my onCheckedChanged
}
public void wifiStatus() {
Switch s = (Switch) findViewById(R.id.wifi_switch);
s.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do what you need to do
}
});
}

RadioButtons in RadioGroup arent working as intended

I have a RadioGroup with two RadioButton's:
<RadioGroup
android:id="#+id/main_radiogroup_lock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/main_layout_lock_dummy"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/main_radiobutton_lock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="110dp"
android:background="#drawable/custom_radiobutton_lock"
android:button="#null"
android:contentDescription="#string/imageview_description" />
<RadioButton
android:id="#+id/main_radiobutton_unlock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="230dp"
android:background="#drawable/custom_radiobutton_unlock"
android:button="#null"
android:checked="true"
android:contentDescription="#string/imageview_description" />
</RadioGroup>
I initialize the RadioButton's and set an OnCheckedChangeListener:
...
this.radioButtonLock = (RadioButton) view.findViewById(R.id.main_radiobutton_lock);
this.radioButtonLock.setOnCheckedChangeListener(this);
this.radioButtonUnlock = (RadioButton) view.findViewById(R.id.main_radiobutton_unlock);
this.radioButtonUnlock.setOnCheckedChangeListener(this);
...
#Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
if(view.getId() == R.id.main_radiobutton_lock) {
Log.v("MainFragment", "Lock");
this.imageButtonLanguage.setEnabled(false);
this.buttonOpen.setEnabled(false);
this.buttonAutomatic.setEnabled(false);
this.buttonClose.setEnabled(false);
this.buttonFog.setEnabled(false);
this.checkBoxRed.setEnabled(false);
this.checkBoxGreen.setEnabled(false);
this.checkBoxBlue.setEnabled(false);
return;
} else if(view.getId() == R.id.main_radiobutton_unlock) {
Log.v("MainFragment", "Unlock");
this.imageButtonLanguage.setEnabled(true);
this.buttonOpen.setEnabled(true);
this.buttonAutomatic.setEnabled(true);
this.buttonClose.setEnabled(true);
this.buttonFog.setEnabled(true);
this.checkBoxRed.setEnabled(true);
this.checkBoxGreen.setEnabled(true);
this.checkBoxBlue.setEnabled(true);
return;
}
}
So when i now start the app my radioButtonUnlock is checked. When i know chec the radioButtonLock nothing happens and no log output but the checked image is set so the checking is working. When i after that again check the radioButtonUnlock the following output appears:
02-04 09:59:31.355: V/MainFragment(533): Lock
02-04 09:59:31.355: V/MainFragment(533): Unlock
So both RadioButton's are fired. After that everytime i press one of the RadioButton's all events are fired. After pressing the radioButtonLock the following happens:
02-04 09:59:32.850: V/MainFragment(533): Unlock
02-04 09:59:32.850: V/MainFragment(533): Lock
Whats the point of that? I set different ID's so why are always both RadioButtons's fired?
Try this hope will work -
1)set check change listener to radio group
RedioGroup rg =(RadioGroup)findviewByid(R.id.main_radiogroup_lock);
rg.setcheckchangedlistener(true);
2)handle on check change listener event
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch(checkedId){
case R.id.rdb1:
radio_selected = R.id.rdb1;
Toast.makeText(getApplicationContext(),"first",Toast.LENGTH_LONG).show();
case R.id.rdb2:
radio_selected = R.id.rdb2;
Toast.makeText(getApplicationContext(),"second",Toast.LENGTH_LONG).show();
}
}

How to do something when a checkbox change state?

This is my code:
<CheckBox
android:id="#+id/sprint_checkbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/sprint_game" />
<CheckBox
android:id="#+id/marathon_checkbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/marathon" />
<CheckBox
android:id="#+id/never_ending_checkbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/never_ending" />
What i want to do is "detect" when one of these is checked and then set the other two to "disable", so the user can select only one at time.
I tried to use the ".setOnCheckedChangeListener", but i can't do that, can someone help me with some code?
Thanks a lot guys!
This is the way you are notified about checked changes:
CheckBox check = findViewById(R.id.sprint_checkbox);
check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//do stuff
}
});
You can also let your activity implements the OnCheckedChangeListener interface and then:
CheckBox check1 = findViewById(R.id.sprint_checkbox);
CheckBox check2 = findViewById(R.id.marathon_checkbox);
CheckBox check3 = findViewById(R.id.never_ending_checkbox);
check1.setOnCheckedChangeListener(this);
check2.setOnCheckedChangeListener(this);
check3.setOnCheckedChangeListener(this);
Overriding the interface method:
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch(buttonView.getId()){
case R.id.sprint_checkbox:
//do stuff
break;
case R.id.marathon_checkbox:
//do stuff
break;
case R.id.never_ending_checkbox:
//do stuff
break;
}
}
I believe a RadioButton would be more suitable for your aims.
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/radio_group1">
<RadioButton
android:id="#+id/sprint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rad_option1"
android:checked="true"
android:onClick="onRadioButtonClick"
/>
<RadioButton
android:id="#+id/marathon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rad_option2"
android:onClick="onRadioButtonClick"
/>
<RadioButton
android:id="#+id/neverending"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rad_option3"
android:onClick="onRadioButtonClick"
/>
</RadioGroup>
Then in your code:
public void onRadioButtonClick(View v) {
RadioButton button = (RadioButton) v;
// DO YOUR THING HERE DEPENDING ON WHICH RADIOBUTTON IS PICKED
}
In this case, you won't have to deal with disabling other options. The user will have only one option to pick by default.
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int id = buttonView.getId();
if(isChecked){
switch(id){
case R.id.sprint:
//add to database
...
}
}
else{
switch(id){
case R.id.sprint:
//remove from database
...
}
}
}
This should allow you to with ID's. Hope it works.

Categories

Resources