• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..--

app/22-Nov-2023-729451

gradle/wrapper/22-Nov-2023-76

lib/22-Nov-2023-781376

README.mdD22-Nov-20233.8 KiB9466

build.gradleD22-Nov-20231.3 KiB4945

gradle.propertiesD22-Nov-2023726 1413

gradlewD22-Nov-20235.2 KiB173128

gradlew.batD22-Nov-20232.2 KiB8561

settings.gradleD22-Nov-202323 21

README.md

1Drawing a rounded corner background on text
2============
3
4This 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
19Implementation
20---------------
21Depending 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
29The four drawables that need to be drawn depending on the position of the text:
30
31To 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
36All 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
38Our 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
44Getting Started
45---------------
46
47Clone this repository, enter the top level directory and run `./gradlew tasks`
48to get an overview of all the tasks available for this project.
49
50Some important tasks are:
51
52```
53assembleDebug - Assembles all Debug builds.
54installDebug - Installs the Debug build.
55connectedAndroidTest - Installs and runs the tests for Debug build on connected
56devices.
57test - Run all unit tests.
58```
59
60Screenshots
61-----------
62<img src="../screenshots/rounded_bg.png" width="30%" />
63
64Support
65-------
66- Stack Overflow: http://stackoverflow.com/questions/tagged/android-text
67
68If you've found an error in this sample, please file an issue:
69https://github.com/googlesamples/android-text/issues
70
71Patches are encouraged, and may be submitted by forking this project and
72submitting a pull request through GitHub.
73
74License
75--------
76```
77Copyright 2018 The Android Open Source Project
78
79Licensed to the Apache Software Foundation (ASF) under one or more contributor
80license agreements. See the NOTICE file distributed with this work for
81additional information regarding copyright ownership. The ASF licenses this
82file to you under the Apache License, Version 2.0 (the "License"); you may not
83use this file except in compliance with the License. You may obtain a copy of
84the License at
85
86   http://www.apache.org/licenses/LICENSE-2.0
87
88Unless required by applicable law or agreed to in writing, software
89distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
90WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
91License for the specific language governing permissions and limitations under
92the License.
93```
94