Skip to content

Commit fbe3553

Browse files
committed
[ADDED] New setTypeface(Typeface typeface) and setTextAppearance(@StyleRes int textAppearance) API to allow library consumer to set text size color style and other attibutes. The typeface API allows the library consumers to set custom font to conform to branding look.
[REMOVED] `setTextSize()` and `setTextColor()` API as it was redundant and covered by the new `setTextAppearance(@StyleRes int textAppearance)` API. [ADDED] Custom font https://fonts.google.com/specimen/Pacifico?selection.family=Pacifico to show case the `setTypeface(Typeface typeface)` API. Included the license in the assets directory.
1 parent b20bcd6 commit fbe3553

File tree

9 files changed

+232
-34
lines changed

9 files changed

+232
-34
lines changed

app/src/main/assets/OFL.txt

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
Copyright 2011 The Pacifico Project Authors (https://github.com/Fonthausen/Pacifico)
2+
3+
This Font Software is licensed under the SIL Open Font License, Version 1.1.
4+
This license is copied below, and is also available with a FAQ at:
5+
http://scripts.sil.org/OFL
6+
7+
8+
-----------------------------------------------------------
9+
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
10+
-----------------------------------------------------------
11+
12+
PREAMBLE
13+
The goals of the Open Font License (OFL) are to stimulate worldwide
14+
development of collaborative font projects, to support the font creation
15+
efforts of academic and linguistic communities, and to provide a free and
16+
open framework in which fonts may be shared and improved in partnership
17+
with others.
18+
19+
The OFL allows the licensed fonts to be used, studied, modified and
20+
redistributed freely as long as they are not sold by themselves. The
21+
fonts, including any derivative works, can be bundled, embedded,
22+
redistributed and/or sold with any software provided that any reserved
23+
names are not used by derivative works. The fonts and derivatives,
24+
however, cannot be released under any other type of license. The
25+
requirement for fonts to remain under this license does not apply
26+
to any document created using the fonts or their derivatives.
27+
28+
DEFINITIONS
29+
"Font Software" refers to the set of files released by the Copyright
30+
Holder(s) under this license and clearly marked as such. This may
31+
include source files, build scripts and documentation.
32+
33+
"Reserved Font Name" refers to any names specified as such after the
34+
copyright statement(s).
35+
36+
"Original Version" refers to the collection of Font Software components as
37+
distributed by the Copyright Holder(s).
38+
39+
"Modified Version" refers to any derivative made by adding to, deleting,
40+
or substituting -- in part or in whole -- any of the components of the
41+
Original Version, by changing formats or by porting the Font Software to a
42+
new environment.
43+
44+
"Author" refers to any designer, engineer, programmer, technical
45+
writer or other person who contributed to the Font Software.
46+
47+
PERMISSION & CONDITIONS
48+
Permission is hereby granted, free of charge, to any person obtaining
49+
a copy of the Font Software, to use, study, copy, merge, embed, modify,
50+
redistribute, and sell modified and unmodified copies of the Font
51+
Software, subject to the following conditions:
52+
53+
1) Neither the Font Software nor any of its individual components,
54+
in Original or Modified Versions, may be sold by itself.
55+
56+
2) Original or Modified Versions of the Font Software may be bundled,
57+
redistributed and/or sold with any software, provided that each copy
58+
contains the above copyright notice and this license. These can be
59+
included either as stand-alone text files, human-readable headers or
60+
in the appropriate machine-readable metadata fields within text or
61+
binary files as long as those fields can be easily viewed by the user.
62+
63+
3) No Modified Version of the Font Software may use the Reserved Font
64+
Name(s) unless explicit written permission is granted by the corresponding
65+
Copyright Holder. This restriction only applies to the primary font name as
66+
presented to the users.
67+
68+
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
69+
Software shall not be used to promote, endorse or advertise any
70+
Modified Version, except to acknowledge the contribution(s) of the
71+
Copyright Holder(s) and the Author(s) or with their explicit written
72+
permission.
73+
74+
5) The Font Software, modified or unmodified, in part or in whole,
75+
must be distributed entirely under this license, and must not be
76+
distributed under any other license. The requirement for fonts to
77+
remain under this license does not apply to any document created
78+
using the Font Software.
79+
80+
TERMINATION
81+
This license becomes null and void if any of the above conditions are
82+
not met.
83+
84+
DISCLAIMER
85+
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
86+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
87+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
88+
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
89+
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
90+
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
91+
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
92+
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
93+
OTHER DEALINGS IN THE FONT SOFTWARE.
225 KB
Binary file not shown.

