Skip to content

Commit 85911c9

Browse files
committed
[UPDATE] Use ToolTip.Builder() with CharSequence message that is more inclusive than String and Spannable.
[REMOVED] Redundant `ToolTip.Builder()` with `Spannable` message after the API update, and updated code accordingly. [ADDED] First time tooltip message with span to showcase the possibility. [ADDED] `@Nullable` and `@NonNull` to be future proof _(kotlin tsk tsk)_.
1 parent 973e4bb commit 85911c9

File tree

4 files changed

+49
-49
lines changed

4 files changed

+49
-49
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
package com.tomergoldst.tooltipsdemo;
1818

19+
import android.graphics.Color;
1920
import android.graphics.Typeface;
2021
import android.os.Bundle;
2122
import android.support.design.widget.TextInputEditText;
2223
import android.support.v7.app.AppCompatActivity;
24+
import android.text.Html;
2325
import android.text.TextUtils;
2426
import android.util.Log;
2527
import android.view.Gravity;
@@ -127,8 +129,15 @@ public boolean onOptionsItemSelected(MenuItem item) {
127129
public void onWindowFocusChanged(boolean hasFocus) {
128130
super.onWindowFocusChanged(hasFocus);
129131

130-
ToolTip.Builder builder = new ToolTip.Builder(this, mTextView, mRootLayout, TIP_TEXT, ToolTip.POSITION_ABOVE);
132+
// This tip is shows the first time the sample app is loaded. Use a message that gives user
133+
// guide on how to use the sample app. Also try to showcase the ability of the app.
134+
CharSequence initialGuideTex = Html.fromHtml("Click on the <a href='#'>Buttons</a> " +
135+
"and the <a href='#'>Radio Buttons</a> bellow to test various tool tip configurations." +
136+
"<br><br><font color='grey'><small>GOT IT</small></font>");
137+
138+
ToolTip.Builder builder = new ToolTip.Builder(this, mTextView, mRootLayout, initialGuideTex, ToolTip.POSITION_ABOVE);
131139
builder.setAlign(mAlign);
140+
builder.setBackgroundColor(Color.DKGRAY);
132141
builder.setTextAppearance(R.style.TooltipTextAppearance);
133142
mToolTipsManager.show(builder.build());
134143
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
-->
2424
<style name="TooltipTextAppearance">
2525
<item name="android:textColor">@android:color/white</item>
26+
<item name="android:textColorLink">@android:color/holo_blue_light</item>
2627
<item name="android:textSize">16sp</item>
2728
<item name="android:textStyle">bold</item>
2829
</style>

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

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
import android.content.Context;
2020
import android.graphics.Typeface;
2121
import android.support.annotation.IntDef;
22+
import android.support.annotation.NonNull;
2223
import android.support.annotation.Nullable;
2324
import android.support.annotation.StyleRes;
24-
import android.text.Spannable;
2525
import android.view.View;
2626
import android.view.ViewGroup;
2727

@@ -51,10 +51,10 @@ public class ToolTip {
5151
public static final int GRAVITY_LEFT = 1;
5252
public static final int GRAVITY_RIGHT = 2;
5353

54-
private Context mContext;
55-
private View mAnchorView;
56-
private ViewGroup mRootViewGroup;
57-
private String mMessage;
54+
private @NonNull Context mContext;
55+
private @NonNull View mAnchorView;
56+
private @NonNull ViewGroup mRootViewGroup;
57+
private @NonNull CharSequence mMessage;
5858
private @Position int mPosition;
5959
private @Align int mAlign;
6060
private int mOffsetX;
@@ -63,9 +63,8 @@ public class ToolTip {
6363
private int mBackgroundColor;
6464
private float mElevation;
6565
private @Gravity int mTextGravity;
66-
private Spannable mSpannableMessage;
6766
private @StyleRes int mTextAppearanceStyle;
68-
private Typeface mTypeface;
67+
private @Nullable Typeface mTypeface;
6968

7069
public ToolTip(Builder builder){
7170
mContext = builder.mContext;
@@ -81,24 +80,27 @@ public ToolTip(Builder builder){
8180
mBackgroundColor = builder.mBackgroundColor;
8281
mElevation = builder.mElevation;
8382
mTextGravity = builder.mTextGravity;
84-
mSpannableMessage = builder.mSpannableMessage;
8583
mTextAppearanceStyle = builder.mTextAppearanceStyle;
8684
mTypeface = builder.mTypeface;
8785
}
8886

87+
@NonNull
8988
public Context getContext() {
9089
return mContext;
9190
}
9291

92+
@NonNull
9393
public View getAnchorView() {
9494
return mAnchorView;
9595
}
9696

97+
@NonNull
9798
public ViewGroup getRootView() {
9899
return mRootViewGroup;
99100
}
100101

101-
public String getMessage() {
102+
@NonNull
103+
public CharSequence getMessage() {
102104
return mMessage;
103105
}
104106

@@ -172,6 +174,7 @@ public Typeface getTypeface() {
172174
return mTypeface;
173175
}
174176

177+
@NonNull
175178
public int getTextGravity(){
176179
int gravity;
177180
switch (mTextGravity){
@@ -190,15 +193,11 @@ public int getTextGravity(){
190193
return gravity;
191194
}
192195

193-
public Spannable getSpannableMessage() {
194-
return mSpannableMessage;
195-
}
196-
197196
public static class Builder {
198-
private Context mContext;
199-
private View mAnchorView;
200-
private ViewGroup mRootViewGroup;
201-
private String mMessage;
197+
private @NonNull Context mContext;
198+
private @NonNull View mAnchorView;
199+
private @NonNull ViewGroup mRootViewGroup;
200+
private @NonNull CharSequence mMessage;
202201
private @Position int mPosition;
203202
private @Align int mAlign;
204203
private int mOffsetX;
@@ -207,48 +206,28 @@ public static class Builder {
207206
private int mBackgroundColor;
208207
private float mElevation;
209208
private @Gravity int mTextGravity;
210-
private Spannable mSpannableMessage;
211209
private @StyleRes int mTextAppearanceStyle;
212-
private Typeface mTypeface;
210+
private @Nullable Typeface mTypeface;
213211

214212

215213
/**
214+
* Creates the tooltip builder with message and required parameters to show tooltip.
216215
*
217216
* @param context context
218217
* @param anchorView the view which near it we want to put the tip
219218
* @param root a class extends ViewGroup which the created tip view will be added to
220-
* @param message message to show
221-
* @param position put the tip above / below / left to / right to
219+
* @param message message to show. Note: This allows normal text and spannable text with spanned styles.
220+
* @param position put the tip above / below / left to / right to anchor view.
222221
*/
223-
public Builder(Context context, View anchorView, ViewGroup root, String message, @Position int position){
222+
public Builder(@NonNull Context context,
223+
@NonNull View anchorView,
224+
@NonNull ViewGroup root,
225+
@NonNull CharSequence message,
226+
@Position int position) {
224227
mContext = context;
225228
mAnchorView = anchorView;
226229
mRootViewGroup = root;
227230
mMessage = message;
228-
mSpannableMessage = null;
229-
mPosition = position;
230-
mAlign = ALIGN_CENTER;
231-
mOffsetX = 0;
232-
mOffsetY = 0;
233-
mArrow = true;
234-
mBackgroundColor = context.getResources().getColor(R.color.colorBackground);
235-
mTextGravity = GRAVITY_LEFT;
236-
mTextAppearanceStyle = R.style.TooltipDefaultStyle;
237-
}
238-
239-
/**
240-
* @param context context
241-
* @param anchorView the view which near it we want to put the tip
242-
* @param root a class extends ViewGroup which the created tip view will be added to
243-
* @param message spannable message to show
244-
* @param position put the tip above / below / left to / right to
245-
*/
246-
public Builder(Context context, View anchorView, ViewGroup root, Spannable message, @Position int position) {
247-
mContext = context;
248-
mAnchorView = anchorView;
249-
mRootViewGroup = root;
250-
mMessage = null;
251-
mSpannableMessage = message;
252231
mPosition = position;
253232
mAlign = ALIGN_CENTER;
254233
mOffsetX = 0;
@@ -259,11 +238,13 @@ public Builder(Context context, View anchorView, ViewGroup root, Spannable messa
259238
mTextAppearanceStyle = R.style.TooltipDefaultStyle;
260239
}
261240

241+
@NonNull
262242
public Builder setPosition(@Position int position){
263243
mPosition = position;
264244
return this;
265245
}
266246

247+
@NonNull
267248
public Builder setAlign(@Align int align){
268249
mAlign = align;
269250
return this;
@@ -273,6 +254,7 @@ public Builder setAlign(@Align int align){
273254
* @param offset offset to move the tip on x axis after tip was positioned
274255
* @return offset
275256
*/
257+
@NonNull
276258
public Builder setOffsetX(int offset){
277259
mOffsetX = offset;
278260
return this;
@@ -282,41 +264,49 @@ public Builder setOffsetX(int offset){
282264
* @param offset offset to move the tip on y axis after tip was positioned
283265
* @return offset
284266
*/
267+
@NonNull
285268
public Builder setOffsetY(int offset){
286269
mOffsetY = offset;
287270
return this;
288271
}
289272

273+
@NonNull
290274
public Builder withArrow(boolean value){
291275
mArrow = value;
292276
return this;
293277
}
294278

279+
@NonNull
295280
public Builder setBackgroundColor(int color){
296281
mBackgroundColor = color;
297282
return this;
298283
}
299284

285+
@NonNull
300286
public Builder setElevation(float elevation){
301287
mElevation = elevation;
302288
return this;
303289
}
304290

291+
@NonNull
305292
public Builder setGravity(@Gravity int gravity){
306293
mTextGravity = gravity;
307294
return this;
308295
}
309296

297+
@NonNull
310298
public Builder setTextAppearance(@StyleRes int textAppearance) {
311299
mTextAppearanceStyle = textAppearance;
312300
return this;
313301
}
314302

315-
public Builder setTypeface(Typeface typeface) {
303+
@NonNull
304+
public Builder setTypeface(@NonNull Typeface typeface) {
316305
mTypeface = typeface;
317306
return this;
318307
}
319308

309+
@NonNull
320310
public ToolTip build(){
321311
return new ToolTip(this);
322312
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ private void moveTipToCorrectPosition(TextView tipView, Point p) {
139139
@NonNull
140140
private TextView createTipView(ToolTip toolTip) {
141141
TextView tipView = new TextView(toolTip.getContext());
142-
tipView.setText(toolTip.getMessage() != null ? toolTip.getMessage() : toolTip.getSpannableMessage());
142+
tipView.setText(toolTip.getMessage());
143143
tipView.setVisibility(View.INVISIBLE);
144144
tipView.setGravity(toolTip.getTextGravity());
145145
tipView.setTextAppearance(toolTip.getContext(), toolTip.getTextAppearanceStyle());

0 commit comments

Comments
 (0)