Skip to content

Commit

Permalink
Added button v0
Browse files Browse the repository at this point in the history
  • Loading branch information
jrejaud committed Mar 12, 2016
1 parent 2dc8ffd commit 9264bf0
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -124,12 +124,17 @@ 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
public int getCount() {
return onboardingPages.size();
}
}

@Override
public void onOnboardingClick(int position) {
onboardingPages.get(position).getOnButtonClickedListener().onButtonClicked();
}
}
116 changes: 54 additions & 62 deletions onboarder/src/main/java/com/jrejaud/onboarder/OnboardingFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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.
// * <p/>
// * See the Android Training lesson <a href=
// * "http://developer.android.com/training/basics/fragments/communicating.html"
// * >Communicating with Other Fragments</a> 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);
}
}
22 changes: 22 additions & 0 deletions onboarder/src/main/java/com/jrejaud/onboarder/OnboardingPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
}

}

0 comments on commit 9264bf0

Please sign in to comment.