1414import android .util .AttributeSet ;
1515import android .util .TypedValue ;
1616import android .view .Gravity ;
17+ import android .view .KeyEvent ;
1718import android .view .LayoutInflater ;
1819import android .view .MotionEvent ;
1920import android .view .View ;
@@ -130,6 +131,7 @@ public class MaterialIntroView extends RelativeLayout {
130131 * Dismiss on touch any position
131132 */
132133 private boolean dismissOnTouch ;
134+ private boolean dismissOnBackPress ;
133135
134136 /**
135137 * Info dialog view
@@ -259,6 +261,7 @@ private void init(Context context) {
259261 isReady = false ;
260262 isFadeAnimationEnabled = true ;
261263 dismissOnTouch = false ;
264+ dismissOnBackPress = false ;
262265 isLayoutCompleted = false ;
263266 isInfoEnabled = false ;
264267 isDotViewEnabled = false ;
@@ -419,6 +422,9 @@ public void onAnimationStart() {
419422 });
420423 else
421424 setVisibility (VISIBLE );
425+ if (dismissOnBackPress ) {
426+ requestFocus ();
427+ }
422428 }
423429 }, delayMillis );
424430
@@ -573,6 +579,10 @@ private void setDismissOnTouch(boolean dismissOnTouch) {
573579 this .dismissOnTouch = dismissOnTouch ;
574580 }
575581
582+ private void setDismissOnBackPress (boolean dismissOnBackPress ) {
583+ this .dismissOnBackPress = dismissOnBackPress ;
584+ }
585+
576586 private void setFocusGravity (FocusGravity focusGravity ) {
577587 this .focusGravity = focusGravity ;
578588 }
@@ -606,6 +616,11 @@ private void enableDotView(boolean isDotViewEnabled){
606616 this .isDotViewEnabled = isDotViewEnabled ;
607617 }
608618
619+ private void enableDismissOnBackPress () {
620+ setFocusableInTouchMode (true );
621+ setFocusable (true );
622+ }
623+
609624 public void setConfiguration (MaterialIntroConfiguration configuration ) {
610625
611626 if (configuration != null ) {
@@ -615,6 +630,7 @@ public void setConfiguration(MaterialIntroConfiguration configuration) {
615630 this .colorTextViewInfo = configuration .getColorTextViewInfo ();
616631 this .isDotViewEnabled = configuration .isDotViewEnabled ();
617632 this .dismissOnTouch = configuration .isDismissOnTouch ();
633+ this .dismissOnBackPress = configuration .isDismissOnBackPress ();
618634 this .colorTextViewInfo = configuration .getColorTextViewInfo ();
619635 this .focusType = configuration .getFocusType ();
620636 this .focusGravity = configuration .getFocusGravity ();
@@ -710,6 +726,11 @@ public Builder dismissOnTouch(boolean dismissOnTouch) {
710726 return this ;
711727 }
712728
729+ public Builder dismissOnBackPress (boolean dismissOnBackPress ) {
730+ materialIntroView .setDismissOnBackPress (dismissOnBackPress );
731+ return this ;
732+ }
733+
713734 public Builder setUsageId (String materialIntroViewId ) {
714735 materialIntroView .setUsageId (materialIntroViewId );
715736 return this ;
@@ -774,6 +795,10 @@ public MaterialIntroView build() {
774795 }
775796
776797 materialIntroView .setShape (shape );
798+
799+ if (materialIntroView .dismissOnBackPress ) {
800+ materialIntroView .enableDismissOnBackPress ();
801+ }
777802 return materialIntroView ;
778803 }
779804
@@ -784,4 +809,15 @@ public MaterialIntroView show() {
784809
785810 }
786811
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+
787823}
0 commit comments