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"
android: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
A View's visibility status is of Integer type and can have one of three options: VISIBLE (0) - The View is visible to the user. INVISIBLE (4) - The View is invisible to the user, but still takes up space in the layout. GONE (8) - The View is invisible, and it does not take up space in the layout.
View is a basic building block of UI (User Interface) in android. A view is a small rectangular box that responds to user inputs. Eg: EditText, Button, CheckBox, etc. ViewGroup is an invisible container of other views (child views) and other ViewGroup.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With