How to change the android button after 2 clicks ?
the first time to change button i will use this code
{
public void onClick(View v) {
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed));
}
}
I want to change the button view again after pressing it one more time
how can i do that ?
Perhaps do it like this:
int count = 0;
public void onClick(View v) {
count++;
if(count == 2){
count = 0;
b.setBackgroundDrawable(getResources()
.getDrawable(R.drawable.menubuttonpressed));
}
}
This will set the background after every 2nd click on your button (view).
private int clickCount =0;
public void onClick(View v) {
if (clickCount==0) {
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed));
} else {
// do something else
}
clickCount++;
}
Well, one way is to keep a counter.
numberOfClicks = 0;
...
public void onClick(View v) {
...
if(numberOfClicks==0)
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed0));
else if(numberofClicks==1)
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed1));
...
numberofClicks++;
}
Related
I want to disable a button when the user hits the button five times.
Here's the code
if(mLatestindex<=4) {
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
mIsCheater = false;
updateQuestion();
mFalseButton.setEnabled(true);
mTrueButton.setEnabled(true);
mLatestindex++;
}
}
Your mLatestIndex <= 4 is placed wrongly.
You should check the index inside the Click Listener
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mLatestIndex < 5) {
// Do the stuff
// This will execute only if the index is <= 4
}
mLatestIndex++;
}
});
This will execute the code only if mLatestIndex is less than 5, and increment it.
If you need disable button on 5 times click in general just add global counter and increment it in onClick() with check
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clickerCount++;
if (clickerCount >= 5) {
button.setEnabled(false);
}
}
If you need to disable view when it was clicked 5 times in certain amount of time, you could store timestamp of last clicked time and compare it with current time
I would like to know how to set an if else condition where the next button have to be disabled if none of the buttons are being clicked. Otherwise, they are able to proceed to next question?
private AdvancedQuestion nAdvancedQuestion = new AdvancedQuestion();
private TextView nScoresView;
private TextView nQuestionsView;
private TextView tvTime;
private Button nButtonChoices1;
private Button nButtonChoices2;
private Button nButtonChoices3;
private Button nButtonChoices4;
private String nAnswers;
private int nScores = 0;
private int nQuestionNumbers = 0;
Button btnNextz;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_advanced_quiz);
updateQuestions();
nButtonChoices1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (nButtonChoices1.getText() == nAnswers) {
correctSound.start();
nScores = nScores + 1;
nButtonChoices1.setEnabled(false);
nButtonChoices2.setEnabled(false);
nButtonChoices3.setEnabled(false);
nButtonChoices4.setEnabled(false);
nButtonChoices1.getBackground().setColorFilter(Color.GREEN, PorterDuff.Mode.MULTIPLY);
Toast.makeText(advancedQuiz.this, "correct", Toast.LENGTH_SHORT).show();
} else {
wrongSound.start();
Toast.makeText(advancedQuiz.this, "wrong", Toast.LENGTH_SHORT).show();
nButtonChoices1.setEnabled(false);
nButtonChoices2.setEnabled(false);
nButtonChoices3.setEnabled(false);
nButtonChoices4.setEnabled(false);
}
}
});
btnNextz.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nextSound.start();
updateQuestions();
nButtonChoices1.setEnabled(true);
nButtonChoices2.setEnabled(true);
nButtonChoices3.setEnabled(true);
nButtonChoices4.setEnabled(true);
nButtonChoices1.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
nButtonChoices2.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
nButtonChoices3.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
nButtonChoices4.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
}
private void updateQuestions() {
nQuestionsView.setText(nAdvancedQuestion.getQuestions(nQuestionNumbers));
nButtonChoices1.setText(nAdvancedQuestion.getChoices1(nQuestionNumbers));
nButtonChoices2.setText(nAdvancedQuestion.getChoices2(nQuestionNumbers));
nButtonChoices3.setText(nAdvancedQuestion.getChoices3(nQuestionNumbers));
nButtonChoices4.setText(nAdvancedQuestion.getChoices4(nQuestionNumbers));
nAnswers = nAdvancedQuestion.getCorrectAnswers(nQuestionNumbers);
nQuestionNumbers++;
}
private void updateScore(int points) {
nScoresView.setText("" + nScores);
}
Please note there is 4 possible answers. If none of them are selected, they cannot proceed to the next question until one button is press so they can go to the next question. The updateQuestions() is the part where i believe it will show next question.
This is a simple example on how to disable/enable a button based on an if condition -
int count = 0;
if (count == 0) {
NextButton.setEnabled(false);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.otherButtons:
count++;
NextButton.setEnabled(true);
Toast.makeText(this, "Button Disabled", Toast.LENGTH_LONG).show();
break;
case R.id.nextButton:
//Move the user to the next question
break;
}
}
Also check out this link
You will need to add a button element in the view of the activity you want it to appear on, then add an event listener to it either in the activity code or specify which function to call on click in the activities XML layout file.
See: https://developer.android.com/reference/android/widget/Button.html
I want to go back and forth a string-array and put it on my text view, but when coming back program stops, what is my problem?
String[] q = new String[20];
...
int a =0;
q[0]="a";
q[1]="b";
q[2]="c";
...
public void back(View v) {
a--;
u.setText(q[a]);
}
public void next(View v) {
a++;
u.setText(q[a]);
}
public void back(View v) {
if(a > 0){
a--;
u.set Text(q[a]);
}
}
public void next(View v) {
if(a < q.length()){
a++;
u.set Text(q[a]);
}
}
in method next() "q.length" would do, q.length() I'am afraid will not.
I'm a beginner in Android Studio, and I want to make a C button in calc with 2 functions.
How do I do that on one tap the C button erases only one number, and on hold erase all numbers in TextView?
findViewById(R.id.btnClear).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtScreen.setText(""); // Clear the screen
// Reset all the states and flags
lastNumeric = false;
stateError = false;
lastDot = false;
You can set an onClickListener and onLongClickListener to achieve this.
cButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String currentText = textView.getText().toString();
if(currentText.length >= 2){
currentText = currentText.substring(0, currentText.length - 2);
}else{
currentText = "";
}
textView.setText(currentText);
}
});
cButton.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
textView.setText("");
return true;
}
});
Have you looked in to the onClick() and onLongPress() methods?
cancelButton.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
myTextView.setText("");
return true;
}
});
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
removeOneCharacter();
}
});
private void removeOneCharacter()
{
String textViewValue = myTextView.getText().toString();
if (textViewValue != null && textViewValue.length >= 2)
{
myTextView.setText(textViewValue.substring(0, textViewValue.length() - 2);
}
}
The onClick() method removes on char from the TextView at a time. The onClick() method however removes the entire String from the TextView but only considering if there is already 2 or more characters to prevent an Exception from occurring due to the upper-bound of the substring.
An improvement here could be to add another if function that checks the character length within the Long press and performs a clear if there is only one character remaining.
I have one Linearlayout - totalincome and another TableLayout normalincometable, which should appear just below the totalincome. normalincometable will be invisible when the program runs. When the user clicks on "totalincome" the table should display. If the user clicks on "totalincome again", the table should disappear. I have tried this code, but It didnt work.
totalincome.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
int x =0;
// TODO Auto-generated method stub
if (x==0)
{
normalincometable.setVisibility(View.VISIBLE);
x=1;
}
else
{
normalincometable.setVisibility(View.GONE);
x=0;
}
});
}
From this code, I can make the table visible in first click but It doesnt disappear in next click. Are there any options ?
Try this:
#Override
public void onClick(View v) {
if(normalincometable.getVisibility() == View.VISIBLE) {
normalincometable.setVisibility(View.GONE);
} else {
normalincometable.setVisibility(View.VISIBLE);
}
}
You have declared int x =0; inside onClick method. So, when ever onClick is called, it assigns 0 to "x". Declare it outside at class scope.
because you have define x in the button click code so whenever button click it set to 0. define x outside the button click scope.
try this way:put x variable outside the button onclick() or defined x globally
int x =0;
totalincome.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (x==0)
{
normalincometable.setVisibility(View.VISIBLE);
x=1;
}
else
{
normalincometable.setVisibility(View.GONE);
x=0;
}
});
}
Use like this:
int x =0;
totalincome.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (x==0)
{
normalincometable.setVisibility(View.VISIBLE);
x=1;
}
else
{
normalincometable.setVisibility(View.GONE);
x=0;
}
});
}
Try this
Boolean isFirstTimeClicked=true;
totalincome.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (isFirstTimeClicked)
{
normalincometable.setVisibility(View.VISIBLE);
}
else
{
normalincometable.setVisibility(View.GONE);
}
isFirstTimeClicked=!isFirstTimeClicked;
});
}
and in your code you have declared int x =0; inside onClick method. So, when ever onClick is called, it assigns 0 to "x". Declare it outside at class scope.
Easiest Method is
button.setVisibility(View.VISIBLE == button.getVisibility() ? View.GONE:View.VISIBLE);