I want my DatePicker
and the button to be invisible in the begining. And when I press my magic button I want to setVisibility(View.Visible);
The problem
You can think it as a CSS style visibility & display.
<div style="visibility:visible; display:block">
This is View.VISIBLE : Content is displayed normally.
</div>
<div style="visibility:hidden; display:block">
This is View.INVISIBLE : Content is not displayed, but div still takes up place, but empty.
</div>
<div style="display:none">
This is View.GONE : Container div is not shown, you can say the content is not displayed.
</div>
View.GONE makes the view invisible without the view taking up space in the layout. View.INVISIBLE makes the view just invisible still taking up space.
You are first using GONE and then INVISIBLE on the same view.Since, the code is executed sequentially, first the view becomes GONE then it is overridden by the INVISIBLE type still taking up space.
You should add button listener on the button and inside the onClick() method make the views visible. This should be the logic according to me in your onCreate() method.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
final DatePicker dp2 = (DatePicker) findViewById(R.id.datePick2);
final Button btn2 = (Button) findViewById(R.id.btnDate2);
final Button btn3 = (Button) findViewById(R.id.btnVisibility);
dp2.setVisibility(View.INVISIBLE);
btn2.setVisibility(View.INVISIBLE);
bt3.setOnClickListener(new View.OnCLickListener(){
@Override
public void onClick(View view)
{
dp2.setVisibility(View.VISIBLE);
bt2.setVisibility(View.VISIBLE);
}
});
}
I think this should work easily. Hope this helps.
In my case I found that simply clearing the animation on the view before setting the visibility to GONE
works.
dp2.clearAnimation();
dp2.setVisibility(View.GONE);
I had a similar issue where I toggle between two views, one of which must always start off as GONE
- But when I displayed the views again, it was displaying over the first view even if setVisibility(GONE)
was called. Clearing the animation before setting the view to GONE
worked.
Today I had a scenario, where I was performing following:
myViewGroup.setVisibility(View.GONE);
Right on the next frame I was performing an if
check somewhere else for visibility state of that view. Guess what? The following condition was passing:
if(myViewGroup.getVisibility() == View.VISIBLE) {
// this `if` was fulfilled magically
}
Placing breakpoints you can see, that visibility changes to GONE
, but right on the next frame it magically becomes VISIBLE
. I was trying to understand how the hell this could happen.
Turns out there was an animation applied to this view, which internally caused the view to change it's visibility to VISIBLE
until finishing the animation:
public void someFunction() {
...
TransitionManager.beginDelayedTransition(myViewGroup);
...
myViewGroup.setVisibility(View.GONE);
}
If you debug, you'll see that myViewGroup
indeed changes its visibility to GONE
, but right on the next frame it would again become visible in order to run the animation.
So, if you come across with such a situation, make sure you are not performing an if
check in amidst of animating the view.
You can remove all animations on the view via View.clearAnimation().
Because you set visibility either true or false.
try that
setVisible(0)
to visible true .
and setVisible(4)
to visible false.