Reputation: 2927
I developed one Simple app, which contains one textview.and my problem is that I want visible in invisible this text view on button click event.
At loadtime I do this
myTextView.setVisible(View.GONE);
and after this at Button Click event, I do this.
myTextView.setVisible(View.VISIBLE);
The textview is visible but it is overlap on below TextView means myTextView cannot contain space. So what can I do now?
Upvotes: 1
Views: 20756
Reputation: 1
This will show and hide the text(s) when the corresponding buttons are pressed. Very useful if you want to control multiple text with buttons
1)XML File:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="vertical"
android:gravity="center_vertical">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:text="The Origin"
android:id="@+id/btnOrigin"
android:onClick="buttonOnClick"/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:id="@+id/txtOrigin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/about"
android:textColor="#000"
android:textSize="@dimen/text_body"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:visibility="gone"/>
</TableRow>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="vertical"
android:gravity="center_vertical">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:text="Vision"
android:id="@+id/btnVision"
android:onClick="buttonOnClick"/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:id="@+id/txtVision"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/vision"
android:textColor="#000"
android:textSize="@dimen/text_body"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:visibility="gone"/>
</TableRow>
</LinearLayout>
2) JAVA code
private TextView txtOrigin, txtVision;
public void buttonOnClick(View view) {
switch (view.getId()) {
case R.id.btnOrigin:
txtOrigin = (TextView) findViewById(R.id.txtOrigin);
txtOrigin.setVisibility(View.VISIBLE);
txtVision = (TextView) findViewById(R.id.txtVision);
txtVision.setVisibility(View.INVISIBLE);
break;
case R.id.btnVision:
txtVision = (TextView) findViewById(R.id.txtVision);
txtVision.setVisibility(View.VISIBLE);
txtOrigin = (TextView) findViewById(R.id.txtOrigin);
txtOrigin.setVisibility(View.INVISIBLE);
break;
}
}
Upvotes: 0
Reputation: 16354
A slight change in your code should make it work fine I believe.
myTextView.setVisibility(View.INVISIBLE);
and after the button click,
myTextView.setVisibility(View.VISIBLE);
For doing it in the java code, have a go at this(haven't tried it out myself though)...
RelativeLayout rl = new RelativeLayout(this);
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.xxx);
TextView txt = new TextView(this);
txt.setText("XXX");
rl.addView(iv,0);
rl.addView(txt,1);
Upvotes: 0
Reputation: 5373
try this:
RelativeLayout layout = new RelativeLayout(this);
TextView tv1 = new TextView(this);
tv1.setText("A");
tv1.setId(1);
TextView tv2 = new TextView(this);
tv2.setText("B");
tv2.setId(2);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.BELOW, tv1.getId());
layout.addView(tv1);
layout.addView(tv2, lp);
and when you change visibility, call invalidate()
on parent view of myTextView (layout here) or root view of your layout ( layout.getRootView()
)
Upvotes: 0
Reputation: 3288
If you are using relativelayout, specify android:layout_below="id_of_above_text_view"
in the second textview.
If you dont specify the relation to the views in relative layout, it will appear one above other
Upvotes: 0
Reputation: 15701
use
myTextView.setVisible(View.INVISIBLE);
instead of
myTextView.setVisible(View.GONE);
to persist the space in layout.........
Upvotes: 5