Skip to content

Commit

Permalink
Added MaterialTextView component, which supports the ability to pass …
Browse files Browse the repository at this point in the history
…the line height value via text appearance style.

PiperOrigin-RevId: 254790727
  • Loading branch information
raajkumars authored and ymarian committed Jun 24, 2019
1 parent bd4005b commit 12024f6
Show file tree
Hide file tree
Showing 15 changed files with 580 additions and 0 deletions.
54 changes: 54 additions & 0 deletions docs/components/MaterialTextView.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<!--docs:
title: "Material Text View"
layout: detail
section: components
excerpt: "MaterialTextView displays text to the user."
iconId: text_view
path: /catalog/material-text-view/
-->

# Material Text View

A MaterialTextView is a derivative of AppCompatTextView that displays text to
the user. To provide user-editable text, see {@link EditText}.

## Design & API Documentation

- [Class definition](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/textview/MaterialTextView.java)
<!--{: .icon-list-item.icon-list-item--link }-->

## Usage

Example code of how to include the component in your layout is listed here for
reference.

```xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<MaterialTextView
android:id="@+id/text_view_id"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/hello" />
</LinearLayout>
```

### Attributes

`MaterialTextView` supports all of the standard attributes that can be changed
for a
[`AppCompatTextView`](https://developer.android.com/reference/android/support/v7/widget/AppCompatTextView).
Unlike the `AppCompatTextView` which supports specifying the line height only in
a view layout XML, `MaterialTextView` supports the ability to read the line
height from a `TextAppearance` style, which can be applied to the
`MaterialTextView` either using the `style` attribute or using the
`android:textAppearance` attribute.

The following additional attributes can be changed in `TextAppearance` and
applied to a `MaterialTextView`:

Feature | Relevant attributes
:---------- | :-------------------
Line Height | `android:lineHeight`
1 change: 1 addition & 0 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def srcDirs = [
'com/google/android/material/switchmaterial',
'com/google/android/material/tabs',
'com/google/android/material/textfield',
'com/google/android/material/textview',
'com/google/android/material/theme',
'com/google/android/material/transformation',
'com/google/android/material/typography',
Expand Down
22 changes: 22 additions & 0 deletions lib/java/com/google/android/material/textview/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2019 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
~
~ https://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.google.android.material.textview">

<application/>
</manifest>
162 changes: 162 additions & 0 deletions lib/java/com/google/android/material/textview/MaterialTextView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* Copyright (C) 2019 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.google.android.material.textview;

import com.google.android.material.R;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.content.res.TypedArray;
import androidx.appcompat.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.widget.EditText;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* A MaterialTextView is a derivative of {@link AppCompatTextView} that displays text to the user.
* To provide user-editable text, see {@link EditText}.
*
* <p>MaterialTextView supports the ability to read and apply {@code android:lineHeight} value from
* a {@code TextAppearance} style.
*
* <p>The following code sample shows a typical use, with an XML layout and code to modify the
* contents of the material text view:
*
* <pre>
* &lt;LinearLayout
* xmlns:android="http://schemas.android.com/apk/res/android"
* android:layout_width="match_parent"
* android:layout_height="match_parent"&gt;
* &lt;MaterialTextView
* android:id="@+id/text_view_id"
* android:layout_height="wrap_content"
* android:layout_width="wrap_content"
* android:text="@string/hello" /&gt;
* &lt;/LinearLayout&gt;
* </pre>
*
* <p>This code sample demonstrates how to modify the contents of the material text view defined in
* the previous XML layout:
*
* <pre>
* public class MainActivity extends Activity {
*
* protected void onCreate(Bundle savedInstanceState) {
* super.onCreate(savedInstanceState);
* setContentView(R.layout.activity_main);
* final MaterialTextView textView = (MaterialTextView) findViewById(R.id.text_view_id);
* textView.setText(R.string.user_greeting);
* }
* }
* </pre>
*
* <p>To customize the appearance of MaterialTextView, see <a
* href="https://developer.android.com/guide/topics/ui/themes.html">Styles and Themes</a>.
*/
public class MaterialTextView extends AppCompatTextView {

/**
* Flag that will be used to avoid applying line height more than once when {@link
* #setTextAppearance(int)} method is called, the base implementation of which may call the {@link
* #setTextAppearance(Context, int)} internally.
*/
private final AtomicBoolean skipApplyingLineHeight = new AtomicBoolean();

public MaterialTextView(Context context) {
this(context, null /* attrs */);
}

public MaterialTextView(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.textViewStyle);
}

public MaterialTextView(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}

public MaterialTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr);

final Resources.Theme theme = context.getTheme();

if (readLineHeightFromLayout(theme, attrs, defStyleAttr, defStyleRes) < 0) {
int resId = findViewAppearanceResourceId(theme, attrs, defStyleAttr, defStyleRes);
if (resId != -1) {
applyLineHeightFromViewAppearance(theme, resId);
}
}
}

