14
14
import android .util .AttributeSet ;
15
15
import android .util .TypedValue ;
16
16
import android .view .Gravity ;
17
+ import android .view .KeyEvent ;
17
18
import android .view .LayoutInflater ;
18
19
import android .view .MotionEvent ;
19
20
import android .view .View ;
@@ -130,6 +131,7 @@ public class MaterialIntroView extends RelativeLayout {
130
131
* Dismiss on touch any position
131
132
*/
132
133
private boolean dismissOnTouch ;
134
+ private boolean dismissOnBackPress ;
133
135
134
136
/**
135
137
* Info dialog view
@@ -259,6 +261,7 @@ private void init(Context context) {
259
261
isReady = false ;
260
262
isFadeAnimationEnabled = true ;
261
263
dismissOnTouch = false ;
264
+ dismissOnBackPress = false ;
262
265
isLayoutCompleted = false ;
263
266
isInfoEnabled = false ;
264
267
isDotViewEnabled = false ;
@@ -419,6 +422,9 @@ public void onAnimationStart() {
419
422
});
420
423
else
421
424
setVisibility (VISIBLE );
425
+ if (dismissOnBackPress ) {
426
+ requestFocus ();
427
+ }
422
428
}
423
429
}, delayMillis );
424
430
@@ -573,6 +579,10 @@ private void setDismissOnTouch(boolean dismissOnTouch) {
573
579
this .dismissOnTouch = dismissOnTouch ;
574
580
}
575
581
582
+ private void setDismissOnBackPress (boolean dismissOnBackPress ) {
583
+ this .dismissOnBackPress = dismissOnBackPress ;
584
+ }
585
+
576
586
private void setFocusGravity (FocusGravity focusGravity ) {
577
587
this .focusGravity = focusGravity ;
578
588
}
@@ -606,6 +616,11 @@ private void enableDotView(boolean isDotViewEnabled){
606
616
this .isDotViewEnabled = isDotViewEnabled ;
607
617
}
608
618
619
+ private void enableDismissOnBackPress () {
620
+ setFocusableInTouchMode (true );
621
+ setFocusable (true );
622
+ }
623
+
609
624
public void setConfiguration (MaterialIntroConfiguration configuration ) {
610
625
611
626
if (configuration != null ) {
@@ -615,6 +630,7 @@ public void setConfiguration(MaterialIntroConfiguration configuration) {
615
630
this .colorTextViewInfo = configuration .getColorTextViewInfo ();
616
631
this .isDotViewEnabled = configuration .isDotViewEnabled ();
617
632
this .dismissOnTouch = configuration .isDismissOnTouch ();
633
+ this .dismissOnBackPress = configuration .isDismissOnBackPress ();
618
634
this .colorTextViewInfo = configuration .getColorTextViewInfo ();
619
635
this .focusType = configuration .getFocusType ();
620
636
this .focusGravity = configuration .getFocusGravity ();
@@ -710,6 +726,11 @@ public Builder dismissOnTouch(boolean dismissOnTouch) {
710
726
return this ;
711
727
}
712
728
729
+ public Builder dismissOnBackPress (boolean dismissOnBackPress ) {
730
+ materialIntroView .setDismissOnBackPress (dismissOnBackPress );
731
+ return this ;
732
+ }
733
+
713
734
public Builder setUsageId (String materialIntroViewId ) {
714
735
materialIntroView .setUsageId (materialIntroViewId );
715
736
return this ;
@@ -774,6 +795,10 @@ public MaterialIntroView build() {
774
795
}
775
796
776
797
materialIntroView .setShape (shape );
798
+
799
+ if (materialIntroView .dismissOnBackPress ) {
800
+ materialIntroView .enableDismissOnBackPress ();
801
+ }
777
802
return materialIntroView ;
778
803
}
779
804
@@ -784,4 +809,15 @@ public MaterialIntroView show() {
784
809
785
810
}
786
811
812
+ @ Override
813
+ public boolean dispatchKeyEvent (KeyEvent event ) {
814
+ if (dismissOnBackPress && event .getKeyCode () == KeyEvent .KEYCODE_BACK ) {
815
+ if (event .getAction () == KeyEvent .ACTION_UP ) {
816
+ dismiss ();
817
+ }
818
+ return true ;
819
+ }
820
+ return super .dispatchKeyEvent (event );
821
+ }
822
+
787
823
}
0 commit comments