Reputation: 9439
My ListView item has two TextViews. I want to show one of them at a time using XML. I don't want to do this programmatically. How to do it?
For example: When TextView A has text, TextView B disappears. When TextView A is empty, TextView B appears.
My code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/groupNameTextView"
android:layout_width="wrap_content"
android:layout_height="50dip"
android:text="TextView"
android:textSize="20dip"
android:textColor="@color/ForestGreen"
/>
<TextView
android:id="@+id/topRatedPlaceNameTextView"
android:layout_width="wrap_content"
android:layout_height="50dip"
android:text="TextView"
android:textSize="20dip"
android:textColor="@color/Azure"
/>
</LinearLayout>
Upvotes: 0
Views: 113
Reputation: 10203
Will you ever have both of them shown with text at the same time ? If not, and if you don't have style difference (text size/color), you could just use a single text view ?
Else, if A and B have different size / color / font / whatever, and assuming they will never be having text at the same time, using wrap_content as you do should ensure that A gets a width of 0 when it has no text, and B will take all the space. Else, if B has no text, A will take as much space as needed by its content.
Upvotes: 1
Reputation: 6201
TextView A has a text then in TextView B set setVisibility(View.VISIBLE) or setVisibility(View.VGONE) and same for TextView B. you do this in your CustomAdapter Class getView Method.
Thanks
Upvotes: 1
Reputation: 42026
Sorry, you cant do that with xml, by Programmatically in your getView()
of List's Adapter check like
if(textview1.getText().toString.length()>0)
textview2.setVisibilty(View.GONE);
else
textview1.setVisibilty(View.GONE);
Upvotes: 4