Android Toggle Button with timed delay off - java

I would like to create a toggle button that when pressed has about a 1 second delay and toggles off.
Any help would be appreciated.
public void onToggleClicked(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
// Relay On
} else {
// Relay Off
}
}

From what I have read setTimeout() function is your best bet. http://www.w3schools.com/js/js_timing.asp has a tutorial on it's use.
I imagine it could be used something like this.
public void onToggleClicked(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
// Relay On
setTimeout(toggle(){((ToggleButton) view).setChecked(false)},1000);
} else {
// Relay Off
}
}

Related

Back Button Webview Problem (Delayed work after few clicks)

I made a back button for my webview, but I got a problem.
When I click it more time (for example 5/6) it's back to the main page, but later button work like on delay, so I'm on some page and my app goes back. It's possible to limit the click of the button to only 1 click per page?
Thanks.
Button przycisk_powrot = (Button) findViewById(R.id.przycisk_powrot);
przycisk_powrot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
You can set the button state to enabled/disabled - and can make a check like this:
backButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (webView.canGoBack()) {
webView.goBack();
// Now check is it still can go back, ?
backButton.setEnabled(webView.canGoBack());
backButton.setAlpha((float)1.0);
}
else {
backButton.setEnabled(false);
// Also make the button little dim, so will show to user that it is currently in disabled state
backButton.setAlpha((float)0.5);
}
}
});
Enjoy..!
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (myWebView.canGoBack()) {
myWebView.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}

change the background color of three textInputlayouts by using listener

I have three text input layouts in my activity, I apply a listener on them and it changes background color when I click on it .but need to click again if I want to click the other two .my question is that how I implement such type of logic that when it 1st clicked and I click on one of the other two, the first one clickable color disappear and 2nd one or third one clicked and its background color change and same for others
View.OnClickListener listener = new View.OnClickListener() {
#SuppressLint("NonConstantResourceId")
#Override
public void onClick(View v) {
if (v.getId() == R.id.textViewLoseWeightSubtitle) {
if (mStateChanged) {
v.setBackgroundResource(R.drawable.textview_after_click);
// mLoseWgt.setTextColor(Color.WHITE);
mStateChanged = false;
mFittedToned.setClickable(false);
mBuildMuscle.setClickable(false);
} else {
v.setBackgroundResource(R.drawable.textview_outline_style);
//mLoseWgt.setTextColor(Color.parseColor("#363C60"));
mStateChanged = true;
mFittedToned.setClickable(true);
mBuildMuscle.setClickable(true);
}
}
if (v.getId() == R.id.textViewBuildMusclesSubtitle) {
if (mStateChanged) {
v.setBackgroundResource(R.drawable.textview_after_click);
// mLoseWgt.setTextColor(Color.WHITE);
mStateChanged = false;
mLoseWgt.setClickable(false);
mFittedToned.setClickable(false);
} else {
v.setBackgroundResource(R.drawable.textview_outline_style);
//mLoseWgt.setTextColor(Color.parseColor("#363C60"));
mStateChanged = true;
mLoseWgt.setClickable(true);
mFittedToned.setClickable(true);
}
}
if (v.getId() == R.id.textViewFittedAndTonedSubtitle) {
if (mStateChanged) {
v.setBackgroundResource(R.drawable.textview_after_click);
// mLoseWgt.setTextColor(Color.WHITE);
mStateChanged = false;
mLoseWgt.setClickable(false);
mBuildMuscle.setClickable(false);
} else {
v.setBackgroundResource(R.drawable.textview_outline_style);
//mLoseWgt.setTextColor(Color.parseColor("#363C60"));
mStateChanged = true;
mLoseWgt.setClickable(true);
mBuildMuscle.setClickable(true);
}
}
}
};
mLoseWgt.setOnClickListener(listener);
mBuildMuscle.setOnClickListener(listener);
mFittedToned.setOnClickListener(listener);
}
What you need is a onFocusChangedListener(). It gives a callback with a boolean which can be used to identify whether the current view is selected or not.
Declare it as:
View.OnFocusChangeListener listener = new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
v.setBackgroundResource(R.drawable.textview_after_click);
// mLoseWgt.setTextColor(Color.WHITE);
} else {
v.setBackgroundResource(R.drawable.textview_outline_style);
//mLoseWgt.setTextColor(Color.parseColor("#363C60"));
}
}
});
Set it as:
mLoseWgt.setOnFocusChangeListener(listener);
mBuildMuscle.setOnFocusChangeListener(listener);
mFittedToned.setOnFocusChangeListener(listener);
That's it. Your other code seems redundant. For EditText specific functions, you can typecast the view provided by onFocusChange().

