Here is my image button Click action below.
It will show "1 null" after clicked, while holding the button it shows rightAnswer. I want to illustrate the saved string of questionLabel after finishing the holding button.
What I want to do is that define "sharedFact" before Action_down happens and keep that text to show after the user button untouched.
The "rightAnswer" should be only shown while the user holding imageButton
I tried to use normal String text like "test", it works, however String sharedFact = questionLabel.getText().toString();
↑ is not working like what I thought, what is the problem and how to fix?
findViewById(R.id.image_button_check).setOnTouchListener((v, event) -> {
String sharedFact = null;
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
sharedFact = questionLabel.getText().toString();
questionLabel.setText(rightAnswer);
break;
case MotionEvent.ACTION_UP:
//Here is only test "1" is for identify this code is working
//How can I save questionLabel Text before action down but inside click action?
questionLabel.setText(1 + sharedFact);
break;
}
return false;
});
move your definition from inside the function to a class data member.
String sharedFact = null;
onCreate(){
// your code
findViewById(R.id.image_button_check).setOnTouchListener((v, event) -> {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
sharedFact = questionLabel.getText().toString();
questionLabel.setText(rightAnswer);
break;
case MotionEvent.ACTION_UP:
//Here is only test "1" is for identify this code is working
//How can I save questionLabel Text before action down but inside click action?
questionLabel.setText(1 + sharedFact);
sharedFact = null
break;
}
return false;
});
}
Related
Basically what I'm trying to do is setting the color of the buttons. The "ganzjahrbutton" includes the other buttons. Therefore I wanna change the color of all the buttons (besides the ganzjahrbutton) to the grey color (162,162,162) if it's clicked.
If any of the other buttons is clicked, they should turn green and the ganzjahrbutton should become grey again. It kinda works, but the buttons need to be pressed twice.
Does anybody have an idea why?
switch (v.getId()){
case R.id.ganzjahrbutton:
ganzjahrbtnstate = !ganzjahrbtnstate;
if (ganzjahrbtnstate==true){
ganzjahrbutton.setBackgroundColor(Color.rgb(30,168,1));
fruhlungbutton.setBackgroundColor(Color.rgb(162,162,162));
sommerbutton.setBackgroundColor(Color.rgb(162,162,162));
herbstbutton.setBackgroundColor(Color.rgb(162,162,162));
winterbutton.setBackgroundColor(Color.rgb(162,162,162));
}
else {ganzjahrbutton.setBackgroundColor(Color.rgb(162,162,162));}
break;
case R.id.fruhlingbutton:
fruhlingbtnstate = !fruhlingbtnstate;
if (fruhlingbtnstate==true){fruhlungbutton.setBackgroundColor(Color.rgb(30,168,1));
ganzjahrbutton.setBackgroundColor(Color.rgb(162,162,162)); }
else {fruhlungbutton.setBackgroundColor(Color.rgb(162,162,162));}
break;
case R.id.sommerbutton:
sommerbtnstate = !sommerbtnstate;
if (sommerbtnstate==true){sommerbutton.setBackgroundColor(Color.rgb(30,168,1));
ganzjahrbutton.setBackgroundColor(Color.rgb(162,162,162)); }
else {sommerbutton.setBackgroundColor(Color.rgb(162,162,162));}
break;
case R.id.herbstbutton:
herbstbtnstate = !herbstbtnstate;
if (herbstbtnstate==true){herbstbutton.setBackgroundColor(Color.rgb(30,168,1));
ganzjahrbutton.setBackgroundColor(Color.rgb(162,162,162)); }
else {herbstbutton.setBackgroundColor(Color.rgb(162,162,161));}
break;
case R.id.winterbutton:
winterbtnstate = !winterbtnstate;
if (winterbtnstate==true){winterbutton.setBackgroundColor(Color.rgb(30,168,1));
ganzjahrbutton.setBackgroundColor(Color.rgb(162,162,162)); }
else {winterbutton.setBackgroundColor(Color.rgb(162,162,162));}
break;
Move the lines where you change the boolean states to the end of cases just before break statements
case R.id.ganzjahrbutton:
****FROM HERE*****ganzjahrbtnstate = !ganzjahrbtnstate;****
if (ganzjahrbtnstate==true){
ganzjahrbutton.setBackgroundColor(Color.rgb(30,168,1));
fruhlungbutton.setBackgroundColor(Color.rgb(162,162,162));
sommerbutton.setBackgroundColor(Color.rgb(162,162,162));
herbstbutton.setBackgroundColor(Color.rgb(162,162,162));
winterbutton.setBackgroundColor(Color.rgb(162,162,162));
}
else {ganzjahrbutton.setBackgroundColor(Color.rgb(162,162,162));}
TO HERE*****ganzjahrbtnstate = !ganzjahrbtnstate;****
break;
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;
}
}
});
So I've done layouting my android application, the problem now is that, how could I combine the two buttons, like when I click the first block and the 3rd block that will generate a associate "letter" So basically in every combination of buttons there is a corresponding "letter"
Or please do correct me if I'm wrong if using a "button" to implement what I wanted is the right thing to use? If not, please state what's the right way to do to implement what I wanted.
If I haven't explain well, I would state some examples: For e.g clicking the 1st block, 3rd block and the 5th block that generates a letter "z".
Please pardon me if my explanation is confusing. Sorry I am still learning java.
What you can do is create your layout with all the buttons and respective ids and values for example I am giving you one button
<Button
android:id="#+id/btnA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
android:textSize="16sp" />
just like this create buttons with respective ids and text values...
Now inside your activity setOnClickListener for all buttons.
inside your onCreate()
like this
Button A = (Button)findViewById(R.id.btnA);
Button B = (Button)findViewById(R.id.btnB);
.
.
.
. just like this
then setOnClickListener
A.setOnClickListener(this);
B.setOnClickListener(this);
After this inside your onClick() method just based on the which button clicked append their value to a string. and first declare one global string variable for example String letter;
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnA:
letter += "A";
break;
case R.id.btnB:
letter += "B";
break;
.
.
Hope this will help
Ok so now I am editing my answer...
m just gonna give you small idea depending on that you have to decide
so as per your question what you want is suppose user press btnA and btnB you want to write z on the screen. This is not a simple job you its actually lengthy. First of all you have to decide on what combination you want to generate what just like btnA and btnB you need to generate z
so keep your clicks same as i have given above just inside your onClick function change the code that I am giving you.
so first
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnA:
letter += "A";
checkCombo(letter);
break;
case R.id.btnB:
letter += "B";
checkCombo(letter);
break;
.
.
So now create one function named as checkCombo(String letter) with one string parameter
Now the actual part comes where you have to decide what combinations you want so if you want on A and B clicks to generate Z then create a switch case like below
function checkCombo(String mletter) {
String generatedLetter;
switch(letter) {
case "AB":
generatedLetter = 'Z';// or you can set it to your textview here
letter = ""; // if it goes inside any of your switch case clear your
global variable
break;
case "DEF":
generatedLetter = 'P'
letter = "";
break;
}
}
This is just a lengthy solution but will work
Get the button id on click and do your logic like below
First set onClickListener for all buttons then onClick method do like this
#Override
Public void onClick(View v){
switch(v.getId()) {
case R.id.first_button_id:
case R.id.second_button_id:
case R.id.third_button_id:
String value = "your character";
break;
}
I have a simple calculator app. When the user presses calculate the edit text where they entered their number changes color and outputs the result. When reset is pressed I would like the line under the edit text to re-appear but it doesn't show up the way I current have my code written. This is a snipped of my current code.
#Override
public void onClick(View v){
switch (v.getId()){
case R.id.calculate:
myFunction.doWork();
voltageInputEditText.setBackgroundColor(Color.DKGRAY);
voltageInputEditText.setTextColor(Color.WHITE);
break;
case R.id.reset:
myFunction.reset();
voltageInputEditText.setText("");
voltageInputEditText.setBackgroundColor(Color.WHITE);
break;
}
}
This is the desired result
This is the current result
This is the look after I run myFunction.(doWork);
When you call setBackgroundColor internally it is calling setBackgroundDrawable with a ColorDrawable, so you're replacing the original background, the one that displays the line inside EditText.
Setting a white background color to begin with would equally remove this line.
It should work if you save the original background drawable via getBackground and later restore it via setBackgroundDrawable.
You should simply save it.
String savedInputText = "";
#Override
public void onClick(View v){
switch (v.getId()){
case R.id.calculate:
myFunction.doWork();
savedInputText = voltageInputEditText.getText();
voltageInputEditText.setBackgroundColor(Color.DKGRAY);
voltageInputEditText.setTextColor(Color.WHITE);
break;
case R.id.reset:
myFunction.reset();
voltageInputEditText.setText(savedInputText);
voltageInputEditText.setBackgroundColor(Color.WHITE);
break;
}
}
I've tried quite a few different tactics to achieve my desired result, but nothing is making these buttons do what they're 'sposed to... Basically I have 14 buttons. Four with the text "X", digitOne, digitTwo, digitThree and digitFour. Then, there are 10 with "1", "2", etc, named "one", "two", etc. All the buttons are tied to the same OnClickListener that will use a switch statement to determine which button was pressed, then find the soonest display button (buttons initially marked "X"), and change that buttons text to the entered digit. What I want to happen is:
Say someone clicks the "5" button. If its the first button pressed, the first "digit" button will change from displaying "X" to "5", and so-on, so-forth. This is not what is happening... In fact, nomatter what I've tried, nothing is happening. Not even an error... An error would be nice, at least I'd know where my logical flaw is -_-. Here's the code:
The button declarations:
one=(Button)findViewById(R.id.button1);
two=(Button)findViewById(R.id.Button2);
three=(Button)findViewById(R.id.Button3);
four=(Button)findViewById(R.id.Button4);
five=(Button)findViewById(R.id.Button5);
six=(Button)findViewById(R.id.Button6);
seven=(Button)findViewById(R.id.Button7);
eight=(Button)findViewById(R.id.Button8);
nine=(Button)findViewById(R.id.Button9);
zero=(Button)findViewById(R.id.Button0);
add=(Button)findViewById(R.id.buttonAdd);
digitOne=(Button)findViewById(R.id.Number1);
digitTwo=(Button)findViewById(R.id.Number2);
digitThree=(Button)findViewById(R.id.Number3);
digitFour=(Button)findViewById(R.id.Number4);
one.setOnClickListener(listener);
two.setOnClickListener(listener);
three.setOnClickListener(listener);
four.setOnClickListener(listener);
five.setOnClickListener(listener);
six.setOnClickListener(listener);
seven.setOnClickListener(listener);
eight.setOnClickListener(listener);
nine.setOnClickListener(listener);
zero.setOnClickListener(listener);
The OnClickListener private inner class (I guess that's what you'd call it. It's inside Activity class):
private OnClickListener listener = new OnClickListener(){
public void onClick(View button) {
switch(button.getId()){
case R.id.Button0:
addANumber(0);
break;
case R.id.button1:
addANumber(1);
break;
case R.id.Button2:
addANumber(2);
break;
case R.id.Button3:
addANumber(3);
break;
case R.id.Button4:
addANumber(4);
break;
case R.id.Button5:
addANumber(5);
break;
case R.id.Button6:
addANumber(6);
break;
case R.id.Button7:
addANumber(7);
break;
case R.id.Button8:
addANumber(8);
break;
case R.id.Button9:
addANumber(9);
break;
}
}
};
And finally, the "addANumber" method being called:
public void addANumber(int i){
if(digitOne.getText()=="X"){
digitOne.setText(i);
}else if(digitTwo.getText()=="X"){
digitTwo.setText(i);
}else if(digitThree.getText()=="X"){
digitThree.setText(i);
}else if(digitFour.getText()=="X"){
digitFour.setText(i);
}
}
I've done this before... I know I'm missing something so blatantly stupid it deserves a smack in the head...
Before all:
digitOne.getText()=="X" should be "X".equals(digitOne.getText())
you need checking for string equality in terms of content, not in term of reference.
Nothing happens because with == none of your if condition is evaluated to true and addANumber() simply results as an empty method