From 01dd56b978e68e1eb18fd9e59cb0f71af907435c Mon Sep 17 00:00:00 2001 From: Muragijimana Date: Mon, 8 Oct 2018 07:56:32 +0200 Subject: [PATCH] kotlin sample app --- app/build.gradle | 5 + .../taptargetviewsample/MainActivity.java | 127 ------------------ .../taptargetviewsample/MainActivity.kt | 120 +++++++++++++++++ build.gradle | 2 + 4 files changed, 127 insertions(+), 127 deletions(-) delete mode 100644 app/src/main/java/com/getkeepsafe/taptargetviewsample/MainActivity.java create mode 100644 app/src/main/java/com/getkeepsafe/taptargetviewsample/MainActivity.kt diff --git a/app/build.gradle b/app/build.gradle index 297b7ee..72a1bcd 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,4 +1,5 @@ apply plugin: 'com.android.application' +apply plugin: 'kotlin-android' android { compileSdkVersion defCompileSdkVersion @@ -24,4 +25,8 @@ dependencies { implementation "androidx.appcompat:appcompat:$defAndroidXVersion" implementation "com.google.android.material:material:$defMaterialVersion" implementation 'com.facebook.stetho:stetho:1.5.0' + compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" +} +repositories { + mavenCentral() } diff --git a/app/src/main/java/com/getkeepsafe/taptargetviewsample/MainActivity.java b/app/src/main/java/com/getkeepsafe/taptargetviewsample/MainActivity.java deleted file mode 100644 index 77e4df6..0000000 --- a/app/src/main/java/com/getkeepsafe/taptargetviewsample/MainActivity.java +++ /dev/null @@ -1,127 +0,0 @@ -package com.getkeepsafe.taptargetviewsample; - -import android.content.DialogInterface; -import android.graphics.Rect; -import android.graphics.Typeface; -import android.graphics.drawable.Drawable; -import android.os.Bundle; -import androidx.core.content.ContextCompat; -import androidx.appcompat.app.AlertDialog; -import androidx.appcompat.app.AppCompatActivity; -import androidx.appcompat.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 :("); - } - }); - } -} diff --git a/app/src/main/java/com/getkeepsafe/taptargetviewsample/MainActivity.kt b/app/src/main/java/com/getkeepsafe/taptargetviewsample/MainActivity.kt new file mode 100644 index 0000000..ac02997 --- /dev/null +++ b/app/src/main/java/com/getkeepsafe/taptargetviewsample/MainActivity.kt @@ -0,0 +1,120 @@ +package com.getkeepsafe.taptargetviewsample + +import android.content.DialogInterface +import android.graphics.Rect +import android.graphics.Typeface +import android.graphics.drawable.Drawable +import android.os.Bundle +import androidx.core.content.ContextCompat +import androidx.appcompat.app.AlertDialog +import androidx.appcompat.app.AppCompatActivity +import androidx.appcompat.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.view.View +import android.widget.TextView +import android.widget.Toast + +import com.getkeepsafe.taptargetview.TapTarget +import com.getkeepsafe.taptargetview.TapTargetSequence +import com.getkeepsafe.taptargetview.TapTargetView + +class MainActivity : AppCompatActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + + val toolbar = findViewById(R.id.toolbar) + toolbar.inflateMenu(R.menu.menu_main) + toolbar.navigationIcon = 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 + val display = windowManager.defaultDisplay + // Load our little droid guy + val droid = ContextCompat.getDrawable(this, R.drawable.ic_android_black_24dp) + // Tell our droid buddy where we want him to appear + val droidTarget = Rect(0, 0, droid!!.intrinsicWidth * 2, droid.intrinsicHeight * 2) + // Using deprecated methods makes you look way cool + droidTarget.offset(display.width / 2, display.height / 2) + + val sassyDesc = SpannableString("It allows you to go back, sometimes") + sassyDesc.setSpan(StyleSpan(Typeface.ITALIC), sassyDesc.length - "sometimes".length, sassyDesc.length, 0) + + // We have a sequence of targets, so lets build it! + val sequence = 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(object : TapTargetSequence.Listener { + // This listener will tell us when interesting(tm) events happen in regards + // to the sequence + override fun onSequenceFinish() { + (findViewById(R.id.educated) as TextView).text = "Congratulations! You're educated now!" + } + + override fun onSequenceStep(lastTarget: TapTarget, targetClicked: Boolean) { + Log.d("TapTargetView", "Clicked on " + lastTarget.id()) + } + + override fun onSequenceCanceled(lastTarget: TapTarget) { + val dialog = AlertDialog.Builder(this@MainActivity) + .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), object : TapTargetView.Listener() { + override fun onTargetClick(view: TapTargetView) { + super.onTargetClick(view) + dialog.dismiss() + } + }) + } + }) + + // You don't always need a sequence, and for that there's a single time tap target + val spannedDesc = SpannableString("This is the sample app for TapTargetView") + spannedDesc.setSpan(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), object : TapTargetView.Listener() { + override fun onTargetClick(view: TapTargetView) { + super.onTargetClick(view) + // .. which evidently starts the sequence we defined earlier + sequence.start() + } + + override fun onOuterCircleClick(view: TapTargetView) { + super.onOuterCircleClick(view) + Toast.makeText(view.context, "You clicked the outer circle!", Toast.LENGTH_SHORT).show() + } + + override fun onTargetDismissed(view: TapTargetView, userInitiated: Boolean) { + Log.d("TapTargetViewSample", "You dismissed me :(") + } + }) + } +} diff --git a/build.gradle b/build.gradle index 97aceb4..04dee40 100644 --- a/build.gradle +++ b/build.gradle @@ -1,4 +1,5 @@ buildscript { + ext.kotlin_version = '1.2.71' repositories { jcenter() google() @@ -7,6 +8,7 @@ buildscript { classpath 'com.android.tools.build:gradle:3.2.0' classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1' classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4' + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } }