Want to make some types invisible at run time in an Android app

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 the output at runtime, it should show only one text view and a button corresponding to that output. im defines the layout in the main.xml file, and also am am ew in this field.

Does anyone have any ideas. thanks in advance

+6
source share
6 answers

I assume that you know how to get a link to the views you define, for example:

 Button button = (Button)findViewById(R.id.emailbutton) 

You will need to define an identifier for each view that you want to use in the code, just as you did with the email button:

 android:id="@+id/emailbutton" 

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 visibility as you like. The difference between INVISIBLE and GONE is that GONE completely removes the view from the layout, and INVISIBLE "saves" the space that this view occupies.

You can see this in the API examples.

+11
source

To remove your code in Java code:

 Button btn=(Button)findViewById(R.id.btn); btn.setVisibility(View.GONE); 

For a transparent representation in Java code:

 Button btn=(Button)findViewById(R.id.btn); btn.setVisibility(View.INVISIBLE); 

To remove your view in an Xml file:

 <yourView android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone"/> 

To the transparent button in the Xml file:

 <yourView android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="invisible"/> 
+6
source

To make the view visible or invisible:

 yourView.setVisibility(View.GONE); yourView.setVisibility(View.VISIBLE); 
+1
source

Use textView.setVisibility (View.GONE); - make View Gone and textView.setVisibility (View.INVISIBLE); - make view INVIVIBLE

+1
source

View visibility can be changed using the View.setVisibility() method, check this link for more information. Hope this helps.

0
source

Get the view by identifier and make it invisible. For example, for your text TextView:

 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!

0
source

Source: https://habr.com/ru/post/895656/


All Articles