resetting the data in a radio button - java

I have 2 radio buttons on my initial page to choose a gender. Depending on boy or girl this then sets gender to 1 or 2. I have a reset button to go back to the main screen and in this button I have used, rg.setSelected(false) to reset the radio button, but for some reason even though I have also set gender back to 0 when clicking reset. When I choose the opposite sex radio button the gender int stays as the one that was first selected? I have done a Log to ensure gender reverts back to 0 on pressing reset which it does.
I am also wanting to reset the name, so when pressing the reset button I have done reader = "" but again using a Log, the name always remains as the first one that was input.
Can anyone help with how I get these to display the new values when entering them again without having to close the app?
Edited with code,
// code to get which radio button is selected,
public void onRadioButtonClicked(View view) {
if (rg.getCheckedRadioButtonId() == R.id.boyButton) {
gender = 1;
} else if (rg.getCheckedRadioButtonId() == R.id.girlButton){
gender = 2;
}
}
//code to reset,
public void reset(View view) {
setContentView(R.layout.start);
gender = 0;
reader = "";
rg.clearCheck();
Log.i("Gender", Integer.toString(gender));
Log.i("name", reader);
}
(I changed the code to clear the checkbox to rg.clearCheck() and that now seems to clear it, gender changes to 0 but then when I select a new gender and enter gender stays at 0 now.
It's like the second time I enter the details it's not being picked up and the code for the enter button isn't running
//code for enter button
public void enterName(View view) {
reader = nameText1.getText().toString();
setContentView(R.layout.melvin);
melvinSpeaks1 = findViewById(R.id.melvinSpeak1);
melvinText1 = findViewById(R.id.melvinText1);
rudolphSpeaks1 = findViewById(R.id.rudolphSpeak1);
rudolphText1 = findViewById(R.id.rudolphText1);
melvin1 = findViewById(R.id.melvinBubble);
rudolph1 = findViewById(R.id.rudolphBubble);
reset = findViewById(R.id.resetButton);
melvinText1.setVisibility(View.INVISIBLE);
rudolphText1.setVisibility(View.INVISIBLE);
rudolph1.setVisibility(View.INVISIBLE);
melvin1.setVisibility(View.INVISIBLE);
Log.i("Gender", Integer.toString(gender));
Log.i("name", reader)
}
Many thanks

Related

Statement to change the background color

I have a button that is a type Button that is gonna be clicked. When this is clicked, It is changing color to green.
When I click the button it changes color to green, but when I click it again, it should go back to the standard color.
I have 2 drawable files with names checked_list and not_checked_list.
These two are working good.
But when I click the button, the click has happened. And I can't click it again for some reason.
I have a Button field with a public void sendMessage method that is hooked to the buttons onClick. Is it better to just set an onClickEvent for the button in the code instead.
Here is the code I have so far.
int checked = 0;
Button gotIt;
gotIt = (Button)findViewById(R.id.got_it);
switch(checked) {
case 0:
gotIt.setBackgroundResource(R.drawable.checked_list);
checked = 1;
break;
case 1:
gotIt.setBackgroundResource(R.drawable.not_checked_list);
checked = 0;
break;
}
So here I want it to change between these two colors when I click it.
Any suggestions?
If all of the code you posted is inside your onClick method, then checked int is always 0 and will never be 1 because it is set in the first line of the method. Move your checked int outside of this method and it should work.
Setting click listener dynamically will have same result as setting in XML layout.
int checked = 0;
Button gotIt;
void sendMessage(View v) {
gotIt = (Button)findViewById(R.id.got_it);
switch(checked) {
case 0:
gotIt.setBackgroundResource(R.drawable.checked_list);
checked = 1;
break;
case 1:
gotIt.setBackgroundResource(R.drawable.not_checked_list);
checked = 0;
break;
}
}
You have to keep track of the last value for checked. Right now you are resetting it every time to 0 because it is a local field in your method call. Make checked as a class field and it will work as expected.
You can try this method
//global variables
boolean isChecked = true;
Button gotIt;
//put this in onCreate()
gotIt = (Button)findViewById(R.id.got_it);
gotIt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(isChecked == true){
gotIt.setBackgroundResource(R.drawable.checked_list);
isChecked = false;
}else{
gotIt.setBackgroundResource(R.drawable.not_checked_list);
isChecked = true;
}
}
});

How to set in a textview how many times a button is being clicked?

