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.
Related
I'm trying to make visibility changes for a view under MotionLayout using this answer https://stackoverflow.com/a/62658424/5412554
but for me, it's not working under observe. For eg:
viewModel.messageLinkedList.observe(viewLifecycleOwner) {
binding.motionLayout.getConstraintSet(R.id.start).getConstraint(binding.deleteAllText.id).propertySet.mVisibilityMode = 1; // 1 - ignore or 0 - normal
binding.deleteAllText.visibility = View.GONE
}
If I use simply in onCreateView of fragment it works.
For eg:
binding.motionLayout.getConstraintSet(R.id.start).getConstraint(binding.deleteAllText.id).propertySet.mVisibilityMode = 1; // 1 - ignore or 0 - normal
binding.deleteAllText.visibility = View.GONE
Please help me with the correct solution.
I don't know it works with data-binding but I found a Solution for Kotlin, maybe you can something figure out:
motion_layout_id.getConstraintSet(R.id.start)?.let {
it.setVisibility(R.id.deleteAllText, View.GONE)
motion_layout_main.requestLayout() // this must be done to apply the visibility change
}
I have 30 buttons in a page for each time slot like 9AM, 10AM ... for 3 days. I have to enable or disable the button based on the availability. Is there any easier way to enable or disable buttons at a time or i just have to follow the same method of using the button.setVisibility(view.GONE) for each button?
After initializing your 30 buttons, u can collect them in a List<Button>. For changing their visibility in bulk, u can just iterate through your list in a for loop and change their visibility. Something like below:
List<Button> buttons = new ArrayList<>();
buttons.add(button1);
buttons.add(button2);
........
private void setButtonsVisibility(boolean isVisible) {
if (buttons != null) {
for (Button button : buttons) {
button.setVisibility(isVisible ? View.VISIBLE : View.GONE);
}
}
}
You can try the following methods
If the buttons have same parent layout you can set the visibility to View.VISIBLE or View.GONE of the parent layout and it will be reflected to the button too.
If not than you have to set the visibility of each button or set the enabled property to true or false according to the condition.
Hope this works.
I'm using LayoutAnimation for animating my <View>. I toggle it with:
LayoutAnimation.configureNext(LayoutAnimation.Presets.spring);
this.showView = !this.showView;
but the animation is not working at the moment of create, only when the View disappears.
(iOS works perfect)
You need to add this code segment for Android.
import { UIManager, LayoutAnimation } from 'react-native';
...
if (Platform.OS === "android") {
UIManager.setLayoutAnimationEnabledExperimental &&
UIManager.setLayoutAnimationEnabledExperimental(true);
}
It was probably something wrong with the loading of text that was displayed in my View. Now I'm loading the text from the JSON file and it works.
I'm writing test to my project with Robolectric-2.3.
I'd like to test my UI properties such as Views visibility. The actions of showing/hiding views are wrapped into Animations. How to test it ?
I tried to use ShadowSystemClock.sleep() method to wait until animation ends but it doesn't seem to work as I expected.
#Test
public void testHideSearch() throws Exception {
mListFragment.hideSearch(); //<--- animation launched here
sleep(1000);
View searchEditText = mListFragment.getView().findViewById(R.id.filterEditText);
assertFalse(searchEditText.getVisibility() == View.VISIBLE);
}
What is the correct approach to the issue ?
Try using this instead of sleep:
Robolectric.getUiThreadScheduler.advanceBy(1000);
If you use Animator's your approach should work, but if you use Animations, you'll need to use ShadowAnimation instead. Assuming the actual visibility of your view is changed in onAnimationEnd(), something like the following should work:
ShadowAnimation animation = Robolectric.shadowOf(searchEditText.getAnimation());
animation.invokeEnd();
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