android - setVisibility(View.VISIBLE) doesn't show TextView -


i have following layout in res/layout/main.xml second textview hidden

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"               android:layout_width="match_parent"               android:layout_height="match_parent">     <textview android:id="@+id/tv1"               android:layout_width="match_parent"               android:layout_height="wrap_content"               android:text="hello"/>     <textview android:id="@+id/tv2"               android:layout_width="match_parent"               android:layout_height="wrap_content"               android:text="world"               android:visibility="gone"/> </linearlayout> 

and android activity, should display second textview

public class mainactivity extends activity {     @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.main);         view tv2 = findviewbyid(r.id.tv2);         tv2.setvisibility(view.visible);     } } 

but when activity displayed, first textview shown.

so, wrong here? how can make second textview visible programmatically?

although solution embarrassingly simple, took me quite time figure out.

the reason "invisible" element default orientation of linearlayout. right @ beginning says

class overview
... default orientation horizontal.

both textviews have width of match_parent, means first textview occupies whole width of parent. since forgot set orientation explicitly, second textview layed out right , off screen.

setting orientation vertical fixes issue , makes second textview visible

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"               android:layout_width="match_parent"               android:layout_height="match_parent"               android:orientation="vertical">     <!-- ... --> </linearlayout> 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -