6

I want to show just two TextViews on the screen, one on top of the other.

TextView B: 400dp high, at the bottom, TextView A: fill the rest of the screen.

However, if the height of TextView A is less than 100dp, it shouldn't be displayed (only TextView A visible, the rest is just white space).

Can this be achieved just by XML?

Currently I'm using something like this:

<?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:background="@color/white"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/blue"
        android:text="Image A"
        android:gravity="center"
        android:textSize="40sp" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="400dp"
        android:background="@color/green"
        android:text="Image B: 400dp"
        android:gravity="center"        
        android:textSize="40sp" />

</LinearLayout>
Luuklag
  • 3,897
  • 11
  • 38
  • 57
Hello_World
  • 387
  • 3
  • 13
  • 1
    in code you can do if textviewb.getHeight < 100 then textviewb.setVisibility(View.Gone); – Marko Niciforovic Apr 24 '13 at 14:58
  • 1
    How can your view B be less than 100dp, if you want to set it to 400dp and fill the rest of the space with View A? A typo maybe? And you will have to do some check in the code to achieve this – Alex Orlov Apr 24 '13 at 15:01
  • I think you want `android:maxHeight="400dp"` instead of `android:layout_height="400dp"` becuase that will make your TExtView ALWAYS 400dp and so you will never have less than 100. Then fill the TextView programatically and then follow MarkoNiciforovic's suggestion. – TronicZomB Apr 24 '13 at 15:13
  • The second TextView should have "layout_weight="0"", I think. – Christine Apr 24 '13 at 15:44
  • @Alex Orlov: Yes, it's a typo, sorry. I mean "if the height of TextView A is less than 100dp, it shouldn't be displayed". – Hello_World Apr 24 '13 at 18:14

1 Answers1

0

If you were going to use this feature in several other layouts, I'd consider building it into a custom view, where you can add an extra attribute customer:minSize=100dp. In that custom view, you could probably then add Marko's suggestion (i.e. textview.getHeight > customer:minSize then textview.setVisibility(View.Gone/Invisible)

http://developer.android.com/training/custom-views/index.html http://developer.android.com/guide/topics/ui/custom-components.html ... http://mobile.tutsplus.com/tutorials/android/android-sdk-creating-custom-views-2/

Otherwise, I'd just implement it in your activity.

Evan Anger
  • 712
  • 1
  • 5
  • 24