Skip to content

Commit 1b50a4e

Browse files
committed
add support for adding a 'back' button
1 parent af38f38 commit 1b50a4e

File tree

6 files changed

+93
-9
lines changed

6 files changed

+93
-9
lines changed

library/src/main/java/xyz/klinker/android/floating_tutorial/TutorialPage.kt

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ abstract class TutorialPage(private val activity: FloatingTutorialActivity) : Fr
4040
private val pageContent: FrameLayout by lazy { rootLayout.findViewById<View>(R.id.tutorial_page_content) as FrameLayout }
4141
private val progressHolder: LinearLayout by lazy { rootLayout.findViewById<View>(R.id.tutorial_progress) as LinearLayout }
4242
private val nextButton: Button by lazy { rootLayout.findViewById<View>(R.id.tutorial_next_button) as Button }
43+
private val backButton: Button by lazy { rootLayout.findViewById<View>(R.id.tutorial_back_button) as Button }
4344

4445
private var data: Any? = null
4546
private var pageIndex: Int? = null
@@ -56,6 +57,7 @@ abstract class TutorialPage(private val activity: FloatingTutorialActivity) : Fr
5657

5758
if (pageIndex == pageCount - 1) setNextButtonText(R.string.tutorial_finish)
5859
nextButton.setOnClickListener { activity.onNextPressed() }
60+
backButton.setOnClickListener { activity.onBackPressed() }
5961

6062
initPage()
6163

@@ -162,6 +164,51 @@ abstract class TutorialPage(private val activity: FloatingTutorialActivity) : Fr
162164
return activity.provider.tutorialPages[pageIndex!! - 1].getPageResultData()
163165
}
164166

167+
/**
168+
* Set the color of the "Back" button's text. The default is close to black.
169+
*
170+
* @param newColor the color resource you want to use.
171+
*/
172+
fun setBackButtonTextColorResource(@ColorRes newColor: Int) {
173+
setBackButtonTextColor(activity.resources.getColor(newColor))
174+
}
175+
176+
/**
177+
* Set the color of the "Back" button's text. The default is close to black.
178+
*
179+
* @param newColor the color value you want to use.
180+
*/
181+
fun setBackButtonTextColor(newColor: Int) {
182+
nextButton.setTextColor(newColor)
183+
}
184+
185+
/**
186+
* Set the text for the "Back" button. The default is simply "Back".
187+
*
188+
* @param text the string you want to use.
189+
*/
190+
fun setBackButtonText(@StringRes text: Int) {
191+
setBackButtonText(activity.getString(text))
192+
}
193+
194+
/**
195+
* Set the text for the "Back" button. The default is simply "Back".
196+
*
197+
* @param text the string you want to use.
198+
*/
199+
fun setBackButtonText(text: String) {
200+
backButton.text = text
201+
showBackButton()
202+
}
203+
204+
/**
205+
* Show the "Back" button to explicitly handle returning to the previous page.
206+
*/
207+
fun showBackButton() {
208+
backButton.visibility = View.VISIBLE
209+
progressHolder.visibility = View.GONE
210+
}
211+
165212
/**
166213
* Hide the "Next" button to handle advancing the paging through a [View] you define in the page.
167214
*/
@@ -257,7 +304,7 @@ abstract class TutorialPage(private val activity: FloatingTutorialActivity) : Fr
257304
}
258305