How to prevent second click on radio button if it was already checked in Android?

I want to increase the score by 1, when someone clicks on the right radio button. If someone clicks 2 times, the score is increased by 2. I am trying to prevent that with a boolean but it doesn't work. Were am i wrong?
This is my code:
boolean isChecked;
answer[j].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RadioButton checkedRadioButton = ((RadioButton) v);
if (isChecked) {
if (checkedRadioButton.isChecked() & CorrectAnswer == 1) {
score++;
isChecked = false;
}
}
}
});
Thanks!
Assuming you don't need the value outside the OnClickListener, the easiest thing to do is to make it a member variable of that class:
new View.OnClickListener() {
private boolean isChecked = true; // HERE
#Override
public void onClick(View v) {
RadioButton checkedRadioButton = ((RadioButton) v);
if (isChecked) {
if (checkedRadioButton.isChecked() && CorrectAnswer == 1) {
score++;
isChecked = false;
}
}
}
};
You also need to set its value to true initially, since it will otherwise be false and so the conditional's body never runs.
You could also simply use
if (!checkedRadioButton.isChecked())
instead of a dedicated checker
if (isChecked)

Automated button clicking happens simultaneously

I've tried a hundred different methods to implement a delay between automated button clicks, including thread.sleep, Handler.postDelayed, and so on… It could be I have used it incorrectly somehow. My most recent attempt was with a simple boolean toggle. It seems that no matter how I try, all the buttons that are to be automatically clicked happen at the same time after the delay, INSTEAD of being delayed between clicks.
my code as it stands now:
setting up button onClickListener:
for (int i = 0; i < mDifficulty; i++) {
ButtonsOCLArray[i].setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
animating = true;
while (animating) {
animateButtons(v);
}
}
});
}
animation of buttons:
public static void animateButtons(View v) {
AlphaAnimation fadeInAnimation = new AlphaAnimation(0F, 1F);
fadeInAnimation.setDuration(1500);
fadeInAnimation.setFillAfter(true);
v.startAnimation(fadeInAnimation);
animating = false;
}
and finally, from a separate class, the automatic button setup:
public void pushAiButton(final View [] v){
mWhichButton = (mGameAi.getRandomNumber(MainActivity.mDifficulty)); // get random number for random button to press
mListOfAiButtonsToPress.add(mWhichButton); // add random number to mLOABTP
mListOfAiButtonsTemp.addAll(mListOfAiButtonsToPress); // add all elements of mLOABTP to mLOABT
boolean empty = false;
while (!empty) {
if (empty) {
break;
}
tempArrayIndex = mListOfAiButtonsTemp.get(0); // tempArray given value in first slot of mLOABT
mListOfAiButtonsTemp.remove(mListOfAiButtonsTemp.get(0)); // first slot of MLOABT removed
if (mListOfAiButtonsTemp.isEmpty()) {
// looped through whole list, empty now
empty = true;
} // end if
v[tempArrayIndex].performClick(); // click button at index *button*[*index*]
} // end !empty while
} // end pushAiButton()
any ideas HIGHLY appreciated! thanks!
UPDATE
This got it working:
mButtonStart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
Log.d(TAG, "START BUTTON CLICKED!");
if (firstRun) {
mGameAi.setupAiButtons();
firstRun = false;
}
v.setVisibility(View.INVISIBLE);
animateButtons(ButtonsOCLArray[mGameAi.getFirstButtonInList()]);
mGameAi.deleteFirstButtonInList();
v.postDelayed(new Runnable() {
public void run() {
if (!mGameAi.buttonsListIsEmpty()) {
v.performClick();
}
else {
v.setVisibility(View.VISIBLE);
firstRun = true;
}
}
}, 500);
System.out.println("end of mButtonStart's onclicklistener");
}
});
You have coded it so that they all click nearly simultaneously. The automatic button setup does a "while" loop that iterates through all the buttons - removing them one at a time nearly simultaneously.
In other words, your while loop iterating through the buttons needs to pause (or not queue another click) until the animation is complete.
Here is the problem said another way; when each "onClick" occurs, the boolean "animateButtons" is true and they all enter into the animateButtons method.
You need to have a thread with a wait call on in pushAiButton and wait for each button to finish its animation.

Get Key Press Action

In some phone we have the option in which we can state if power button locks the screen or just switches off the back light. I was wondering if there's a way to get the keypress action because as far as my understanding goes, they are just changing the actions in power key press event.
Can anyone help? Thanks in advance!
Try this.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);//prevent phone from being locked
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_POWER)
{
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
//do something
}
else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
//do something
}
}
return super.onKeyDown(keyCode, event);
}

Categories