@Override
public void setTextAppearance(int resId) {
skipApplyingLineHeight.set(true);
super.setTextAppearance(resId);
skipApplyingLineHeight.set(false);
applyLineHeightFromViewAppearance(getContext().getTheme(), resId);
}

@Override
public void setTextAppearance(Context context, int resId) {
super.setTextAppearance(context, resId);
if (!skipApplyingLineHeight.get()) {
applyLineHeightFromViewAppearance(context.getTheme(), resId);
}
}

private void applyLineHeightFromViewAppearance(Theme theme, int resId) {
TypedArray attributes = theme.obtainStyledAttributes(resId, R.styleable.MaterialTextAppearance);
int lineHeight = readLineHeighAttribute(attributes);
attributes.recycle();

if (lineHeight >= 0) {
setLineHeight(lineHeight);
}
}

private static int readLineHeightFromLayout(
Theme theme, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
TypedArray attributes =
theme.obtainStyledAttributes(
attrs, R.styleable.MaterialTextView, defStyleAttr, defStyleRes);
int lineHeight = readLineHeighAttribute(attributes);
attributes.recycle();

return lineHeight;
}

private static int readLineHeighAttribute(TypedArray attributes) {
int lineHeight =
attributes.getDimensionPixelSize(R.styleable.MaterialTextView_android_lineHeight, -1);
if (lineHeight < 0) {
lineHeight = attributes.getDimensionPixelSize(R.styleable.MaterialTextView_lineHeight, -1);
}

return lineHeight;
}

private static int findViewAppearanceResourceId(
Theme theme, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
TypedArray attributes =
theme.obtainStyledAttributes(
attrs, R.styleable.MaterialTextView, defStyleAttr, defStyleRes);
int appearanceAttrId =
attributes.getResourceId(R.styleable.MaterialTextView_android_textAppearance, -1);
attributes.recycle();
return appearanceAttrId;
}
}
37 changes: 37 additions & 0 deletions lib/java/com/google/android/material/textview/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'

archivesBaseName = getArchivesBaseName(project.name)
version = rootProject.ext.mdcLibraryVersion

dependencies {
implementation compatibility("appcompat")

implementation project(fromPath("lib/java/com/google/android/material/animation"))
implementation project(fromPath("lib/java/com/google/android/material/internal"))
implementation project(fromPath("lib/java/com/google/android/material/math"))
implementation project(fromPath("lib/java/com/google/android/material/shape"))
}

android {
sourceSets {
main.manifest.srcFile 'AndroidManifest.xml'
main.java.srcDir '.'
main.java.excludes = [
'**/build/**',
]
main.res.srcDirs = [
'res',
'res-public'
]
main.assets.srcDir 'assets'
}
}

uploadArchives {
repositories {
mavenDeployer {
repository(url: rootProject.ext.mavenRepoUrl)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2019 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
~
~ https://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.
-->

<!-- Definitions of attributes to be exposed as public -->
<resources>
<public name="Widget.MaterialComponents.TextView" type="style" />
</resources>
33 changes: 33 additions & 0 deletions lib/java/com/google/android/material/textview/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2019 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
~
~ https://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="MaterialTextAppearance" parent="TextAppearance">
<!--
Specifies explicit line height for this TextView. This is equivalent to the vertical
distance between subsequent baselines in the TextView.
-->
<attr name="android:lineHeight"/>
<attr name="lineHeight"/>
</declare-styleable>

<declare-styleable name="MaterialTextView" parent="AppCompatTextView">
<attr name="android:textAppearance"/>
<attr name="android:lineHeight"/>
<attr name="lineHeight"/>
</declare-styleable>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2019 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
~
~ https://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 style for TextView. You should use one of the sub-styles instead. -->
<style name="Base.Widget.MaterialComponents.TextView" parent="Widget.AppCompat.TextView" />
<style name="Widget.MaterialComponents.TextView" parent="Base.Widget.MaterialComponents.TextView" />
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2019 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"
xmlns:tools="http://schemas.android.com/tools"
package="com.google.android.material.textview">

<uses-sdk
tools:overrideLibrary="androidx.test.core"/>

<application/>
</manifest>
Loading

0 comments on commit 12024f6

Please sign in to comment.