259306
companion object {
260-
private val DARKNESS_THRESHOLD = 0.30
307+
private const val DARKNESS_THRESHOLD = 0.30
261308
private fun isColorDark(color: Int): Boolean {
262309
val darkness = 1 - (0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(color)) / 255
263310
return darkness >= DARKNESS_THRESHOLD

library/src/main/res/layout/tutorial_page_root.xml

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,44 @@
4040

4141
<LinearLayout
4242
android:id="@+id/tutorial_progress"
43-
android:layout_width="0dp"
43+
android:layout_width="wrap_content"
4444
android:layout_height="match_parent"
45-
android:layout_marginLeft="12dp"
46-
android:layout_marginRight="12dp"
47-
android:layout_weight="1"
45+
android:layout_marginStart="12dp"
46+
android:layout_marginEnd="12dp"
4847
android:gravity="center_vertical"
4948
android:orientation="horizontal" />
5049

51-
<Button
50+
<com.google.android.material.button.MaterialButton
51+
android:id="@+id/tutorial_back_button"
52+
style="@style/Widget.MaterialComponents.Button.TextButton"
53+
android:layout_width="wrap_content"
54+
android:layout_height="wrap_content"
55+
android:layout_gravity="start"
56+
android:layout_marginStart="12dp"
57+
android:layout_marginLeft="12dp"
58+
android:layout_marginBottom="6dp"
59+
android:text="@string/tutorial_back"
60+
android:textColor="@android:color/black"
61+
android:visibility="gone"
62+
app:rippleColor="@color/tutorial_ripple_color" />
63+
64+
<View
65+
android:layout_width="0dp"
66+
android:layout_height="match_parent"
67+
android:layout_weight="1" />
68+
69+
<com.google.android.material.button.MaterialButton
5270
android:id="@+id/tutorial_next_button"
5371
style="@style/Widget.MaterialComponents.Button.TextButton"
54-
app:rippleColor="@color/tutorial_ripple_color"
5572
android:layout_width="wrap_content"
5673
android:layout_height="wrap_content"
5774
android:layout_gravity="end"
5875
android:layout_marginEnd="12dp"
5976
android:layout_marginRight="12dp"
60-
android:background="?selectableItemBackgroundBorderless"
77+
android:layout_marginBottom="6dp"
6178
android:text="@string/tutorial_next"
62-
android:textColor="@android:color/black" />
79+
android:textColor="@android:color/black"
80+
app:rippleColor="@color/tutorial_ripple_color" />
6381

6482
</LinearLayout>
6583

library/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
<resources>
1919

20+
<string name="tutorial_back">Back</string>
2021
<string name="tutorial_next">Next</string>
2122
<string name="tutorial_finish">Finish</string>
2223

sample-java/src/main/java/xyz/klinker/floating_tutorial/examples/RateItExample.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ public PageTwo(FloatingTutorialActivity activity) {
9494
@Override
9595
public void initPage() {
9696
setContentView(R.layout.example_rate_it_page_2);
97+
98+
findViewById(R.id.tutorial_back_button).setOnClickListener(new OnClickListener() {
99+
@Override
100+
public void onClick(View view) {
101+
setActivityResult(Activity.RESULT_CANCELED, new Intent());
102+
getActivity().finishAnimated();
103+
}
104+
});
97105
}
98106

99107
@Override
@@ -113,6 +121,7 @@ public void onShown(boolean firstTimeShown) {
113121
setActivityResult(Activity.RESULT_OK, data);
114122

115123
setNextButtonText(R.string.rate_it);
124+
setBackButtonText(R.string.no_thanks);
116125
} else {
117126
titleText.setText(R.string.rate_it_page_2_bad_title);
118127
summaryText.setText(R.string.rate_it_page_2_bad_summary);
@@ -122,6 +131,7 @@ public void onShown(boolean firstTimeShown) {
122131
setActivityResult(Activity.RESULT_OK, data);
123132

124133
setNextButtonText(R.string.send_email);
134+
setBackButtonText(R.string.no_thanks);
125135
}
126136
}
127137

sample-kotlin/src/main/java/xyz/klinker/floating_tutorial/examples/RateItExample.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ class RateItExample : FloatingTutorialActivity() {
6868
private class PageTwo(activity: FloatingTutorialActivity) : TutorialPage(activity) {
6969
override fun initPage() {
7070
setContentView(R.layout.example_rate_it_page_2)
71+
72+
findViewById<View>(R.id.tutorial_back_button).setOnClickListener {
73+
setActivityResult(Activity.RESULT_CANCELED, Intent())
74+
getActivity().finishAnimated()
75+
}
7176
}
7277

7378
override fun onShown(firstTimeShown: Boolean) {
@@ -86,6 +91,7 @@ class RateItExample : FloatingTutorialActivity() {
8691
setActivityResult(Activity.RESULT_OK, data)
8792

8893
setNextButtonText(R.string.rate_it)
94+
setBackButtonText(R.string.no_thanks)
8995
} else {
9096
titleText.setText(R.string.rate_it_page_2_bad_title)
9197
summaryText.setText(R.string.rate_it_page_2_bad_summary)
@@ -95,6 +101,7 @@ class RateItExample : FloatingTutorialActivity() {
95101
setActivityResult(Activity.RESULT_OK, data)
96102

97103
setNextButtonText(R.string.send_email)
104+
setBackButtonText(R.string.no_thanks)
98105
}
99106
}
100107

sample-shared/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<string name="rate_it_page_2_bad_summary">Not loving the app yet? Please get in touch to let us know how to make it better!</string>
4848
<string name="rate_it">Rate It!</string>
4949
<string name="send_email">Send Email</string>
50+
<string name="no_thanks">No thanks</string>
5051

5152
<!-- Feature Walk-through Example -->
5253
<string name="feature_1_title">My awesome first feature</string>

0 commit comments

Comments
 (0)