Lesson 4 Text and Scrolling Views
Lesson 4 Text and Scrolling Views
Lesson 4
Text and
Android
Android Developer
Developer Fundamentals
Fundamentals V2
V2 Commons Attribution 4.0 International
Creative Commons Attribution 4.0 International License
Scrolling Views 1
License.
Creative Commons Attribution 4.0 International License
Text and scrolling views
Text and
Android Developer Fundamentals V2 Commons Attribution 4.0 International
Creative Commons Attribution 4.0 International License
Scrolling Views 2
License.
Creative Commons Attribution 4.0 International License
Contents
● TextView
● ScrollView
Text and
Android Developer Fundamentals V2 Commons Attribution 4.0 International
Creative Commons Attribution 4.0 International License
Scrolling Views 5
License.
Creative Commons Attribution 4.0 International License
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 (\)
This work is licensed under a Creative
Creative Commons Attribution 4.0 International License
Text and
Android Developer Fundamentals V2 Commons Attribution 4.0 International
Creative Commons Attribution 4.0 International License
Scrolling Views 6
License.
Creative Commons Attribution 4.0 International License
Creating TextView in XML
<TextView android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/my_story"/>
Text and
Android Developer Fundamentals V2 Commons Attribution 4.0 International
Creative Commons Attribution 4.0 International License
Scrolling Views 7
License.
Creative Commons Attribution 4.0 International License
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
This work is licensed under a Creative
Creative Commons Attribution 4.0 International License
Text and
Android Developer Fundamentals V2 Commons Attribution 4.0 International
Creative Commons Attribution 4.0 International License
Scrolling Views 8
License.
Creative Commons Attribution 4.0 International License
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"/> Don’t use HTML
for a web link in
free-form text
autoLink values:"web", "email", "phone", "map", "all"
This work is licensed under a Creative
Creative Commons Attribution 4.0 International License
Text and
Android Developer Fundamentals V2 Commons Attribution 4.0 International
Creative Commons Attribution 4.0 International License
Scrolling Views 9
License.
Creative Commons Attribution 4.0 International License
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);
Text and
Android Developer Fundamentals V2 Commons Attribution 4.0 International
Creative Commons Attribution 4.0 International License
Scrolling Views 10
License.
Creative Commons Attribution 4.0 International License
ScrollView
Text and
Android Developer Fundamentals V2 Commons Attribution 4.0 International
Creative Commons Attribution 4.0 International License
Scrolling Views 12
License.
Creative Commons Attribution 4.0 International License
ScrollView for scrolling content
● ScrollView is a subclass of FrameLayout
● 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
Text and
Android Developer Fundamentals V2 Commons Attribution 4.0 International
Creative Commons Attribution 4.0 International License
Scrolling Views 13
License.
Creative Commons Attribution 4.0 International License
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>
This work is licensed under a Creative
Creative Commons Attribution 4.0 International License
Text and
Android Developer Fundamentals V2 Commons Attribution 4.0 International
Creative Commons Attribution 4.0 International License
Scrolling Views 14
License.
Creative Commons Attribution 4.0 International License
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>
Text and
Android Developer Fundamentals V2 Commons Attribution 4.0 International
Creative Commons Attribution 4.0 International License
Scrolling Views 15
License.
Creative Commons Attribution 4.0 International License
ScrollView with image and button
<ScrollView...>
One child of ScrollView
<LinearLayout...>
which can be a layout
<ImageView.../>
<Button.../> Children of the layout
<TextView.../>
</LinearLayout>
</ScrollView>
This work is licensed under a Creative
Creative Commons Attribution 4.0 International License
Text and
Android Developer Fundamentals V2 Commons Attribution 4.0 International
Creative Commons Attribution 4.0 International License
Scrolling Views 16
License.
Creative Commons Attribution 4.0 International License
END