If I click a button 1 time, so it should to show number "1" in a textview. If I click again, so its should to show "2"...
#Override
public void onClick(View p1){
int id = p1.getId();
double x = 0;
//button clicked
if(id == R.id.button_contar){
x++; /*its only shows "1". When I click again, shows "1" again*/
this.mViewHolder.contados.setText(String.format("%.0f", x));
}
}
One possible approach is to initially set 0 in the textview, and with every button click you first fetch the current value in textView and then increment the value and set the new Value
int id = p1.getId();
//button clicked
if(id == R.id.button_contar){
int current = Integer.parseInt(this.mViewHolder.contados.getText().toString());
current++;
this.mViewHolder.contados.setText(String.format("%.0f", current));
}
I don't have any editor right now, so there might be some syntax errors with the above code. It will give you a rough idea on how you should solve your issue

How to make a clicked action operate only once

I just want an action to occur once, just once when clicked. Here is the code:
String previousValue = Counter.getText(" ");
Counter.setText("0");
AmouseClicked(java.awt.event.MouseEvent evt) {
If(callquestion==1 && A.isFocusable()) {
int d= Integer.parseInt(Counter.getText());
int e= 10;
int f=d+10;
Counter.setText(f+" ");
}
}
Note- Counter is a JLabel that displays the output, A is Jlabel that holds the correct answer .I have A,B,C,D jlabels.
The initial value is 0. So I want each time a button is cliked, 10 just be added ones, if u click again. The 10 remains. I have callquestion1 to 20. So after a play gets the correct answer, he clicks the next to go to the next Qus.
I just need each button to add 10 if clicked again do nothing.

Multiple buttons. Passing actions to the next button in Java

Java Android question
I have x, say 5, buttons in a row.
Each button has a different number value displayed on the button.
Button one is active, the rest are not- not clickable. They are greyed out.
To show Button 1 is active it fades up and down.
Once clicked the button pops up a message. The user Ok's that, this activates Button 2, and deactivates Button 1.
Then it happens through all buttons, one by one. The final button doesn't produce the pop up message.
My question...
I want to create a method that sets the first button as current and then once clicked sets the next as current, and so on.
Can anyone tell me how to do this? I don't need to know how to fade buttons etc, its literally how to set button as current, and within that method the user click sets the next button as current.
Many thanks in advance.
EDIT
OK, I've had a go...its not working, but it seems so close...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_workout_one);
int[] buttonIds = new int[] {R.id.button_1,R.id.button_2,R.id.button_3,R.id.button_4,R.id.button_5};
setButton(buttonIds);
}
private void setButton(int[] buttId){
int isCurrent = 0;
while(isCurrent < 5) {
Button currentButton = (Button) findViewById(buttId[isCurrent]);
//TODO Make current button pulse
currentButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.clearAnimation();
v.setBackgroundColor(0xFF00FF00);
v.setFocusable(false);
v.setFocusableInTouchMode(false);
v.setClickable(false);
setTimer();
isCurrent++;
}
});
I know that the problem is the isCurrent++ is not accessible outside the onClick method. How do I right this? Am I close or is this a major funk up and do I have to rethink?
Just use a global variable which track the current button, and check this variable to identify the current active button for determining the action in onClickListener. To fade out a button try this code snippet
button.setClickable(false);
button.setBackgroundColor(Color.parseColor("#808080"));
You need something like this:
private int activeButton = 1;
private void buttonClickHandler(){
switch(activeButton++){
case : 1
button1.setEnabled(true):
// show popup, hide/animate for button 1
break;
case : 2
button2.setEnabled(true);
// same for button 2
case : 3
// same for button 3
case : 4
// same for button 4
}

Trying to edit value of TextView on LongClick -- Almost working

I have two elements (TextView) in my XML layout that when a LongClick is pressed it will prompt the user to enter in a new value and then when the DONE button is clicked it should show the newly inputed value to the tvScoreHome using setText().
When I do a Long Click on the mentioned element the edit field and keyboard appear as expected. However, it won't allow me to type anything. When I type something it nothing shows up (but the device vibrates as if a button was pressed) and when the DONE button is clicked it vibrates as well but it does not exit the keyboard and show anything in the tvScoreHome element.
Any ideas why?
// set the onLongClickListener for tvScoreHome
tvScoreHome.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
final EditText userInput = (EditText) findViewById(R.id.userInput);
InputMethodManager imm = (InputMethodManager) context.getSystemService(Service.INPUT_METHOD_SERVICE);
userInput.setVisibility(View.VISIBLE);
imm.showSoftInput(userInput, 0);
tvScoreHome.setText( userInput.getText() );
userInput.setVisibility(View.INVISIBLE);
return true;
}
});
You need to give the user a chance to input some text before copying the data and hiding the EditText.
Remove these two lines from your listener:
tvScoreHome.setText( userInput.getText() );
userInput.setVisibility(View.INVISIBLE);
Perhaps you could use an OnFocusChangeListener to run these two lines when userInput loses focus.

Categories

Resources