I am playing around with background images of TextView objects in Android, and I am wondering about the strange behaviour with the height.
How come I can’t set the height of the TextView to 0 so it gets “invisible” and doesn’t use up any space anymore – when setting a background-image?
Here is some test-code that demonstrates the – for me – strange behaviour.
public class HelloAndroid extends Activity implements View.OnClickListener{
TextView btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btn = new TextView(this);
btn.setOnClickListener(this);
//Put in any drawable of you in here
btn.setBackgroundDrawable(getResources().getDrawable(R.drawable.restablebg5));
btn.setHeight(80);
LinearLayout v = new LinearLayout(this);
v.setOrientation(LinearLayout.VERTICAL);
v.addView(btn);
setContentView(v);
}
public void onClick(View arg0) {
btn.setHeight(0);
}
}
How can I set the height of the textview to 0?
Thanks
Do this instead:
btn.setVisibility(View.GONE);
you can also use:
btn.setVisibility(View.INVISIBLE);
the second option will maintain the space, but your item won’t be visible – the first option completely removes your item from the display (and doesn’t maintain the space).