Radio button cannot be unchecked - android

I have created a radio button in my app. Now when radio button isChecked I call a function, and it's working fine. But my problem is that once I checked the radio button then and again click of that radio button doesn't make it unchecked.
So, how that can be done?
My code:
super.onCreate(savedInstanceState);
setContentView(R.layout.setting_frm);
rb1=(RadioButton)findViewById(R.id.option1);
rb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton v, boolean arg1) {
// TODO Auto-generated method stub
if(rb1.isChecked() == true)
t1.setText("Selected is : "+rb1.getText());
if(rb1.isChecked() == false)
t1.setText("Selected is : ");
}
});
t1=(TextView)findViewById(R.id.TextView01);
}

You can't do that with a radio button. Go for CheckBox.
Try this

you can use boolean Flag option ..here is the code
boolean flag = false;
rb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(flag){
rb.setChecked(false);
flag = false;
}
else{
rb.setChecked(true);
flag = true;
}
}
});

Related

Get buttons to do something when they are checked

I got multiple buttons which are managed by this code:
ToggleButton.OnCheckedChangeListener listener = new ToggleButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton v, boolean isChecked) {
int index;
for(index = 0; index < viewIds.length; index++) {
if (v.getId() == viewIds[index]) {
getSharedPreferences("PREFS_NAME", MODE_PRIVATE)
.edit()
.putBoolean(stringIds[index], isChecked)
.apply();
break;
}
}
}
};
i've created method
scanButton();
like this:
private void scanButton() {
Button scan = (Button)findViewById(R.id.button_scan);
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//here what will be done when button is clicked
Log.d(TAG, "Button \"SCAN\" is pressed");
Toast.makeText(RandDActivity.this, "Scan pressed", Toast.LENGTH_LONG)
.show();
//WHAT HERE
}
});
}
now Question: what to put here to get only those buttons which are isChecked with "true" state to do something?
Firstly you need to see whether the user has checked the button or not for that you can Load your SharedPreferences that you already saved in your onCheckedChanged() method, save it to a Boolean variable and then put a condition Like this:
if(isChecked){
//do your stuff with the button
}
I am assuming isChecked is your boolean variable that you just set with the value that you got from SharedPreferences.

Android checkbox listen for click before change

