Back Button Webview Problem (Delayed work after few clicks) - java

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);
}

Related

How to disable momentarily onClick event of ImageView and Button in Android Studio

I have 2 elements:
Button answer;
Imageview play;
When 'answer' is clicked - the app records the user.
When 'play' is clicked - the app plays a clue to the user.
So when one of the above is clicked, the other should be disabled and not allow an onclick event.
Problem is - I've tried both setClickable() and setEnabled() but they don't do what I expected them to do.
Also, I've tried using a boolean flag, but it also didn't do the trick.
What am I doing wrong?
Here is the attempt with the boolean flag:
answer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mIsAudioResourcesFree)
{
mIsAudioResourcesFree = false;
//RECORDS AND STOPS RECORDING
mIsAudioResourcesFree = true;
}
}
});
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mIsAudioResourcesFree)
{
mIsAudioResourcesFree = false;
//PLAYS SOME SHORT AUDIO
mIsAudioResourcesFree = true;
}
}
});

Android Studio Databinding: Can I access the contents of a binding within an onKey event?

I am trying to implement some button events without any reference to the XML-File and with databinding instead of FindByID. Is this possible? I am having the problem that, within the OnKeyListener, the bound InputBox from which I try to get the typed text seems inaccessible (this.binding shows in red color where I put it bold). Is this a wrong approach or am I making a mistake? I'd like to avoid all that FindByID-Lines.
this.binding =
DataBindingUtil.setContentView(this, R.layout.content_main);
this.binding.EditNumber.setText("553");
this.binding.EditNumber.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
Cat supertest = Manager.CreateMainCat(this.**binding**.EditNumber.toString());
this.**binding**.DisplayCurrentBudget.setText(supertest.getsName());
return true;
default:
break;
}
}
return false;
}
});
Thank you very much
Strangely, it works when I simply put the binding in another method:
(...)
this.binding.Submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
xxx();
}
});
}
public void xxx()
{
Cat supertest = Manager.CreateMainCat(this.binding.EditNumber.getText().toString());
this.binding.DisplayCurrentBudget.setText(supertest.getsName());
}
But this doesn't:
this.binding.Submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Cat supertest = Manager.CreateMainCat(this.binding.EditNumber.getText().toString());
this.binding.DisplayCurrentBudget.setText(supertest.getsName());
}
The propblem is solved easily, but I'd be very interested to know what's going if someone has the answer :)

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)

What is the proper implementation of the back button press in same activity?

I want to detect the back button.
However my current implementation does not even detect the back button.
CODE:
#Override
public boolean onTouch(MotionEvent e, int scaledX, int scaledY) {
//... OTHER CODE ...
else if(e.getAction() == MotionEvent.BUTTON_BACK){
System.out.println("BACK BUTTON PRESSED");
setCurrentState(new MenuState());
}
return true;
}
}
You can use onBackPressed() inside your Activity:
#Override
public void onBackPressed() {
//Do something
}
It's written in the documentation:
public static final int BUTTON_BACK
Button constant: Back button pressed (mouse back button). The system may send a KEYCODE_BACK key press to the application when this button is pressed.
You need to override onKeyUp function from the Activity (not from the View):
public boolean onKeyUp(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
//todo
}
}
Use Intent inside onBackPressed()like this:
#Override
public void onBackPressed()
{
Intent BackIntent = new Intent(getApplicationContext(), NewActivity.class);
startActivityForResult(BackIntent);
finish();
}

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