From 68937babb996c53d3594fa46c042c01c7267f13d Mon Sep 17 00:00:00 2001 From: dikkemulle Date: Tue, 5 Apr 2016 20:27:39 +0200 Subject: [PATCH 1/2] Added second button --- .../amlcurran/showcaseview/ShowcaseView.java | 179 +++++++++++++++++- sample/src/main/AndroidManifest.xml | 3 + .../sample/ButtonTwoActivity.java | 109 +++++++++++ .../showcaseview/sample/MainActivity.java | 4 +- .../main/res/layout/activity_two_buttons.xml | 25 +++ sample/src/main/res/values/strings.xml | 14 ++ 6 files changed, 327 insertions(+), 7 deletions(-) create mode 100644 sample/src/main/java/com/github/amlcurran/showcaseview/sample/ButtonTwoActivity.java create mode 100644 sample/src/main/res/layout/activity_two_buttons.xml diff --git a/library/src/main/java/com/github/amlcurran/showcaseview/ShowcaseView.java b/library/src/main/java/com/github/amlcurran/showcaseview/ShowcaseView.java index 1e7d5bcb2..52a2b6839 100644 --- a/library/src/main/java/com/github/amlcurran/showcaseview/ShowcaseView.java +++ b/library/src/main/java/com/github/amlcurran/showcaseview/ShowcaseView.java @@ -64,6 +64,9 @@ public class ShowcaseView extends RelativeLayout } private Button mEndButton; + private Button mButtonTwo; + + private final TextDrawer textDrawer; private ShowcaseDrawer showcaseDrawer; private final ShowcaseAreaCalculator showcaseAreaCalculator; @@ -121,6 +124,7 @@ protected ShowcaseView(Context context, AttributeSet attrs, int defStyle, boolea fadeOutMillis = getResources().getInteger(android.R.integer.config_mediumAnimTime); mEndButton = (Button) LayoutInflater.from(context).inflate(R.layout.showcase_button, null); + mButtonTwo = (Button) LayoutInflater.from(context).inflate(R.layout.showcase_button, null); if (newStyle) { showcaseDrawer = new NewShowcaseDrawer(getResources(), context.getTheme()); } else { @@ -151,12 +155,29 @@ private void init() { addView(mEndButton); } + } private boolean hasShot() { return shotStateStore.hasShot(); } + + private void addButtonTwo() { + if (mButtonTwo.getParent() == null) { + int margin = (int) getResources().getDimension(R.dimen.button_margin); + RelativeLayout.LayoutParams lps = (LayoutParams) generateDefaultLayoutParams(); + lps.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); + lps.addRule(RelativeLayout.ALIGN_PARENT_LEFT); + lps.setMargins(margin, margin, margin, margin); + mButtonTwo.setLayoutParams(lps); + mButtonTwo.setText(android.R.string.cancel); + if (!hasCustomClickListener) { + mButtonTwo.setOnClickListener(hideOnClickListener); + } + addView(mButtonTwo); + } + } void setShowcasePosition(Point point) { setShowcasePosition(point.x, point.y); } @@ -225,21 +246,19 @@ public boolean hasShowcaseView() { } public void setShowcaseX(int x) { - setShowcasePosition(x, getShowcaseY()); + setShowcasePosition(x, showcaseY); } public void setShowcaseY(int y) { - setShowcasePosition(getShowcaseX(), y); + setShowcasePosition(showcaseX, y); } public int getShowcaseX() { - getLocationInWindow(positionInWindow); - return showcaseX + positionInWindow[0]; + return showcaseX; } public int getShowcaseY() { - getLocationInWindow(positionInWindow); - return showcaseY + positionInWindow[1]; + return showcaseY; } /** @@ -261,6 +280,43 @@ public void overrideButtonClick(OnClickListener listener) { hasCustomClickListener = true; } + public void overrideButtonTwoClick(OnClickListener listener) { + if (shotStateStore.hasShot()) { + return; + } + if (mButtonTwo != null) { + if (listener != null) { + mButtonTwo.setOnClickListener(listener); + } else { + mButtonTwo.setOnClickListener(hideOnClickListener); + } + } + hasCustomClickListener = true; + } + + + + public void overrideButtonsClick(OnClickListener listener1,OnClickListener listener2) { + if (shotStateStore.hasShot()) { + return; + } + if (mEndButton != null) { + if (listener1 != null) { + mEndButton.setOnClickListener(listener1); + } else { + mEndButton.setOnClickListener(hideOnClickListener); + } + } + if (mButtonTwo != null) { + if (listener2 != null) { + mButtonTwo.setOnClickListener(listener2); + } else { + mButtonTwo.setOnClickListener(hideOnClickListener); + } + } + hasCustomClickListener = true; + } + public void setOnShowcaseEventListener(OnShowcaseEventListener listener) { if (listener != null) { mEventListener = listener; @@ -275,6 +331,22 @@ public void setButtonText(CharSequence text) { } } + /** + * Set text of BOTH buttons + * + * @param text1 Listener to listen to on click events for first button + * @param text2 Listener to listen to on click events for second button + */ + public void setButtonsText(CharSequence text1,CharSequence text2) { + if (mEndButton != null) { + mEndButton.setText(text1); + } + if (mButtonTwo != null) { + mButtonTwo.setText(text2); + } + + } + private void recalculateText() { boolean recalculatedCling = showcaseAreaCalculator.calculateShowcaseRect(showcaseX, showcaseY, showcaseDrawer); boolean recalculateText = recalculatedCling || hasAlteredText; @@ -424,6 +496,14 @@ public void showButton() { mEndButton.setVisibility(VISIBLE); } + public void hideButtonTwo() { + mButtonTwo.setVisibility(GONE); + } + + public void showButtonTwo() { + mButtonTwo.setVisibility(VISIBLE); + } + /** * Builder class which allows easier creation of {@link ShowcaseView}s. * It is recommended that you use this Builder class. @@ -555,6 +635,16 @@ public Builder setOnClickListener(OnClickListener onClickListener) { return this; } + public Builder setOnClickListeners(OnClickListener onClickListener1,OnClickListener onClickListener2) { + showcaseView.overrideButtonsClick(onClickListener1,onClickListener2); + return this; + } + + public Builder setOnClickButtonTwoListener(OnClickListener onClickListener) { + showcaseView.overrideButtonTwoClick(onClickListener); + return this; + } + /** * Don't make the ShowcaseView block touches on itself. This doesn't * block touches in the showcased area. @@ -628,6 +718,14 @@ public Builder replaceEndButton(Button button) { showcaseView.setEndButton(button); return this; } + /** + * Call addSecondButton first! + * + */ + public Builder replaceButtonTwo(Button button) { + showcaseView.setButtonTwo(button); + return this; + } /** * Replace the end button with the one provided. Note that this resets any OnClickListener provided @@ -640,6 +738,17 @@ public Builder replaceEndButton(int buttonResourceId) { } return replaceEndButton((Button) view); } + /** + * Call addSecondButton first! + * + */ + public Builder replaceButtonTwo(int buttonResourceId) { + View view = LayoutInflater.from(activity).inflate(buttonResourceId, showcaseView, false); + if (!(view instanceof Button)) { + throw new IllegalArgumentException("Attempted to replace showcase button with a layout which isn't a button"); + } + return replaceButtonTwo((Button) view); + } /** * Block any touch made on the ShowcaseView, even inside the showcase @@ -658,6 +767,14 @@ public Builder useDecorViewAsParent() { this.parentIndex = -1; return this; } + /** + * Call this first if you plan to use other Builder methods + * using the second button + */ + public Builder addSecondButton(){ + showcaseView.addButtonTwo(); + return this; + } } private void setEndButton(Button button) { @@ -670,6 +787,27 @@ private void setEndButton(Button button) { addView(button); } + private void setButtonTwo(Button button) { + LayoutParams copyParams = (LayoutParams) mButtonTwo.getLayoutParams(); + mButtonTwo.setOnClickListener(null); + removeView(mButtonTwo); + mButtonTwo = button; + button.setOnClickListener(hideOnClickListener); + button.setLayoutParams(copyParams); + addView(button); + } + + + private void setButtons(Button button1,Button button2) { + if(button1 != null) { + setEndButton(button1); + } + + if(button2 != null) { + setButtonTwo(button2); + } + } + private void setShowcaseDrawer(ShowcaseDrawer showcaseDrawer) { this.showcaseDrawer = showcaseDrawer; this.showcaseDrawer.setBackgroundColour(backgroundColor); @@ -717,6 +855,14 @@ public void setButtonPosition(RelativeLayout.LayoutParams layoutParams) { mEndButton.setLayoutParams(layoutParams); } + public void setButtonTwoPosition(RelativeLayout.LayoutParams layoutParams) { + mButtonTwo.setLayoutParams(layoutParams); + } + + public void setButtonPositions(RelativeLayout.LayoutParams layoutParams1,RelativeLayout.LayoutParams layoutParams2) { + setButtonPosition(layoutParams1); + setButtonTwoPosition(layoutParams2); + } /** * Sets the text alignment of the detail text */ @@ -803,6 +949,7 @@ private void updateStyle(TypedArray styled, boolean invalidate) { showcaseDrawer.setBackgroundColour(backgroundColor); tintButton(showcaseColor, tintButton); mEndButton.setText(buttonText); + mButtonTwo.setText(buttonText); textDrawer.setTitleStyling(titleTextAppearance); textDrawer.setDetailStyling(detailTextAppearance); hasAlteredText = true; @@ -812,6 +959,26 @@ private void updateStyle(TypedArray styled, boolean invalidate) { } } + private void tintButtons(int showcaseColor, boolean tintButton) { + if (tintButton) { + mEndButton.getBackground().setColorFilter(showcaseColor, PorterDuff.Mode.MULTIPLY); + mButtonTwo.getBackground().setColorFilter(showcaseColor, PorterDuff.Mode.MULTIPLY); + } else { + mEndButton.getBackground().setColorFilter(HOLO_BLUE, PorterDuff.Mode.MULTIPLY); + mButtonTwo.getBackground().setColorFilter(HOLO_BLUE, PorterDuff.Mode.MULTIPLY); + + } + } + + private void tintButtonTwo(int showcaseColor, boolean tintButton) { + if (tintButton) { + mButtonTwo.getBackground().setColorFilter(showcaseColor, PorterDuff.Mode.MULTIPLY); + } else { + mButtonTwo.getBackground().setColorFilter(HOLO_BLUE, PorterDuff.Mode.MULTIPLY); + + } + } + private void tintButton(int showcaseColor, boolean tintButton) { if (tintButton) { mEndButton.getBackground().setColorFilter(showcaseColor, PorterDuff.Mode.MULTIPLY); diff --git a/sample/src/main/AndroidManifest.xml b/sample/src/main/AndroidManifest.xml index 31379c98c..90b3b6a3a 100644 --- a/sample/src/main/AndroidManifest.xml +++ b/sample/src/main/AndroidManifest.xml @@ -50,5 +50,8 @@ + + + diff --git a/sample/src/main/java/com/github/amlcurran/showcaseview/sample/ButtonTwoActivity.java b/sample/src/main/java/com/github/amlcurran/showcaseview/sample/ButtonTwoActivity.java new file mode 100644 index 000000000..27873a66b --- /dev/null +++ b/sample/src/main/java/com/github/amlcurran/showcaseview/sample/ButtonTwoActivity.java @@ -0,0 +1,109 @@ +package com.github.amlcurran.showcaseview.sample; + +import android.app.Activity; +import android.os.Bundle; +import android.view.MotionEvent; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; + +import com.espian.showcaseview.sample.R; +import com.github.amlcurran.showcaseview.OnShowcaseEventListener; +import com.github.amlcurran.showcaseview.ShowcaseView; +import com.github.amlcurran.showcaseview.targets.ViewTarget; + +/** + * Created by Kevin on 5/04/2016. + */ +public class ButtonTwoActivity extends Activity implements OnShowcaseEventListener { + + + private ShowcaseView sv; + private boolean clickedButton1; + private boolean clickedButton2; + private TextView mTextView; + private Button mButtonAgain; + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_two_buttons); + mTextView = (TextView) findViewById(R.id.Textview_two_buttons); + mButtonAgain = (Button) findViewById(R.id.Button_two_buttons); + mButtonAgain.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + showShowCase(); + mTextView.setText(R.string.two_buttons); + } + }); + showShowCase(); + } + + private void showShowCase(){ + clickedButton1 = false; + clickedButton2 = false; + ViewTarget target = new ViewTarget(R.id.Textview_two_buttons, this); + sv = new ShowcaseView.Builder(this) + .withMaterialShowcase() + .hideOnTouchOutside() + .setTarget(target) + .setContentTitle(R.string.title_two_buttons) + .setContentText(R.string.showcase_twobuttons_message) + .setStyle(R.style.CustomShowcaseTheme2) + .setShowcaseEventListener(this) + .addSecondButton() + .replaceEndButton(R.layout.view_custom_button) + .replaceButtonTwo(R.layout.view_custom_button) + .build(); + sv.setButtonsText(getString(R.string.button1),getString(R.string.button2)); + sv.overrideButtonClick(new View.OnClickListener() { + @Override + public void onClick(View v) { + clickedButton1 = true; + sv.hide(); + } + }); + sv.overrideButtonTwoClick(new View.OnClickListener() { + @Override + public void onClick(View v) { + clickedButton2 = true; + sv.hide(); + } + }); + + + } + + @Override + public void onShowcaseViewHide(ShowcaseView showcaseView) { + + if(clickedButton1){ + mTextView.setText(R.string.clicked_button1); + clickedButton1 = false; + } + else if(clickedButton2) { + mTextView.setText(R.string.clicked_button2); + clickedButton2 = false; + } + else{ + mTextView.setText(R.string.clicked_showcase); + } + } + + @Override + public void onShowcaseViewDidHide(ShowcaseView showcaseView) { + + } + + @Override + public void onShowcaseViewShow(ShowcaseView showcaseView) { + + } + + @Override + public void onShowcaseViewTouchBlocked(MotionEvent motionEvent) { + + } +} diff --git a/sample/src/main/java/com/github/amlcurran/showcaseview/sample/MainActivity.java b/sample/src/main/java/com/github/amlcurran/showcaseview/sample/MainActivity.java index e54473550..0bbd57934 100644 --- a/sample/src/main/java/com/github/amlcurran/showcaseview/sample/MainActivity.java +++ b/sample/src/main/java/com/github/amlcurran/showcaseview/sample/MainActivity.java @@ -166,7 +166,9 @@ private enum DemoOption { ANIMATIONS(R.string.title_animations, R.string.sum_animations, AnimationSampleActivity.class), CUSTOM_TEXT(R.string.custom_text, R.string.custom_text_summary, CustomTextActivity.class), CUSTOM_SHOWCASE(R.string.custom_showcase_title, R.string.custom_showcase_summary, CustomShowcaseActivity.class), - MEMORY(R.string.title_memory, R.string.sum_memory, MemoryManagementTesting.class); + MEMORY(R.string.title_memory, R.string.sum_memory, MemoryManagementTesting.class), + TWOBUTTONS(R.string.title_two_buttons, R.string.sum_twobuttons, ButtonTwoActivity.class); + final int titleRes; final int summaryRes; diff --git a/sample/src/main/res/layout/activity_two_buttons.xml b/sample/src/main/res/layout/activity_two_buttons.xml new file mode 100644 index 000000000..321fa3ecd --- /dev/null +++ b/sample/src/main/res/layout/activity_two_buttons.xml @@ -0,0 +1,25 @@ + + + + + +