I have a requirement where a checkbox is displayed for a specific setting. When the user taps on the checkbox, I want to display an alert dialog. The checkbox should then only change if the user taps on the confirm button (or similar).
My point is that the OnCheckedChanged listener only fires after the checkbox has changed state, whereas I want to listen for the click before it changes state.
you can use onTouchListener and intercept ACTION_DOWN event for showing alert.
on users choice change checked state of you checkbox programatically.
example:
checkbox.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
//show alert
return true; //this will prevent checkbox from changing state
}
return false;
}
});
then call checkbox.setChecked(true); or checkbox.setChecked(false); as user selects yes or no.`
Use this:
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked==true){
//Show your alert
}
}else if(isChecked==false){
//Show your alert
}
}
});
If would suggest to use an OnTouchListener on the checkbox. If your condition is fulfilled then you can call checkBox.performClick().
You can try it;
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// do anything
// perform logic
}
}
});
You can set onClickListener and set checkbox's checked state accordingly.
myCheckBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// createYourDialog
// onPositiveButtonClicked
myCheckBox.setChecked(true);
// onNegativeButtonClicked
myCheckBox.setChecked(false);
// yourDialog.show();
}
});

Android "Toggle off all buttons"

I am making an app that toggles off and on a switch. I have 4 switches in total and at the bottom I would like to have a button that will toggle them all off at the same time - like an override switch. I'm trying to follow the same format as when creating the 4 switches, but I can't wrap my head around how it would be. I already tried looking through stackOverFlow and I can't find anything on it, maybe I just don't know the key words.
Switch toggleapp1 = (Switch) findViewById(R.id.app1);
toggleapp1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
toggleapp1(true);
Toast.makeText(getApplicationContext(), "[Application1] Enabled!", Toast.LENGTH_LONG).show();
} else {
toggleapp1(false);
Toast.makeText(getApplicationContext(), "[Application1] Disabled!", Toast.LENGTH_LONG).show();
}
}
});
Is how one of the switches look. toggleapp1 is switched with 2,3,4.
public boolean toggleapp1(boolean status) {
if (status == true) {
return true;
}
else if (status == false) {
return false;
}
return status;
}
I have 4 switches in total and at the bottom I would like to have a button that will toggle them all off at the same time - like an override switch.
If i understood the problem you have something like:
toggleapp1 = (Switch) findViewById(R.id.app1);
toggleapp2 = (Switch) findViewById(R.id.app2);
toggleapp3 = (Switch) findViewById(R.id.app3);
toggleapp4 = (Switch) findViewById(R.id.app4);
And you want to disable all of them togheter.
You can create a method that do this:
private toggleOffSwitches(boolean state) {
toggleapp1.setChecked(state);
toggleapp2.setChecked(state);
toggleapp3.setChecked(state);
toggleapp4.setChecked(state);
}
And call it in the OnClickListener of the button:
Button yourButton = (Button) findViewById(R.id.yourButton);
yourButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
toggleOffSwitches(false);
}
});
Remember to declare your Switches as field class variables in order to use them in the toggleOffSwitches method!
UPDATE
For example:
public class MainActivity extends Activity {
private Switch toggleapp1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
....
toggleapp1 = (Switch) findViewById(R.id.app1);
....
}

Clearing previously entered edittext data automatically on checking a checkbox

I have 3 checkboxes and 2 edittexts. When the user checks one checkbox and enters data in one edittext calculations take place. If i check another checkbox the edittext values should automatically clear. However the data in the edittexts exist even after i check other checkboxes.I tried finish() which shuts the application. Any idea how can this be achieved without closing the app?
Sorry if this question sounds weird. Iam just learning android
Thank you.
code for my onCheckedChangedListener
public void onCheckedChanged(CompoundButton predictionChkView, boolean isPredictionChecked)
{
// TODO Auto-generated method stub
switch(predictionChkView.getId())
{
case R.id.chkLastMileage1:
isChkLastMileage1=true;
chkLastMileage5.setChecked(false);
chkLastMileage10.setChecked(false);
ETPredictKm.setText("");
ETPredictFuelQty.setText("");
break;
case R.id.chkLastMileage5:
isChkLastMileage5=true;
chkLastMileage1.setChecked(false);
chkLastMileage10.setChecked(false);
ETPredictKm.setText("");
ETPredictFuelQty.setText("");
break;
case R.id.chkLastMileage10:
isChkLastMileage10 =true;
chkLastMileage1.setChecked(false);
chkLastMileage5.setChecked(false);
ETPredictKm.setText("");
ETPredictFuelQty.setText("");
break;
}
}
Code for the onFocusChangedListener
public void onFocusChange(View predictionFocusView, boolean hasPredictionETFocus)
{
// TODO Auto-generated method stub
FuelStoredInfo predictInfo = new FuelStoredInfo(this);
predictInfo.open();
predictInfo.getAvgMileage(this);
predictInfo.close();
try
{
predictKm = Long.parseLong(ETPredictKm.getText().toString());
predictFuetlQty = Double.parseDouble(ETPredictFuelQty.getText().toString());
}
catch(NumberFormatException ne)
{
ne.printStackTrace();
}
if(isChkLastMileage1 ==true || isChkLastMileage5==true||isChkLastMileage10==true)
{
if(ETPredictKm.hasFocus())
{
ETPredictKm.setText("");
if(predictFuetlQty!=0)
{
predictKm =(long) (predictionMileage*predictFuetlQty);
//setPredictKm(predictKm);
ETPredictKm.setText(String.valueOf(predictKm));
}
}
else if(ETPredictFuelQty.hasFocus())
{
ETPredictFuelQty.setText("");
if(predictKm!=0)
{
predictFuetlQty =predictKm/predictionMileage;
//setPredictFuetlQty(predictFuetlQty);
ETPredictFuelQty.setText(new DecimalFormat("##.##").format(predictFuetlQty));
}
}
}
else
{
Toast.makeText(getApplicationContext(), "Please check a checkbox!", Toast.LENGTH_LONG).show();
}
}
public void onClick(View v)
{
// TODO Auto-generated method stub
ETPredictKm.setText("");
ETPredictFuelQty.setText("");
chkLastMileage1.setChecked(false);
chkLastMileage5.setChecked(false);
chkLastMileage10.setChecked(false);
}
Add to your function.
EditText editText = (EditText)findViewById(R.id.edittext);
editText.setText("");
If you store a reference to your editText you can just call
editText.setText("");
Do this inside your onCheckedChange listener when the checkbox is in your desired state.
Add a checkbox oncheckedchangelistener and then put edittest.setText(""); inside.
Do something like this:
checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
edittext.setText("");
}
});
Also, don't forget to declare variables for your editexts, checkboxes, etc like this:
//example
final CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
Let me know if it works :)
Alternative:
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( checkbox.isChecked() )
{
edittext.setText("");
}
}
});
When you check the other checkbox do this
edittext.setText("");
for other EditText
Edit -----
Add FocusChangeListener for all of your EditText and do this ----
ETPredictKm.setOnFocusChangeListener(new OnFocusChangeListener()
{
#Override
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus)
ETPredictKm.setText("");
}
});
CheckBox check1 = (CheckBox) findViewById(R.id.checkbox1);
Checkbox check2 = (Checkbox) findViewById(R.id.checkbox2);
if (check1.isChecked()) {
EditText myEditText = (EditText) findViewById(R.id.edit_text_1);
myEditText.setText("");
}
if (check2.isChecked()) {
EditText myEditText2 = (EditText) findViewById(R.id.edit_text_2);
myEditText1.setText("");
}

How to create single check box event for all check boxes in android?

I'm new to android it is possible in .net, But i want to build this logic in android is it possible to do that please help me, i know only creating check box event for each check box instead of that i want to create single event for all Check boxes please help me.
Vb.net
private sub Chkbox1_Checkchanged(byval sen as object,byval e as system.eventargs) handles Chkbox1.checked,chkbox2.checked,chkbox3.checked
blnvalue=true
end sub
android
chkbox1= (CheckBo1) findViewById(R.id.chkbox1);
rbtn1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
blnvalue=true;
}
});
implements onCheckChangedEvent for for all checkBox.
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.getId() == activity.getCheckRefresh()) {
activity.setRefreshRate("1");
activity.setEnable(isChecked);
return;
}
}
Assuming you are in an Activity, you can make your Activity implement View.OnClickListener, and then use this for each View within your Activity, even ones that aren't CheckBox.
public class MyActivity extends Activity implements View.OnClickListener {
//declare checkboxes
public void onCreate(Bundle something) {
//setup stuff and get views
rbtn1.setOnClickListner(this);
rbtn2.setOnClickListner(this);
rbtn3.setOnClickListner(this);
rbtn4.setOnClickListner(this);
}
public void onClick(View v) {
if(v == rbtn1) {
//do stuff for checkbox1
} else if(v == rbtn2) {
//do stuff for checkbox2
} else if(v == rbtn3) {
//do stuff for checkbox3
} else if(v == rbtn4) {
//do stuff for checkbox4
}
}
}
You can also use onChackChanged Event & OnClickEvent for checkbox.
chkbox1= (CheckBo1) findViewById(R.id.chkbox1);
chkbox2= (CheckBo1) findViewById(R.id.chkbox2);
chkbox1.setOnClickListener(this);
chkbox1.setOnCheckedChangeListener(this);
chkbox2.setOnClickListener(this);
chkbox2.setOnCheckedChangeListener(this);
public void onClick(View v)
{
if(v.getId() == R.id.chkbox2)
// your code
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.getId() == activity.getCheckRefresh()) {
activity.setRefreshRate("1");
activity.setEnable(isChecked);
return;
}
}
u can do this.. here click is an inner class.. and create an instance of it n pass it to every setOnClickListener(click)
class click implements OnClickListener
{
public void onClick(View v)
{
// your code
}
}

Categories

Resources