app/src/main/java/com/tomergoldst/tooltipsdemo/MainActivity.java

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,22 @@
1616

1717
package com.tomergoldst.tooltipsdemo;
1818

19+
import android.graphics.Typeface;
1920
import android.os.Bundle;
2021
import android.support.design.widget.TextInputEditText;
2122
import android.support.v7.app.AppCompatActivity;
2223
import android.text.TextUtils;
2324
import android.util.Log;
25+
import android.view.Gravity;
26+
import android.view.Menu;
27+
import android.view.MenuInflater;
28+
import android.view.MenuItem;
2429
import android.view.View;
2530
import android.widget.Button;
2631
import android.widget.RadioButton;
2732
import android.widget.RelativeLayout;
2833
import android.widget.TextView;
34+
import android.widget.Toast;
2935

3036
import com.tomergoldst.tooltips.ToolTip;
3137
import com.tomergoldst.tooltips.ToolTipsManager;
@@ -36,7 +42,9 @@ public class MainActivity extends AppCompatActivity implements
3642
View.OnClickListener{
3743

3844
private static final String TAG = MainActivity.class.getSimpleName();
39-
public static final String TIP_TEXT = "Tip";
45+
public static final String TIP_TEXT = "Tool Tip";
46+
public static final String TIP_TEXT_SMALL = "Small Tool Tip";
47+
public static final String TIP_TEXT_LARGE = "Large Tool Tip";
4048

4149
ToolTipsManager mToolTipsManager;
4250
RelativeLayout mRootLayout;
@@ -54,6 +62,7 @@ public class MainActivity extends AppCompatActivity implements
5462
RadioButton mAlignCenter;
5563

5664
@ToolTip.Align int mAlign = ToolTip.ALIGN_CENTER;
65+
Typeface mCustomFont = null;
5766

5867
@Override
5968
protected void onCreate(Bundle savedInstanceState) {
@@ -91,12 +100,36 @@ protected void onCreate(Bundle savedInstanceState) {
91100

92101
}
93102

103+
@Override
104+
public boolean onCreateOptionsMenu(Menu menu) {
105+
MenuInflater inflater = getMenuInflater();
106+
inflater.inflate(R.menu.main_activity_actions, menu);
107+
return true;
108+
}
109+
110+
@Override
111+
public boolean onOptionsItemSelected(MenuItem item) {
112+
// Handle item selection
113+
switch (item.getItemId()) {
114+
case R.id.use_custom_font_menu_item:
115+
mCustomFont = Typeface.createFromAsset(getAssets(), "Pacifico-Regular.ttf");
116+
Toast toast = Toast.makeText(this, "Custom font set. Re-try demo.", Toast.LENGTH_SHORT);
117+
toast.setGravity(Gravity.CENTER, 0, 0);
118+
toast.show();
119+
return true;
120+
default:
121+
return super.onOptionsItemSelected(item);
122+
}
123+
}
124+
125+
94126
@Override
95127
public void onWindowFocusChanged(boolean hasFocus) {
96128
super.onWindowFocusChanged(hasFocus);
97129

98130
ToolTip.Builder builder = new ToolTip.Builder(this, mTextView, mRootLayout, TIP_TEXT, ToolTip.POSITION_ABOVE);
99131
builder.setAlign(mAlign);
132+
builder.setTextAppearance(R.style.TooltipTextAppearance);
100133
mToolTipsManager.show(builder.build());
101134
}
102135

@@ -119,29 +152,33 @@ public void onClick(View view) {
119152
mToolTipsManager.findAndDismiss(mTextView);
120153
builder = new ToolTip.Builder(this, mTextView, mRootLayout, text, ToolTip.POSITION_ABOVE);
121154
builder.setAlign(mAlign);
155+
builder.setTypeface(mCustomFont);
122156
mToolTipsManager.show(builder.build());
123157
break;
124158
case R.id.button_below:
125159
mToolTipsManager.findAndDismiss(mTextView);
126160
builder = new ToolTip.Builder(this, mTextView, mRootLayout, text, ToolTip.POSITION_BELOW);
127161
builder.setAlign(mAlign);
162+
builder.setTextAppearance(R.style.TooltipTextAppearance);
163+
builder.setTypeface(mCustomFont);
128164
builder.setBackgroundColor(getResources().getColor(R.color.colorOrange));
129165
mToolTipsManager.show(builder.build());
130166
break;
131167
case R.id.button_left_to:
132168
mToolTipsManager.findAndDismiss(mTextView);
133-
builder = new ToolTip.Builder(this, mTextView, mRootLayout, text, ToolTip.POSITION_LEFT_TO);
169+
builder = new ToolTip.Builder(this, mTextView, mRootLayout, TIP_TEXT.equals(text) ? TIP_TEXT_SMALL : text, ToolTip.POSITION_LEFT_TO);
134170
builder.setBackgroundColor(getResources().getColor(R.color.colorLightGreen));
135-
builder.setTextColor(getResources().getColor(R.color.colorBlack));
136171
builder.setGravity(ToolTip.GRAVITY_CENTER);
137-
builder.setTextSize(12);
172+
builder.setTypeface(mCustomFont);
173+
builder.setTextAppearance(R.style.TooltipTextAppearance_Small_Black);
138174
mToolTipsManager.show(builder.build());
139175
break;
140176
case R.id.button_right_to:
141177
mToolTipsManager.findAndDismiss(mTextView);
142-
builder = new ToolTip.Builder(this, mTextView, mRootLayout, text, ToolTip.POSITION_RIGHT_TO);
178+
builder = new ToolTip.Builder(this, mTextView, mRootLayout, TIP_TEXT.equals(text) ? TIP_TEXT_LARGE : text, ToolTip.POSITION_RIGHT_TO);
143179
builder.setBackgroundColor(getResources().getColor(R.color.colorDarkRed));
144-
builder.setTextColor(getResources().getColor(android.R.color.white));
180+
builder.setTextAppearance(R.style.TooltipTextAppearance_Large);
181+
builder.setTypeface(mCustomFont);
145182
mToolTipsManager.show(builder.build());
146183
break;
147184
case R.id.button_align_center:
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<menu xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools">
5+
6+
<!-- Allows to use custom font -->
7+
<item
8+
android:id="@+id/use_custom_font_menu_item"
9+
android:title="Use Custom Font"
10+
android:visible="true"
11+
app:showAsAction="always"
12+
tools:ignore="HardcodedText" />
13+
</menu>

app/src/main/res/values/styles.xml

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,35 @@
88
<item name="colorAccent">@color/colorAccent</item>
99
</style>
1010

11-
</resources>
11+
12+
<!--
13+
Text appearance style allows you to override multiple properties at the same time.
14+
Android platform allows you to set following text appearances:
15+
* textColor
16+
* textColorHighlight
17+
* textColorHint
18+
* textColorLink
19+
* textSize
20+
* textStyle
21+
22+
Reference: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/styles.xml#805
23+
-->
24+
<style name="TooltipTextAppearance">
25+
<item name="android:textColor">@android:color/white</item>
26+
<item name="android:textSize">16sp</item>
27+
<item name="android:textStyle">bold</item>
28+
</style>
29+
30+
<style name="TooltipTextAppearance.Small">
31+
<item name="android:textSize">12sp</item>
32+
</style>
33+
34+
<style name="TooltipTextAppearance.Small.Black">
35+
<item name="android:textColor">@android:color/black</item>
36+
</style>
37+
38+
<style name="TooltipTextAppearance.Large">
39+
<item name="android:textSize">18sp</item>
40+
</style>
41+
42+
</resources>

tooltips/src/main/java/com/tomergoldst/tooltips/ToolTip.java

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
package com.tomergoldst.tooltips;
1818

1919
import android.content.Context;
20+
import android.graphics.Typeface;
2021
import android.support.annotation.IntDef;
22+
import android.support.annotation.Nullable;
23+
import android.support.annotation.StyleRes;
2124
import android.text.Spannable;
2225
import android.view.View;
2326
import android.view.ViewGroup;
@@ -58,11 +61,11 @@ public class ToolTip {
5861
private int mOffsetY;
5962
private boolean mArrow;
6063
private int mBackgroundColor;
61-
private int mTextColor;
6264
private float mElevation;
6365
private @Gravity int mTextGravity;
6466
private Spannable mSpannableMessage;
65-
private int mTextSize;
67+
private @StyleRes int mTextAppearanceStyle;
68+
private Typeface mTypeface;
6669

6770
public ToolTip(Builder builder){
6871
mContext = builder.mContext;
@@ -76,11 +79,11 @@ public ToolTip(Builder builder){
7679
mOffsetY = builder.mOffsetY;
7780
mArrow = builder.mArrow;
7881
mBackgroundColor = builder.mBackgroundColor;
79-
mTextColor = builder.mTextColor;
8082
mElevation = builder.mElevation;
8183
mTextGravity = builder.mTextGravity;
8284
mSpannableMessage = builder.mSpannableMessage;
83-
mTextSize = builder.mTextSize;
85+
mTextAppearanceStyle = builder.mTextAppearanceStyle;
86+
mTypeface = builder.mTypeface;
8487
}
8588

8689
public Context getContext() {
@@ -123,10 +126,6 @@ public int getBackgroundColor() {
123126
return mBackgroundColor;
124127
}
125128

126-
public int getTextColor() {
127-
return mTextColor;
128-
}
129-
130129
public boolean positionedLeftTo(){
131130
return POSITION_LEFT_TO == mPosition;
132131
}
@@ -163,8 +162,14 @@ public float getElevation() {
163162
return mElevation;
164163
}
165164

166-
public int getTextSize() {
167-
return mTextSize;
165+
@StyleRes
166+
public int getTextAppearanceStyle() {
167+
return mTextAppearanceStyle;
168+
}
169+
170+
@Nullable
171+
public Typeface getTypeface() {
172+
return mTypeface;
168173
}
169174

170175
public int getTextGravity(){
@@ -200,11 +205,11 @@ public static class Builder {
200205
private int mOffsetY;
201206
private boolean mArrow;
202207
private int mBackgroundColor;
203-
private int mTextColor;
204208
private float mElevation;
205209
private @Gravity int mTextGravity;
206210
private Spannable mSpannableMessage;
207-
private int mTextSize;
211+
private @StyleRes int mTextAppearanceStyle;
212+
private Typeface mTypeface;
208213

209214

210215
/**
@@ -227,9 +232,8 @@ public Builder(Context context, View anchorView, ViewGroup root, String message,
227232
mOffsetY = 0;
228233
mArrow = true;
229234
mBackgroundColor = context.getResources().getColor(R.color.colorBackground);
230-
mTextColor = context.getResources().getColor(R.color.colorText);
231235
mTextGravity = GRAVITY_LEFT;
232-
mTextSize = 14;
236+
mTextAppearanceStyle = R.style.TooltipDefaultStyle;
233237
}
234238

235239
/**
@@ -251,9 +255,8 @@ public Builder(Context context, View anchorView, ViewGroup root, Spannable messa
251255
mOffsetY = 0;
252256
mArrow = true;
253257
mBackgroundColor = context.getResources().getColor(R.color.colorBackground);
254-
mTextColor = context.getResources().getColor(R.color.colorText);
255258
mTextGravity = GRAVITY_LEFT;
256-
mTextSize = 14;
259+
mTextAppearanceStyle = R.style.TooltipDefaultStyle;
257260
}
258261

259262
public Builder setPosition(@Position int position){
@@ -294,11 +297,6 @@ public Builder setBackgroundColor(int color){
294297
return this;
295298
}
296299

297-
public Builder setTextColor(int color){
298-
mTextColor = color;
299-
return this;
300-
}
301-
302300
public Builder setElevation(float elevation){
303301
mElevation = elevation;
304302
return this;
@@ -309,8 +307,13 @@ public Builder setGravity(@Gravity int gravity){
309307
return this;
310308
}
311309

312-
public Builder setTextSize(int sizeInSp) {
313-
mTextSize = sizeInSp;
310+
public Builder setTextAppearance(@StyleRes int textAppearance) {
311+
mTextAppearanceStyle = textAppearance;
312+
return this;
313+
}
314+
315+
public Builder setTypeface(Typeface typeface) {
316+
mTypeface = typeface;
314317
return this;
315318
}
316319

0 commit comments

Comments
 (0)