How to do something when a checkbox change state? - android

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.

Related

How to handle dynamically added radiobutton check and uncheck in android

I have crated 4 radiobutton in recyclerview but when i check one it checked but when i checked another one radiobutton then first one can not be unchecked
holder.cb_votes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
for (int i = 0; i < pollItems.size(); i++) {
if (isChecked) {
holder.cb_votes.setChecked(true);
} else {
holder.cb_votes.setChecked(false);
}
}
}
});
this is my xml
<RadioGroup
android:id="#+id/rg_cb_votes"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5">
<RadioButton
android:id="#+id/cb_votes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:text="Yes"
android:textSize="12dp" />
</RadioGroup>
Radiobutton in wikipedia says:
A radio button or option button is a type of graphical user interface element that allows the user to choose only one of a predefined set of options.
You would have to just use checkboxes instead of radio button for allowing multiple options to be selected,else you are doing something which is inherently wrong in a UI be it mobile/web.
:-)

Button visible and invisible

I am new in android app development.
I am searching an example like bellow:
There is a checkbox and two buttons named btn1 and btn2.
Logic should be when user will checked on the checkbox then btn2 will visible and btn1 will invisible.
In my XML file:
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_provider"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatCheckBox
android:id="#+id/input_chk_provider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:duplicateParentState="false"
android:text="#string/user_type_provider" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/btn_signup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/btn_sing_up"
android:background="#color/ic_launcher_background"
android:layout_marginTop="40dp"
android:textColor="#android:color/white"/>
<Button
android:id="#+id/btn_provider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/btn_provider_dtl"
android:background="#color/ic_launcher_background"
android:layout_marginTop="40dp"
android:textColor="#android:color/white"/>
Without having the code...
You'll need a OnCheckedChangeListener for the Checkbox so you can adjust the button visibility when it's checked/unchecked.
Something like:
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
btn1.setVisibility(View.GONE);
btn2.setVisibility(View.VISIBLE);
} else {
btn1.setVisibility(View.VISIBLE);
btn2.setVisibility(View.GONE);
}
});
In your XML use android:visibility="invisible" to set the visibility of the buttons.
In the code, get your views with findViewById()
CheckBox checkBox = (CheckBox) findViewById(R.id.check1);
Button yourButton = (Button) findViewById(R.id.btn1);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
if(isChecked){
yourButton.setVisibility(View.VISIBLE);
} else {
yourButton.setVisibility(View.INVISIBLE);
}
}
});
Use the method .setVisibility(View.VISIBLE) to change the visibility.
There are 3 states:
View.GONE
View.VISIBLE
View.INVISIBLE
Checkbox has OnCheckedChangeListener() method which will give you the state of the checkbox. Based on the state of the Checkbox you can use Button1.setVisibility(true/false); to show/hide the button.
try this..
<CheckBox
android:id="#+id/input_chk_provider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:state_checked="true"
android:checked="false"
android:onClick="itemClicked"
android:duplicateParentState="false" />
public void itemClicked(View v) {
//code to check if this checkbox is checked!
CheckBox checkBox = (CheckBox)v;
if(checkBox.isChecked()==true){
bt1.setVisibility(View.VISIBLE);
bt2.setVisibility(View.INVISIBLE);
}else if(checkBox.isChecked()==false){
bt2.setVisibility(View.VISIBLE);
bt1.setVisibility(View.INVISIBLE);
}
}

Android How to hide the phone number programmatically

How can I hide or show my phone number during a call without going into the settings?
you can implement this by using a check box :
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cb_Show" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text"
/>
_
CheckBox cbShow = (CheckBox) findViewById(R.id.cb_Show);
TextView text=findViewById(R.id.text);
text.setText("some text to hide");
cbShow.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
text.setVisibility(VIEW.INVISIBLE);
else
text.setVisibility(VIEW.VISIBLE);
}
});
or you can use a toggleButton instead example
good luck

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

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.

hide and show button when check box clicked

i want code en eclipse android when i clicked checkbox i want to show hidden button
i'm beginner in android so please help me :(
this is my xml code :
<CheckBox
android:id="#+id/CB1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="19dp"
android:fontFamily="Segoe UI"
android:text="I Want To Select A Favorite Station And Receive Notification."
android:textStyle="bold" />
CheckBox checkBox = (CheckBox)findViewById(R.id.CB1);
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
btn.setVisibility(View.GONE);//TO HIDE THE BUTTON
btn.setVisibility(View.VISIBLE);//TO SHOW THE BUTTON
}
});
where btn is your button.
Reference the checkbox in your Activity and set an OnCheckedChangeListener:
CheckBox checkBox = (CheckBox)findViewById(R.id.CB1);
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do whatever
}
});

Categories

Resources