├── .gitignore
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── tomergoldst
│ │ └── tooltipsdemo
│ │ └── ApplicationTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── assets
│ │ ├── OFL.txt
│ │ └── Pacifico-Regular.ttf
│ ├── java
│ │ └── com
│ │ │ └── tomergoldst
│ │ │ └── tooltipsdemo
│ │ │ └── MainActivity.java
│ └── res
│ │ ├── layout
│ │ └── activity_main.xml
│ │ ├── menu
│ │ └── main_activity_actions.xml
│ │ ├── mipmap-hdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-mdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xhdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xxhdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xxxhdpi
│ │ └── ic_launcher.png
│ │ ├── values-w820dp
│ │ └── dimens.xml
│ │ └── values
│ │ ├── colors.xml
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── tomergoldst
│ └── tooltipsdemo
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── tooltips
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
├── androidTest
└── java
│ └── com
│ └── tomergoldst
│ └── tooltips
│ └── ApplicationTest.java
├── main
├── AndroidManifest.xml
├── java
│ └── com
│ │ └── tomergoldst
│ │ └── tooltips
│ │ ├── Coordinates.java
│ │ ├── DefaultToolTipAnimator.java
│ │ ├── ToolTip.java
│ │ ├── ToolTipAnimator.java
│ │ ├── ToolTipBackgroundConstructor.java
│ │ ├── ToolTipCoordinatesFinder.java
│ │ ├── ToolTipsManager.java
│ │ └── UiUtils.java
└── res
│ ├── drawable-xxhdpi
│ ├── tooltip_arrow_down.9.png
│ ├── tooltip_arrow_down_left.9.png
│ ├── tooltip_arrow_down_right.9.png
│ ├── tooltip_arrow_left.9.png
│ ├── tooltip_arrow_right.9.png
│ ├── tooltip_arrow_up.9.png
│ ├── tooltip_arrow_up_left.9.png
│ ├── tooltip_arrow_up_right.9.png
│ └── tooltip_no_arrow.9.png
│ └── values
│ ├── colors.xml
│ ├── strings.xml
│ └── styles.xml
└── test
└── java
└── com
└── tomergoldst
└── tooltips
└── ExampleUnitTest.java
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .idea
7 | .DS_Store
8 | /build
9 | /captures
10 | secring.gpg
11 |
12 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Tooltips
2 | Simple to use library for android, Enabling to add a tooltip near any view with ease
3 |
4 |
5 |
6 | ## Instructions
7 | Add a dependency to your app build.gradle
8 | ```groovy
9 | dependencies {
10 | implementation 'com.tomergoldst.android:tooltips:1.1.1'
11 | }
12 | ```
13 |
14 | Create a `ToolTipsManager` object
15 | ```java
16 | public class MainActivity extends Activity {
17 |
18 | ToolTipsManager mToolTipsManager;
19 |
20 | @Override
21 | protected void onCreate(Bundle savedInstanceState) {
22 | super.onCreate(savedInstanceState);
23 | setContentView(R.layout.activity_main);
24 |
25 | mToolTipsManager = new ToolTipsManager();
26 |
27 | }
28 |
29 | }
30 | ```
31 |
32 | Use the `ToolTip.Builder` to construct your tip
33 | ```java
34 | public class MainActivity extends Activity {
35 |
36 | @Override
37 | public void onWindowFocusChanged(boolean hasFocus) {
38 | super.onWindowFocusChanged(hasFocus);
39 |
40 | ToolTip.Builder builder = new ToolTip.Builder(this, mTextView, mRootLayout, "Tip message", ToolTip.POSITION_ABOVE);
41 | }
42 | }
43 | ```
44 | `mTextView` here is the view which near it the tip will be shown and `mRootLayout` is the layout where the tip view will be added to.
45 | **The root layout must be** of `RelativeLayout`, `FrameLayout` or similar. `LinearLayout` won't work but you can always wrap your `LinearLayout`
46 | with another layout. Prefer to pass in a layout which is higher in the xml tree as this will give the
47 | tip view more visible space.
48 |
49 | **OPTIONAL**: Customize your tip with background color, text color, alignment, text gravity, type face and more.
50 | ```java
51 | public class MainActivity extends Activity {
52 |
53 | @Override
54 | public void onWindowFocusChanged(boolean hasFocus) {
55 | super.onWindowFocusChanged(hasFocus);
56 |
57 | ToolTip.Builder builder = new ToolTip.Builder(this, mTextView, mRootLayout, "Tip message", ToolTip.POSITION_ABOVE);
58 | builder.setAlign(ToolTip.ALIGN_LEFT);
59 | builder.setBackgroundColor(getResources().getColor(R.color.colorOrange));
60 | builder.setGravity(ToolTip.GRAVITY_RIGHT);
61 | builder.setTextAppearance(R.style.TooltipTextAppearance); // from `styles.xml`
62 | builder.setTypeface(mCustomFontTypeface);
63 | }
64 | }
65 | ```
66 |
67 | Here is an example on how you can define your text appearance in your `styles.xml`
68 |
69 | ```xml
70 |
75 | ```
76 |
77 | You can also customize the animation used to show and hide the tooltip view by providing `ToolTipAnimator` implementation and setting it in the `ToolTipsManager`.
78 | ```java
79 | public class MainActivity extends Activity {
80 | ToolTipsManager mToolTipsManager;
81 |
82 | @Override
83 | protected void onCreate(Bundle savedInstanceState) {
84 | super.onCreate(savedInstanceState);
85 | setContentView(R.layout.activity_main);
86 |
87 | mToolTipsManager = new ToolTipsManager();
88 | mToolTipsManager.setToolTipAnimator(MyCustomToolTipAnimator());
89 | }
90 |
91 | }
92 | ```
93 |
94 | Use `ToolTipManger` to show the tip
95 |
96 | **IMPORTANT**: This must be called after the layout has been drawn
97 | You can override the `onWindowFocusChanged()` of an Activity and show there, Start a delayed runnable from `onStart()`, react to user action or any other method that works for you
98 | ```java
99 | public class MainActivity extends Activity {
100 |
101 | @Override
102 | public void onWindowFocusChanged(boolean hasFocus) {
103 | super.onWindowFocusChanged(hasFocus);
104 |
105 | ToolTip.Builder builder = new ToolTip.Builder(this, mTextView, mRootLayout, "Tip message", ToolTip.POSITION_ABOVE);
106 |
107 | // Rest of builder configurations removed for brevity
108 |
109 | mToolTipsManager.show(builder.build());
110 | }
111 | }
112 | ```
113 |
114 | Each tip is dismissable by clicking on it, if you want to dismiss a tip from code there are a few options, the most simple way is to do the following
115 | ```java
116 | public class MainActivity extends Activity {
117 |
118 | @Override
119 | protected void onCreate(Bundle savedInstanceState) {
120 |
121 | mDismissBtn.setOnClickListener(new View.OnClickListener() {
122 | @Override
123 | public void onClick(View view) {
124 | mToolTipsManager.findAndDismiss(mTextView);
125 | }
126 | });
127 | }
128 |
129 | }
130 | ```
131 | Where `mTextView` is the same view we asked to position a tip near it
132 |
133 | If you want to react when tip has been dismissed, Implement `ToolTipsManager.TipListener` interface and use appropriate `ToolTipsManager` constructor
134 | ```java
135 | public class MainActivity extends Activity implements ToolTipsManager.TipListener {
136 |
137 | @Override
138 | protected void onCreate(Bundle savedInstanceState) {
139 | mToolTipsManager = new ToolTipsManager(this);
140 | }
141 |
142 | @Override
143 | public void onTipDismissed(View view, int anchorViewId, boolean byUser) {
144 | Log.d(TAG, "tip near anchor view " + anchorViewId + " dismissed");
145 |
146 | if (anchorViewId == R.id.text_view) {
147 | // Do something when a tip near view with id "R.id.text_view" has been dismissed
148 | }
149 | }
150 |
151 | }
152 | ```
153 |
154 | ### License
155 | ```
156 | Copyright 2016 Tomer Goldstein
157 |
158 | Licensed under the Apache License, Version 2.0 (the "License");
159 | you may not use this file except in compliance with the License.
160 | You may obtain a copy of the License at
161 |
162 | http://www.apache.org/licenses/LICENSE-2.0
163 |
164 | Unless required by applicable law or agreed to in writing, software
165 | distributed under the License is distributed on an "AS IS" BASIS,
166 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
167 | See the License for the specific language governing permissions and
168 | limitations under the License.
169 | ```
170 |
171 |
172 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 31
5 |
6 | defaultConfig {
7 | applicationId "com.tomergoldst.tooltipsdemo"
8 | minSdkVersion 14
9 | targetSdkVersion 31
10 | versionCode 2
11 | versionName "1.1"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | implementation fileTree(dir: 'libs', include: ['*.jar'])
23 | testImplementation 'junit:junit:4.13.2'
24 | implementation 'com.google.android.material:material:1.4.0'
25 | implementation project(path: ':tooltips')
26 | }
27 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in C:\Users\Tomer\AppData\Local\Android\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/tomergoldst/tooltipsdemo/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.tomergoldst.tooltipsdemo;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
11 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/assets/OFL.txt:
--------------------------------------------------------------------------------
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.
94 |
--------------------------------------------------------------------------------
/app/src/main/assets/Pacifico-Regular.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomergoldst/tooltips/8e2b65ce51f26fee17a142b8fd5078c5b89946a2/app/src/main/assets/Pacifico-Regular.ttf
--------------------------------------------------------------------------------
/app/src/main/java/com/tomergoldst/tooltipsdemo/MainActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2016 Tomer Goldstein
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 |
17 | package com.tomergoldst.tooltipsdemo;
18 |
19 | import android.app.Activity;
20 | import android.graphics.Color;
21 | import android.graphics.Typeface;
22 | import android.os.Bundle;
23 | import android.text.Html;
24 | import android.text.TextUtils;
25 | import android.util.Log;
26 | import android.view.Gravity;
27 | import android.view.Menu;
28 | import android.view.MenuInflater;
29 | import android.view.MenuItem;
30 | import android.view.View;
31 | import android.widget.Button;
32 | import android.widget.RadioButton;
33 | import android.widget.RelativeLayout;
34 | import android.widget.TextView;
35 | import android.widget.Toast;
36 |
37 | import com.google.android.material.textfield.TextInputEditText;
38 | import com.tomergoldst.tooltips.ToolTip;
39 | import com.tomergoldst.tooltips.ToolTipsManager;
40 |
41 | public class MainActivity extends Activity implements
42 | ToolTipsManager.TipListener,
43 | View.OnClickListener {
44 |
45 | private static final String TAG = MainActivity.class.getSimpleName();
46 | public static final String TIP_TEXT = "Tool Tip";
47 | public static final String TIP_TEXT_SMALL = "Small Tool Tip";
48 | public static final String TIP_TEXT_LARGE = "Large Tool Tip";
49 |
50 | ToolTipsManager mToolTipsManager;
51 | RelativeLayout mRootLayout;
52 | TextInputEditText mEditText;
53 | TextView mTextView;
54 |
55 | Button mAboveBtn;
56 | Button mBelowBtn;
57 | Button mLeftToBtn;
58 | Button mRightToBtn;
59 |
60 | RadioButton mAlignRight;
61 | RadioButton mAlignLeft;
62 | RadioButton mAlignCenter;
63 |
64 | @ToolTip.Align
65 | int mAlign = ToolTip.ALIGN_CENTER;
66 |
67 | Typeface mCustomFont = null;
68 |
69 | @Override
70 | protected void onCreate(Bundle savedInstanceState) {
71 | super.onCreate(savedInstanceState);
72 | setContentView(R.layout.activity_main);
73 |
74 | mRootLayout = findViewById(R.id.root_layout);
75 | mTextView = findViewById(R.id.text_view);
76 |
77 | mToolTipsManager = new ToolTipsManager(this);
78 |
79 | mAboveBtn = findViewById(R.id.button_above);
80 | mBelowBtn = findViewById(R.id.button_below);
81 | mLeftToBtn = findViewById(R.id.button_left_to);
82 | mRightToBtn = findViewById(R.id.button_right_to);
83 |
84 | mAboveBtn.setOnClickListener(this);
85 | mBelowBtn.setOnClickListener(this);
86 | mLeftToBtn.setOnClickListener(this);
87 | mRightToBtn.setOnClickListener(this);
88 | mTextView.setOnClickListener(this);
89 |
90 | mAlignCenter = findViewById(R.id.button_align_center);
91 | mAlignRight = findViewById(R.id.button_align_right);
92 | mAlignLeft = findViewById(R.id.button_align_left);
93 |
94 | mAlignCenter.setOnClickListener(this);
95 | mAlignLeft.setOnClickListener(this);
96 | mAlignRight.setOnClickListener(this);
97 |
98 | mAlignCenter.setChecked(true);
99 |
100 | mEditText = findViewById(R.id.text_input_edit_text);
101 |
102 | }
103 |
104 | @Override
105 | public boolean onCreateOptionsMenu(Menu menu) {
106 | MenuInflater inflater = getMenuInflater();
107 | inflater.inflate(R.menu.main_activity_actions, menu);
108 | return true;
109 | }
110 |
111 | @Override
112 | public boolean onOptionsItemSelected(MenuItem item) {
113 | // Handle item selection
114 | switch (item.getItemId()) {
115 | case R.id.use_custom_font_menu_item:
116 | mCustomFont = Typeface.createFromAsset(getAssets(), "Pacifico-Regular.ttf");
117 | Toast toast = Toast.makeText(this, "Custom font set. Re-try demo.", Toast.LENGTH_SHORT);
118 | toast.setGravity(Gravity.CENTER, 0, 0);
119 | toast.show();
120 | return true;
121 | default:
122 | return super.onOptionsItemSelected(item);
123 | }
124 | }
125 |
126 |
127 | @Override
128 | public void onWindowFocusChanged(boolean hasFocus) {
129 | super.onWindowFocusChanged(hasFocus);
130 |
131 | // This tip is shows the first time the sample app is loaded. Use a message that gives user
132 | // guide on how to use the sample app. Also try to showcase the ability of the app.
133 | CharSequence initialGuideTex = Html.fromHtml("Click on the Buttons " +
134 | "and the Radio Buttons bellow to test various tool tip configurations." +
135 | "
GOT IT");
136 |
137 | ToolTip.Builder builder = new ToolTip.Builder(this, mTextView, mRootLayout, initialGuideTex, ToolTip.POSITION_ABOVE);
138 | builder.setAlign(mAlign);
139 | builder.setBackgroundColor(Color.DKGRAY);
140 | builder.setTextAppearance(R.style.TooltipTextAppearance);
141 | mToolTipsManager.show(builder.build());
142 | }
143 |
144 | @Override
145 | public void onTipDismissed(View view, int anchorViewId, boolean byUser) {
146 | Log.d(TAG, "tip near anchor view " + anchorViewId + " dismissed");
147 |
148 | if (anchorViewId == R.id.text_view) {
149 | // Do something when a tip near view with id "R.id.text_view" has been dismissed
150 | }
151 | }
152 |
153 | @Override
154 | public void onClick(View view) {
155 | String text = TextUtils.isEmpty(mEditText.getText()) ? TIP_TEXT : mEditText.getText().toString();
156 | ToolTip.Builder builder;
157 |
158 | switch (view.getId()) {
159 | case R.id.button_above:
160 | mToolTipsManager.findAndDismiss(mTextView);
161 | builder = new ToolTip.Builder(this, mTextView, mRootLayout, text, ToolTip.POSITION_ABOVE);
162 | builder.setAlign(mAlign);
163 | builder.setTypeface(mCustomFont);
164 | mToolTipsManager.show(builder.build());
165 | break;
166 | case R.id.button_below:
167 | mToolTipsManager.findAndDismiss(mTextView);
168 | builder = new ToolTip.Builder(this, mTextView, mRootLayout, text, ToolTip.POSITION_BELOW);
169 | builder.setAlign(mAlign);
170 | builder.setTextAppearance(R.style.TooltipTextAppearance);
171 | builder.setTypeface(mCustomFont);
172 | builder.setBackgroundColor(getResources().getColor(R.color.colorOrange));
173 | mToolTipsManager.show(builder.build());
174 | break;
175 | case R.id.button_left_to:
176 | mToolTipsManager.findAndDismiss(mTextView);
177 | builder = new ToolTip.Builder(this, mTextView, mRootLayout, TIP_TEXT.equals(text) ? TIP_TEXT_SMALL : text, ToolTip.POSITION_LEFT_TO);
178 | builder.setBackgroundColor(getResources().getColor(R.color.colorLightGreen));
179 | builder.setGravity(ToolTip.GRAVITY_CENTER);
180 | builder.setTypeface(mCustomFont);
181 | builder.setTextAppearance(R.style.TooltipTextAppearance_Small_Black);
182 | mToolTipsManager.show(builder.build());
183 | break;
184 | case R.id.button_right_to:
185 | mToolTipsManager.findAndDismiss(mTextView);
186 | builder = new ToolTip.Builder(this, mTextView, mRootLayout, TIP_TEXT.equals(text) ? TIP_TEXT_LARGE : text, ToolTip.POSITION_RIGHT_TO);
187 | builder.setBackgroundColor(getResources().getColor(R.color.colorDarkRed));
188 | builder.setTextAppearance(R.style.TooltipTextAppearance_Large);
189 | builder.setTypeface(mCustomFont);
190 | mToolTipsManager.show(builder.build());
191 | break;
192 | case R.id.button_align_center:
193 | mAlign = ToolTip.ALIGN_CENTER;
194 | break;
195 | case R.id.button_align_left:
196 | mAlign = ToolTip.ALIGN_LEFT;
197 | break;
198 | case R.id.button_align_right:
199 | mAlign = ToolTip.ALIGN_RIGHT;
200 | break;
201 | case R.id.text_view:
202 | mToolTipsManager.dismissAll();
203 | break;
204 | }
205 | }
206 | }
207 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
12 |
13 |
17 |
18 |
26 |
27 |
28 |
29 |
36 |
37 |
43 |
44 |
52 |
53 |
61 |
62 |
63 |
71 |
72 |
80 |
81 |
82 |
83 |
94 |
95 |
102 |
103 |
110 |
111 |
118 |
119 |
120 |
121 |
127 |
128 |
139 |
140 |
141 |
142 |
143 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/main_activity_actions.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomergoldst/tooltips/8e2b65ce51f26fee17a142b8fd5078c5b89946a2/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomergoldst/tooltips/8e2b65ce51f26fee17a142b8fd5078c5b89946a2/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomergoldst/tooltips/8e2b65ce51f26fee17a142b8fd5078c5b89946a2/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomergoldst/tooltips/8e2b65ce51f26fee17a142b8fd5078c5b89946a2/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomergoldst/tooltips/8e2b65ce51f26fee17a142b8fd5078c5b89946a2/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 | #8b0000
8 | #ffa500
9 | #a0db8e
10 | #FFFFFF
11 | #000000
12 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Tooltips Demo
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
24 |
30 |
31 |
34 |
35 |
38 |
39 |
43 |
44 |
--------------------------------------------------------------------------------
/app/src/test/java/com/tomergoldst/tooltipsdemo/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.tomergoldst.tooltipsdemo;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | google()
6 | mavenCentral()
7 | }
8 | dependencies {
9 | classpath 'com.android.tools.build:gradle:7.0.4'
10 | classpath 'com.vanniktech:gradle-maven-publish-plugin:0.18.0'
11 | // NOTE: Do not place your application dependencies here; they belong
12 | // in the individual module build.gradle files
13 | }
14 | }
15 |
16 | allprojects {
17 | repositories {
18 | google()
19 | mavenCentral()
20 | }
21 | }
22 |
23 | task clean(type: Delete) {
24 | delete rootProject.buildDir
25 | }
26 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
19 | android.enableJetifier=true
20 | android.useAndroidX=true
21 |
22 | GROUP=com.tomergoldst.android
23 | POM_ARTIFACT_ID=tooltips
24 | VERSION_NAME=1.1.1
25 |
26 | POM_NAME=tooltips
27 | POM_PACKAGING=aar
28 |
29 | POM_DESCRIPTION=Simple to use library for android, enabling to add a tooltip near any view with ease
30 | POM_INCEPTION_YEAR=2016
31 |
32 | POM_URL=https://github.com/tomergoldst/tooltips
33 | POM_SCM_URL=https://github.com/tomergoldst/tooltips
34 | POM_SCM_CONNECTION=scm:git@github.com:tomergoldst/tooltips.git
35 | POM_SCM_DEV_CONNECTION=scm:git@github.com:tomergoldst/tooltips.git
36 |
37 | POM_LICENCE_NAME=The Apache Software License, Version 2.0
38 | POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt
39 | POM_LICENCE_DIST=repo
40 |
41 | POM_DEVELOPER_ID=tomergoldst
42 | POM_DEVELOPER_NAME=Tomer Goldstein
43 | POM_DEVELOPER_URL=https://github.com/tomergoldst
44 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomergoldst/tooltips/8e2b65ce51f26fee17a142b8fd5078c5b89946a2/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Jan 10 18:55:02 IST 2022
2 | distributionBase=GRADLE_USER_HOME
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
4 | distributionPath=wrapper/dists
5 | zipStorePath=wrapper/dists
6 | zipStoreBase=GRADLE_USER_HOME
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':tooltips'
2 |
--------------------------------------------------------------------------------
/tooltips/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/tooltips/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.vanniktech.maven.publish'
3 |
4 | File localPropsFile = project.rootProject.file('local.properties')
5 | if (localPropsFile.exists()) {
6 | // Read local.properties file first if it exists
7 | def localProps = new Properties()
8 | new FileInputStream(localPropsFile).withCloseable { is -> localProps.load(is) }
9 | localProps.each { name, value -> ext[name] = value }
10 | }
11 |
12 | android {
13 | compileSdkVersion 31
14 |
15 | defaultConfig {
16 | minSdkVersion 14
17 | targetSdkVersion 31
18 | versionCode 14
19 | versionName "1.1.1"
20 | }
21 | buildTypes {
22 | release {
23 | minifyEnabled false
24 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
25 | }
26 | }
27 |
28 | publishing {
29 | repositories {
30 | maven {
31 | def releasesRepoUrl = "$buildDir/repos/releases"
32 | def snapshotsRepoUrl = "$buildDir/repos/snapshots"
33 | url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
34 | }
35 | }
36 | }
37 | }
38 |
39 | dependencies {
40 | implementation fileTree(dir: 'libs', include: ['*.jar'])
41 | testImplementation 'junit:junit:4.13.2'
42 | implementation 'androidx.appcompat:appcompat:1.4.0'
43 | }
--------------------------------------------------------------------------------
/tooltips/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in C:\Users\Tomer\AppData\Local\Android\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/tooltips/src/androidTest/java/com/tomergoldst/tooltips/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.tomergoldst.tooltips;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/tooltips/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/tooltips/src/main/java/com/tomergoldst/tooltips/Coordinates.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2016 Tomer Goldstein
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 |
17 | package com.tomergoldst.tooltips;
18 |
19 | import android.view.View;
20 |
21 | class Coordinates {
22 |
23 | int left;
24 | int top;
25 | int right;
26 | int bottom;
27 |
28 | Coordinates(View view) {
29 | int[] location = new int[2];
30 | view.getLocationOnScreen(location);
31 | left = location[0];
32 | right = left + view.getWidth();;
33 | top = location[1];
34 | bottom = top + view.getHeight();
35 | }
36 |
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/tooltips/src/main/java/com/tomergoldst/tooltips/DefaultToolTipAnimator.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2016 Tomer Goldstein
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 |
17 | package com.tomergoldst.tooltips;
18 |
19 | import android.animation.Animator;
20 | import android.animation.AnimatorListenerAdapter;
21 | import android.animation.ObjectAnimator;
22 | import android.animation.PropertyValuesHolder;
23 | import android.view.View;
24 | import android.view.animation.AnticipateOvershootInterpolator;
25 | import android.view.animation.OvershootInterpolator;
26 |
27 | class DefaultToolTipAnimator implements ToolTipAnimator {
28 |
29 | @Override
30 | public ObjectAnimator popup(final View view, final long duration) {
31 | view.setAlpha(0);
32 | view.setVisibility(View.VISIBLE);
33 |
34 | ObjectAnimator popup = ObjectAnimator.ofPropertyValuesHolder(view,
35 | PropertyValuesHolder.ofFloat("alpha", 0f, 1f),
36 | PropertyValuesHolder.ofFloat("scaleX", 0f, 1f),
37 | PropertyValuesHolder.ofFloat("scaleY", 0f, 1f));
38 | popup.setDuration(duration);
39 | popup.setInterpolator(new OvershootInterpolator());
40 |
41 | return popup;
42 | }
43 |
44 | @Override
45 | public ObjectAnimator popout(final View view, final long duration, final AnimatorListenerAdapter animatorListenerAdapter) {
46 | ObjectAnimator popout = ObjectAnimator.ofPropertyValuesHolder(view,
47 | PropertyValuesHolder.ofFloat("alpha", 1f, 0f),
48 | PropertyValuesHolder.ofFloat("scaleX", 1f, 0f),
49 | PropertyValuesHolder.ofFloat("scaleY", 1f, 0f));
50 | popout.setDuration(duration);
51 | popout.addListener(new AnimatorListenerAdapter() {
52 | @Override
53 | public void onAnimationEnd(Animator animation) {
54 | super.onAnimationEnd(animation);
55 | view.setVisibility(View.GONE);
56 | if (animatorListenerAdapter != null) {
57 | animatorListenerAdapter.onAnimationEnd(animation);
58 | }
59 | }
60 | });
61 | popout.setInterpolator(new AnticipateOvershootInterpolator());
62 |
63 | return popout;
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/tooltips/src/main/java/com/tomergoldst/tooltips/ToolTip.java:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | Copyright 2016 Tomer Goldstein
4 |
5 | Licensed under the Apache License, Version 2.0 (the "License");
6 | you may not use this file except in compliance with the License.
7 | You may obtain a copy of the License at
8 |
9 | http://www.apache.org/licenses/LICENSE-2.0
10 |
11 | Unless required by applicable law or agreed to in writing, software
12 | distributed under the License is distributed on an "AS IS" BASIS,
13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | See the License for the specific language governing permissions and
15 | limitations under the License.
16 | */
17 | package com.tomergoldst.tooltips;
18 |
19 | import android.content.Context;
20 | import android.graphics.Typeface;
21 | import android.view.View;
22 | import android.view.ViewGroup;
23 |
24 | import androidx.annotation.IntDef;
25 | import androidx.annotation.NonNull;
26 | import androidx.annotation.Nullable;
27 | import androidx.annotation.StyleRes;
28 |
29 | import java.lang.annotation.Retention;
30 |
31 | import static java.lang.annotation.RetentionPolicy.SOURCE;
32 |
33 | public class ToolTip {
34 |
35 | @Retention(SOURCE)
36 | @IntDef({POSITION_ABOVE, POSITION_BELOW, POSITION_LEFT_TO, POSITION_RIGHT_TO})
37 | public @interface Position {}
38 | public static final int POSITION_ABOVE = 0;
39 | public static final int POSITION_BELOW = 1;
40 | public static final int POSITION_LEFT_TO = 3;
41 | public static final int POSITION_RIGHT_TO = 4;
42 |
43 | @Retention(SOURCE)
44 | @IntDef({ALIGN_CENTER, ALIGN_LEFT, ALIGN_RIGHT})
45 | public @interface Align {}
46 | public static final int ALIGN_CENTER = 0;
47 | public static final int ALIGN_LEFT = 1;
48 | public static final int ALIGN_RIGHT = 2;
49 |
50 | @Retention(SOURCE)
51 | @IntDef({GRAVITY_CENTER, GRAVITY_LEFT, GRAVITY_RIGHT})
52 | public @interface Gravity {}
53 | public static final int GRAVITY_CENTER = 0;
54 | public static final int GRAVITY_LEFT = 1;
55 | public static final int GRAVITY_RIGHT = 2;
56 |
57 | @NonNull private final Context mContext;
58 | @NonNull private final View mAnchorView;
59 | @NonNull private final ViewGroup mRootViewGroup;
60 | @NonNull private final CharSequence mMessage;
61 | private @Position int mPosition;
62 | private final @Align int mAlign;
63 | private final int mOffsetX;
64 | private final int mOffsetY;
65 | private final boolean mArrow;
66 | private final int mBackgroundColor;
67 | private final float mElevation;
68 | private final @Gravity int mTextGravity;
69 | private final @StyleRes int mTextAppearanceStyle;
70 | @Nullable private final Typeface mTypeface;
71 | private final int mMaxWidth;
72 |
73 | public ToolTip(Builder builder){
74 | mContext = builder.mContext;
75 | mAnchorView = builder.mAnchorView;
76 | mRootViewGroup = builder.mRootViewGroup;
77 | mMessage = builder.mMessage;
78 | mPosition = builder.mPosition;
79 | mAlign = builder.mAlign;
80 | mOffsetX = builder.mOffsetX;
81 | mOffsetY = builder.mOffsetY;
82 | mArrow = builder.mArrow;
83 | mBackgroundColor = builder.mBackgroundColor;
84 | mElevation = builder.mElevation;
85 | mTextGravity = builder.mTextGravity;
86 | mTextAppearanceStyle = builder.mTextAppearanceStyle;
87 | mTypeface = builder.mTypeface;
88 | mMaxWidth = builder.mMaxWidth;
89 | }
90 |
91 | @NonNull
92 | public Context getContext() {
93 | return mContext;
94 | }
95 |
96 | @NonNull
97 | public View getAnchorView() {
98 | return mAnchorView;
99 | }
100 |
101 | @NonNull
102 | public ViewGroup getRootView() {
103 | return mRootViewGroup;
104 | }
105 |
106 | @NonNull
107 | public CharSequence getMessage() {
108 | return mMessage;
109 | }
110 |
111 | public int getPosition() {
112 | return mPosition;
113 | }
114 |
115 | public int getAlign() {
116 | return mAlign;
117 | }
118 |
119 | public int getOffsetX() {
120 | return mOffsetX;
121 | }
122 |
123 | public int getOffsetY() {
124 | return mOffsetY;
125 | }
126 |
127 | public boolean hideArrow() {
128 | return !mArrow;
129 | }
130 |
131 | public int getBackgroundColor() {
132 | return mBackgroundColor;
133 | }
134 |
135 | public boolean positionedLeftTo(){
136 | return POSITION_LEFT_TO == mPosition;
137 | }
138 |
139 | public boolean positionedRightTo(){
140 | return POSITION_RIGHT_TO == mPosition;
141 | }
142 |
143 | public boolean positionedAbove(){
144 | return POSITION_ABOVE == mPosition;
145 | }
146 |
147 | public boolean positionedBelow(){
148 | return POSITION_BELOW == mPosition;
149 | }
150 |
151 | public boolean alignedCenter(){
152 | return ALIGN_CENTER == mAlign;
153 | }
154 |
155 | public boolean alignedLeft(){
156 | return ALIGN_LEFT == mAlign;
157 | }
158 |
159 | public boolean alignedRight(){
160 | return ALIGN_RIGHT == mAlign;
161 | }
162 |
163 | public void setPosition(@Position int position){
164 | mPosition = position;
165 | }
166 |
167 | public float getElevation() {
168 | return mElevation;
169 | }
170 |
171 | @StyleRes
172 | public int getTextAppearanceStyle() {
173 | return mTextAppearanceStyle;
174 | }
175 |
176 | @Nullable
177 | public Typeface getTypeface() {
178 | return mTypeface;
179 | }
180 |
181 | @NonNull
182 | public int getTextGravity(){
183 | int gravity;
184 | switch (mTextGravity){
185 | case GRAVITY_LEFT:
186 | gravity = android.view.Gravity.START;
187 | break;
188 | case GRAVITY_RIGHT:
189 | gravity = android.view.Gravity.END;
190 | break;
191 | case GRAVITY_CENTER:
192 | default:
193 | gravity = android.view.Gravity.CENTER;
194 | }
195 | return gravity;
196 | }
197 |
198 | public int getMaxWidth() {
199 | return mMaxWidth;
200 | }
201 |
202 | public static class Builder {
203 | private final @NonNull Context mContext;
204 | private final @NonNull View mAnchorView;
205 | private final @NonNull ViewGroup mRootViewGroup;
206 | private final @NonNull CharSequence mMessage;
207 | private @Position int mPosition;
208 | private @Align int mAlign;
209 | private int mOffsetX;
210 | private int mOffsetY;
211 | private boolean mArrow;
212 | private int mBackgroundColor;
213 | private float mElevation;
214 | private @Gravity int mTextGravity;
215 | private @StyleRes int mTextAppearanceStyle;
216 | private @Nullable Typeface mTypeface;
217 | private int mMaxWidth;
218 |
219 | /**
220 | * Creates the tooltip builder with message and required parameters to show tooltip.
221 | *
222 | * @param context context
223 | * @param anchorView the view which near it we want to put the tip
224 | * @param root a class extends ViewGroup which the created tip view will be added to
225 | * @param message message to show. Note: This allows normal text and spannable text with spanned styles.
226 | * @param position put the tip above / below / left to / right to anchor view.
227 | */
228 | public Builder(@NonNull Context context,
229 | @NonNull View anchorView,
230 | @NonNull ViewGroup root,
231 | @NonNull CharSequence message,
232 | @Position int position) {
233 | mContext = context;
234 | mAnchorView = anchorView;
235 | mRootViewGroup = root;
236 | mMessage = message;
237 | mPosition = position;
238 | mAlign = ALIGN_CENTER;
239 | mOffsetX = 0;
240 | mOffsetY = 0;
241 | mArrow = true;
242 | mBackgroundColor = context.getResources().getColor(R.color.colorBackground);
243 | mTextGravity = GRAVITY_LEFT;
244 | mTextAppearanceStyle = R.style.TooltipDefaultStyle;
245 | mMaxWidth = 0;
246 | }
247 |
248 | @NonNull
249 | public Builder setPosition(@Position int position){
250 | mPosition = position;
251 | return this;
252 | }
253 |
254 | @NonNull
255 | public Builder setAlign(@Align int align){
256 | mAlign = align;
257 | return this;
258 | }
259 |
260 | /**
261 | * @param offset offset to move the tip on x axis after tip was positioned
262 | * @return offset
263 | */
264 | @NonNull
265 | public Builder setOffsetX(int offset){
266 | mOffsetX = offset;
267 | return this;
268 | }
269 |
270 | /**
271 | * @param offset offset to move the tip on y axis after tip was positioned
272 | * @return offset
273 | */
274 | @NonNull
275 | public Builder setOffsetY(int offset){
276 | mOffsetY = offset;
277 | return this;
278 | }
279 |
280 | @NonNull
281 | public Builder withArrow(boolean value){
282 | mArrow = value;
283 | return this;
284 | }
285 |
286 | @NonNull
287 | public Builder setBackgroundColor(int color){
288 | mBackgroundColor = color;
289 | return this;
290 | }
291 |
292 | @NonNull
293 | public Builder setElevation(float elevation){
294 | mElevation = elevation;
295 | return this;
296 | }
297 |
298 | @NonNull
299 | public Builder setGravity(@Gravity int gravity){
300 | mTextGravity = gravity;
301 | return this;
302 | }
303 |
304 | @NonNull
305 | public Builder setTextAppearance(@StyleRes int textAppearance) {
306 | mTextAppearanceStyle = textAppearance;
307 | return this;
308 | }
309 |
310 | @NonNull
311 | public Builder setTypeface(@NonNull Typeface typeface) {
312 | mTypeface = typeface;
313 | return this;
314 | }
315 |
316 | public Builder setMaxWidth(int maxPixels){
317 | mMaxWidth = maxPixels;
318 | return this;
319 | }
320 |
321 | @NonNull
322 | public ToolTip build(){
323 | return new ToolTip(this);
324 | }
325 |
326 | }
327 | }
328 |
--------------------------------------------------------------------------------
/tooltips/src/main/java/com/tomergoldst/tooltips/ToolTipAnimator.java:
--------------------------------------------------------------------------------
1 | package com.tomergoldst.tooltips;
2 |
3 | import android.animation.AnimatorListenerAdapter;
4 | import android.animation.ObjectAnimator;
5 | import android.view.View;
6 |
7 | public interface ToolTipAnimator {
8 | /**
9 | * Object animator for the tooltip view to pop-up.
10 | * @param view The tooltip view.
11 | * @param duration Duration for the animator.
12 | * @return ObjectAnimator
13 | */
14 | ObjectAnimator popup(final View view, final long duration);
15 |
16 | /**
17 | * Object animator for the tooltip view to pop-out/hide.
18 | * @param view The tooltip view.
19 | * @param duration Duration for the animator.
20 | * @param animatorListenerAdapter The animator listener adapter to listen for animation event.
21 | * @return ObjectAnimator
22 | */
23 | ObjectAnimator popout(final View view, final long duration, final AnimatorListenerAdapter animatorListenerAdapter);
24 | }
25 |
--------------------------------------------------------------------------------
/tooltips/src/main/java/com/tomergoldst/tooltips/ToolTipBackgroundConstructor.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2016 Tomer Goldstein
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 |
17 | package com.tomergoldst.tooltips;
18 |
19 | import android.content.Context;
20 | import android.graphics.PorterDuff;
21 | import android.graphics.drawable.Drawable;
22 | import android.os.Build;
23 | import android.view.View;
24 |
25 | class ToolTipBackgroundConstructor {
26 |
27 | /**
28 | * Select which background will be assign to the tip view
29 | */
30 | static void setBackground(View tipView, ToolTip toolTip) {
31 |
32 | // show tool tip without arrow. no need to continue
33 | if (toolTip.hideArrow()) {
34 | setToolTipNoArrowBackground(tipView, toolTip.getBackgroundColor());
35 | return;
36 | }
37 |
38 | // show tool tip according to requested position
39 | switch (toolTip.getPosition()) {
40 | case ToolTip.POSITION_ABOVE:
41 | setToolTipAboveBackground(tipView, toolTip);
42 | break;
43 | case ToolTip.POSITION_BELOW:
44 | setToolTipBelowBackground(tipView, toolTip);
45 | break;
46 | case ToolTip.POSITION_LEFT_TO:
47 | setToolTipLeftToBackground(tipView, toolTip.getBackgroundColor());
48 | break;
49 | case ToolTip.POSITION_RIGHT_TO:
50 | setToolTipRightToBackground(tipView, toolTip.getBackgroundColor());
51 | break;
52 | }
53 |
54 | }
55 |
56 | private static void setToolTipAboveBackground(View tipView, ToolTip toolTip) {
57 | switch (toolTip.getAlign()) {
58 | case ToolTip.ALIGN_CENTER:
59 | setTipBackground(tipView, R.drawable.tooltip_arrow_down, toolTip.getBackgroundColor());
60 | break;
61 | case ToolTip.ALIGN_LEFT:
62 | setTipBackground(tipView,
63 | !UiUtils.isRtl() ?
64 | R.drawable.tooltip_arrow_down_left :
65 | R.drawable.tooltip_arrow_down_right
66 | , toolTip.getBackgroundColor());
67 | break;
68 | case ToolTip.ALIGN_RIGHT:
69 | setTipBackground(tipView,
70 | !UiUtils.isRtl() ?
71 | R.drawable.tooltip_arrow_down_right :
72 | R.drawable.tooltip_arrow_down_left
73 | , toolTip.getBackgroundColor());
74 | break;
75 | }
76 | }
77 |
78 | private static void setToolTipBelowBackground(View tipView, ToolTip toolTip) {
79 |
80 | switch (toolTip.getAlign()) {
81 | case ToolTip.ALIGN_CENTER:
82 | setTipBackground(tipView, R.drawable.tooltip_arrow_up, toolTip.getBackgroundColor());
83 | break;
84 | case ToolTip.ALIGN_LEFT:
85 | setTipBackground(tipView,
86 | !UiUtils.isRtl() ?
87 | R.drawable.tooltip_arrow_up_left :
88 | R.drawable.tooltip_arrow_up_right
89 | , toolTip.getBackgroundColor());
90 | break;
91 | case ToolTip.ALIGN_RIGHT:
92 | setTipBackground(tipView,
93 | !UiUtils.isRtl() ?
94 | R.drawable.tooltip_arrow_up_right :
95 | R.drawable.tooltip_arrow_up_left
96 | , toolTip.getBackgroundColor());
97 | break;
98 | }
99 |
100 | }
101 |
102 | private static void setToolTipLeftToBackground(View tipView, int color) {
103 | setTipBackground(tipView, !UiUtils.isRtl() ?
104 | R.drawable.tooltip_arrow_right : R.drawable.tooltip_arrow_left,
105 | color);
106 | }
107 |
108 | private static void setToolTipRightToBackground(View tipView, int color) {
109 | setTipBackground(tipView, !UiUtils.isRtl() ?
110 | R.drawable.tooltip_arrow_left : R.drawable.tooltip_arrow_right,
111 | color);
112 | }
113 |
114 | private static void setToolTipNoArrowBackground(View tipView, int color) {
115 | setTipBackground(tipView, R.drawable.tooltip_no_arrow, color);
116 | }
117 |
118 | private static void setTipBackground(View tipView, int drawableRes, int color){
119 | Drawable paintedDrawable = getTintedDrawable(tipView.getContext(),
120 | drawableRes, color);
121 | setViewBackground(tipView, paintedDrawable);
122 | }
123 |
124 | private static void setViewBackground(View view, Drawable drawable){
125 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
126 | view.setBackground(drawable);
127 | } else {
128 | view.setBackgroundDrawable(drawable);
129 | }
130 | }
131 |
132 | private static Drawable getTintedDrawable(Context context, int drawableRes, int color){
133 | Drawable drawable;
134 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
135 | drawable = context.getResources().getDrawable(drawableRes, null);
136 | if (drawable != null) {
137 | drawable.setTint(color);
138 | }
139 | } else {
140 | drawable = context.getResources().getDrawable(drawableRes);
141 | if (drawable != null) {
142 | drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
143 | }
144 | }
145 |
146 | return drawable;
147 | }
148 |
149 | }
150 |
--------------------------------------------------------------------------------
/tooltips/src/main/java/com/tomergoldst/tooltips/ToolTipCoordinatesFinder.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2016 Tomer Goldstein
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 |
17 | package com.tomergoldst.tooltips;
18 |
19 | import android.graphics.Point;
20 | import android.view.View;
21 | import android.view.ViewGroup;
22 | import android.widget.TextView;
23 |
24 | class ToolTipCoordinatesFinder {
25 |
26 | /**
27 | * return the top left coordinates for positioning the tip
28 | *
29 | * @param tipView - the newly created tip view
30 | * @param tooltip - tool tip object
31 | * @return point
32 | */
33 | static Point getCoordinates(final TextView tipView, ToolTip tooltip) {
34 | Point point = new Point();
35 | final Coordinates anchorViewCoordinates = new Coordinates(tooltip.getAnchorView());
36 | final Coordinates rootCoordinates = new Coordinates(tooltip.getRootView());
37 |
38 | tipView.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
39 |
40 | switch (tooltip.getPosition()) {
41 | case ToolTip.POSITION_ABOVE:
42 | point = getPositionAbove(tipView, tooltip,
43 | anchorViewCoordinates, rootCoordinates);
44 | break;
45 | case ToolTip.POSITION_BELOW:
46 | point = getPositionBelow(tipView, tooltip,
47 | anchorViewCoordinates, rootCoordinates);
48 | break;
49 | case ToolTip.POSITION_LEFT_TO:
50 | point = getPositionLeftTo(tipView, tooltip,
51 | anchorViewCoordinates, rootCoordinates);
52 | break;
53 | case ToolTip.POSITION_RIGHT_TO:
54 | point = getPositionRightTo(tipView, tooltip,
55 | anchorViewCoordinates, rootCoordinates);
56 | break;
57 | }
58 |
59 | // add user defined offset values
60 | point.x += UiUtils.isRtl() ? -tooltip.getOffsetX() : tooltip.getOffsetX();
61 | point.y += tooltip.getOffsetY();
62 |
63 | // coordinates retrieved are relative to 0,0 of the root layout
64 | // added view to root is subject to root padding
65 | // we need to subtract the top and left padding of root from coordinates. to adjust
66 | // top left tip coordinates
67 | point.x -= tooltip.getRootView().getPaddingLeft();
68 | point.y -= tooltip.getRootView().getPaddingTop();
69 |
70 | return point;
71 |
72 | }
73 |
74 | private static Point getPositionRightTo(TextView tipView, ToolTip toolTip, Coordinates anchorViewCoordinates, Coordinates rootLocation) {
75 | Point point = new Point();
76 | point.x = anchorViewCoordinates.right;
77 | AdjustRightToOutOfBounds(tipView, toolTip.getRootView(), point, anchorViewCoordinates, rootLocation);
78 | point.y = anchorViewCoordinates.top + getYCenteringOffset(tipView, toolTip);
79 | return point;
80 | }
81 |
82 | private static Point getPositionLeftTo(TextView tipView, ToolTip toolTip, Coordinates anchorViewCoordinates, Coordinates rootLocation) {
83 | Point point = new Point();
84 | point.x = anchorViewCoordinates.left - tipView.getMeasuredWidth();
85 | AdjustLeftToOutOfBounds(tipView, toolTip.getRootView(), point, anchorViewCoordinates, rootLocation);
86 | point.y = anchorViewCoordinates.top + getYCenteringOffset(tipView, toolTip);
87 | return point;
88 | }
89 |
90 | private static Point getPositionBelow(TextView tipView, ToolTip toolTip, Coordinates anchorViewCoordinates, Coordinates rootLocation) {
91 | Point point = new Point();
92 | point.x = anchorViewCoordinates.left + getXOffset(tipView, toolTip);
93 | if (toolTip.alignedCenter()) {
94 | AdjustHorizontalCenteredOutOfBounds(tipView, toolTip.getRootView(), point, rootLocation);
95 | } else if (toolTip.alignedLeft()){
96 | AdjustHorizontalLeftAlignmentOutOfBounds(tipView, toolTip.getRootView(), point, anchorViewCoordinates, rootLocation);
97 | } else if (toolTip.alignedRight()){
98 | AdjustHorizotalRightAlignmentOutOfBounds(tipView, toolTip.getRootView(), point, anchorViewCoordinates, rootLocation);
99 | }
100 | point.y = anchorViewCoordinates.bottom;
101 | return point;
102 | }
103 |
104 | private static Point getPositionAbove(TextView tipView, ToolTip toolTip,
105 | Coordinates anchorViewCoordinates, Coordinates rootLocation) {
106 | Point point = new Point();
107 | point.x = anchorViewCoordinates.left + getXOffset(tipView, toolTip);
108 | if (toolTip.alignedCenter()) {
109 | AdjustHorizontalCenteredOutOfBounds(tipView, toolTip.getRootView(), point, rootLocation);
110 | } else if (toolTip.alignedLeft()){
111 | AdjustHorizontalLeftAlignmentOutOfBounds(tipView, toolTip.getRootView(), point, anchorViewCoordinates, rootLocation);
112 | } else if (toolTip.alignedRight()){
113 | AdjustHorizotalRightAlignmentOutOfBounds(tipView, toolTip.getRootView(), point, anchorViewCoordinates, rootLocation);
114 | }
115 | point.y = anchorViewCoordinates.top - tipView.getMeasuredHeight();
116 | return point;
117 | }
118 |
119 | private static void AdjustRightToOutOfBounds(TextView tipView, ViewGroup root, Point point, Coordinates anchorViewCoordinates, Coordinates rootLocation) {
120 | ViewGroup.LayoutParams params = tipView.getLayoutParams();
121 | int availableSpace = rootLocation.right - root.getPaddingRight() - anchorViewCoordinates.right;
122 | if (point.x + tipView.getMeasuredWidth() > rootLocation.right - root.getPaddingRight()){
123 | params.width = availableSpace;
124 | params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
125 | tipView.setLayoutParams(params);
126 | measureViewWithFixedWidth(tipView, params.width);
127 | }
128 | }
129 |
130 | private static void AdjustLeftToOutOfBounds(TextView tipView, ViewGroup root, Point point, Coordinates anchorViewCoordinates, Coordinates rootLocation) {
131 | ViewGroup.LayoutParams params = tipView.getLayoutParams();
132 | int rootLeft = rootLocation.left + root.getPaddingLeft();
133 | if (point.x < rootLeft){
134 | int availableSpace = anchorViewCoordinates.left - rootLeft;
135 | point.x = rootLeft;
136 | params.width = availableSpace;
137 | params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
138 | tipView.setLayoutParams(params);
139 | measureViewWithFixedWidth(tipView, params.width);
140 | }
141 | }
142 |
143 | private static void AdjustHorizotalRightAlignmentOutOfBounds(TextView tipView, ViewGroup root,
144 | Point point, Coordinates anchorViewCoordinates,
145 | Coordinates rootLocation) {
146 | ViewGroup.LayoutParams params = tipView.getLayoutParams();
147 | int rootLeft = rootLocation.left + root.getPaddingLeft();
148 | if (point.x < rootLeft){
149 | int availableSpace = anchorViewCoordinates.right - rootLeft;
150 | point.x = rootLeft;
151 | params.width = availableSpace;
152 | params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
153 | tipView.setLayoutParams(params);
154 | measureViewWithFixedWidth(tipView, params.width);
155 | }
156 | }
157 |
158 | private static void AdjustHorizontalLeftAlignmentOutOfBounds(TextView tipView, ViewGroup root,
159 | Point point, Coordinates anchorViewCoordinates,
160 | Coordinates rootLocation) {
161 | ViewGroup.LayoutParams params = tipView.getLayoutParams();
162 | int rootRight = rootLocation.right - root.getPaddingRight();
163 | if (point.x + tipView.getMeasuredWidth() > rootRight){
164 | params.width = rootRight - anchorViewCoordinates.left;
165 | params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
166 | tipView.setLayoutParams(params);
167 | measureViewWithFixedWidth(tipView, params.width);
168 | }
169 | }
170 |
171 | private static void AdjustHorizontalCenteredOutOfBounds(TextView tipView, ViewGroup root,
172 | Point point, Coordinates rootLocation) {
173 | ViewGroup.LayoutParams params = tipView.getLayoutParams();
174 | int rootWidth = root.getWidth() - root.getPaddingLeft() - root.getPaddingRight();
175 | if (tipView.getMeasuredWidth() > rootWidth) {
176 | point.x = rootLocation.left + root.getPaddingLeft();
177 | params.width = rootWidth;
178 | params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
179 | tipView.setLayoutParams(params);
180 | measureViewWithFixedWidth(tipView, rootWidth);
181 | }
182 | }
183 |
184 |
185 | private static void measureViewWithFixedWidth(TextView tipView, int width) {
186 | tipView.measure(View.MeasureSpec.makeMeasureSpec(width,
187 | View.MeasureSpec.EXACTLY), ViewGroup.LayoutParams.WRAP_CONTENT);
188 | }
189 |
190 | /**
191 | * calculate the amount of movement need to be taken inorder to align tip
192 | * on X axis according to "align" parameter
193 | * @return int
194 | */
195 | private static int getXOffset(View tipView, ToolTip toolTip) {
196 | int offset;
197 |
198 | switch (toolTip.getAlign()) {
199 | case ToolTip.ALIGN_CENTER:
200 | offset = ((toolTip.getAnchorView().getWidth() - tipView.getMeasuredWidth()) / 2);
201 | break;
202 | case ToolTip.ALIGN_LEFT:
203 | offset = 0;
204 | break;
205 | case ToolTip.ALIGN_RIGHT:
206 | offset = toolTip.getAnchorView().getWidth() - tipView.getMeasuredWidth();
207 | break;
208 | default:
209 | offset = 0;
210 | break;
211 | }
212 |
213 | return offset;
214 | }
215 |
216 | /**
217 | * calculate the amount of movement need to be taken inorder to center tip
218 | * on Y axis
219 | * @return int
220 | */
221 | private static int getYCenteringOffset(View tipView, ToolTip toolTip) {
222 | return (toolTip.getAnchorView().getHeight() - tipView.getMeasuredHeight()) / 2;
223 | }
224 |
225 | }
226 |
--------------------------------------------------------------------------------
/tooltips/src/main/java/com/tomergoldst/tooltips/ToolTipsManager.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2016 Tomer Goldstein
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 |
17 | package com.tomergoldst.tooltips;
18 |
19 | import android.animation.Animator;
20 | import android.animation.AnimatorListenerAdapter;
21 | import android.annotation.SuppressLint;
22 | import android.graphics.Outline;
23 | import android.graphics.Point;
24 | import android.graphics.Typeface;
25 | import android.os.Build;
26 | import android.util.Log;
27 | import android.view.View;
28 | import android.view.ViewOutlineProvider;
29 | import android.widget.TextView;
30 |
31 | import androidx.annotation.NonNull;
32 | import androidx.annotation.Nullable;
33 |
34 | import java.util.ArrayList;
35 | import java.util.HashMap;
36 | import java.util.List;
37 | import java.util.Map;
38 |
39 | public class ToolTipsManager {
40 |
41 | private static final String TAG = ToolTipsManager.class.getSimpleName();
42 |
43 | private static final int DEFAULT_ANIM_DURATION = 400;
44 |
45 | // Parameter for managing tip creation or reuse
46 | private Map mTipsMap = new HashMap<>();
47 |
48 | private int mAnimationDuration;
49 | @NonNull
50 | private ToolTipAnimator mToolTipAnimator;
51 | @Nullable
52 | private TipListener mListener;
53 |
54 | public interface TipListener {
55 | void onTipDismissed(View view, int anchorViewId, boolean byUser);
56 | }
57 |
58 | public ToolTipsManager(){
59 | mAnimationDuration = DEFAULT_ANIM_DURATION;
60 | mToolTipAnimator = new DefaultToolTipAnimator();
61 | }
62 |
63 | public ToolTipsManager(@NonNull TipListener listener){
64 | this();
65 | mListener = listener;
66 | }
67 |
68 | public View show(ToolTip toolTip) {
69 | View tipView = create(toolTip);
70 | if (tipView == null) {
71 | return null;
72 | }
73 |
74 | // animate tip visibility
75 | mToolTipAnimator.popup(tipView, mAnimationDuration).start();
76 |
77 | return tipView;
78 | }
79 |
80 | private View create(ToolTip toolTip) {
81 |
82 | if (toolTip.getAnchorView() == null) {
83 | Log.e(TAG, "Unable to create a tip, anchor view is null");
84 | return null;
85 | }
86 |
87 | if (toolTip.getRootView() == null) {
88 | Log.e(TAG, "Unable to create a tip, root layout is null");
89 | return null;
90 | }
91 |
92 | // only one tip is allowed near an anchor view at the same time, thus
93 | // reuse tip if already exist
94 | if (mTipsMap.containsKey(toolTip.getAnchorView().getId())) {
95 | return mTipsMap.get(toolTip.getAnchorView().getId());
96 | }
97 |
98 | // init tip view parameters
99 | TextView tipView = createTipView(toolTip);
100 |
101 | // on RTL languages replace sides
102 | if (UiUtils.isRtl()) {
103 | switchToolTipSidePosition(toolTip);
104 | }
105 |
106 | // set tool tip background / shape
107 | ToolTipBackgroundConstructor.setBackground(tipView, toolTip);
108 |
109 | // add tip to root layout
110 | toolTip.getRootView().addView(tipView);
111 |
112 | // find where to position the tool tip
113 | Point p = ToolTipCoordinatesFinder.getCoordinates(tipView, toolTip);
114 |
115 | // move tip view to correct position
116 | moveTipToCorrectPosition(tipView, p);
117 |
118 | // set dismiss on click
119 | tipView.setOnClickListener(new View.OnClickListener() {
120 | @Override
121 | public void onClick(View view) {
122 | dismiss(view, true);
123 | }
124 | });
125 |
126 | // bind tipView with anchorView id
127 | int anchorViewId = toolTip.getAnchorView().getId();
128 | tipView.setTag(anchorViewId);
129 |
130 | // enter tip to map by 'anchorView' id
131 | mTipsMap.put(anchorViewId, tipView);
132 |
133 | return tipView;
134 |
135 | }
136 |
137 | private void moveTipToCorrectPosition(TextView tipView, Point p) {
138 | Coordinates tipViewCoordinates = new Coordinates(tipView);
139 | int translationX = p.x - tipViewCoordinates.left;
140 | int translationY = p.y - tipViewCoordinates.top;
141 | tipView.setTranslationX(!UiUtils.isRtl() ? translationX : -translationX);
142 | tipView.setTranslationY(translationY);
143 | }
144 |
145 | @NonNull
146 | private TextView createTipView(ToolTip toolTip) {
147 | TextView tipView = new TextView(toolTip.getContext());
148 | tipView.setText(toolTip.getMessage());
149 | tipView.setVisibility(View.INVISIBLE);
150 | tipView.setGravity(toolTip.getTextGravity());
151 | setTextAppearance(tipView, toolTip);
152 | setTextTypeFace(tipView, toolTip);
153 | setTipViewElevation(tipView, toolTip);
154 | setTipViewMaxWidth(tipView, toolTip);
155 | return tipView;
156 | }
157 |
158 | private void setTextAppearance(TextView tipView, ToolTip toolTip) {
159 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
160 | tipView.setTextAppearance(toolTip.getTextAppearanceStyle());
161 | } else {
162 | tipView.setTextAppearance(toolTip.getContext(), toolTip.getTextAppearanceStyle());
163 | }
164 | }
165 |
166 | /**
167 | * Sets the custom typeface on the tipView if it was provided via {@link ToolTip}.
168 | */
169 | private void setTextTypeFace(TextView tipView, ToolTip toolTip) {
170 | if (toolTip.getTypeface() != null) {
171 | Typeface existingTypeFace = tipView.getTypeface();
172 | if (existingTypeFace != null) {
173 | // Preserve the text style defined in the text appearance style if available
174 | tipView.setTypeface(toolTip.getTypeface(), existingTypeFace.getStyle());
175 | } else {
176 | tipView.setTypeface(toolTip.getTypeface());
177 | }
178 | }
179 | }
180 |
181 | private void setTipViewElevation(TextView tipView, ToolTip toolTip) {
182 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
183 | if (toolTip.getElevation() > 0) {
184 | ViewOutlineProvider viewOutlineProvider = new ViewOutlineProvider() {
185 | @SuppressLint("NewApi")
186 | @Override
187 | public void getOutline(View view, Outline outline) {
188 | outline.setEmpty();
189 | }
190 | };
191 | tipView.setOutlineProvider(viewOutlineProvider);
192 | tipView.setElevation(toolTip.getElevation());
193 | }
194 | }
195 | }
196 |
197 | private void setTipViewMaxWidth(TextView tipView, ToolTip toolTip) {
198 | if (toolTip.getMaxWidth() > 0) {
199 | tipView.setMaxWidth(toolTip.getMaxWidth());
200 | }
201 | }
202 |
203 | private void switchToolTipSidePosition(ToolTip toolTip) {
204 | if (toolTip.positionedLeftTo()) {
205 | toolTip.setPosition(ToolTip.POSITION_RIGHT_TO);
206 | } else if (toolTip.positionedRightTo()) {
207 | toolTip.setPosition(ToolTip.POSITION_LEFT_TO);
208 | }
209 | }
210 |
211 | public void setAnimationDuration(int duration){
212 | mAnimationDuration = duration;
213 | }
214 |
215 | /**
216 | * Set a custom tooltip animator to override show and hide animation.
217 | * @param animator ToolTipAnimator
218 | */
219 | public void setToolTipAnimator(@NonNull ToolTipAnimator animator) {
220 | mToolTipAnimator = animator;
221 | }
222 |
223 | public boolean dismiss(View tipView, boolean byUser) {
224 | if (tipView != null && isVisible(tipView)) {
225 | int key = (int) tipView.getTag();
226 | mTipsMap.remove(key);
227 | animateDismiss(tipView, byUser);
228 | return true;
229 | }
230 | return false;
231 | }
232 |
233 | public boolean dismiss(Integer key) {
234 | return mTipsMap.containsKey(key) && dismiss(mTipsMap.get(key), false);
235 | }
236 |
237 | public View find(Integer key) {
238 | if (mTipsMap.containsKey(key)) {
239 | return mTipsMap.get(key);
240 | }
241 | return null;
242 | }
243 |
244 | public boolean findAndDismiss(final View anchorView) {
245 | View view = find(anchorView.getId());
246 | return view != null && dismiss(view, false);
247 | }
248 |
249 | public void dismissAll() {
250 | if (!mTipsMap.isEmpty()) {
251 | List> entries = new ArrayList<>(mTipsMap.entrySet());
252 | for (Map.Entry entry : entries) {
253 | dismiss(entry.getValue(), false);
254 | }
255 | }
256 | mTipsMap.clear();
257 | }
258 |
259 | private void animateDismiss(final View view, final boolean byUser) {
260 | mToolTipAnimator.popout(view, mAnimationDuration, new AnimatorListenerAdapter() {
261 | @Override
262 | public void onAnimationEnd(Animator animation) {
263 | super.onAnimationEnd(animation);
264 | if (mListener != null){
265 | mListener.onTipDismissed(view, (Integer) view.getTag(), byUser);
266 | }
267 | }
268 | }).start();
269 | }
270 |
271 | public boolean isVisible(View tipView) {
272 | return tipView.getVisibility() == View.VISIBLE;
273 | }
274 |
275 | }
276 |
--------------------------------------------------------------------------------
/tooltips/src/main/java/com/tomergoldst/tooltips/UiUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2016 Tomer Goldstein
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 |
17 | package com.tomergoldst.tooltips;
18 |
19 | import java.util.Locale;
20 |
21 | class UiUtils {
22 |
23 | public static boolean isRtl() {
24 | return isRtl(Locale.getDefault());
25 | }
26 |
27 | private static boolean isRtl(Locale locale) {
28 | final int directionality = Character.getDirectionality(locale.getDisplayName().charAt(0));
29 | return directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT ||
30 | directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/tooltips/src/main/res/drawable-xxhdpi/tooltip_arrow_down.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomergoldst/tooltips/8e2b65ce51f26fee17a142b8fd5078c5b89946a2/tooltips/src/main/res/drawable-xxhdpi/tooltip_arrow_down.9.png
--------------------------------------------------------------------------------
/tooltips/src/main/res/drawable-xxhdpi/tooltip_arrow_down_left.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomergoldst/tooltips/8e2b65ce51f26fee17a142b8fd5078c5b89946a2/tooltips/src/main/res/drawable-xxhdpi/tooltip_arrow_down_left.9.png
--------------------------------------------------------------------------------
/tooltips/src/main/res/drawable-xxhdpi/tooltip_arrow_down_right.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomergoldst/tooltips/8e2b65ce51f26fee17a142b8fd5078c5b89946a2/tooltips/src/main/res/drawable-xxhdpi/tooltip_arrow_down_right.9.png
--------------------------------------------------------------------------------
/tooltips/src/main/res/drawable-xxhdpi/tooltip_arrow_left.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomergoldst/tooltips/8e2b65ce51f26fee17a142b8fd5078c5b89946a2/tooltips/src/main/res/drawable-xxhdpi/tooltip_arrow_left.9.png
--------------------------------------------------------------------------------
/tooltips/src/main/res/drawable-xxhdpi/tooltip_arrow_right.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomergoldst/tooltips/8e2b65ce51f26fee17a142b8fd5078c5b89946a2/tooltips/src/main/res/drawable-xxhdpi/tooltip_arrow_right.9.png
--------------------------------------------------------------------------------
/tooltips/src/main/res/drawable-xxhdpi/tooltip_arrow_up.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomergoldst/tooltips/8e2b65ce51f26fee17a142b8fd5078c5b89946a2/tooltips/src/main/res/drawable-xxhdpi/tooltip_arrow_up.9.png
--------------------------------------------------------------------------------
/tooltips/src/main/res/drawable-xxhdpi/tooltip_arrow_up_left.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomergoldst/tooltips/8e2b65ce51f26fee17a142b8fd5078c5b89946a2/tooltips/src/main/res/drawable-xxhdpi/tooltip_arrow_up_left.9.png
--------------------------------------------------------------------------------
/tooltips/src/main/res/drawable-xxhdpi/tooltip_arrow_up_right.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomergoldst/tooltips/8e2b65ce51f26fee17a142b8fd5078c5b89946a2/tooltips/src/main/res/drawable-xxhdpi/tooltip_arrow_up_right.9.png
--------------------------------------------------------------------------------
/tooltips/src/main/res/drawable-xxhdpi/tooltip_no_arrow.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomergoldst/tooltips/8e2b65ce51f26fee17a142b8fd5078c5b89946a2/tooltips/src/main/res/drawable-xxhdpi/tooltip_no_arrow.9.png
--------------------------------------------------------------------------------
/tooltips/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #FF4081
4 |
--------------------------------------------------------------------------------
/tooltips/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/tooltips/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
--------------------------------------------------------------------------------
/tooltips/src/test/java/com/tomergoldst/tooltips/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.tomergoldst.tooltips;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------