Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
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

like image 601
Ritu Avatar asked Aug 23 '11 07:08

Ritu


People also ask

How do you set view visibility?

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.

What is the use of view in Android?

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.


3 Answers

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.

like image 187
IncrediApp Avatar answered Nov 26 '22 06:11

IncrediApp


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"/>
like image 40
ghader Avatar answered Nov 26 '22 08:11

ghader


to make a view visible or invisible try this:

yourView.setVisibility(View.GONE);
yourView.setVisibility(View.VISIBLE);
like image 28
Vineet Shukla Avatar answered Nov 26 '22 08:11

Vineet Shukla