I am comparing strings in if condition and printing the value of selectedradbtn inside the if condition. Here the selectedradbtn is printing inside the if condition...but when I am printing before if condition the value is printing...
My code:
ArrayList<String> array2;
static int selectedradbtn;
selectedradbtn = Previouspage.selectedradiobtn;
System.out.println("selected radio button outside if"+selectedradbtn); //here value is printing
if(Integer.toString(selectedradbtn).equals(array2.get(0)) && array2.get(3).equals("1") )
{
System.out.println("selected radio button in if"+selectedradbtn); //value not printing
correct = correct+ 1;
System.out.println("correct if"+correct);
}
Where I am going wrong...please help me regarding this...
Thanks in advance...
not sure, but does this help
selectedradbtn = Previouspage.selectedradiobtn;
System.out.println("selected radio button outside if"+selectedradbtn); //here value is printing
if((Integer.toString(selectedradbtn).equals(array2.get(0)) && (array2.get(3).equals("1")))
{
System.out.println("selected radio button in if"+selectedradbtn); //value not printing
correct = correct+ 1;
System.out.println("correct if"+correct);
}
Add an else {} and check if your condition is satisfied or not.
Related
So I'm trying to learn Kotlin and have been using Android Studios to practice and learn. Currently I'm trying to make a simple activity with RadioGroup (with Radio Buttons), save the selected value, and then display how much of each value (radiobutton) was selected.
My question is, how do I print which button was selected, and how many of this type of button was selected?
I tried the following:
//in MainActivity.kt in my MainActivity class
s1RadioGroup.setOnCheckedChangeListener { _, checkedId ->
//if catButton was selected add 1 to variable cat
if(checkedId == R.id.catRadio) {
catSum += 1
print(catSum)
}
//if dogButton was selected add 1 to variable dog
if(checkedID == R.id.dogRadio) {
dogSum += 1
print(dogSum)
}
Not sure if I'm going about it the right way, but the desired output is:
I have layout, ID's, clear button, and everything else working. But I'm not sure how to use onClickListener event on 'SaveButton' to save selected radio button and then displaying results (Ex: Cat = 1, Dog =2). I would appreciate any suggestions, or if you can point me in the right direction.
You can maybe try something like this:
RadioButton rb = (RadioButton) findViewById(R.id.radio_button);
// restore previous state
rb.setChecked(lastButtonState);
// set a listener
rb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// call this to enable editing of the shared preferences file
// in the event of a change
SharedPreferences.Editor editor = sharedpreferences.edit();
Boolean isChecked = rb.isChecked();
// use this to add the new state
editor.putBoolean(BUTTON_STATE, isChecked);
// save
editor.apply();
}
});
I realize that this is in Java, and you're asking for kotlin, but a SharedPreference, is what you would need to save the radio button's state.
if you want to save all datam you can use database or sharedprefrence.
and if you only want just display value is clicked, you can make like this in button save.
String result1 = ""
String result2 = ""
String result3 = ""
RadioGroup radioGroup = findViewById('yourRGidFromXml')
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
int selectedId = radioGroup.getCheckedRadioButtonId();
RadioButton rb = findViewById(selecetedId)
result1= rb.getText.toString()
Log.i("ID", String.valueOf(selectedId));
}
});
//this just for see result
btnSave.OnclikListener(view -> {
Log.i("Result1",result1)
})
you can copy code and android will convert that code to kotlin.
This is basically the situation im in
(view link)
Getting RadioButton cannot be resolved to a type
i have an integer storing values for the radio buttons - however, i dont know how to accept it in the other activity.
This is what i have so far (but i get an error)
Intent i = getIntent();
int value = intent.getIntExtra("inputValue", a);
if(a = 1)
{
//Enter action
}
Use value in if condition to check inputValue from previous Activity :
int value = intent.getIntExtra("inputValue", 0);
if(value == 1) {
//Enter action
}
Also Intent.getIntExtra take first argument as key and second as default value an int if key not found in intent.
Ok im making app and it have 15 button's and 6 textview's.I want when I press first button to change value of first textview(value is "") to something (number one for example).But problem is when i press second button if first textview is already set to some value to set set second textview to second value.
If you need something else ask in comments (sorry for bad English)
here is what I was doing(this is under onclick)when i press second button
if(textview1.equals("1")){
textview2.setText("2");}
else if (textview1.equals("")){
textview1.setText("2");
}
It sounds like you wish to show last 6 buttons pressed.
Store all pressed buttons in a List (i.e. LinkedList) of size 6. Initially, it will be empty.
Then whenever any button is pressed do two things:
1) add it to the List and delete old elements if size exceeds six.
2) set button values from List.
Second step can be achieved like this:
// all TextViews should be in a list
private List<TextView> textViews;
// declare as field
private List<String> memoryQueue = new ArrayList<String>();
public void update() {
//set fields for the pressed buttons
for (int i=0; i<6 && i<memoryQueue.size(); i++) {
String text = memoryQueue.get(i);
textViews.get(i).setText(text);
}
// set empty fields
for (int i = memoryQueue.size(); i<6; i++) {
textViews.get(i).setText("");
}
}
This code snippet assumes that you store your TextViews in a List.
And Easiest way to keep track of last six button:
public void buttonPressed(Button button) {
//get text based on your button
String text = button.getText();
if (memoryQueue.contains(text)) {
return;
}
memoryQueue.add(text);
if (memoryQueue.size() > 6) {
memoryQueue.remove(0);
}
}
Since you're concerned with the text inside of your text view, you should be using the object's getText method:
if( textview1.getText().equals("1") ){ // Edited
textview2.setText("2");
} else if (textview1.getText().equals("")){ //Edited
textview1.setText("2");
}
At first, you have to get the String text from TextView using getText() method then you can compare that String with another String. Now, change your condition as follows...
if(textview1.getText().toString().equals("1")){
textview2.setText("2");}
else if (textview1.getText().toString().equals("")){
textview1.setText("2");
}
In my project, I get the value of the gender from the database as 0 and 1.
0 for male and 1 for female.
Based on this values, I need to check the corresponding RadioButton in a RadioGroup.
If 0 is pressed, radio0 will be checked or radio1 will be checked.
I don't know how to check the radio button based on this string values...
This is the code I tried:
String gendd=ViewProfileActivity.participantview.get(4);
System.out.println("Gender:::::"+gendd);
male=(RadioButton)findViewById(R.id.radio0);
female=(RadioButton)findViewById(R.id.radio1);
if(gendd.equals("0")){
male.setSelected(true);
female.setSelected(false);
}
else
{
male.setSelected(false);
female.setSelected(true);
}
But it fails. Can anyone help me?
Try this.
if(gendd.contains("0"))
{
male.setChecked(true);
female.setChecked(false);
}
else
{
male.setChecked(false);
female.setChecked(true);
}
You shoud use check(radioButtonId) method on parent RadioGroup.
E.g.
radioGroup.check(gendd.equals("0") ? R.id.radio0 : R.id.radio1);
(assuming the comparison holds as the types haven't been mentioned)
Android multiple button click
I have a button and I want on the button's first click i display a thing, and on it's second i display another etc ..
I have a button and want it to have 11 clicks .. on the first click Num.settext("First");
on second click Num.settext("Second");
etc .. until the tenth click .. then on the 11th click its's Num.settext("0"); and it resets from the begning ..
like ..
1,2,3,4,5,6,7,8,9,10,11(0) 1,2,3,4,5,6,7,8,9,10,11(0)
Wouldn't be easier if you store the click number in a variable?
For instance:
//...
int clickNumber = 0;
//...
public void onClick() {
if(clickNumber > 10) {//reset variable
clickNumber = 0;
}
if(clickNumber == 0) {
Num.setText("First");
clickNumber++;
}
else if(clickNumber == 1) {
Num.setText("Second");
clickNumber++;
}
//...
}
//...
Maintain a field called cycle and an array of texts.
Then on click:
Num.setText(texts[cycle]);
cycle=(cycle + 1)%texts.length;