how to clear the checked box after we get the checked result ?
something like selection.clear(); but, that only clear the output, not the checkbox.
what i am trying to do is, to set the checkbox to the original state, which is unchecked.
after user checked the checkbox then click button to get result of checked checkbox, i wish to clear all the selection in the checkbox. how please help?
public class DessertIngAvail extends Dessert {
ArrayList<String> selection = new ArrayList<String>();
TextView final_text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dessert_ing_avail);
final_text = (TextView)findViewById(R.id.final_result);
final_text.setEnabled(false);
}
public void selectItem(View view){
boolean checked = ((CheckBox) view).isChecked();
switch (view.getId()) {
case R.id.checkBox181:
if(checked) {
if(!selection.contains("Tebaloi"))
selection.add("Tebaloi");
if(!selection.contains("Tumpik"))
selection.add("Tumpik");
}
break;
case R.id.checkBox182:
if(checked) {
if(!selection.contains("Ambuyat"))
selection.add("Ambuyat");
}
break;
case R.id.checkBox183:
if(checked) {
if(!selection.contains("Tumpik"))
selection.add("Tumpik");
}
break;
case R.id.checkBoxCM:
if(checked) {
if(!selection.contains("Honey Frankincense Cake"))
selection.add("Honey Frankincense Cake");
if(!selection.contains(" Ray Heart Cake"))
selection.add(" Ray Heart Cake");
}
break;
}
}
public void finalSelection(View view) {
String final_fruit_selection = "";
for(String Selection : selection) {
final_fruit_selection = final_fruit_selection + Selection + "\n";
}
final_text.setText(final_fruit_selection);
selection.clear();
final_text.setEnabled(true);
}
}
You can use
checkBox.setChecked(boolean);
//to clear the check box
checkBox.setChecked(false);
Updated finalSelection(View view) method;
public void finalSelection(View view){
String final_fruit_selection = "";
for(String Selection : selection){
final_fruit_selection = final_fruit_selection + Selection + "\n";
}
final_text.setText(final_fruit_selection);
selection.clear();
final_text.setEnabled(true);
//now clear checkboxes
checkBox181.setChecked(false);
checkBox182.setChecked(false);
checkBox183.setChecked(false);
checkBoxCM.setChecked(false);
}
Changing really really really basic things
//right below TextView final_text; at the top add this
CheckBox checkBox181,checkBox182,checkBox183,checkBoxCM;
//declare then in onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dessert_ing_avail);
final_text = (TextView)findViewById(R.id.final_result);
final_text.setEnabled(false);
checkBox181=(CheckBox)findViewById(R.id.checkBox181);
checkBox182=(CheckBox)findViewById(R.id.checkBox182);
checkBox183=(CheckBox)findViewById(R.id.checkBox183);
checkBoxCM=(CheckBox)findViewById(R.id.checkBoxCM);
}
I suggest you to follow up some java course online. This is a good place to start.
To start learning android development check here.
To check:
checkBox.setChecked(true);
and to uncheck:
checkBox.setChecked(false);
Use setChecked():
checkBox.setChecked(true/false)
Docs: https://developer.android.com/reference/android/widget/CheckBox.html
I'm not sure I understand your question but you can set a checkbox by using checkBox.setChecked(false). To uncheck all checked checkboxes, simply loop over them and uncheck the checked ones.
If you'r sure checkbox are checked, You can toggle them.
checkbox.toggle();
Otherwise
checkBox.setChecked(false); // pass true if you want to select.
Related
Like this https://androidexample365.com/content/images/2019/05/OtpEditText.gif
if wordbox1.setText("C") get clicked wordbox1 become invisible and matchbox1.getText("C") when matchbox1 get clicked wordbox1 become visible and matchbox1.getText(null)
I want check if matchbox1 , matchbox2 ,matchbox3 = getText(null) to fill who null
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//my answer buttons
//if you click on any button of my question buttons look for
// who is null first of my answer buttons
Button matchbox1,matchbox2,matchbox3;
//my question buttons wordbox1 = "C" wordbox2 = "A" wordbox3 = "R"
// car is the correct answer
Button wordbox1,wordbox2,wordbox3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
matchbox1= (Button)findViewById(R.id.matchbox1);
matchbox2= (Button)findViewById(R.id.matchbox2);
matchbox3= (Button)findViewById(R.id.matchbox3);
wordbox1= (Button)findViewById(R.id.wordbox1);
wordbox2= (Button)findViewById(R.id.wordbox2);
wordbox3= (Button)findViewById(R.id.wordbox3);
//for example if matchbox1.getText(R) wordbox3.setVisibility(View.Visible);
matchbox1.setOnClickListener(this);
//for example if matchbox2.getText(A) wordbox2.setVisibility(View.Visible);
matchbox2.setOnClickListener(this);
//for example if matchbox3.getText(C) wordbox1.setVisibility(View.Visible);
matchbox3.setOnClickListener(this);
wordbox1.setOnClickListener(this);
wordbox2.setOnClickListener(this);
wordbox3.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.wordbox1:
matchbox1.setText("C");
wordbox1.setVisibility(View.INVISIBLE);
break;
case R.id.wordbox2:
matchbox2.setText("A");
wordbox2.setVisibility(View.INVISIBLE);
break;
case R.id.wordbox3:
matchbox3.setText("R");
wordbox3.setVisibility(View.INVISIBLE);
break;
//for example any button pressed
case R.id.matchbox1:
wordbox3.setVisibility(View.VISIBLE);
matchbox1.setText("");
break;
case R.id.matchbox2:
wordbox2.setVisibility(View.VISIBLE);
matchbox2.setText("");
break;
case R.id.matchbox3:
wordbox1.setVisibility(View.VISIBLE);
matchbox3.setText("");
break;
}
if (matchbox1.getText().toString().equals("C") &&
matchbox2.getText().toString().equals("A")&&
matchbox3.getText().toString().equals("R")){
matchbox1.setBackgroundColor(Color.GREEN);
matchbox2.setBackgroundColor(Color.GREEN);
matchbox3.setBackgroundColor(Color.GREEN);
}
}
}
I hope i find the answer becase I need it very much
I have a basic hangman game, and the buttons to make the guesses are all placed on screen with ImageButtons (I had this working perfectly).
I declare the image button ID's like so:
private static final int[] BUTTON_IDS = {
R.id.imageButtonA, R.id.imageButtonB, R.id.imageButtonC, R.id.imageButtonD, R.id.imageButtonE,
R.id.imageButtonF, R.id.imageButtonG, R.id.imageButtonH, R.id.imageButtonI, R.id.imageButtonJ,
R.id.imageButtonK, R.id.imageButtonL, R.id.imageButtonM, R.id.imageButtonN, R.id.imageButtonO,
R.id.imageButtonP, R.id.imageButtonQ, R.id.imageButtonR, R.id.imageButtonS, R.id.imageButtonT,
R.id.imageButtonU, R.id.imageButtonV, R.id.imageButtonW, R.id.imageButtonX, R.id.imageButtonY, R.id.imageButtonZ
};
private List<ImageButton> buttonLetters = new ArrayList<ImageButton>();
Edited to show solution: I register their OnClick by doing:
for (int id: BUTTON_IDS) {
final ImageButton buttonLettersUsage = (ImageButton)findViewById(id);
if (buttonLetters != null) {
assert buttonLettersUsage != null;
buttonLettersUsage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(HangmanActivity.this, " " + buttonLettersUsage.getId(), Toast.LENGTH_SHORT).show();
switch (buttonLettersUsage.getId()){
case R.id.imageButtonQ:
strGuessed = "Q";
break;
case R.id.imageButtonW:
strGuessed = "W";
break;
case R.id.imageButtonE:
strGuessed = "E";
break;
// ... Repeat for rest of buttons ...
default:
break;
}
btnGuessClicked(theIncorrectGuesses, theWord);
buttonLettersUsage.setVisibility(View.INVISIBLE);
}
});
}
}
The problem is now, I've been working on the app some more since implementing this part, and all the buttonLettersUsage.getId()'s have changed.
My question is:
Will they change at any time?
I've not put any new components onto the activity, why have they changed?
Is there a better way to find out which button has been pressed, and give a different value for strGuessed depending on which has been pressed?
Many thanks.
Don't use the numerical ids in the switch case use it as follows.
switch (buttonLettersUsage.getId()){
case R.id.imageButtonA:
strGuessed = "Q";
break;
case R.id.imageButtonB:
strGuessed = "W";
break;
case R.id.imageButtonC:
strGuessed = "E";
break;
// ... Repeat for rest of buttons ...
default:
break;
}
And also don't implement onClickListener like this, use your Activity to implement View.OnClickListener and then set the listener in loop as
buttonLettersUsage.setOnClickListener(this);
Where this is the context of your activity which implements the OnClickListener
YourActivity implements View.OnClickListener{
then in oncreate(){
//add is buton
View buttonAdd = findViewById(R.id.add);
buttonAdd.setOnClickListener(this);}
then
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add:
{
}
break;}
I am working with an Arduino and trying use two toggle buttons to connect and enable the Arduino. I don't want to be able to press the enable button until I have connected the robot. Here is my main activity:
public class MainActivity extends Activity {
Arduino arduino = null;
ToggleButton enable, connect;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ToggleButton connect = (ToggleButton) findViewById(R.id.buttonConnect);
ToggleButton enable = (ToggleButton) findViewById(R.id.buttonEnable);
}
public void onToggleClicked(View view) {
switch(view.getId()) {
case R.id.buttonConnect:
boolean on = ((ToggleButton) view).isChecked();
if(on){
//Take the text from editText1 and make it the IP Address
EditText text = (EditText)findViewById(R.id.editText1);
String ip = text.getText().toString();
arduino = new Arduino(ip);
arduino.connect();
arduino.disable();
} else {
arduino.disable();
arduino.disconnect();
enable.setChecked(false);
}
break;
case R.id.buttonEnable:
boolean click = ((ToggleButton) view).isChecked();
if(click && connect.isChecked()) {
arduino.enable();
}else {
System.out.println("Not Connected");
enable.setChecked(False);
arduino.disable();
}
break;
default:
break;
}
}
I know the program is not reaching the else statement in the buttonEnable case because it is not printing out "Not Connected." I have also tried to use connect.isEnabled()in my if statement but that doesn't work either. Does anybody have answer? Thank you!
Try to set on click listener to attach the code execution to you button
I am trying to reduce the amount of code I have by making everything more efficient
I want to implement a switch statement instead of a long if statement
here is the switch i have began creating
public void onClick(View v) {
switch(v.getId()){
case R.id.buttonA:
displayLetters(v);
break;
case R.id.buttonB:
displayLetters(v);
break;
and here is the displayLetters method that I want each case to run
private void displayLetters(View v) {
NewDisplayWord = EditText.getText().toString();
NewDisplayWord = NewDisplayWord + v.getTag();
EditText.setText(NewDisplayWord);
}
However instead of displaying A or B in the textedit when I press the buttons, I get null when I press any of the buttons
It seems that you are trying to get the text from the EditText by doing EditText.getText(). I think what you mean to do is use the v variable passed into displayLetters(). You can do something like this:
private void displayLetters(View v) {
NewDisplayWord = ((EditText)v).getText().toString();
NewDisplayWord = NewDisplayWord + v.getTag();// I do not know what is the use of `getTag()` here...
((EditText)v).setText(NewDisplayWord);
}
If the displayLetters() will only deal with EditTexts, you can make its parameter be an EditText
First, let's change displayLetters to accept String
private void displayLetters(final String letter) {
newDisplayWord = editText.getText().toString();
newDisplayWord = newDisplayWord + letter;
editText.setText(newDisplayWord);
}
Then call displayLetters() with String param:
public void onClick(View v) {
switch(v.getId()){
case R.id.buttonA:
displayLetters(v.getTag());
break;
case R.id.buttonB:
displayLetters(v.getTag());
break;
default:
break;
}
}
If your onClick() listener will serve only the buttons, you could probably simplify it even further:
#Override
public void onClick(View v) {
//cast view to button
Button bt = (Button) v;
//get text from button
final String letter = bt.getText().toString();
//pass it to displayLetters()
displayLetters(letter);
}
Please follow Java naming convention, that is variable names start with lowercase. Also, I'm assuming that you have properly set your button and editText:
editText = (EditText) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
Do you need to inflate a view in order to get its id for the onClick method? Because when I run my program in the emulator and click on the specific button it does nothing! I want it to go back to the main.xml layout! Do I need to procces the onClick some other way?
public void onClick(View v) {
switch( v.getId()){
case R.id.play:
setContentView(R.layout.play);
setQuestion();
break;
case R.id.level:
setContentView(R.layout.level);
if(lvl.equals("1")) {
lvl1.setChecked(true);
}
if(lvl.equals("2")) {
lvl2.setChecked(true);
}
if(lvl.equals("3")) {
lvl3.setChecked(true);
}
if(lvl.equals("4")) {
lvl4.setChecked(true);
}
if(lvl.equals("5")) {
lvl5.setChecked(true);
}
break;
case R.id.setLevel:
if(lvl1.isChecked()) {
setLevel("1");
}
if(lvl2.isChecked()) {
setLevel("2");
}
if(lvl3.isChecked()) {
setLevel("3");
}
if(lvl4.isChecked()) {
setLevel("4");
}
if(lvl5.isChecked()) {
setLevel("5");
}
setContentView(R.layout.main);
break;
}
}
Here is how I get the views:
setContentView(R.layout.main);
Button play = (Button)findViewById(R.id.play);
play.setOnClickListener(this);
Button level = (Button)findViewById(R.id.level);
level.setOnClickListener(this);
Button setLevel = (Button)getLayoutInflater().inflate(R.layout.level, null).findViewById(R.id.setLevel);
setLevel.setOnClickListener(this);
lvl1 = (RadioButton)getLayoutInflater().inflate(R.layout.level, null).findViewById(R.id.lvl1);
lvl2 = (RadioButton)getLayoutInflater().inflate(R.layout.level, null).findViewById(R.id.lvl2);
lvl3 = (RadioButton)getLayoutInflater().inflate(R.layout.level, null).findViewById(R.id.lvl3);
lvl4 = (RadioButton)getLayoutInflater().inflate(R.layout.level, null).findViewById(R.id.lvl4);
lvl5 = (RadioButton)getLayoutInflater().inflate(R.layout.level, null).findViewById(R.id.lvl5);
What should I do so that when I click the setLevel button it changes the view back to the main.xml view
Andrew,
You may want to make individual onClick() methods for each item. That's how I handle my click-able objects.
Example:
Button play = (Button)findViewById(R.id.play);
play.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//perform play button actions here
}
});
This way you already have the button object created based on its ID value, and the onClickListener is specifically tailored to that item.
good luck!