want to make some views invisible during runtime in android application
this is my main .xml file :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dip"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/mbackground1"
android:gravity="center_horizontal"
android:text="@string/decode_label"
android:padding="5dip"
/>
<TextView
android:id="@+id/mytext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:background="@color/mbackground2"
android:textColor="@color/mytextcolor"
android:padding="5dip"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/continue_label"
android:gravity="center_horizontal"
android:textColor="@color/mytextcolor"
android:padding="5dip"
/>
<Button
android:id="@+id/webbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/web_button"
android:textColor="@color/mytextcolor"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/continue_label1"
android:gravity="center_horizontal"
android:textColor="@color/mytextcolor"
android:padding="5dip"
/>
<Button
android:id="@+id/callbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/call_button"
android:textColor="@color/mytextcolor"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/continue_label2"
a开发者_如何转开发ndroid:gravity="center_horizontal"
android:textColor="@color/mytextcolor"
android:padding="5dip"
/>
<Button
android:id="@+id/emailbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/sendemail_button"
android:textColor="@color/mytextcolor"
/>
</LinearLayout>
i want that based on output at runtime it should show only one textview and button corresponding to that output. im defining layout in main.xml file and also i am ew in this field.
does any one have any idea. thanks in advance
I assume you know how to get a reference to the views you defined, for example:
Button button = (Button)findViewById(R.id.emailbutton)
You will need to define an id to each and every view you want to use in the code, just like you did to the emailbutton:
android:id="@+id/emailbutton"
In order to set the visibility of a view you call:
button.setVisibility(View.GONE);
you have the option to set the visibility to INVISIBLE
and VISIBLE
.
Then you can play with the visibility as you like.
The differnece between INVISIBLE
and GONE
is that GONE
removes the view completely from the layout while INVISIBLE
"saves" the space this view takes.
You can see that in the API examples.
To remove yourview in java code:
Button btn=(Button)findViewById(R.id.btn);
btn.setVisibility(View.GONE);
To transparent yourview in java code:
Button btn=(Button)findViewById(R.id.btn);
btn.setVisibility(View.INVISIBLE);
To remove yourview in Xml file:
<yourView
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
To transparent button in Xml file:
<yourView
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"/>
to make a view visible or invisible try this:
yourView.setVisibility(View.GONE);
yourView.setVisibility(View.VISIBLE);
Use textView.setVisibility(View.GONE); - to make View Gone and textView.setVisibility(View.INVISIBLE); - to make view INVISIBLE
The view's visibility can be changed using the View.setVisibility()
method, check this link for more info. Hope this helps.
Get the View by ID and make it invisible. For your "mytext" TextView for example:
TextView my = (TextView) findViewById(R.id.mytext); // Get the view you want to manipulate
my.setVisibility(View.INVISIBLE); // Make it invisible
my.setVisibility(View.VISIBLE); // Make it visible again
Always check the documentation first!
精彩评论