Create Tap Target View in the Android application: 10 minute
Tap Target View
Android
application
- By Code solution
- Jan 20th, 2021
- 0 comments
- 0
Create tap target view in the Android application.A Tap Target implementation in Android based on Material Design Onboarding guidelines.Animation View to Highlight particular Views for Android, it can be Used with Views that you see important.
Creating New Project
- Create a new project in Android Studio from File ⇒ New Project. When selecting Empty Activity and proceed.
- Add taptargetview and stetho dependency to your build.gradle and rebuild the project.
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support:design:27.1.1' implementation 'com.android.support.constraint:constraint-layout:1.0.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' implementation 'com.getkeepsafe.taptargetview:taptargetview:1.11.0' implementation 'com.facebook.stetho:stetho:1.5.0' }
Choosing the Colors
- Open colors.xml located under res ⇒ values and add the below color values.
colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> </resources>
- Open dimens.xml located under res ⇒ values and add the below values.
<resources> <!-- Default screen margins, per the Android Design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> <dimen name="title_text_size">20dp</dimen> </resources>
- Open Style.xml located under res ⇒ values and add the below values.
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> </resources>
- Create four XML layouts named activity_main.xml under res ⇒ layouts.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <FrameLayout android:layout_width="match_parent" android:layout_height="250dp" android:background="#444444"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/transparent" android:theme="@style/Theme.AppCompat" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:orientation="vertical" android:padding="16dp" android:background="@color/colorPrimary"> <TextView style="@style/TextAppearance.AppCompat.Subhead" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Some place" android:theme="@style/Theme.AppCompat"/> <TextView style="@style/TextAppearance.AppCompat.Body1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="8dp" android:text="Building" android:theme="@style/Theme.AppCompat"/> </LinearLayout> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end|bottom" android:layout_marginBottom="50dp" android:layout_marginEnd="16dp" android:src="@drawable/ic_directions_car_black_24dp" tools:ignore="RtlCompat" /> </FrameLayout> <TextView android:id="@+id/educated" style="@style/TextAppearance.AppCompat.Display1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="16dp"/> </LinearLayout>
- Create Class SampleApplication.java and modify the code as below
package targetview.wappteh.com.targetview; import android.app.Application; import com.facebook.stetho.Stetho; public class SampleApplication extends Application { @Override public void onCreate() { super.onCreate(); Stetho.initializeWithDefaults(this); } }
- Open MainActivity.java and modify the code as below.
package targetview.wappteh.com.targetview; import android.content.DialogInterface; import android.graphics.Rect; import android.graphics.Typeface; import android.graphics.drawable.Drawable; import android.support.v4.content.ContextCompat; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.text.SpannableString; import android.text.style.StyleSpan; import android.text.style.UnderlineSpan; import android.util.Log; import android.view.Display; import android.widget.TextView; import android.widget.Toast; import com.getkeepsafe.taptargetview.TapTarget; import com.getkeepsafe.taptargetview.TapTargetSequence; import com.getkeepsafe.taptargetview.TapTargetView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.inflateMenu(R.menu.menu_main); toolbar.setNavigationIcon(ContextCompat.getDrawable(this, R.drawable.ic_arrow_back_white_24dp)); // We load a drawable and create a location to show a tap target here // We need the display to get the width and height at this point in time final Display display = getWindowManager().getDefaultDisplay(); // Load our little droid guy final Drawable droid = ContextCompat.getDrawable(this, R.drawable.ic_android_black_24dp); // Tell our droid buddy where we want him to appear final Rect droidTarget = new Rect(0, 0, droid.getIntrinsicWidth() * 2, droid.getIntrinsicHeight() * 2); // Using deprecated methods makes you look way cool droidTarget.offset(display.getWidth() / 2, display.getHeight() / 2); final SpannableString sassyDesc = new SpannableString("It allows you to go back, sometimes"); sassyDesc.setSpan(new StyleSpan(Typeface.ITALIC), sassyDesc.length() - "sometimes".length(), sassyDesc.length(), 0); // We have a sequence of targets, so lets build it! final TapTargetSequence sequence = new TapTargetSequence(this) .targets( // This tap target will target the back button, we just need to pass its containing toolbar TapTarget.forToolbarNavigationIcon(toolbar, "This is the back button", sassyDesc).id(1), // Likewise, this tap target will target the search button TapTarget.forToolbarMenuItem(toolbar, R.id.search, "This is a search icon", "As you can see, it has gotten pretty dark around here...") .dimColor(android.R.color.black) .outerCircleColor(R.color.colorAccent) .targetCircleColor(android.R.color.black) .transparentTarget(true) .textColor(android.R.color.black) .id(2), // You can also target the overflow button in your toolbar TapTarget.forToolbarOverflow(toolbar, "This will show more options", "But they're not useful :(").id(3), // This tap target will target our droid buddy at the given target rect TapTarget.forBounds(droidTarget, "Oh look!", "You can point to any part of the screen. You also can't cancel this one!") .cancelable(false) .icon(droid) .id(4) ) .listener(new TapTargetSequence.Listener() { // This listener will tell us when interesting(tm) events happen in regards // to the sequence @Override public void onSequenceFinish() { ((TextView) findViewById(R.id.educated)).setText("Congratulations! You're educated now!"); } @Override public void onSequenceStep(TapTarget lastTarget, boolean targetClicked) { Log.d("TapTargetView", "Clicked on " + lastTarget.id()); } @Override public void onSequenceCanceled(TapTarget lastTarget) { final AlertDialog dialog = new AlertDialog.Builder(MainActivity.this) .setTitle("Uh oh") .setMessage("You canceled the sequence") .setPositiveButton("Oops", null).show(); TapTargetView.showFor(dialog, TapTarget.forView(dialog.getButton(DialogInterface.BUTTON_POSITIVE), "Uh oh!", "You canceled the sequence at step " + lastTarget.id()) .cancelable(false) .tintTarget(false), new TapTargetView.Listener() { @Override public void onTargetClick(TapTargetView view) { super.onTargetClick(view); dialog.dismiss(); } }); } }); // You don't always need a sequence, and for that there's a single time tap target final SpannableString spannedDesc = new SpannableString("This is the sample app for TapTargetView"); spannedDesc.setSpan(new UnderlineSpan(), spannedDesc.length() - "TapTargetView".length(), spannedDesc.length(), 0); TapTargetView.showFor(this, TapTarget.forView(findViewById(R.id.fab), "Hello, world!", spannedDesc) .cancelable(false) .drawShadow(true) .titleTextDimen(R.dimen.title_text_size) .tintTarget(false), new TapTargetView.Listener() { @Override public void onTargetClick(TapTargetView view) { super.onTargetClick(view); // .. which evidently starts the sequence we defined earlier sequence.start(); } @Override public void onOuterCircleClick(TapTargetView view) { super.onOuterCircleClick(view); Toast.makeText(view.getContext(), "You clicked the outer circle!", Toast.LENGTH_SHORT).show(); } @Override public void onTargetDismissed(TapTargetView view, boolean userInitiated) { Log.d("TapTargetViewSample", "You dismissed me :("); } }); } }
- Finally open AndroidManifest.XML and make WelcomeActivity as launcher activity.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="targetview.wappteh.com.targetview"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:name=".SampleApplication" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>