Android :getVisibility() error - android

i have alistview when i click the list view it goes to login screen, when login is successful it comes back to the listview with a icon ..so far its working good, problem is when the icon appear again if i click on the listview the loginscreen is comming ..i dnt want the login screen to load again once the icon is visible..i tried the following but it is giving errors
if (img.getVisibility() == 8) {
Intent intent = new Intent(MainActivity.this,LoginActivity.class);
startActivity(intent);
}
Any help is appreciated.

use
if (img.getVisibility() != View.Visible)
Dont use hardcode values.

Change your condition as:
if (img.getVisibility() == View.Visible)
EDIT : or better way you can use View.isShown() for checking View or it's child's are Visible or not

You should not really on your UI state for app logic. You would better use startActivityForResult, then in onActivityResult set a flag and use this flag in your click listener, use it also to make the icon visible or not.
You should also consider setting the flag in the shared preferences for persistence if you leave your activity.

what kind of error you face ?? can you show it to use ? you should not use a hard code value to compare the visibility use one of these View.GONE or View.INVISIBLE or VIEW.VISIBLE
like this
if(img.getVisibility != View.VISIBLE){
// do something
}

Use
if (img.getVisibility() != View.Visible)
instead of
if (img.getVisibility() == 8)
check http://developer.android.com/reference/android/view/View.html#setVisibility%28int%29

Related

Change image after click on ImageView

everyone. After clicking on an ImageView, I would like to change it depending on what kind of drawable is currently displayed. To do this, I compare the currently set image in the ImageView with one from the drawable folder. Unfortunately, it is currently the case that only the else branch is executed. The image changes after the first click, but then it no longer switches. What am I doing wrong?
bookmark_site.setOnClickListener{
vibrator.vibrate(50);
var draw = bookmark_site.drawable.constantState
if(draw == ContextCompat.getDrawable(this,R.drawable.ic_baseline_bookmark_filled_24)?.constantState)
{
bookmark_site.setImageDrawable(resources.getDrawable(R.drawable.ic_outline_bookmark_border_outline_24))
}
else{
//Only this two is ever executed
bookmark_site.setImageDrawable(resources.getDrawable(R.drawable.ic_baseline_bookmark_filled_24))
}
}
you can't base on constantState, its not desired to compare drawables as there is a different instance for every drawable, thus your if isn't true never ever. in fact there is no way for getting resource of drawable already set for ImageView. most convenient way would be to keep some boolean flag outside onClick method informing that image is switched or not. optionally you may keep this flag "inside" ImageView using setTag(...) method
bookmark_site.setOnClickListener{
vibrator.vibrate(50);
val switched = bookmark_site.tag != null
bookmark_site.setImageResource(
if (switched) R.drawable.ic_outline_bookmark_border_outline_24
else R.drawable.ic_baseline_bookmark_filled_24
)
bookmark_site.tag = if (switched) null else "SWITCHED"
}
you should keep category or tag along with the image in the model class and then change the image on the basis of that tag or category.

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

SwipeRefreshLayout Without Progress Spinner

Is it possible to perform SwipeRefreshLayout functionality without showing progress spinner all together. Right now its working perfectly fine with its default behavior of pull to refresh shows Progress spinner and onRefresh() I hide It. But I want to hide it all together just want to use the pull to refresh functionality but without progress spinner.
After Done some RnD found a solution that may help others who want to achieve such functionality
try {
Field f = mSwipeRefreshLayout.getClass().getDeclaredField("mCircleView");
f.setAccessible(true);
ImageView img = (ImageView)f.get(mSwipeRefreshLayout);
img.setAlpha(0.0f);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
swipeRefreshLayout.setProgressViewEndTarget(false, 0)
You can send loader away
yourSwipeToRefresh?.setProgressViewEndTarget(true, -screenHeight)
I don't agree with hiding this spinner completely. but untill the loading starts then hiding it.
I have in my layout many in-progress UIs ..
so what I have done:
app:refreshing="#{vm.loadingUI1 && vm.loadingUI2 && false}">
loadingUI1 is LiveData
With this approach you show interactive loading as action only and it will be hidden immediately when loading-process starts with any component
In addition to Atakan Akar's answer and to enable future swipe refreshing:
// Reduses spinner size to 0
swipeRefreshLayout.setProgressViewEndTarget(false, 0);
// Turns off layout refreshing status
swipeRefreshLayout.setRefreshing(false);
swipe_refresh_layout.setProgressViewOffset(
false,
-200,
-200
)

How to set conditional visibility in android

I want to set visibility of a imageview on the basis of a condition. how would i do it ??
here is my code :
if (web.canGoBack() != true) {
bc.setVisibility(View.INVISIBLE);
}else {
bc.setVisibility(View.VISIBLE);
}
here bc is the imageview over a webview which is in framelayout
What you are trying to do is fine.
But, if want it in 1 line then.
bc.setVisibility(web.canGoBack()? View.VISIBLE : View.INVISIBLE);
Try bc.setVisibility(View.GONE);
But the real issue may be your response from web.canGoBack(). Please do check that condition is working fine.

Android disable button with case differentiation

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

Categories

Resources