0% found this document useful (0 votes)
111 views9 pages

Scroll and Text View Rangkum

This document discusses TextView and ScrollView in Android. It provides details on: - Using TextView to display single and multi-line text, setting text statically from XML or dynamically from code. - Formatting text using HTML tags and special characters. - Embedding a TextView in a ScrollView to allow scrolling of large amounts of text. - ScrollView can only hold one direct child but that child can be a layout containing multiple views.

Uploaded by

putri ika
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
111 views9 pages

Scroll and Text View Rangkum

This document discusses TextView and ScrollView in Android. It provides details on: - Using TextView to display single and multi-line text, setting text statically from XML or dynamically from code. - Formatting text using HTML tags and special characters. - Embedding a TextView in a ScrollView to allow scrolling of large amounts of text. - ScrollView can only hold one direct child but that child can be a layout containing multiple views.

Uploaded by

putri ika
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 9

1.

3 Text and Scrolling Views


TextView
• TextView for text
● TextView is a view for displaying single and multi-line text
● EditText is a subclass of TextView with editable text
● Controlled with layout attributes
● Set text statically from a string resource in XML, or dynamically
from Java code and any source
• Formatting text in string resource
● Use <b> and <i> HTML tags for bold and italics
● All other HTML tags are ignored
● String resources: one unbroken line = one paragraph
● \n starts a new a line or paragraph
● Escape apostrophes and quotes with backslash (\", \')
● Escape any non-ASCII characters with backslash (\)
Creating TextView in XML
<TextView android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/my_story"/>

Common TextView attributes

android:text—text to display
android:textColor—color of text
android:textAppearance—predefined style or theme
android:textSize—text size in sp
android:textStyle—normal, bold, italic, or bold|italic
android:typeface—normal, sans, serif, or monospace
android:lineSpacingExtra—extra space between lines in sp
Formatting active web links
<string name="article_text">... www.rockument.com ...</string>

<TextView
android:id="@+id/article"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:text="@string/article_text"/>

autoLink values:"web", "email", "phone", "map", "all"

Don’t use HTML for a web link in free-form text


Creating TextView in Java code
TextView myTextview = new TextView(this);
myTextView.setWidth(LayoutParams.MATCH_PARENT);
myTextView.setHeight(LayoutParams.WRAP_CONTENT);
myTextView.setMinLines(3);
myTextView.setText(R.string.my_story);
myTextView.append(userComment);
ScrollView
• What about large amounts of text?
● The user may need to scroll.
○ News stories, articles, ...
● To allow users to scroll a TextView, embed it in a ScrollView.
● Other Views can be embedded in a ScrollView.
○ LinearLayout, TextView, Button, ...

• ScrollView for scrolling content


● ScrollView is a subclass of FrameLayout
● Can only hold one view (which can be a ViewGroup)
● Holds all content in memory
● Not good for long texts, complex layouts
● Do not nest multiple scrolling views
● Use HorizontalScrollView for horizontal scrolling
● Use a RecyclerView for lists
ScrollView layout with one TextView
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/article_subheading">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
.../>
</ScrollView>
ScrollView layout with a view group
<ScrollView ...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/article_subheading"
.../>

<TextView
android:id="@+id/article" ... />
</LinearLayout>
</ScrollView>
ScrollView with image and button
<ScrollView...>
<LinearLayout...> One child of ScrollView
which can be a layout
<ImageView.../>
<Button.../> Children of the layout

<TextView.../>
</LinearLayout>
</ScrollView>

You might also like