Android disable button with case differentiation - android

So I really have no clue why this isn't wokring - basic stuff I did hundred times before:
I want to disable the button 'loadGame' by doing the following if the boolean is false:
if(saveExists == false){
loadGame.setEnabled(false);
}else{
loadGame.setEnabled(true);
}
saveExists is a boolean taken from a sharedPreferences object which is by default false (works, because I already checked saveExists in an output line).
Basically no matter what cases I create with if/elseif/else the button doesn't seem to get disabled at any point - did I really miss something big?
Okay so here's some code concerning the SharedPreference:
SharedPreferences settings;
SharedPreferences.Editor preferenceEditor;
...
In the onCreate of the class where I want to disable the button:
Resource.settings = getSharedPreferences(Resource.PREFERENCE_AUDIO, MODE_PRIVATE);
Resource.preferenceEditor = Resource.settings.edit();
saveExists = Resource.settings.getBoolean("settings", false);
Log.d(TAG, "saveExists="+saveExists);
and finally, the part to disable the Button:
loadGame = (ImageButton) findViewById(R.id.loadButton);
loadGame.setEnabled(saveExists);
loadGame.setOnClickListener(new OnClickListener() {...}
Oh btw we're talking about a ImageButton here
SOLUTION: You won't see this coming
God I hate to break it to you but that was clearly my fault. My projectpartner toggled the buttons invisible/visible as part of the AsyncTask and it's executed after I'm doing what I tried here all the time - yup shame on me I guess :x

It's very hard to determine the problem without more information. If the is/elseif/else doesn't seem to be working, you can try modifying your code to this to see what's going on:
if(saveExists == false){
Log.v("TEST", "saveExists is false");
}else{
Log.v("TEST", "saveExists is true");
}
If that is working then the problem seems to be with disabling the button. Does execution this line without the if/else work?
loadGame.setEnabled(false);
EDIT
From your update, I see you're using an ImageButton. For disabling an ImageButton, use setClickable(saveExists)

why don't use saveExist itself into the argument no need to used if/else
loadGame.setEnabled(saveExists);
if it is true then it is enabled otherwise not

Related

Check if a button is pressed and then check again

I am rather new to android developing and can't figure out how to do a double check on a button press.
I am working in Android Studio and am trying to create a little simple game and have the following: two buttons (button1 and button2)declared and instantiated in an onClick method.
When button 1 is pressed a counter is incremented and a specific value is shown on the button from an array with the values 0, 10, 50, 100.
This part works like a charm. What I can't do is create a working "tiebreaker rule" which should be something like:
if (button1 value == 100 and button2 value == 100) or this can be done with the counter if (counter1 == 3 && counter2 == 3)
tiebreaker();
this is what I need to do:
public void tiebreaker(){
if (button1 is pressed){
button1.setText(String.valueOf("OK. One more!");
if (button1 is pressed (again){
resetCounters();
goForPlayer1(); //distinct method
} else if (button2 is pressed) {
//set both values back to 100
tiebreaker(); //calls itself in order to repeat until a button is pressed twice consecutivelly
} (else?)
if(button2 is presed){
//same code as above, just with the numbers 1 and 2 swapped
}
}
I have tried implementing this tiebreaker method in different ways, but with no final success. I tried with if(button1.isPressed()) as condintion, having a boolean turn true when the button is pressed and using it as a condition, using the values from the buttons with button.equals("100") or whatever should be written but nothing.
Also, I tried making the tiebreaker as a second onClick method and doing checks based on switch (View.getId()) but the problem here is that the method being view dependant (tiebreaker(View v)) I am not sure how to call it from a different method.
IMO the main issue is that if within another if or switch/case within another switch/case can't be used when it comes to button presses (I could be wrong, though), but I don't know of any other way to do this.
I hope I did not miss anything and managed to make myself clear.
I appreciate any input in helping me solve this tiebreaker rule. Thank you
it's probably something like this ...
whenButtonAIsPressed() {
do some stuff
possiblyRunTiebreakerMaterial()
}
whenButtonBIsPressed() {
do some stuff
possiblyRunTiebreakerMaterial()
}
Note that you always run possiblyRunTiebreakerMaterial() and you always run it at the end of doing all your other processing.
Here's the nature of your possiblyRunTiebreakerMaterial() function...
possiblyRunTiebreakerMaterial() {
// there are some conditions, which mean you DO want
// to run the tie breaker:
do some processing, such as
value of A = blah blah
value of B = blah blah
// now have a boolean, and figure it out
boolean shouldWeRunTheTieBreaker
processing
processing
shouldWeRunTheTieBreaker = blah
and now finally:
is shouldWeRunTheTieBreaker == true { tiebreaker() }
}
and then have your tiebreaker() function.
What you're really looking for is a state machine, but don't worry about that for now.
The general "programming trick" you're looking for here is that you must do these things:
always do your processing
then, only afterwards,
in all cases, check IF you need to run the tiebreaker
run it if needed
That's the pattern.
Unfortunately, I don't have the rating to upvote.
I managed to resolve the issue. Instead of a function, I created a boolean for tiebreaker and it looks something like:
if (p1 = 100 || p2 == 100)
tiebreak = true;
if (p1 == 100 && tiebreaker){
p1 == OK;
tiebreaker = false;
} else //same thing for p2

How do I make my TTS stop speaking when a button is pressed?

How do I make my Text-To-Speech stop speaking? Right now, my TTS will start speaking when I launch my application, but I'm trying to make it stop speaking when a button on my Main Activity is pressed.
I have tried creating an onStop() method and using .stop, but to no avail.
May I please get some help on this issue?
You could do something along the lines of this ...
stopSpeakingButton.setOnClickListener(new
View.OnClickListener() {
String toSpeak = " ";
textToSpeech.speak(
toSpeaks,TextToSpeech.QUEUE_FLUSH,null);
});
As you gave us no code this a concept , not the most beautiful solution but will work if you adjust for your use case, the idea is to make it read string with onny a space in it!

Catching the Back key on Android with an Inputmultiplexer

ive got another Question for you.
So im trying to get the user input working on an options menu. For this i got:
1. The Stage and
2. An extra Inputadapter
I need the extra Inputadapter to catch the BACK key on Android. So i have used an Inputmultiflexer, which allows me to use both inputprocessors.
InputMultiplexer multiplexer = new InputMultiplexer();
multiplexer.addProcessor(this);
multiplexer.addProcessor(stage);
Gdx.input.setInputProcessor(multiplexer);
Gdx.input.setCatchBackKey(true);
And my method to check for the BACK button looks like this:
#Override
public boolean keyUp(int keycode){
if(keycode == Input.Keys.ESCAPE || keycode == Input.Keys.BACK){
new MenuScreen(game);
return true;
}
return false;
}
The problem: Its not working at all. It does not go back, when using the Back key on Android or the Escape key on Desktop. The only thing the Console is printing out when pressing the Button is :
Load KCM of non-default device may incur unexpected result
To be honest, i have no idea what its means and Google didnt help me either with that.
So how do i get this to work?
First when debugging, you should use something like :
System.out.println("back was pressed");
If you see that output in the console then you know input is working. If that works, the only problem I see with your code is that you may have created a new screen class, you never made it switch screens. I noticed you passed in the game object, so you should probably have something like this:
game.setScreen(new MenuScreen(game));
Hope this helps.

animation["name"].length works in Editor but not on Android Build

here is some code that works when using play mode in the editor with unity remote, but it does not work when running on an actual android build. Just the function with a problem.
public IEnumerator Reload(){
if(isReloading == false){
Debug.Log ("Reloading");
if(isShooting == true){
CeaseFire();
}
isReloading = true;
fpsRigAnimation.CrossFade (reloadAnim);
guiText.text = "before animation length";
//problem here
yield return new WaitForSeconds (fpsRigAnimation[reloadAnim].length);
guiText.text = "after animation length";
fpsRigAnimation.CrossFade (poseAnim);
Debug.Log ("Reloaded");
isReloading = false;
}
}
The line where I yield for the animation length simply does not run on the android build. I get the "before animation length" in the gui, but not the "after animation length". But both show up when I play it in the editor through the unity remote app. Also the reload animation runs through fine, as well as the debug.log calls in the editor, but the reload animation does not go through at all in the android build.
Edit
I just tested the yield statement with a plain float value (2.23f), and it still does not work. So that means the yield statement is the problem too. But the animation clip length is still a problem because when I try making the guitext say the value, it shows nothing on the android, but it does in the editor. I do have other yield statements in the game that all work accordingly.
Edit: Solution
It turns out the text file that I was reading in all of the stats from needed to be in a special folder called "Resources" to be included in an actual build.
It turns out the text file that I was reading in all of the stats from needed to be in a special folder called "Resources" to be included in an actual build.
May revise your logic giving conditions shall work.
Like
if (animation[“name”].time > animation.["name"].length)
{
//do something
}
Give it a try and tell.

Android OnLongClickListener strange / unreliable behaviour

I'm currently fighting against the OnLongClickListener on Android Api Lvl 8.
Take this code:
this.webView.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
System.out.println("long click");
return true;
}
});
It works perfectly. I can press anywhere on the WebView and the event triggers every time.
Now take a look at this one:
this.webView.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
final EditText editText = getUrlTextField();
switch (editText.getVisibility()) {
case View.VISIBLE:
editText.setVisibility(View.GONE);
return true;
case View.GONE:
editText.setVisibility(View.VISIBLE);
return true;
default:
return false;
}
}
});
Assuming the URL EditText components is currently visible, it gets gone from the display and should be shown again when another long click event is triggered.
But if you run this, the event just works once (!) when one performs a long click on any position on the WebView. To make things complicated, the long click works again when it is performed on a link on the website...
Can anyone explain if it is a bug in the sdk and/or if there is a mistake in my thinking how the OnLongClickListener is working?!? :/
EDIT:
I've run now several different scenarios on a Nexus One and come to following conclussion: Changing the layout on runtime more or less kills the OnLongClickListener... I haven't found a way to get it work reliably at all...
I would really appreciate if anyone could give me a hint... I'm at my wits end :(
Personnally, I ended up by re-setting the listener after each relayout.
I've run into this issue as well. It seems that if the view layout changes in a way that child view bounds need to be modified (i.e. TextView is wrap_content width and you set its text to something longer/shorter than it was before), views in the hierarchy will have their onStartTemporaryDetach method called (most likely due to a layout pass, although I haven't dug deep enough to find out for sure). If you look at the source for View that onStartTemporaryDetach ultimately unsets the pressed state of the view.
Changing the views in your layout that will be updated periodically to have bounds that will not change regardless of the value you set, will fix the issue. Although, that is still not awesome.

Categories

Resources