Issues passing Edit text into an If statement - android

I have a quiz app that should contains the following:
1- radio groups/buttons
2- checkbox
3- editText
all should calculate a score. I manage to get the radio groups, checkbox to work and calculate the score. however having problems with the editText
I am trying to compare the value of what's in name2 variable but I am unable to do so. I've implemented the .equals syntax but it's not working accordingly.
Any suggestions/solutions are grateful.
public class Quiz extends AppCompatActivity {
int score = 0;
String name;
String name2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
}
public void onRadioButtonCliked(View view) {
Boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.yes_radio_buttonG1B3:
if (checked)
score++;
break;
case R.id.yes_radio_buttonG2B2:
if (checked)
score++;
break;
case R.id.yes_radio_buttonG3B2:
score++;
if (checked)
break;
case R.id.yes_radio_buttonG4B1:
if (checked)
score++;
break;
case R.id.yes_radio_buttonG5B2:
if (checked)
score++;
break;
case R.id.yes_radio_buttonG6B3:
if (checked)
score++;
break;
}
}
public void onCheckBoxCliked(View view) {
Boolean checked = ((CheckBox) view).isChecked();
CheckBox cB1 = (CheckBox) findViewById(R.id.checkBox1);
CheckBox cB2 = (CheckBox) findViewById((R.id.checkBox2));
CheckBox cB3 = (CheckBox) findViewById(R.id.checkBox3);
if (cB1.isChecked() && cB2.isChecked() && !cB3.isChecked()) {
score++;
}
}
public void EditText(View view) {
EditText text = (EditText) findViewById(R.id.editText1);
name2 = text.getText().toString();
if (name2.equals("8")) {
score++;
}
}
public void submitOrder(View view) {
EditText username = (EditText) findViewById(R.id.editText);
name = username.getText().toString();
Toast.makeText(this, "Thank you " + name, Toast.LENGTH_SHORT).show();
Toast.makeText(this, "You have got 8/" + score, Toast.LENGTH_SHORT).show();
}
}

Since it's a string variable, you must use "" to indicate it's a string.
Whole number are only applicable for int variable type.
if(name2.equals("8")){
score++;
}

Try replacing the 8 with "8" as you are supposed to be comparing a String, not an integer.

As already mentioned in a comment and in the two other answers, you can't compare a String to a number.
Instead of checking a String, convert that string to an integer(assuming it is a valid int) and compare the ints instead.
First, convert:
int value;
try{
value = Integer.parseInt(name2);//throws an exception if it isn't a valid integer or if name == null
}catch(Exception e){
return;//not valid, don't continue
}
Then compare:
if(value == 8){
score++;
}

Related

How to validate which checkbox is checked?

I am new to android ,Here I have 7 check boxes in my application and I have customized it as in the image .Each checkbox is represents a day of the week .
What I want to do is ,If a user clicked on a checkbox the text of the clicked checkbox should appear on the above TextView (by default if there is any checkbox is not clicked Textview text should be as "Never").
Here the Textview text should be in a ordered way ,I mean If I select all the check boxes randomly but the TextView text should be in a ordered way like Sun,Mon,.....Sat.
I know how to validate a checkbox is checked or not ,but When it comes to the above situation I don't know how it should be .
Can anyone help me to get this .
private TreeMap<Integer, String> mAlarmDays = new TreeMap<>();
public void onClick(View v) {
switch (v.getId()) {
case R.id.mSun:
if (mRepeat.getText().toString().contains("Sun")) {
mAlarmDays.remove(0);
Toast.makeText(getApplicationContext(),"Sun is clicked",Toast.LENGTH_LONG).show();
} else
mAlarmDays.put(0, "Sun");
break;
case R.id.mMon:
if (mRepeat.getText().toString().contains("Mon")) {
mAlarmDays.remove(1);
} else
mAlarmDays.put(1, "Mon");
break;
case R.id.mTue:
if (mRepeat.getText().toString().contains("Tue")) {
mAlarmDays.remove(2);
} else
mAlarmDays.put(2, "Tue");
break;
case R.id.mWed:
if (mRepeat.getText().toString().contains("Wed")) {
mAlarmDays.remove(3);
} else
mAlarmDays.put(3, "Wed");
break;
case R.id.mThu:
if (mRepeat.getText().toString().contains("Thu")) {
mAlarmDays.remove(4);
} else
mAlarmDays.put(4, "Thu");
break;
case R.id.mFri:
if (mRepeat.getText().toString().contains("Fri")) {
mAlarmDays.remove(5);
} else
mAlarmDays.put(5, "Fri");
break;
case R.id.mSat:
if (mRepeat.getText().toString().contains("Sat")) {
mAlarmDays.remove(6);
} else
mAlarmDays.put(6, "Sat");
break;
}
StringBuilder repeatDays = new StringBuilder();
if (mAlarmDays.size() == 0) {
repeatDays = new StringBuilder("Never");
} else {
for (String day:mAlarmDays.values()) {
repeatDays.append(day).append(" ");
}
}
mRepeat.setText(repeatDays.toString());
}
I suggest to create one checkbox listener for all checkboxes which will iterate over them and build string for textview
Something like this:
CheckBox sunday = ...;
CheckBox monday = ...;
//......
CheckBox saturday = ...;
TextView label = ...;
CompoundButton.OnCheckedChangeListener changeListener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
List<String> days = new ArrayList<>();
if (sunday.isChecked()) {
days.add("Sun");
}
if (monday.isChecked()) {
days.add("Mon");
}
//....
if (saturday.isChecked()) {
days.add("Sat");
}
if (days.size() != 0) {
label.setText(TextUtils.join(", ", days));
} else {
label.setText("Never");
}
}
};
sunday.setOnCheckedChangeListener(changeListener);
monday.setOnCheckedChangeListener(changeListener);
//......
saturday.setOnCheckedChangeListener(changeListener);
It really depends how you have the text set... but you can do something like.
if (checkBox.isChecked()) {
System.out.println("checkBox clicked");
}
try this for every checkbox.
CheckBox checkBox = (CheckBox)v;
if(checkBox.isChecked()){
Log.i("Checkbox", "Checkbox selected.");
}else{
Log.i("Checkbox", "Checkbox Not selected.");
}

