Clearing previously entered edittext data automatically on checking a checkbox - android

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("");
}

Related

How to set checkbox to a custom text in string and save into database?

I am working on android. I have a checkbox in my app and I want to save a custom text against my checkbox when it is checked or not.
CheckBox atbSealedCheckBox;
String selectedAtbSealedValue ="";
atbSealedCheckBox = (CheckBox) view.findViewById(R.id.atbCheckBox);
Now, If I checked the checkbox then I want to store Yes otherwise No. Also, I want to store it in my DB. For this I have already created get and set methods in my Model
private String atbSealed;
public String getAtbSealed(){return atbSealed;}
public void setAtbSealed(String atbSealed){this.atbSealed=atbSealed;}
Update 1
As per suggestion, I have tried below
atbSealedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(isatbSealedChecked)
{
selectedAtbSealedValue = "Yes";
ctQuantitySpinner.setEnabled(false);
ctQuantitySpinner.setSelection(0);
isatbSealedChecked = true;
}
else
{
selectedAtbSealedValue = "No";
ctQuantitySpinner.setEnabled(true);
ctQuantitySpinner.setSelection(0);
isatbSealedChecked = false;
}
}
});
But It doesn't help me out. Also, the ctQuantitySpinner is not disabled on atbSealedCheckbox checked. Also if checked the boolean value of the checkbox is not changed to true. Although in log I do see the selectedAtbSealedValue but it's not set in the app. In my SaveDataLocal() function I have set the value like below
survey.setAtbSealed(this.selectedAtbSealedValue);
SurveyManager dbHelper = new SurveyManager(getActivity());
dbHelper.addSurvey(survey);
I have also tried the below code
atbSealedCheckBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(((CheckBox)v).isChecked())
{
ctQuantitySpinner.setEnabled(false);
ctQuantitySpinner.setSelection(0);
selectedAtbSealedValue = "Yes";
isatbSealedChecked = true;
//atbCheckBoxEdittext.setText(selectedAtbCheckBox);
}
else {
ctQuantitySpinner.setEnabled(true);
ctQuantitySpinner.setSelection(0);
selectedAtbSealedValue = "No";
isatbSealedChecked = false;
//atbCheckBoxEdittext.setText(selectedAtbCheckBox);
}
}
});
This code shows me the selected checkbox value, disabled the spinner and set the spinner value to null. but again the checkbox value is not saved.
How can I save custom text in a string?
Try this, it work for me:
atbSealedCheckBox.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged( CompoundButton buttonView, boolean isChecked ) {
if (isChecked) {
selectedAtbSealedValue = "yes";
} else {
selectedAtbSealedValue = "no";
}
}
} );
After that, you can store selectedAtbSealedValue into DB;
Update: Your fixed code:
atbSealedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b)
{
selectedAtbSealedValue = "Yes";
ctQuantitySpinner.setEnabled(false);
ctQuantitySpinner.setSelection(0);
isatbSealedChecked = true;
}
else
{
selectedAtbSealedValue = "No";
ctQuantitySpinner.setEnabled(true);
ctQuantitySpinner.setSelection(0);
isatbSealedChecked = false;
}
}
});
As long as I understand your question, you want to change this variable String selectedAtbSealedValue =""; when user checked your checkbox.
To achieve this statement, you can use onCheckedListener that is triggered as soon as users checked or removed the check from your checkbox.
atbSealedCheckBox.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged( CompoundButton buttonView, boolean isChecked ) {
// Todo something.
}
} );
This will help you.
At submit button get the current value of checkbox.
submit_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
control_of_vectors_value = control_of_vectors_checkbox.isChecked() == true ? "yes" : "no"; }}

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();
}
});

How can I detect focused EditText in android?

