This question already has answers here:
Remove an onclick listener
(10 answers)
Closed 6 years ago.
I have around 18 views and a button that has onclicklisteners. My goal is to disable the listeners on the 18 views once the button is clicked to avoid them from getting flipped over again once they are pressed.
My problem: after i have initialized the onclicklisteners those 18 views still has their listeners and stil doing the method being called once they are clicked. I have tried setting click listeners to null and setting clickable to false and they are still clickable.
Any way to disable theses views from being pressed?
Method to disable:
private void makeUnclickable() {
for (int x = 9; x < 9; x++) {
front[x].setOnClickListener(null);
back[x].setOnClickListener(null);
front[x].setClickable(false);
back[x].setClickable(false);
}
}
Take a boolean variable and store the state of the button you want to enable or disable.Then:
public void onClick(View v)
{
if(isEnabled)
{
//write your click listeners here
}
else
{
return true;
}
}
Related
So we are working on a quiz game/app and we have a problem with just one thing.
We got 4 buttons for the possible answers and only one of them is the correct one (obviusly). They are regular buttons with text on them, not the radio ones. The thing is that in order to avoid creating an activity for each question we wanna keep the buttons in one activity, and when pressed on the "correct answer" to change the buttons functions to be different. For example buttons 1,2,3 all send the player to gave over screen, while button 4 is the correct one. Then it should change the text that is displayed on the buttons and change all the buttons' functions so that 1,3,4 are now the "game over buttons" and 2 is the correct one. We tried if statements and integers, and booleans, to no avail. Any hints or solutions that could help us?
Thanks
Given your current setup I'd have each button's onClick call a method like validateCorrectAnswer(int buttonNumber) and then from there you'd do your validations. So for button1 you'd call validateCorrectAnswer(1).
From validateCorrectAnswer(...) you'd have an array of correct answers, so it could be something like int[] correctAnswers = {4, 2, 3, 4, 1, 3, 2, 1, ...}; and depending on which question they are on you'd check. So let's say you are on question 3 you'd check correctAnswers[2] == buttonNumber.
So to simplify that further within your activity store what question they are currently on in a global variable, something like private int currentQuestion = 0 and then after each question you increment that number.
So the final method would be something like:
void validateCorrectAnswer(int buttonNumber) {
if (correctAnswer[currentQuestion] == buttonNumber) {
currentQuestion++;
// correct answer, cool move on
} else {
// wrong answer, game over
}
}
In your question class, you can set the answer to a text / button id and check on a button click if the button text / id is the same as your answer.
Also, see this example from butterknife:
//Specify multiple IDs in a single binding for common event handling.
#OnClick({ R.id.door1, R.id.door2, R.id.door3 })
public void pickDoor(DoorView door) {
if (door.hasPrizeBehind()) {
Toast.makeText(this, "You win!", LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Try again", LENGTH_SHORT).show();
}
}
source: Butterknife docs
You could use the setTag(tagValue) and getTag(tagValue) methods for the 4 buttons.
1 - correct
0 - wrong
And then just switch the tagValues according to the answer given.
#Override
public void onClick(View view) {
int answer = 0;
if(view instanceof Button) {
answer = (int) ((Button)view).getTag();
}
if(answer == 1) {
//true answer
refreshButtons();
} else {
//game over
}
Log.d("TAG", "a: "+answer);
}
public void refreshButtons () {
questionNr ++;
Log.d("TAG", "q: "+questionNr);
setAnswers(questionNr);
}
public void setAnswers (int question) {
switch (question){
case 1: // Second question
v1.setTag(0);//wrong
v2.setTag(1);//correct
v3.setTag(0);//wrong
v4.setTag(0);//wrong
break;
case 2:// Third question
v1.setTag(0);//wrong
v2.setTag(0);//wrong
v3.setTag(1);//correct
v4.setTag(0);//wrong
break;
case 3:// Forth question
v1.setTag(0);//wrong
v2.setTag(0);//wrong
v3.setTag(0);//wrong
v4.setTag(1);//correct
break;
}
}
You could also setup the texts for the other views in the same setAnswer() Method
The onCreate() method could be something like this :
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//here you setup the answers for the first question
v1 = (Button) findViewById(R.id.button1);
v1.setOnClickListener(this);
v1.setTag(1);
v2 = (Button) findViewById(R.id.button2);
v2.setOnClickListener(this);
v2.setTag(0);
v3 = (Button) findViewById(R.id.button3);
v3.setOnClickListener(this);
v3.setTag(0);
v4 = (Button) findViewById(R.id.button4);
v4.setOnClickListener(this);
v4.setTag(0);
I am duplicating my linear layout dynamically and I have to set onClickListeners for buttons inside the linear layout.
for(int i = 0; i <10 ; i++){
// other code here
Button approve_btn = (Button) findViewById(R.id.rent_number_up_btn);
approve_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
approve_btn.setText(String.valueOf(i));
}
});
}
Everything works fine except that my button's text is always set to 9. I think that's because when the listener is called the value of i is 9 at that time. What I want the value of i at the time the button's listener is set and I am not sure how to do that.
How can I solve this problem? Any help is appreciated.
The issue is that you are setting click listener to the same button (by calling findViewById()) 10 times in a row. You get the value 9 because thats the last click listener which you added to the button.
for(int i = 0; i <10 ; i++){
// other code here
Button button = new Button(<Activity Instance>);
button.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
approve_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
approve_btn.setText(String.valueOf(i));
}
});
}
In above code you need to add those buttons in your linearlayout.
Hope this will help you,
Thanks
I am not sure what you want to do but:
Like #Shaishav said your are using same button (R.id.rent_number_up_btn) and your are replacing the click listeners on top of each other. The last value (of the your counter "i") before your loop finish is 9 , that's why it show 9 all the time. If you want to add 10 buttons inside your Linear layout just create new Button(context) every time when your loop starts and add this button to your layout via
yourLinearlayout.addView(yourNewButton);
Then if you set click listener to your new button maybe it will show different values :)
I have a Activity in android that has 4 buttons.
The first 3 buttons fetches a json data from a weather API for 1 day, next 5 days and next 10 days respectively.
I have a 4th button placed at the bottom of the screen, which takes user to second activity.
I want to restrict the entry of user to second Activity if no button from top 3 is clicked.
If the data is fetched, I mean any one of the top 3 buttons have been clicked, allow him to go to second activity on 4th button click else show a message.
How can i check on click of 4th button if any of the top 3 buttons have been clicked before?
Thanks
Put a boolean field in your activity, name it clicked and set it to false on the onCreate method of your first activity, then in the onClick method of your 3 buttons, set it to true,
and in the onClick method of your 4th button check it, if it's true go startActivity, else launch a Toast
You can make the 4th button look disable in "OnCreate" with the function "setEnabled"(may be wrong),
and then just set "setOnClickListener" for the 4th button when you click any of the others.
ps.
Can provide code example if needed.
Why don't you use if statement? You can keep the clicked count data under the first three buttons. Like this;
import java.util.stream.*;
int[] btnMemory = new int[4];
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
btnMemory[0] = 1;
// your code
}
});
after, you can check it with if statement under 4th button;
button4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int sum = IntStream.of(btnMemory).sum();
if(sum >= 3)
// your code
}
});
This question already has answers here:
How to find out which view is focused?
(6 answers)
Closed 6 years ago.
My app uses keyboard control and when moving around I've noticed that occasionally you get what I'll refer to as a "Mystery View" - I say that because nothing visible seems to be selected, yet nothing visible is focussed either
Is there any way to find out what I'm focussed on at any given time? eg. a listener that fires on a view selection, etc.
Thanks
** edit - some code for ntc **
LinearLayout col1 = (LinearLayout)findViewById(R.id.col1);
Button btn = new Button(this);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
LinearLayout col1 = (LinearLayout)findViewById(R.id.col1);
if(col1.getFocusedChild() != null) {
Log.d(TAG, col1.getFocusedChild().toString());
}
}
});
col1.addChild(btn);
getWindow().getCurrentFocus();
You can probably use this method for every view. Maybe this may gets bigger and hectic but is sure to work:
if(yourChildView.isFocused()) {
yourChildView.setFocusable(false);
}
This question already has answers here:
Get text from pressed button
(8 answers)
Closed 9 years ago.
for( i=0; i<26; i++) {
btnAlpha[i] = new Button(this);
btnAlpha[i].setBackgroundColor(Color.TRANSPARENT);
btnAlpha[i].setTextColor(Color.GREEN);
btnAlpha[i].setText(Character.toString ((char)(j+i)));
btnAlpha[i].setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
//Want to get the text from the current button.
btnAlpha[i].getText();
//But it gives error that "i" cannot be accessed.
}});
I get the error "cannot refer to a non-final variable inside an inner class defined in a different method". But I need to get the text at that right time. How to do it? Is there any other way to do it?
Button is a subclass of View, so the argument to onClick, v, is the Button being clicked. Try
public void onClick(View v) {
((Button) v).getText();
}
create a new variable final int x = i; in the for loop and use x in place of i in the onClick method
edit: actually im not sure this will work correctly. oops. Kype P's answer looks pretty good.