Assigning points to each radio button selected

I am currently developing a questionnaire based application where by there are options. I need to assign numerical values to each radio button so when one of it is selected it will give a certain amount of points which will then be used to produce a cumulative amount. I need help in coding the java to do this. any help will be greatly appreciated. Thank you!
int score = 0;
public TextView tv;
public void onRadioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getID()) {
case R.id.radioButton1:
if(checked)
score +=1;
break;
case R.id.radioButton2:
if(checked)
score +=1;
break;
case R.id.radioButton3:
if(checked)
score +=3;
break;
}
}
public void updateScore(int score) {
tv = (TextView)findViewById(R.id.textview1);
tv.setText(" " + score);
}
in the XML I have coded a textview with "0" and a text id of #+id/textview1
the issue is that the score remains at 0 even if any radio buttons are selected
Make the following changes to your code
public TextView tv;
int score = 0;
public void onRadioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.radioButton1:
if (checked) {
score += 1;
}
break;
case R.id.radioButton2:
if(checked)
score +=1;
break;
case R.id.radioButton3:
if(checked)
score +=3;
break;
}
updateScore(score); // <- Added this line here
}
public void updateScore(int score) {
// Don't initialise your view here. Take it to onCreate()
tv.setText(" " + score);
}
EDIT
Is your XML for the radio button similar to what I have here?
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="asas"
android:onClick="onRadioButtonClicked"/>
Make sure you have added the android:onClick="onRadioButtonClicked" property to each of the radio buttons

affect a variable with radiobutton

I'm new to android, I have a button that is when its clicked it will get the double value from an EditText and show it in a TextView.
Also I have two radio buttons, one is positive and other is negative, I want when the user toggle the negative button to multiply the double by -1, my code is like:
public void buttonClicked(View view) {
try {
myDouble = Double.parseDouble((myDoubleEditText.getText().toString()));
} catch (NumberFormatException e) {
Toast.makeText(getApplicationContext(), "Enter a valid Double", LENGTH_LONG).show();
}
myTextView.setText("My Double is",""+myDouble)
}
public void onRadioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId()) {
case R.id.positive:
if (checked)
myDouble = myDouble;
break;
case R.id.negative:
if (checked)
myDouble = myDouble * -1;
break;
}
}
}
Please guide me to do it.
Thanks
I'd think a better way is to use RadioGroup and set the listener on this to change and update the View accordingly (saves you having 2 or 3 or 4 etc listeners).
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
switch(checkedId) {
case R.id.positive:
myDouble = myDouble;
break;
case R.id.negative:
myDouble = myDouble * -1;
break;
}
}
});

checkbox text not getting displayed on textview

