From 9264bf0f07036fd8fb0e9cd08c4f8a2d016bbb30 Mon Sep 17 00:00:00 2001 From: jrejaud Date: Fri, 11 Mar 2016 22:33:01 -0800 Subject: [PATCH] Added button v0 --- .../jrejaud/onboarder/OnboardingActivity.java | 9 +- .../jrejaud/onboarder/OnboardingFragment.java | 116 ++++++++---------- .../com/jrejaud/onboarder/OnboardingPage.java | 22 ++++ 3 files changed, 83 insertions(+), 64 deletions(-) diff --git a/onboarder/src/main/java/com/jrejaud/onboarder/OnboardingActivity.java b/onboarder/src/main/java/com/jrejaud/onboarder/OnboardingActivity.java index e6916f8..b77e79e 100644 --- a/onboarder/src/main/java/com/jrejaud/onboarder/OnboardingActivity.java +++ b/onboarder/src/main/java/com/jrejaud/onboarder/OnboardingActivity.java @@ -17,7 +17,7 @@ import java.io.Serializable; import java.util.List; -public class OnboardingActivity extends AppCompatActivity { +public class OnboardingActivity extends AppCompatActivity implements OnboardingFragment.onOnboardingButtonClickListener { private final static String BACKGROUND_IMAGE_RES_ID = "BACKGROUND_IMAGE_RES_ID"; private @DrawableRes int backgroundImageResId; private final static String BACKGROUND_COLOR_RES_ID = "BACKGROUND_COLOR_RES_ID"; @@ -124,7 +124,7 @@ public OnboardingFragmentPagerAdapter(FragmentManager fragmentManager) { @Override public Fragment getItem(int position) { //Build a new fragment from an Onboarding Page (Since Fragment bundles don't seem to be serializable - return OnboardingFragment.newInstance(onboardingPages.get(position)); + return OnboardingFragment.newInstance(onboardingPages.get(position),position); } @Override @@ -132,4 +132,9 @@ public int getCount() { return onboardingPages.size(); } } + + @Override + public void onOnboardingClick(int position) { + onboardingPages.get(position).getOnButtonClickedListener().onButtonClicked(); + } } diff --git a/onboarder/src/main/java/com/jrejaud/onboarder/OnboardingFragment.java b/onboarder/src/main/java/com/jrejaud/onboarder/OnboardingFragment.java index de5b636..da9b66b 100644 --- a/onboarder/src/main/java/com/jrejaud/onboarder/OnboardingFragment.java +++ b/onboarder/src/main/java/com/jrejaud/onboarder/OnboardingFragment.java @@ -7,6 +7,7 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; @@ -27,17 +28,8 @@ public class OnboardingFragment extends Fragment implements Serializable { private static final String TITLE = "TITLE"; private static final String BODY_TEXT = "BODY_TEXT"; private static final String IMAGE_RESOURCE_ID = "IMAGE_RESOURCE_ID"; - - /** The title which is displayed at the top of the fragment */ - private String title; - - /** The body text which is displayed in the middle of the fragment */ - private String bodyText; - - /** The image resource which is displayed in the middle of the fragment, above the text */ - private @DrawableRes int imageResId; - -// private OnFragmentInteractionListener mListener; + private static final String POSITION = "POSITION"; + private static final String BUTTON_TEXT = "BUTTON_TEXT"; public OnboardingFragment() { // Required empty public constructor @@ -53,22 +45,18 @@ public OnboardingFragment() { * @return A new instance of fragment OnboardingFragment. */ // TODO: Rename and change types and number of parameters - public static OnboardingFragment newInstance(OnboardingPage onboardingPage) { + public static OnboardingFragment newInstance(OnboardingPage onboardingPage, int position) { OnboardingFragment fragment = new OnboardingFragment(); Bundle args = new Bundle(); args.putString(TITLE, onboardingPage.getTitle()); args.putString(BODY_TEXT, onboardingPage.getBodyText()); args.putInt(IMAGE_RESOURCE_ID, onboardingPage.getImageResId()); + args.putInt(POSITION, position); + args.putString(BUTTON_TEXT, onboardingPage.getButtonText()); fragment.setArguments(args); return fragment; } - @Override - public void onAttach(Context context) { - super.onAttach(context); - //TODO get a reference to the activity here - } - @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -77,74 +65,78 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, // Inflate the layout for this fragment Bundle bundle = getArguments(); - title = bundle.getString(TITLE,null); - bodyText = bundle.getString(BODY_TEXT, null); - imageResId = bundle.getInt(IMAGE_RESOURCE_ID,-1); + /* The title which is displayed at the top of the fragment */ + String title = bundle.getString(TITLE, null); + /* The body text which is displayed in the middle of the fragment */ + String bodyText = bundle.getString(BODY_TEXT, null); + /* The image resource which is displayed in the middle of the fragment, above the text */ + int imageResId = bundle.getInt(IMAGE_RESOURCE_ID, -1); + /* The position that the fragment is in adapter */ + final int position = bundle.getInt(POSITION, 0); + /* The button text (if the user set any) */ + String buttonText = bundle.getString(BUTTON_TEXT, null); TextView titleTextView = (TextView) view.findViewById(R.id.onboarding_fragment_title); TextView bodyTextView = (TextView) view.findViewById(R.id.onboarding_fragment_body_text); ImageView imageView = (ImageView) view.findViewById(R.id.onboarding_fragment_image); + Button button = (Button) view.findViewById(R.id.onboarding_fragment_button); //Set the title - if (title!=null) { + if (title !=null) { titleTextView.setText(title); } else { titleTextView.setVisibility(View.GONE); } //Set the body text - if (bodyText!=null) { + if (bodyText !=null) { bodyTextView.setText(bodyText); } else { bodyTextView.setVisibility(View.GONE); } //Set the image - if (imageResId!=-1) { + if (imageResId !=-1) { imageView.setImageResource(imageResId); } else { imageView.setVisibility(View.GONE); } + //Set the button text and link it to the method + if (buttonText!=null) { + button.setText(buttonText); + button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + clickOnboardingButton(position); + } + }); + } else { + button.setVisibility(View.GONE); + } + return view; } -// // TODO: Rename method, update argument and hook method into UI event -// public void onButtonPressed(Uri uri) { -// if (mListener != null) { -// mListener.onFragmentInteraction(uri); -// } -// } -// -// @Override -// public void onAttach(Context context) { -// super.onAttach(context); -// if (context instanceof OnFragmentInteractionListener) { -// mListener = (OnFragmentInteractionListener) context; -// } else { -// throw new RuntimeException(context.toString() -// + " must implement OnFragmentInteractionListener"); -// } -// } -// -// @Override -// public void onDetach() { -// super.onDetach(); -// mListener = null; -// } -// -// /** -// * This interface must be implemented by activities that contain this -// * fragment to allow an interaction in this fragment to be communicated -// * to the activity and potentially other fragments contained in that -// * activity. -// *

-// * See the Android Training lesson Communicating with Other Fragments for more information. -// */ -// public interface OnFragmentInteractionListener { -// // TODO: Update argument type and name -// void onFragmentInteraction(Uri uri); -// } + private onOnboardingButtonClickListener buttonClickListener; + + private void clickOnboardingButton(int position) { + buttonClickListener.onOnboardingClick(position); + } + + @Override + public void onAttach(Context context) { + super.onAttach(context); + buttonClickListener = (onOnboardingButtonClickListener) context; + } + + @Override + public void onDetach() { + super.onDetach(); + buttonClickListener = null; + } + + public interface onOnboardingButtonClickListener { + void onOnboardingClick(int position); + } } diff --git a/onboarder/src/main/java/com/jrejaud/onboarder/OnboardingPage.java b/onboarder/src/main/java/com/jrejaud/onboarder/OnboardingPage.java index 6e9c644..3f78127 100644 --- a/onboarder/src/main/java/com/jrejaud/onboarder/OnboardingPage.java +++ b/onboarder/src/main/java/com/jrejaud/onboarder/OnboardingPage.java @@ -14,6 +14,14 @@ public class OnboardingPage implements Serializable { private @DrawableRes int imageResId; + public interface onButtonClickedListener { + void onButtonClicked(); + } + + //Set these to null by default and set the value if the user sets a button + private String buttonText = null; + private onButtonClickedListener onButtonClickedListener = null; + public String getTitle() { return title; } @@ -26,6 +34,10 @@ public int getImageResId() { return imageResId; } + public String getButtonText() { + return buttonText; + } + public OnboardingPage(@Nullable String title,@Nullable String bodyText) { this.title = title; this.bodyText = bodyText; @@ -36,4 +48,14 @@ public OnboardingPage(@Nullable String title,@Nullable String bodyText, int imag this.bodyText = bodyText; this.imageResId = imageResId; } + + public OnboardingPage.onButtonClickedListener getOnButtonClickedListener() { + return onButtonClickedListener; + } + + public void setButton(String buttonText, OnboardingPage.onButtonClickedListener onButtonClickedListener) { + this.buttonText = buttonText; + this.onButtonClickedListener = onButtonClickedListener; + } + }