1. Are you ready for the Galaxy S20? Here is everything we know so far!

How to create a TextView of set size with scroll bars when needed?

Discussion in 'Android Development' started by UmBottesWillen, Jul 31, 2019.

  1. UmBottesWillen

    Thread Starter

    Hello, I want to create a TextView of a set size inside a horizontal linear layout, that is scrollable when the content gets too long for the TextView to properly display. Here's the XML structure I am currently using:
    Code (Text):
    1.     <LinearLayout
    2.         android:id="@+id/item_bottom
    3.  
    4.         android:layout_width="match_parent"
    5.  
    6.  
    7.         android:layout_height="match_parent"
    8.  
    9.  
    10.         android:orientation="horizontal"
    11.  
    12.  
    13.         android:weightSum="8">
    14.  
    15.  
    16.  
    17.  
    18.  
    19.         <HorizontalScrollView
    20.  
    21.  
    22.             android:layout_width="0dp"
    23.  
    24.  
    25.             android:layout_height="match_parent"
    26.  
    27.  
    28.             android:layout_weight="5"
    29.  
    30.  
    31.  
    32.  
    33.  
    34.             android:clipToPadding="true"
    35.  
    36.  
    37.             android:fadeScrollbars="true"
    38.  
    39.  
    40.             android:scrollbarStyle="insideOverlay"
    41.  
    42.  
    43.             android:scrollbars="horizontal">
    44.  
    45.  
    46.  
    47.  
    48.  
    49.             <TextView
    50.  
    51.  
    52.                 android:id="@+id/item_balance"
    53.  
    54.  
    55.                 android:layout_width="wrap_content"
    56.  
    57.  
    58.                 android:layout_height="match_parent"
    59.  
    60.  
    61.                 android:fontFamily="sans-serif-black"
    62.  
    63.  
    64.                 android:singleLine="true"
    65.  
    66.  
    67.                 android:text="26677889.44 €"
    68.  
    69.  
    70.                 android:textColor="@android:color/black"
    71.  
    72.  
    73.                 android:textSize="36sp"
    74.  
    75.  
    76.                 android:textStyle="bold" />
    77.  
    78.  
    79.         </HorizontalScrollView>
    80.  
    81.  
    82.  
    83.  
    84.  
    85.         <HorizontalScrollView
    86.  
    87.  
    88.             android:layout_width="0dp"
    89.  
    90.  
    91.             android:layout_height="match_parent"
    92.  
    93.  
    94.             android:layout_weight="3"
    95.  
    96.  
    97.  
    98.  
    99.  
    100.             android:clipToPadding="true"
    101.  
    102.  
    103.             android:fadeScrollbars="true"
    104.  
    105.  
    106.             android:scrollbarStyle="insideOverlay"
    107.  
    108.  
    109.             android:scrollbars="horizontal">
    110.  
    111.  
    112.  
    113.  
    114.  
    115.             <TextView
    116.  
    117.  
    118.                 android:id="@+id/item_payment"
    119.  
    120.  
    121.                 android:layout_width="wrap_content"
    122.  
    123.  
    124.                 android:layout_height="match_parent"
    125.  
    126.  
    127.                 android:background="@color/lightred"
    128.  
    129.  
    130.                 android:gravity="bottom|end"
    131.  
    132.  
    133.                 android:singleLine="true"
    134.  
    135.  
    136.                 android:text="22222"
    137.  
    138.  
    139.                 android:textColor="@android:color/black"
    140.  
    141.  
    142.                 android:textSize="24sp" />
    143.  
    144.  
    145.         </HorizontalScrollView>
    146.  
    147.  
    148.  
    149.  
    150.  
    151.     </LinearLayout>
    This however leads to the TextView elements inside the HorizontalScrollView elements to have their width be set to wrap_content. Manually changing this to match_parent or adding android:fillViewport="true" to the HorizontalScrollView elements did, unlike with normal ScrollView elements, not help.

    This is what it looks like in the design view:
    androidstudio.jpg

    The HorizontalScrollView elements already have the correct size, the only issue is that the TextView elements inside of them do not allow their width to be set to match_parent. Is there something I missed?
     


  2. Best Answer:
    Post #2 by UmBottesWillen, Aug 1, 2019 (1 points)
  3. UmBottesWillen

    Thread Starter

    I have found a solution for my specific problem. Instead of using a TextView object inside a HorizontalSrollView object to allow me to scroll the TextView sideways, I'm using an EditText object with all of its additional properties removed, save for its scrolling feature:
    Code (Text):
    1. <LinearLayout
    2.             android:id="@+id/item_bottom"
    3.             android:layout_width="match_parent"
    4.             android:layout_height="wrap_content"
    5.             android:orientation="horizontal"
    6.             android:weightSum="8">
    7.  
    8.             <EditText
    9.                 android:id="@+id/item_balance"
    10.                 android:layout_width="0dp"
    11.                 android:layout_height="wrap_content"
    12.                 android:layout_weight="5"
    13.                 android:background="@android:color/transparent"
    14.                 android:clickable="false"
    15.                 android:cursorVisible="false"
    16.                 android:focusable="false"
    17.                 android:fontFamily="sans-serif-black"
    18.                 android:singleLine="true"
    19.                 android:text="26677889.44 €"
    20.                 android:textColor="@android:color/black"
    21.                 android:textSize="36sp"
    22.                 android:textStyle="bold" />
     
Loading...
Similar Threads - create TextView set
  1. Kayem59
    Replies:
    4
    Views:
    263
  2. sersaber
    Replies:
    1
    Views:
    217
  3. MoodyBlues
    Replies:
    8
    Views:
    1,057
  4. harrytapp
    Replies:
    0
    Views:
    157
  5. mdsousa
    Replies:
    0
    Views:
    476
  6. mdsousa
    Replies:
    3
    Views:
    421
  7. Stokestack
    Replies:
    0
    Views:
    610
  8. TheOuz
    Replies:
    0
    Views:
    573
  9. raccoon95
    Replies:
    0
    Views:
    456
  10. monInspiration
    Replies:
    0
    Views:
    577

Share This Page

Loading...