I am new to android technology, i wanted the checkbox's text to be displayed using textview.
I think the problem is in TextUtils.join function
For e.g
If my checklist is a, b, c, d.
I want to display the textview like
//Text view string
you have selected a, b, c, d.
Code Snippet :
public class Menu3 extends AppCompatActivity {
CheckBox checkBox;
CheckBox checkBox2;
CheckBox checkBox3;
CheckBox checkBox4;
TextView tView;
ArrayList<String> arrayList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu3);
checkBox = (CheckBox)findViewById(R.id.checkBox);
checkBox2 = (CheckBox)findViewById(R.id.checkBox2);
checkBox3 = (CheckBox)findViewById(R.id.checkBox3);
checkBox4 = (CheckBox)findViewById(R.id.checkBox4);
tView = (TextView)findViewById(R.id.textView3);
checkBox.setOnClickListener(ocl);
checkBox2.setOnClickListener(ocl);
checkBox3.setOnClickListener(ocl);
checkBox4.setOnClickListener(ocl);
String all = TextUtils.join(",", arrayList);
tView.setText(all);
}
View.OnClickListener ocl = new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean checked = ((CheckBox) v).isChecked();
if(checked) {
String text = null;
switch (v.getId()) {
case R.id.checkBox:
text = checkBox.getText().toString();
Toast.makeText(Menu3.this, text + " checked.", Toast.LENGTH_SHORT).show();
arrayList.add(text);
break;
case R.id.checkBox2:
text = checkBox2.getText().toString();
Toast.makeText(Menu3.this, text + " checked.", Toast.LENGTH_SHORT).show();
arrayList.add(text);
break;
case R.id.checkBox3:
text = checkBox3.getText().toString();
Toast.makeText(Menu3.this, text + " checked.", Toast.LENGTH_SHORT).show();
arrayList.add(text);
break;
case R.id.checkBox4:
text = checkBox4.getText().toString();
Toast.makeText(Menu3.this, text + " checked.", Toast.LENGTH_SHORT).show();
arrayList.add(text);
break;
}
}
}
};
}
The problem is that you are calling setText right away in onCreate, and it isn't called again when the checkboxes are changed. Personally, I'd set up a method that gets called when a checkbox is changed to handle all of this
Something like this
private void updateTextView(String text) {
Toast.makeText(Menu3.this, text + " checked.", Toast.LENGTH_SHORT).show();
arrayList.add(text);
String all = TextUtils.join(",", arrayList);
tView.setText(all);
}
Then call it from all of your OnClickListener for the checkboxes
case R.id.checkBox:
updateTextView(checkBox.getText().toString());
...
You are setting the text first, after then, you are not setting anything. You must set text every time you add/remove string to your arraylist.

Editext and RadioButton in android

I have two EditText boxes and a RadioGroup which contains two radio buttons. When I start entering input in any of edittext boxes, All the radiobuttons has to be checked off (if any one of buttons has checked already) automatically and another edittext also should be cleared. And If i check any of two radio buttons then all edittext boxes should be cleared(if they have any inputted data). Here is what i have done so far.
Inside oncreate() :
et1 = (EditText)findViewById(R.id.editText1);
et1.setOnFocusChangeListener(this);
et1.addTextChangedListener(this);
et2 = (EditText)findViewById(R.id.editText2);
et2.setOnFocusChangeListener(this);
et2.addTextChangedListener(this);
radioButton1 = (RadioButton)findViewById(R.id.rb1);
radioButton2 = (RadioButton)findViewById(R.id.rb2);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup group, int checkedId) {
et1.setText("");
et2.setText("");
}
});
And ...
#Override
public void onFocusChange(View v, boolean hasFocus) {
switch (v.getId()) {
case R.id.editText1:
whoHasFocus = 1;
break;
case R.id.editText2 :
whoHasFocus = 2;
break;
}
}
And
#Override
public void afterTextChanged(Editable edt) {
if(whoHasFocus == 1){
String enteredName1 = edt.toString().trim();
if(enteredName1.length() == 1){
if(et2.getText().toString().trim().length() >= 1)
et2.setText("");
radioButton1.setChecked(false);
radioButton2.setChecked(false);
}
}else if(whoHasFocus == 2){
String enteredName2 = edt.toString().trim();
if(enteredName2.length() == 1){
if(et1.getText().toString().trim().length() >= 1)
et1.setText("");
radioButton1.setChecked(false);
radioButton2.setChecked(false);
}
}
}
When i check any radio button and start typing in edittext, radiobutton is getting unchecked but nothing is entering into edittext for the first time. If i enter next letter then it is entering the second letter and working fine then onwards. But First letter is not being entered into endittext when a radiobutton is checked. please help me. Where iam going wrong?
Solved
radioGroup.setOnCheckedChangeListener(null);
radioButton1.setChecked(false);
radioButton2.setChecked(false);
radioGroup.setOnCheckedChangeListener(this);
And
et1.removeTextChangedListener(MyActivity.this);
et2.removeTextChangedListener(MyActivity.this);
et1.setText("");
et2.setText("");
et1.addTextChangedListener(MyActivity.this);
et2.addTextChangedListener(MyActivity.this);

Categories

Resources