README.md
1 Drawing a rounded corner background on text
2 ============
3
4 This sample shows how to draw a **rounded** corner background on text. It supports the following cases:
5
6 * Set the background on **one line** text
7
8 <img src="../screenshots/single.png" width="30%" />
9
10 * Set the background on text over **two or multiple lines**
11
12 <img src="../screenshots/multi.png" width="30%" />
13
14 * Set the background on **right-to-left** text
15
16 <img src="../screenshots/rtl.png" width="30%" />
17
18
19 Implementation
20 ---------------
21 Depending on the position of the text, we need to draw four different drawables as text backgrounds:
22
23 * Text fits on one line: we only need one drawable
24 * Text fits on 2 lines: we need drawables for the start and end of the text
25 * Text spans multiple lines: we need drawables for the start, middle and end of the text
26
27 <img src="../screenshots/lines.png" width="30%" />
28
29 The four drawables that need to be drawn depending on the position of the text:
30
31 To position the background, we need to:
32 * Determine whether the text spans multiple lines
33 * Find the start and end lines
34 * Find the start and end offset depending on the paragraph direction
35
36 All of these can be computed based on the text Layout. To render the background behind the text we need access to the Canvas. A custom TextView has access to all of the information necessary to position the drawables and render them.
37
38 Our solution involves splitting our problem into 4 parts and creating classes dealing with them individually:
39 * **Marking the position of the background** is done in the XML resources via Annotation spans and then, in the code, we read them in the `TextRoundedBgHelper`
40 * Providing the background **drawables as attributes** of the TextView - implemented in `TextRoundedBgAttributeReader`
41 * **Rendering the drawables** depending on whether the text runs across **one or multiple lines** - `TextRoundedBgHelper` interface and its implementations: `SingleLineRenderer` and `MultiLineRenderer`
42 * Supporting **custom drawing** on a TextView - `RoundedBgTextView`, a class that extends `AppCompatTextView`, reads the attributes with the help of `TextRoundedBgAttributeReader`, overrides `onDraw` where it uses `TextRoundedBgHelper` to draw the background.
43
44 Getting Started
45 ---------------
46
47 Clone this repository, enter the top level directory and run `./gradlew tasks`
48 to get an overview of all the tasks available for this project.
49
50 Some important tasks are:
51
52 ```
53 assembleDebug - Assembles all Debug builds.
54 installDebug - Installs the Debug build.
55 connectedAndroidTest - Installs and runs the tests for Debug build on connected
56 devices.
57 test - Run all unit tests.
58 ```
59
60 Screenshots
61 -----------
62 <img src="../screenshots/rounded_bg.png" width="30%" />
63
64 Support
65 -------
66 - Stack Overflow: http://stackoverflow.com/questions/tagged/android-text
67
68 If you've found an error in this sample, please file an issue:
69 https://github.com/googlesamples/android-text/issues
70
71 Patches are encouraged, and may be submitted by forking this project and
72 submitting a pull request through GitHub.
73
74 License
75 --------
76 ```
77 Copyright 2018 The Android Open Source Project
78
79 Licensed to the Apache Software Foundation (ASF) under one or more contributor
80 license agreements. See the NOTICE file distributed with this work for
81 additional information regarding copyright ownership. The ASF licenses this
82 file to you under the Apache License, Version 2.0 (the "License"); you may not
83 use this file except in compliance with the License. You may obtain a copy of
84 the License at
85
86 http://www.apache.org/licenses/LICENSE-2.0
87
88 Unless required by applicable law or agreed to in writing, software
89 distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
90 WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
91 License for the specific language governing permissions and limitations under
92 the License.
93 ```
94