diff options
Diffstat (limited to 'ui')
56 files changed, 1925 insertions, 38 deletions
diff --git a/ui/text/README.md b/ui/text/README.md index e6119549..8d08bc8b 100644 --- a/ui/text/README.md +++ b/ui/text/README.md @@ -1,47 +1,19 @@ -Text Styling -============ -These samples shows how to style text on Android using spans, in [Java](https://github.com/googlesamples/android-text/tree/master/TextStyling-Java) and in [Kotlin](https://github.com/googlesamples/android-text/tree/master/TextStyling-Kotlin). +Android Text Samples +==================== +These samples show how to work with text in Android. +Explore the samples +------------------- +The **TextStyling** [Java](https://github.com/googlesamples/android-text/tree/master/TextStyling-Java) and [Kotlin](https://github.com/googlesamples/android-text/tree/master/TextStyling-Kotlin) samples show how to style text using spans. -Introduction ------------- -The difference between [TextStyling-Java](https://github.com/googlesamples/android-text/tree/master/TextStyling-Java) and [TextStyling-Kotlin](https://github.com/googlesamples/android-text/tree/master/TextStyling-Kotlin) is only in the language. They have the same set of features, same class names and similar ways of testing the functionality. -## Features -Parse some hardcoded text and do the following: -* Paragraphs starting with “> ” are transformed into quotes. -* Text enclosed in “```” will be transformed into inline code block. -* Lines starting with “+ ” or “* ” will be transformed into bullet points. -To update the text, modify the value of `R.string.display_text`. -This project is not meant to fully cover the markdown capabilities and has several limitations; for example, quotes do not support nesting other elements. - -## Implementation -The text is parsed in the `Parser.parse` method and the spans are created in the `MarkdownBuilder.markdownToSpans` method. -To see how to apply a span, check out `MarkdownBuilder.buildCodeBlockSpan`. To see how to apply multiple spans on the same string, see `MarkdownBuilder.buildQuoteSpan`. For examples of creating custom spans, see `BulletPointSpan`, `CodeBlockSpan` or `FontSpan`. - -## Testing -Text parsing is tested with JUnit tests in `ParserTest`. Span building is tested via Android JUnit tests, in `MarkdownBuilderTest`. - - -Getting Started ---------------- - -Clone this repository, enter the top level directory and run `./gradlew tasks` -to get an overview of all the tasks available for this project. - -Some important tasks are: - -``` -assembleDebug - Assembles all Debug builds. -installDebug - Installs the Debug build. -connectedAndroidTest - Installs and runs the tests for Debug build on connected -devices. -test - Run all unit tests. -``` +The **RoundedBackground** sample shows how to draw a rounded corner background on a text that extends across one or multiple lines, supporting right-to-left text also. Screenshots ----------- <img src="screenshots/main_activity.png" width="30%" /> +<img src="screenshots/rounded_bg.png" width="30%" /> + Support ------- - Stack Overflow: http://stackoverflow.com/questions/tagged/android-text @@ -72,4 +44,3 @@ WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ``` - diff --git a/ui/text/RoundedBackground-Kotlin/README.md b/ui/text/RoundedBackground-Kotlin/README.md new file mode 100644 index 00000000..8bd96e79 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/README.md @@ -0,0 +1,93 @@ +Drawing a rounded corner background on text +============ + +This sample shows how to draw a **rounded** corner background on text. It supports the following cases: + +* Set the background on **one line** text + +<img src="../screenshots/single.png" width="30%" /> + +* Set the background on text over **two or multiple lines** + +<img src="../screenshots/multi.png" width="30%" /> + +* Set the background on **right-to-left** text + +<img src="../screenshots/rtl.png" width="30%" /> + + +Implementation +--------------- +Depending on the position of the text, we need to draw four different drawables as text backgrounds: + +* Text fits on one line: we only need one drawable +* Text fits on 2 lines: we need drawables for the start and end of the text +* Text spans multiple lines: we need drawables for the start, middle and end of the text + +<img src="../screenshots/lines.png" width="30%" /> + +The four drawables that need to be drawn depending on the position of the text: + +To position the background, we need to: +* Determine whether the text spans multiple lines +* Find the start and end lines +* Find the start and end offset depending on the paragraph direction + +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. + +Our solution involves splitting our problem into 4 parts and creating classes dealing with them individually: +* **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` +* Providing the background **drawables as attributes** of the TextView - implemented in `TextRoundedBgAttributeReader` +* **Rendering the drawables** depending on whether the text runs across **one or multiple lines** - `TextRoundedBgHelper` interface and its implementations: `SingleLineRenderer` and `MultiLineRenderer` +* 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. + +Getting Started +--------------- + +Clone this repository, enter the top level directory and run `./gradlew tasks` +to get an overview of all the tasks available for this project. + +Some important tasks are: + +``` +assembleDebug - Assembles all Debug builds. +installDebug - Installs the Debug build. +connectedAndroidTest - Installs and runs the tests for Debug build on connected +devices. +test - Run all unit tests. +``` + +Screenshots +----------- +<img src="../screenshots/rounded_bg.png" width="30%" /> + +Support +------- +- Stack Overflow: http://stackoverflow.com/questions/tagged/android-text + +If you've found an error in this sample, please file an issue: +https://github.com/googlesamples/android-text/issues + +Patches are encouraged, and may be submitted by forking this project and +submitting a pull request through GitHub. + +License +-------- +``` +Copyright 2018 The Android Open Source Project + +Licensed to the Apache Software Foundation (ASF) under one or more contributor +license agreements. See the NOTICE file distributed with this work for +additional information regarding copyright ownership. The ASF licenses this +file to you under the Apache License, Version 2.0 (the "License"); you may not +use this file except in compliance with the License. You may obtain a copy of +the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +License for the specific language governing permissions and limitations under +the License. +``` diff --git a/ui/text/RoundedBackground-Kotlin/app/build.gradle b/ui/text/RoundedBackground-Kotlin/app/build.gradle new file mode 100644 index 00000000..0b78ce1c --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/build.gradle @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +apply plugin: 'com.android.application' + +apply plugin: 'kotlin-android' + + +android { + compileSdkVersion rootProject.compileSdkVersion + + defaultConfig { + applicationId "com.android.example.text.styling.roundedbg" + minSdkVersion rootProject.minSdkVersion + targetSdkVersion rootProject.targetSdkVersion + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + implementation project(':lib') + implementation "androidx.appcompat:appcompat:$androidxVersion" + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" +}
\ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/AndroidManifest.xml b/ui/text/RoundedBackground-Kotlin/app/src/main/AndroidManifest.xml new file mode 100644 index 00000000..d7429d4b --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/AndroidManifest.xml @@ -0,0 +1,35 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright 2018 The Android Open Source Project + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.example.text.styling.roundedbg.app"> + + <application + android:allowBackup="true" + android:icon="@mipmap/ic_launcher" + android:label="@string/app_name" + android:roundIcon="@mipmap/ic_launcher_round" + android:supportsRtl="true" + android:theme="@style/Theme.RoundedBackground"> + <activity android:name=".MainActivity"> + <intent-filter> + <action android:name="android.intent.action.MAIN"/> + <category android:name="android.intent.category.LAUNCHER"/> + </intent-filter> + </activity> + </application> + +</manifest>
\ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/java/com/android/example/text/styling/roundedbg/app/MainActivity.kt b/ui/text/RoundedBackground-Kotlin/app/src/main/java/com/android/example/text/styling/roundedbg/app/MainActivity.kt new file mode 100644 index 00000000..53ae397c --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/java/com/android/example/text/styling/roundedbg/app/MainActivity.kt @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +*/ +package com.android.example.text.styling.roundedbg.app + +import android.os.Bundle +import androidx.appcompat.app.AppCompatActivity + +/** + * Sample activity that uses [com.android.example.text.styling.roundedbg.RoundedBgTextView]. + */ +class MainActivity : AppCompatActivity() { + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + } +} diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 00000000..888da70e --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,50 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright 2018 The Android Open Source Project + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<vector xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:aapt="http://schemas.android.com/aapt" + android:width="108dp" + android:height="108dp" + android:viewportHeight="108" + android:viewportWidth="108"> + <path + android:fillType="evenOdd" + android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z" + android:strokeColor="#00000000" + android:strokeWidth="1"> + <aapt:attr name="android:fillColor"> + <gradient + android:endX="78.5885" + android:endY="90.9159" + android:startX="48.7653" + android:startY="61.0927" + android:type="linear"> + <item + android:color="#44000000" + android:offset="0.0" /> + <item + android:color="#00000000" + android:offset="1.0" /> + </gradient> + </aapt:attr> + </path> + <path + android:fillColor="#FFFFFF" + android:fillType="nonZero" + android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z" + android:strokeColor="#00000000" + android:strokeWidth="1" /> +</vector> diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/ic_launcher_background.xml b/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 00000000..a5e49139 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,185 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright 2018 The Android Open Source Project + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<vector xmlns:android="http://schemas.android.com/apk/res/android" + android:width="108dp" + android:height="108dp" + android:viewportHeight="108" + android:viewportWidth="108"> + <path + android:fillColor="#26A69A" + android:pathData="M0,0h108v108h-108z" /> + <path + android:fillColor="#00000000" + android:pathData="M9,0L9,108" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M19,0L19,108" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M29,0L29,108" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M39,0L39,108" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M49,0L49,108" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M59,0L59,108" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M69,0L69,108" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M79,0L79,108" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M89,0L89,108" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M99,0L99,108" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M0,9L108,9" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M0,19L108,19" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M0,29L108,29" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M0,39L108,39" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M0,49L108,49" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M0,59L108,59" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M0,69L108,69" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M0,79L108,79" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M0,89L108,89" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M0,99L108,99" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M19,29L89,29" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M19,39L89,39" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M19,49L89,49" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M19,59L89,59" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M19,69L89,69" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M19,79L89,79" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M29,19L29,89" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M39,19L39,89" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M49,19L49,89" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M59,19L59,89" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M69,19L69,89" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> + <path + android:fillColor="#00000000" + android:pathData="M79,19L79,89" + android:strokeColor="#33FFFFFF" + android:strokeWidth="0.8" /> +</vector> diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/rounded.xml b/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/rounded.xml new file mode 100644 index 00000000..f69e6bf9 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/rounded.xml @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright 2018 The Android Open Source Project + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<shape xmlns:android="http://schemas.android.com/apk/res/android"> + <solid android:color="@color/roundedBg"/> + <stroke android:width="@dimen/roundedBorderWidth" android:color="@color/roundedBorder"/> + <corners android:radius="@dimen/roundedBorderRadius"/> +</shape>
\ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/rounded_left.xml b/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/rounded_left.xml new file mode 100644 index 00000000..6883c63e --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/rounded_left.xml @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright 2018 The Android Open Source Project + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<shape xmlns:android="http://schemas.android.com/apk/res/android"> + <solid android:color="@color/roundedBg"/> + <stroke android:width="@dimen/roundedBorderWidth" android:color="@color/roundedBorder"/> + <corners android:topLeftRadius="@dimen/roundedTextBorderRadius" + android:bottomLeftRadius="@dimen/roundedTextBorderRadius"/> +</shape>
\ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/rounded_mid.xml b/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/rounded_mid.xml new file mode 100644 index 00000000..b1714ecd --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/rounded_mid.xml @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright 2018 The Android Open Source Project + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<shape xmlns:android="http://schemas.android.com/apk/res/android"> + <solid android:color="@color/roundedBg"/> + <stroke android:width="@dimen/roundedBorderWidth" android:color="@color/roundedBorder"/> +</shape>
\ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/rounded_right.xml b/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/rounded_right.xml new file mode 100644 index 00000000..8066d972 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/rounded_right.xml @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright 2018 The Android Open Source Project + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<shape xmlns:android="http://schemas.android.com/apk/res/android"> + <solid android:color="@color/roundedBg"/> + <stroke android:width="@dimen/roundedBorderWidth" android:color="@color/roundedBorder"/> + <corners android:topRightRadius="@dimen/roundedBorderRadius" + android:bottomRightRadius="@dimen/roundedBorderRadius"/> +</shape>
\ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/layout/activity_main.xml b/ui/text/RoundedBackground-Kotlin/app/src/main/res/layout/activity_main.xml new file mode 100644 index 00000000..b3738cb3 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,131 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright 2018 The Android Open Source Project + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<ScrollView + xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" + tools:context=".MainActivity"> + + <LinearLayout + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:orientation="vertical"> + + <com.android.example.text.styling.roundedbg.RoundedBgTextView + android:text="@string/ltr" + style="@style/Widget.RoundedBackground.SampleTextView" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_margin="@dimen/textViewMargin"/> + + <View style="@style/Widget.RoundedBackground.Divider" + android:layout_width="match_parent" + android:layout_height="@dimen/dividerHeight"/> + + <com.android.example.text.styling.roundedbg.RoundedBgTextView + android:text="@string/ltr_multi" + style="@style/Widget.RoundedBackground.SampleTextView" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_margin="@dimen/textViewMargin"/> + + <View style="@style/Widget.RoundedBackground.Divider" + android:layout_width="match_parent" + android:layout_height="@dimen/dividerHeight"/> + + <com.android.example.text.styling.roundedbg.RoundedBgTextView + android:text="@string/ltr_long" + style="@style/Widget.RoundedBackground.SampleTextView" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_margin="@dimen/textViewMargin" + app:roundedTextDrawable="@drawable/rounded" + app:roundedTextDrawableLeft="@drawable/rounded_left" + app:roundedTextDrawableMid="@drawable/rounded_mid" + app:roundedTextDrawableRight="@drawable/rounded_right"/> + + <View style="@style/Widget.RoundedBackground.Divider" + android:layout_width="match_parent" + android:layout_height="@dimen/dividerHeight"/> + + <com.android.example.text.styling.roundedbg.RoundedBgTextView + android:text="@string/rtl" + style="@style/Widget.RoundedBackground.SampleTextView" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_margin="@dimen/textViewMargin"/> + + <View style="@style/Widget.RoundedBackground.Divider" + android:layout_width="match_parent" + android:layout_height="@dimen/dividerHeight"/> + + <com.android.example.text.styling.roundedbg.RoundedBgTextView + android:text="@string/rtl_multi" + style="@style/Widget.RoundedBackground.SampleTextView" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_margin="@dimen/textViewMargin"/> + + <View style="@style/Widget.RoundedBackground.Divider" + android:layout_width="match_parent" + android:layout_height="@dimen/dividerHeight"/> + + <com.android.example.text.styling.roundedbg.RoundedBgTextView + android:text="@string/lang1" + style="@style/Widget.RoundedBackground.SampleTextView" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_margin="@dimen/textViewMargin"/> + + <View style="@style/Widget.RoundedBackground.Divider" + android:layout_width="match_parent" + android:layout_height="@dimen/dividerHeight"/> + + <com.android.example.text.styling.roundedbg.RoundedBgTextView + android:text="@string/lang2" + style="@style/Widget.RoundedBackground.SampleTextView" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_margin="@dimen/textViewMargin"/> + + <View style="@style/Widget.RoundedBackground.Divider" + android:layout_width="match_parent" + android:layout_height="@dimen/dividerHeight"/> + + <com.android.example.text.styling.roundedbg.RoundedBgTextView + android:text="@string/lang3" + style="@style/Widget.RoundedBackground.SampleTextView" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_margin="@dimen/textViewMargin"/> + + <View style="@style/Widget.RoundedBackground.Divider" + android:layout_width="match_parent" + android:layout_height="@dimen/dividerHeight"/> + + <com.android.example.text.styling.roundedbg.RoundedBgTextView + android:text="@string/lang4" + style="@style/Widget.RoundedBackground.SampleTextView" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_margin="@dimen/textViewMargin"/> + + </LinearLayout> + + +</ScrollView> diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml new file mode 100644 index 00000000..d3441ca6 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright 2018 The Android Open Source Project + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android"> + <background android:drawable="@drawable/ic_launcher_background" /> + <foreground android:drawable="@drawable/ic_launcher_foreground" /> +</adaptive-icon>
\ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml new file mode 100644 index 00000000..d3441ca6 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright 2018 The Android Open Source Project + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android"> + <background android:drawable="@drawable/ic_launcher_background" /> + <foreground android:drawable="@drawable/ic_launcher_foreground" /> +</adaptive-icon>
\ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-hdpi/ic_launcher.png b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-hdpi/ic_launcher.png Binary files differnew file mode 100644 index 00000000..a2f59082 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-hdpi/ic_launcher.png diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-hdpi/ic_launcher_round.png b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-hdpi/ic_launcher_round.png Binary files differnew file mode 100644 index 00000000..1b523998 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-hdpi/ic_launcher_round.png diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-mdpi/ic_launcher.png b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-mdpi/ic_launcher.png Binary files differnew file mode 100644 index 00000000..ff10afd6 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-mdpi/ic_launcher.png diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-mdpi/ic_launcher_round.png b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-mdpi/ic_launcher_round.png Binary files differnew file mode 100644 index 00000000..115a4c76 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-mdpi/ic_launcher_round.png diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xhdpi/ic_launcher.png b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xhdpi/ic_launcher.png Binary files differnew file mode 100644 index 00000000..dcd3cd80 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xhdpi/ic_launcher.png diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png Binary files differnew file mode 100644 index 00000000..459ca609 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xxhdpi/ic_launcher.png b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xxhdpi/ic_launcher.png Binary files differnew file mode 100644 index 00000000..8ca12fe0 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xxhdpi/ic_launcher.png diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png Binary files differnew file mode 100644 index 00000000..8e19b410 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png Binary files differnew file mode 100644 index 00000000..b824ebdd --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png Binary files differnew file mode 100644 index 00000000..4c19a13c --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/values/colors.xml b/ui/text/RoundedBackground-Kotlin/app/src/main/res/values/colors.xml new file mode 100644 index 00000000..932c9e93 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/values/colors.xml @@ -0,0 +1,26 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright 2018 The Android Open Source Project + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<resources> + <color name="colorPrimary">#DDD</color> + <color name="colorPrimaryDark">#CCC</color> + <color name="colorAccent">#FF7F50</color> + + <color name="divider">#CCC</color> + + <color name="roundedBg">#FFF176</color> + <color name="roundedBorder">#CABF45</color> +</resources> diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/values/dimens.xml b/ui/text/RoundedBackground-Kotlin/app/src/main/res/values/dimens.xml new file mode 100644 index 00000000..02814ded --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/values/dimens.xml @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright 2018 The Android Open Source Project + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<resources> + <dimen name="textViewMargin">8dp</dimen> + <dimen name="dividerHeight">1dp</dimen> + + <dimen name="roundedBorderRadius">6dp</dimen> + <dimen name="roundedBorderWidth">2dp</dimen> +</resources> diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/values/strings.xml b/ui/text/RoundedBackground-Kotlin/app/src/main/res/values/strings.xml new file mode 100644 index 00000000..35cc087d --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/values/strings.xml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright 2018 The Android Open Source Project + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<resources> + <string name="app_name">Rounded Multiline Bg</string> + + <!-- Without the quotes at the begining and end Android strips the whitespace and also starts + the annotation at the wrong position. --> + <string name="ltr">"this is <annotation key="rounded">a regular</annotation> paragraph."</string> + <string name="ltr_multi">"this contains <annotation key="rounded">a\nline break</annotation> and continues."</string> + <string name="ltr_long">"this is <annotation key="rounded">a paragraph \nthat would cover more than\ntwo</annotation> lines."</string> + <string name="rtl"> هذا هو "<annotation key="rounded">الحق في</annotation>" الفقرة اليسرى </string> + <string name="rtl_multi"> هذا هو"<annotation key="rounded">حق\n لفقرة</annotation>" اليسار </string> + <string name="lang1">"ဟ <annotation key="rounded">ယ်လို\nဟယ်လို</annotation> ယ်လို"</string> + <string name="lang2">"អ <annotation key="rounded">៊ីតាលី\nអ៊ីតា</annotation> លី""</string> + <string name="lang3">"नमस्ते <annotation key="rounded">दुनिया\nनमस्ते</annotation> दुनिया"</string> + <string name="lang4">"สวัสดี <annotation key="rounded">ชาวโลก\nสวัสดีชาวโ</annotation> ลก"</string> + +</resources>
\ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/values/styles.xml b/ui/text/RoundedBackground-Kotlin/app/src/main/res/values/styles.xml new file mode 100644 index 00000000..1997dad9 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/values/styles.xml @@ -0,0 +1,42 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright 2018 The Android Open Source Project + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<resources> + + <!-- Base application theme. --> + <style name="Theme.RoundedBackground" parent="Theme.AppCompat.Light"> + <item name="colorPrimary">@color/colorPrimary</item> + <item name="colorPrimaryDark">@color/colorPrimaryDark</item> + <item name="colorAccent">@color/colorAccent</item> + </style> + + <style name="Widget.RoundedBackground" parent="android:Widget"/> + + <style name="Widget.RoundedBackground.SampleTextView"> + <item name="android:lineSpacingExtra">2dp</item> + <item name="android:lineSpacingMultiplier">1.5</item> + <item name="android:padding">4dp</item> + <item name="android:textSize">18sp</item> + <item name="android:includeFontPadding">true</item> + <item name="roundedTextHorizontalPadding">2dp</item> + <item name="roundedTextVerticalPadding">2dp</item> + </style> + + <style name="Widget.RoundedBackground.Divider"> + <item name="android:background">#CCC</item> + <item name="android:padding">2dp</item> + </style> +</resources> diff --git a/ui/text/RoundedBackground-Kotlin/build.gradle b/ui/text/RoundedBackground-Kotlin/build.gradle new file mode 100644 index 00000000..f6df7566 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/build.gradle @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +// Top-level build file where you can add configuration options common to all sub-projects/modules. + +buildscript { + ext.kotlin_version = '1.2.51' + repositories { + google() + mavenCentral() + } + dependencies { + classpath 'com.android.tools.build:gradle:3.3.0-alpha03' + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + } +} + +allprojects { + repositories { + google() + mavenCentral() + } +} + +task clean(type: Delete) { + delete rootProject.buildDir +} + +ext { + // Sdk and tools + minSdkVersion = 15 + targetSdkVersion = 28 + compileSdkVersion = 28 + // App dependencies + androidxVersion = '1.0.0-beta01' + ktxVersion = '1.0.0-alpha1' +}
\ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/gradle.properties b/ui/text/RoundedBackground-Kotlin/gradle.properties new file mode 100644 index 00000000..743d692c --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/gradle.properties @@ -0,0 +1,13 @@ +# Project-wide Gradle settings. +# IDE (e.g. Android Studio) users: +# Gradle settings configured through the IDE *will override* +# any settings specified in this file. +# For more details on how to configure your build environment visit +# http://www.gradle.org/docs/current/userguide/build_environment.html +# Specifies the JVM arguments used for the daemon process. +# The setting is particularly useful for tweaking memory settings. +org.gradle.jvmargs=-Xmx1536m +# When configured, Gradle will run in incubating parallel mode. +# This option should only be used with decoupled projects. More details, visit +# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects +# org.gradle.parallel=true diff --git a/ui/text/RoundedBackground-Kotlin/gradle/wrapper/gradle-wrapper.jar b/ui/text/RoundedBackground-Kotlin/gradle/wrapper/gradle-wrapper.jar Binary files differnew file mode 100644 index 00000000..7a3265ee --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/gradle/wrapper/gradle-wrapper.jar diff --git a/ui/text/RoundedBackground-Kotlin/gradle/wrapper/gradle-wrapper.properties b/ui/text/RoundedBackground-Kotlin/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..37fcd35b --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Wed Jul 11 10:47:22 PDT 2018 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-bin.zip diff --git a/ui/text/RoundedBackground-Kotlin/gradlew b/ui/text/RoundedBackground-Kotlin/gradlew new file mode 100755 index 00000000..cccdd3d5 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/ui/text/RoundedBackground-Kotlin/gradlew.bat b/ui/text/RoundedBackground-Kotlin/gradlew.bat new file mode 100644 index 00000000..e95643d6 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off
+@rem ##########################################################################
+@rem
+@rem Gradle startup script for Windows
+@rem
+@rem ##########################################################################
+
+@rem Set local scope for the variables with windows NT shell
+if "%OS%"=="Windows_NT" setlocal
+
+set DIRNAME=%~dp0
+if "%DIRNAME%" == "" set DIRNAME=.
+set APP_BASE_NAME=%~n0
+set APP_HOME=%DIRNAME%
+
+@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+set DEFAULT_JVM_OPTS=
+
+@rem Find java.exe
+if defined JAVA_HOME goto findJavaFromJavaHome
+
+set JAVA_EXE=java.exe
+%JAVA_EXE% -version >NUL 2>&1
+if "%ERRORLEVEL%" == "0" goto init
+
+echo.
+echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:findJavaFromJavaHome
+set JAVA_HOME=%JAVA_HOME:"=%
+set JAVA_EXE=%JAVA_HOME%/bin/java.exe
+
+if exist "%JAVA_EXE%" goto init
+
+echo.
+echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:init
+@rem Get command-line arguments, handling Windows variants
+
+if not "%OS%" == "Windows_NT" goto win9xME_args
+
+:win9xME_args
+@rem Slurp the command line arguments.
+set CMD_LINE_ARGS=
+set _SKIP=2
+
+:win9xME_args_slurp
+if "x%~1" == "x" goto execute
+
+set CMD_LINE_ARGS=%*
+
+:execute
+@rem Setup the command line
+
+set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
+
+@rem Execute Gradle
+"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
+
+:end
+@rem End local scope for the variables with windows NT shell
+if "%ERRORLEVEL%"=="0" goto mainEnd
+
+:fail
+rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
+rem the _cmd.exe /c_ return code!
+if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
+exit /b 1
+
+:mainEnd
+if "%OS%"=="Windows_NT" endlocal
+
+:omega
diff --git a/ui/text/RoundedBackground-Kotlin/lib/.gitignore b/ui/text/RoundedBackground-Kotlin/lib/.gitignore new file mode 100644 index 00000000..796b96d1 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/.gitignore @@ -0,0 +1 @@ +/build diff --git a/ui/text/RoundedBackground-Kotlin/lib/build.gradle b/ui/text/RoundedBackground-Kotlin/lib/build.gradle new file mode 100644 index 00000000..6c499867 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/build.gradle @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +apply plugin: 'com.android.library' + +apply plugin: 'kotlin-android' + +android { + compileSdkVersion rootProject.compileSdkVersion + + defaultConfig { + minSdkVersion rootProject.minSdkVersion + targetSdkVersion rootProject.targetSdkVersion + versionCode 1 + versionName "1.0" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } + +} + +dependencies { + implementation "androidx.core:core-ktx:${ktxVersion}" + implementation "androidx.appcompat:appcompat:$androidxVersion" + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" +}
\ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/lib/src/main/AndroidManifest.xml b/ui/text/RoundedBackground-Kotlin/lib/src/main/AndroidManifest.xml new file mode 100644 index 00000000..43a5eb06 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/src/main/AndroidManifest.xml @@ -0,0 +1,18 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright 2018 The Android Open Source Project + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<manifest + package="com.android.example.text.styling.roundedbg"/> diff --git a/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/LayoutExtensions.kt b/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/LayoutExtensions.kt new file mode 100644 index 00000000..f7478eb4 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/LayoutExtensions.kt @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.example.text.styling.roundedbg + +import android.os.Build +import android.text.Layout + +// Extension functions for Layout object + +/** + * Android system default line spacing extra + */ +private const val DEFAULT_LINESPACING_EXTRA = 0f + +/** + * Android system default line spacing multiplier + */ +private const val DEFAULT_LINESPACING_MULTIPLIER = 1f + +/** + * Get the line bottom discarding the line spacing added. + */ +fun Layout.getLineBottomWithoutSpacing(line: Int): Int { + val lineBottom = getLineBottom(line) + val lastLineSpacingNotAdded = Build.VERSION.SDK_INT >= 19 + val isLastLine = line == lineCount - 1 + + val lineBottomWithoutSpacing: Int + val lineSpacingExtra = spacingAdd + val lineSpacingMultiplier = spacingMultiplier + val hasLineSpacing = lineSpacingExtra != DEFAULT_LINESPACING_EXTRA + || lineSpacingMultiplier != DEFAULT_LINESPACING_MULTIPLIER + + if (!hasLineSpacing || isLastLine && lastLineSpacingNotAdded) { + lineBottomWithoutSpacing = lineBottom + } else { + val extra: Float + if (lineSpacingMultiplier.compareTo(DEFAULT_LINESPACING_MULTIPLIER) != 0) { + val lineHeight = getLineHeight(line) + extra = lineHeight - (lineHeight - lineSpacingExtra) / lineSpacingMultiplier + } else { + extra = lineSpacingExtra + } + + lineBottomWithoutSpacing = (lineBottom - extra).toInt() + } + + return lineBottomWithoutSpacing +} + +/** + * Get the line height of a line. + */ +fun Layout.getLineHeight(line: Int): Int { + return getLineTop(line + 1) - getLineTop(line) +} + +/** + * Returns the top of the Layout after removing the extra padding applied by the Layout. + */ +fun Layout.getLineTopWithoutPadding(line: Int): Int { + var lineTop = getLineTop(line) + if (line == 0) { + lineTop -= topPadding + } + return lineTop +} + +/** + * Returns the bottom of the Layout after removing the extra padding applied by the Layout. + */ +fun Layout.getLineBottomWithoutPadding(line: Int): Int { + var lineBottom = getLineBottomWithoutSpacing(line) + if (line == lineCount - 1) { + lineBottom -= bottomPadding + } + return lineBottom +}
\ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/RoundedBgTextView.kt b/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/RoundedBgTextView.kt new file mode 100644 index 00000000..9f0d2881 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/RoundedBgTextView.kt @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.example.text.styling.roundedbg + +import android.content.Context +import android.graphics.Canvas +import android.text.Spanned +import android.util.AttributeSet +import androidx.appcompat.widget.AppCompatTextView +import androidx.core.graphics.withTranslation + +/** + * A TextView that can draw rounded background to the portions of the text. See + * [TextRoundedBgHelper] for more information. + * + * See [TextRoundedBgAttributeReader] for supported attributes. + */ +class RoundedBgTextView : AppCompatTextView { + + private val textRoundedBgHelper: TextRoundedBgHelper + + @JvmOverloads + constructor( + context: Context, + attrs: AttributeSet? = null, + defStyleAttr: Int = android.R.attr.textViewStyle + ) : super(context, attrs, defStyleAttr) { + val attributeReader = TextRoundedBgAttributeReader(context, attrs) + textRoundedBgHelper = TextRoundedBgHelper( + horizontalPadding = attributeReader.horizontalPadding, + verticalPadding = attributeReader.verticalPadding, + drawable = attributeReader.drawable, + drawableLeft = attributeReader.drawableLeft, + drawableMid = attributeReader.drawableMid, + drawableRight = attributeReader.drawableRight + ) + } + + override fun onDraw(canvas: Canvas) { + // need to draw bg first so that text can be on top during super.onDraw() + if (text is Spanned && layout != null) { + canvas.withTranslation(totalPaddingLeft.toFloat(), totalPaddingTop.toFloat()) { + textRoundedBgHelper.draw(canvas, text as Spanned, layout) + } + } + super.onDraw(canvas) + } +}
\ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/TextRoundedBgAttributeReader.kt b/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/TextRoundedBgAttributeReader.kt new file mode 100644 index 00000000..5ecc41d9 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/TextRoundedBgAttributeReader.kt @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +*/ +package com.android.example.text.styling.roundedbg + +import android.content.Context +import android.graphics.drawable.Drawable +import android.util.AttributeSet +import androidx.core.content.res.getDrawableOrThrow + +/** + * Reads default attributes that [TextRoundedBgHelper] needs from resources. The attributes read + * are: + * + * - chHorizontalPadding: the padding to be applied to left & right of the background + * - chVerticalPadding: the padding to be applied to top & bottom of the background + * - chDrawable: the drawable used to draw the background + * - chDrawableLeft: the drawable used to draw left edge of the background + * - chDrawableMid: the drawable used to draw for whole line + * - chDrawableRight: the drawable used to draw right edge of the background + */ +class TextRoundedBgAttributeReader(context: Context, attrs: AttributeSet?) { + + val horizontalPadding: Int + val verticalPadding: Int + val drawable: Drawable + val drawableLeft: Drawable + val drawableMid: Drawable + val drawableRight: Drawable + + init { + val typedArray = context.obtainStyledAttributes( + attrs, + R.styleable.TextRoundedBgHelper, + 0, + R.style.RoundedBgTextView + ) + horizontalPadding = typedArray.getDimensionPixelSize( + R.styleable.TextRoundedBgHelper_roundedTextHorizontalPadding, + 0 + ) + verticalPadding = typedArray.getDimensionPixelSize( + R.styleable.TextRoundedBgHelper_roundedTextVerticalPadding, + 0 + ) + drawable = typedArray.getDrawableOrThrow( + R.styleable.TextRoundedBgHelper_roundedTextDrawable + ) + drawableLeft = typedArray.getDrawableOrThrow( + R.styleable.TextRoundedBgHelper_roundedTextDrawableLeft + ) + drawableMid = typedArray.getDrawableOrThrow( + R.styleable.TextRoundedBgHelper_roundedTextDrawableMid + ) + drawableRight = typedArray.getDrawableOrThrow( + R.styleable.TextRoundedBgHelper_roundedTextDrawableRight + ) + typedArray.recycle() + } +} diff --git a/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/TextRoundedBgHelper.kt b/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/TextRoundedBgHelper.kt new file mode 100644 index 00000000..262a5fe9 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/TextRoundedBgHelper.kt @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +*/ +package com.android.example.text.styling.roundedbg + +import android.graphics.Canvas +import android.graphics.drawable.Drawable +import android.text.Annotation +import android.text.Layout +import android.text.Spanned + +/** + * Helper class to draw multi-line rounded background to certain parts of a text. The start/end + * positions of the backgrounds are annotated with [android.text.Annotation] class. Each annotation + * should have the annotation key set to **rounded**. + * + * i.e.: + * ``` + * <!--without the quotes at the begining and end Android strips the whitespace and also starts + * the annotation at the wrong position--> + * <string name="ltr">"this is <annotation key="rounded">a regular</annotation> paragraph."</string> + * ``` + * + * **Note:** BiDi text is not supported. + * + * @param horizontalPadding the padding to be applied to left & right of the background + * @param verticalPadding the padding to be applied to top & bottom of the background + * @param drawable the drawable used to draw the background + * @param drawableLeft the drawable used to draw left edge of the background + * @param drawableMid the drawable used to draw for whole line + * @param drawableRight the drawable used to draw right edge of the background + */ +class TextRoundedBgHelper( + val horizontalPadding: Int, + verticalPadding: Int, + drawable: Drawable, + drawableLeft: Drawable, + drawableMid: Drawable, + drawableRight: Drawable +) { + + private val singleLineRenderer: TextRoundedBgRenderer by lazy { + SingleLineRenderer( + horizontalPadding = horizontalPadding, + verticalPadding = verticalPadding, + drawable = drawable + ) + } + + private val multiLineRenderer: TextRoundedBgRenderer by lazy { + MultiLineRenderer( + horizontalPadding = horizontalPadding, + verticalPadding = verticalPadding, + drawableLeft = drawableLeft, + drawableMid = drawableMid, + drawableRight = drawableRight + ) + } + + /** + * Call this function during onDraw of another widget such as TextView. + * + * @param canvas Canvas to draw onto + * @param text + * @param layout Layout that contains the text + */ + fun draw(canvas: Canvas, text: Spanned, layout: Layout) { + // ideally the calculations here should be cached since they are not cheap. However, proper + // invalidation of the cache is required whenever anything related to text has changed. + val spans = text.getSpans(0, text.length, Annotation::class.java) + spans.forEach { span -> + if (span.value.equals("rounded")) { + val spanStart = text.getSpanStart(span) + val spanEnd = text.getSpanEnd(span) + val startLine = layout.getLineForOffset(spanStart) + val endLine = layout.getLineForOffset(spanEnd) + + // start can be on the left or on the right depending on the language direction. + val startOffset = (layout.getPrimaryHorizontal(spanStart) + + -1 * layout.getParagraphDirection(startLine) * horizontalPadding).toInt() + // end can be on the left or on the right depending on the language direction. + val endOffset = (layout.getPrimaryHorizontal(spanEnd) + + layout.getParagraphDirection(endLine) * horizontalPadding).toInt() + + val renderer = if (startLine == endLine) singleLineRenderer else multiLineRenderer + renderer.draw(canvas, layout, startLine, endLine, startOffset, endOffset) + } + } + } +}
\ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/TextRoundedBgRenderer.kt b/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/TextRoundedBgRenderer.kt new file mode 100644 index 00000000..37d814a7 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/TextRoundedBgRenderer.kt @@ -0,0 +1,209 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +*/ +package com.android.example.text.styling.roundedbg + +import android.graphics.Canvas +import android.graphics.drawable.Drawable +import android.text.Layout +import kotlin.math.max +import kotlin.math.min + +/** + * Base class for single and multi line rounded background renderers. + * + * @param horizontalPadding the padding to be applied to left & right of the background + * @param verticalPadding the padding to be applied to top & bottom of the background + */ +internal abstract class TextRoundedBgRenderer( + val horizontalPadding: Int, + val verticalPadding: Int +) { + + /** + * Draw the background that starts at the {@code startOffset} and ends at {@code endOffset}. + * + * @param canvas Canvas to draw onto + * @param layout Layout that contains the text + * @param startLine the start line for the background + * @param endLine the end line for the background + * @param startOffset the character offset that the background should start at + * @param endOffset the character offset that the background should end at + */ + abstract fun draw( + canvas: Canvas, + layout: Layout, + startLine: Int, + endLine: Int, + startOffset: Int, + endOffset: Int + ) + + /** + * Get the top offset of the line and add padding into account so that there is a gap between + * top of the background and top of the text. + * + * @param layout Layout object that contains the text + * @param line line number + */ + protected fun getLineTop(layout: Layout, line: Int): Int { + return layout.getLineTopWithoutPadding(line) - verticalPadding + } + + /** + * Get the bottom offset of the line and add padding into account so that there is a gap between + * bottom of the background and bottom of the text. + * + * @param layout Layout object that contains the text + * @param line line number + */ + protected fun getLineBottom(layout: Layout, line: Int): Int { + return layout.getLineBottomWithoutPadding(line) + verticalPadding + } +} + +/** + * Draws the background for text that starts and ends on the same line. + * + * @param horizontalPadding the padding to be applied to left & right of the background + * @param verticalPadding the padding to be applied to top & bottom of the background + * @param drawable the drawable used to draw the background + */ +internal class SingleLineRenderer( + horizontalPadding: Int, + verticalPadding: Int, + val drawable: Drawable +) : TextRoundedBgRenderer(horizontalPadding, verticalPadding) { + + override fun draw( + canvas: Canvas, + layout: Layout, + startLine: Int, + endLine: Int, + startOffset: Int, + endOffset: Int + ) { + val lineTop = getLineTop(layout, startLine) + val lineBottom = getLineBottom(layout, startLine) + // get min of start/end for left, and max of start/end for right since we don't + // the language direction + val left = min(startOffset, endOffset) + val right = max(startOffset, endOffset) + drawable.setBounds(left, lineTop, right, lineBottom) + drawable.draw(canvas) + } +} + +/** + * Draws the background for text that starts and ends on different lines. + * + * @param horizontalPadding the padding to be applied to left & right of the background + * @param verticalPadding the padding to be applied to top & bottom of the background + * @param drawableLeft the drawable used to draw left edge of the background + * @param drawableMid the drawable used to draw for whole line + * @param drawableRight the drawable used to draw right edge of the background + */ +internal class MultiLineRenderer( + horizontalPadding: Int, + verticalPadding: Int, + val drawableLeft: Drawable, + val drawableMid: Drawable, + val drawableRight: Drawable +) : TextRoundedBgRenderer(horizontalPadding, verticalPadding) { + + override fun draw( + canvas: Canvas, + layout: Layout, + startLine: Int, + endLine: Int, + startOffset: Int, + endOffset: Int + ) { + // draw the first line + val paragDir = layout.getParagraphDirection(startLine) + val lineEndOffset = if (paragDir == Layout.DIR_RIGHT_TO_LEFT) { + layout.getLineLeft(startLine) - horizontalPadding + } else { + layout.getLineRight(startLine) + horizontalPadding + }.toInt() + + var lineBottom = getLineBottom(layout, startLine) + var lineTop = getLineTop(layout, startLine) + drawStart(canvas, startOffset, lineTop, lineEndOffset, lineBottom) + + // for the lines in the middle draw the mid drawable + for (line in startLine + 1 until endLine) { + lineTop = getLineTop(layout, line) + lineBottom = getLineBottom(layout, line) + drawableMid.setBounds( + (layout.getLineLeft(line).toInt() - horizontalPadding), + lineTop, + (layout.getLineRight(line).toInt() + horizontalPadding), + lineBottom + ) + drawableMid.draw(canvas) + } + + val lineStartOffset = if (paragDir == Layout.DIR_RIGHT_TO_LEFT) { + layout.getLineRight(startLine) + horizontalPadding + } else { + layout.getLineLeft(startLine) - horizontalPadding + }.toInt() + + // draw the last line + lineBottom = getLineBottom(layout, endLine) + lineTop = getLineTop(layout, endLine) + + drawEnd(canvas, lineStartOffset, lineTop, endOffset, lineBottom) + } + + /** + * Draw the first line of a multiline annotation. Handles LTR/RTL. + * + * @param canvas Canvas to draw onto + * @param start start coordinate for the background + * @param top top coordinate for the background + * @param end end coordinate for the background + * @param bottom bottom coordinate for the background + */ + private fun drawStart(canvas: Canvas, start: Int, top: Int, end: Int, bottom: Int) { + if (start > end) { + drawableRight.setBounds(end, top, start, bottom) + drawableRight.draw(canvas) + } else { + drawableLeft.setBounds(start, top, end, bottom) + drawableLeft.draw(canvas) + } + } + + /** + * Draw the last line of a multiline annotation. Handles LTR/RTL. + * + * @param canvas Canvas to draw onto + * @param start start coordinate for the background + * @param top top position for the background + * @param end end coordinate for the background + * @param bottom bottom coordinate for the background + */ + private fun drawEnd(canvas: Canvas, start: Int, top: Int, end: Int, bottom: Int) { + if (start > end) { + drawableLeft.setBounds(end, top, start, bottom) + drawableLeft.draw(canvas) + } else { + drawableRight.setBounds(start, top, end, bottom) + drawableRight.draw(canvas) + } + } +}
\ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/lib/src/main/res/drawable/rounded_text_bg.xml b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/drawable/rounded_text_bg.xml new file mode 100644 index 00000000..0683b1b9 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/drawable/rounded_text_bg.xml @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright 2018 The Android Open Source Project + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<shape xmlns:android="http://schemas.android.com/apk/res/android"> + <solid android:color="@color/roundedTextBg"/> + <stroke android:width="@dimen/roundedTextBorderWidth" android:color="@color/roundedTextBorder"/> + <corners android:radius="@dimen/roundedTextBorderRadius"/> +</shape>
\ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/lib/src/main/res/drawable/rounded_text_bg_left.xml b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/drawable/rounded_text_bg_left.xml new file mode 100644 index 00000000..5d02c93d --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/drawable/rounded_text_bg_left.xml @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright 2018 The Android Open Source Project + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<shape xmlns:android="http://schemas.android.com/apk/res/android"> + <solid android:color="@color/roundedTextBg"/> + <stroke android:width="@dimen/roundedTextBorderWidth" android:color="@color/roundedTextBorder"/> + <corners android:topLeftRadius="@dimen/roundedTextBorderRadius" + android:bottomLeftRadius="@dimen/roundedTextBorderRadius"/> +</shape>
\ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/lib/src/main/res/drawable/rounded_text_bg_mid.xml b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/drawable/rounded_text_bg_mid.xml new file mode 100644 index 00000000..e4c2c570 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/drawable/rounded_text_bg_mid.xml @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright 2018 The Android Open Source Project + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<shape xmlns:android="http://schemas.android.com/apk/res/android"> + <solid android:color="@color/roundedTextBg"/> + <stroke android:width="@dimen/roundedTextBorderWidth" android:color="@color/roundedTextBorder"/> +</shape>
\ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/lib/src/main/res/drawable/rounded_text_bg_right.xml b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/drawable/rounded_text_bg_right.xml new file mode 100644 index 00000000..cf011c5c --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/drawable/rounded_text_bg_right.xml @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright 2018 The Android Open Source Project + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<shape xmlns:android="http://schemas.android.com/apk/res/android"> + <solid android:color="@color/roundedTextBg"/> + <stroke android:width="@dimen/roundedTextBorderWidth" android:color="@color/roundedTextBorder"/> + <corners android:topRightRadius="@dimen/roundedTextBorderRadius" + android:bottomRightRadius="@dimen/roundedTextBorderRadius"/> +</shape>
\ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/lib/src/main/res/values/attrs.xml b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/values/attrs.xml new file mode 100644 index 00000000..737ae07c --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/values/attrs.xml @@ -0,0 +1,26 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright 2018 The Android Open Source Project + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<resources> + <declare-styleable name="TextRoundedBgHelper"> + <attr name="roundedTextHorizontalPadding" format="dimension"/> + <attr name="roundedTextVerticalPadding" format="dimension"/> + <attr name="roundedTextDrawable" format="reference"/> + <attr name="roundedTextDrawableLeft" format="reference"/> + <attr name="roundedTextDrawableMid" format="reference"/> + <attr name="roundedTextDrawableRight" format="reference"/> + </declare-styleable> +</resources>
\ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/lib/src/main/res/values/colors.xml b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/values/colors.xml new file mode 100644 index 00000000..c26ac6af --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/values/colors.xml @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright 2018 The Android Open Source Project + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<resources> + <color name="roundedTextBg">#554fC3f7</color> + <color name="roundedTextBorder">#DD0277BD</color> +</resources> diff --git a/ui/text/RoundedBackground-Kotlin/lib/src/main/res/values/dimens.xml b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/values/dimens.xml new file mode 100644 index 00000000..dbc7f49e --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/values/dimens.xml @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright 2018 The Android Open Source Project + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<resources> + <dimen name="roundedTextBorderRadius">4dp</dimen> + <dimen name="roundedTextBorderWidth">1dp</dimen> +</resources>
\ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/lib/src/main/res/values/styles.xml b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/values/styles.xml new file mode 100644 index 00000000..9bffef3d --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/values/styles.xml @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright 2018 The Android Open Source Project + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<resources> + + <style name="RoundedBgTextView" parent="@android:style/Widget.TextView"> + <item name="roundedTextHorizontalPadding">2dp</item> + <item name="roundedTextVerticalPadding">2dp</item> + <item name="roundedTextDrawable">@drawable/rounded_text_bg</item> + <item name="roundedTextDrawableLeft">@drawable/rounded_text_bg_left</item> + <item name="roundedTextDrawableMid">@drawable/rounded_text_bg_mid</item> + <item name="roundedTextDrawableRight">@drawable/rounded_text_bg_right</item> + </style> + +</resources> diff --git a/ui/text/RoundedBackground-Kotlin/settings.gradle b/ui/text/RoundedBackground-Kotlin/settings.gradle new file mode 100644 index 00000000..3cbe2493 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/settings.gradle @@ -0,0 +1 @@ +include ':app', ':lib' diff --git a/ui/text/screenshots/lines.png b/ui/text/screenshots/lines.png Binary files differnew file mode 100644 index 00000000..dbc86d17 --- /dev/null +++ b/ui/text/screenshots/lines.png diff --git a/ui/text/screenshots/multi.png b/ui/text/screenshots/multi.png Binary files differnew file mode 100644 index 00000000..e37d1e14 --- /dev/null +++ b/ui/text/screenshots/multi.png diff --git a/ui/text/screenshots/rounded_bg.png b/ui/text/screenshots/rounded_bg.png Binary files differnew file mode 100644 index 00000000..1e9acddf --- /dev/null +++ b/ui/text/screenshots/rounded_bg.png diff --git a/ui/text/screenshots/rtl.png b/ui/text/screenshots/rtl.png Binary files differnew file mode 100644 index 00000000..6d8aba31 --- /dev/null +++ b/ui/text/screenshots/rtl.png diff --git a/ui/text/screenshots/single.png b/ui/text/screenshots/single.png Binary files differnew file mode 100644 index 00000000..b264682e --- /dev/null +++ b/ui/text/screenshots/single.png |