I have a number of EditText elements in my page along with two buttons. I want the user to touch on any one EditText field and click any button to insert a certain value into that very EditText field they touched. Giving input using the keypad is not allowed. Please help me to do this.
You can use View.OnFocusChangeListener to detect if any view (edittext) gained or lost focus.
for (EditText view : editList){
view.setOnFocusChangeListener(focusListener);
}
....
private OnFocusChangeListener focusListener = new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus){
focusedView = v;
} else {
focusedView = null;
}
}
}
This goes in your activity or fragment or wherever you have the EditTexts. The .... is just saying that you can put it anywhere else in the class. And obviously you would need to create an array with them in it, thus the editList.
This works for me. It returns a boolean.
myEditText.hasFocus()
One thing that you can do is declare a global variable evalue which will tell you which is the last selected EditText by using onTouchListener and then based on the value of evalue, you can set the text value to the edittext by button click. hope you understood.
the code for it can be as follow:
EditText e1,e2;
Button b1,b2;
String evalue;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
e1=(EditText)findViewById(R.id.editText1);
e2=(EditText)findViewById(R.id.editText2);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
e1.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View arg0, MotionEvent arg1)
{
evalue="1";
return false;
}
});
e2.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View arg0, MotionEvent arg1)
{
evalue="2";
return false;
}
});
b1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0) {
if(evalue=="1")
{
e1.setText("yes");
}
if(evalue=="2")
{
e2.setText("yes");
}
}
});
b2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0) {
if(evalue=="1")
{
e1.setText("No");
}
if(evalue=="2")
{
e2.setText("No");
}
}
});
}
Its a logical coding.. not upto the mark.. if you find a better one. then use it. thank you.
This worked for me.
e1 = (EditText) findViewById(R.id.editText1);
e1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View arg0, boolean hasfocus) {
if (hasfocus) {
Log.e("TAG", "e1 focused")
} else {
Log.e("TAG", "e1 not focused")
}
}
});
With Java 8 lambdas:
EditText editTextTo = findViewById(R.id.editTextTo);
editTextTo.setOnFocusChangeListener((view, b) -> {
if (view.isFocused()) {
// Do whatever you want when the EditText is focused
// Example:
editTextFrom.setText("Focused!");
}
});
Note that the view inside the lambda function is actually an instance of an EditText.
Have your Fragment implement OnTouchListener & OnFocusChangeListener:
public class AttributesFragment extends Fragment
implements OnTouchListener, OnFocusChangeListener {
Then implement using:
#Override
public boolean onTouch(View view, MotionEvent event) {
if (view instanceof EditText) {
view.setOnFocusChangeListener(this); // User touched edittext
}
return false;
}
#Override
public void onFocusChange(View v, boolean hasFocus) {
}
Don't forget to set your listener after creating your EditText
editText.setOnTouchListener(this);
This way if an EditText box is focused by default but user has not changed the field it will not fire the onFocusChange event.
Simple Coding:
if(EditText1.isFocused()){
//EditText1 is focused
}else if(EditText2.isFocused()){
//EditText2 is focused
}
This worked for me using Java 8 (lambdas):
EditText myEditText = findViewById(R.id.editText);
myEditText.setOnFocusChangeListener((view, hasFocus) -> {
if(hasFocus){
Log.e("TAG", "myEditext has focus")
}else{
Log.e("TAG", "myEditext has no focus")
}
});
In your activity or fragment implement OnFocusChangeListener.
edittextNeme= (EditText) findViewById(R.id.edittextNeme);
edittextNeme.setOnFocusChangeListener(this);
You will have to check which view changed focus by getting the view id with view.getId() and handle accordingly.
#Override
public void onFocusChange(View view, boolean hasfocus) {
switch(view.getId()){
case R.id.edittextNeme:
//Do something
break;
...etc
}
}
val currentFocusView = activity.currentFocus
if (currentFocusView is EditText){
currentFocusView.appendText("Has Focus!")
}

Check box android

I added two check box in the on create method
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
checkBox1.setOnCheckedChangeListener(this) ;
checkBox2.setOnCheckedChangeListener(this) ;
the main function of the check boxes that when ischeck() a picture will be added to the mainlayout and when uncheck the picture will be removed >> I used the code bellow, the first check box is working fine the second check box when I do check it shows the pics and then they I can remove them even with uncheck ... where is the wrong in my code ??
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(checkBox1.isChecked())
{
......
mapOverlays.add(custom);
}
else {
mapOverlays.remove(custom) ;
}
if (checkBox2.isChecked())
{
....
mapOverlays.add(custom2);
}
else
{
mapOverlays.remove(custom2) ;
}
}
}
You are handling second checkbox checking differently. May be the code should look like this?
if (checkBox2.isChecked())
{
...
mapOverlays.add(custom2);
}
else
{
mapOverlays.remove(custom2);
}
Upd: if your code looks like in the current edit, then issue is the declaring custom2 variable in the if block. You are deleting not added mapOverlay, but another one declared somewhere else.
Just replace
if (checkBox2.isChecked())
{
MapItemizedOverlay custom2 = ...
by
if (checkBox2.isChecked())
{
custom2 = ...
Upd2: there is yet another issue with your onCheckedChanged() method. First if-else runs not only on checkBox1 check/uncheck but also on checkBox2 check/uncheck. Same for the second if-else.
Try to rewrite method:
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.equals(checkBox1)) {
// first if-else
} else if (buttonView.equals(checkBox2)) {
// second if-else
}
}
or even better:
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.getId() == R.id.checkBox1) {
if (isChecked) {
...
mapOverlays.add(custom);
} else {
mapOverlays.remove(custom);
}
} else if (buttonView.getId() == R.id.checkBox2) {
if (isChecked) {
...
mapOverlays.add(custom2);
} else {
mapOverlays.remove(custom2);
}
}
}
You need to add checkedchangelistener to checkbox 2 as well.
checkBox2.setOnCheckedChangeListener(this) ;

Radio button cannot be unchecked

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;
}
}
});

Categories

Resources