Correct Way To Hide And Show Widgets?
In my app, I need to show and hide widgets like button and textview at a certain time. and how I am doing is as the following: private void hideviews() { image.setVisibility(Vi
Solution 1:
Since you don't know how many testviews will be attached, then I believe that the best approach will be to:
- get the reference of the parent view group (that contains all the textviews),
- loop through all the childs using
getChildAt
, - verify whether the object is an instance of TextView/ImageView and if so set its visibility according to your logic
Solution 2:
Instead of hiding every widget separately hide the root layout.
RelativeLayout rootLayout;
rootLayout= (RelativeLayout) findViewById(R.id.root_layout);
and use something like this to control the visibility.
publicvoidsetLayoutInvisible() {
if (rootLayout.getVisibility() == View.VISIBLE) {
rootLayout.setVisibility(View.GONE);
}
}
publicvoidsetLayoutVisible() {
if (rootLayout.getVisibility() == View.GONE) {
rootLayout.setVisibility(View.VISIBLE);
}
}
Solution 3:
Get the reference to root layout, iterate through the childs, check if the view at certain index is instance of EditText(or View that you dont need to hide), if not hide it
RelativeLayoutroot= findViewById(R.id.root)
for(i=0,i<root.getChildCount()-1,i++){
if(! (root.getChildAt(i) instance of EditText)){
root.getChildAt(i).setVisibility(View.GONE)
}
}
Post a Comment for "Correct Way To Hide And Show Widgets?"