Skip to content

Commit 9264bf0

Browse files
committed
Added button v0
1 parent 2dc8ffd commit 9264bf0

File tree

3 files changed

+83
-64
lines changed

3 files changed

+83
-64
lines changed

Diff for: onboarder/src/main/java/com/jrejaud/onboarder/OnboardingActivity.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import java.io.Serializable;
1818
import java.util.List;
1919

20-
public class OnboardingActivity extends AppCompatActivity {
20+
public class OnboardingActivity extends AppCompatActivity implements OnboardingFragment.onOnboardingButtonClickListener {
2121
private final static String BACKGROUND_IMAGE_RES_ID = "BACKGROUND_IMAGE_RES_ID";
2222
private @DrawableRes int backgroundImageResId;
2323
private final static String BACKGROUND_COLOR_RES_ID = "BACKGROUND_COLOR_RES_ID";
@@ -124,12 +124,17 @@ public OnboardingFragmentPagerAdapter(FragmentManager fragmentManager) {
124124
@Override
125125
public Fragment getItem(int position) {
126126
//Build a new fragment from an Onboarding Page (Since Fragment bundles don't seem to be serializable
127-
return OnboardingFragment.newInstance(onboardingPages.get(position));
127+
return OnboardingFragment.newInstance(onboardingPages.get(position),position);
128128
}
129129

130130
@Override
131131
public int getCount() {
132132
return onboardingPages.size();
133133
}
134134
}
135+
136+
@Override
137+
public void onOnboardingClick(int position) {
138+
onboardingPages.get(position).getOnButtonClickedListener().onButtonClicked();
139+
}
135140
}

Diff for: onboarder/src/main/java/com/jrejaud/onboarder/OnboardingFragment.java

+54-62
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.view.LayoutInflater;
88
import android.view.View;
99
import android.view.ViewGroup;
10+
import android.widget.Button;
1011
import android.widget.ImageView;
1112
import android.widget.TextView;
1213

@@ -27,17 +28,8 @@ public class OnboardingFragment extends Fragment implements Serializable {
2728
private static final String TITLE = "TITLE";
2829
private static final String BODY_TEXT = "BODY_TEXT";
2930
private static final String IMAGE_RESOURCE_ID = "IMAGE_RESOURCE_ID";
30-
31-
/** The title which is displayed at the top of the fragment */
32-
private String title;
33-
34-
/** The body text which is displayed in the middle of the fragment */
35-
private String bodyText;
36-
37-
/** The image resource which is displayed in the middle of the fragment, above the text */
38-
private @DrawableRes int imageResId;
39-
40-
// private OnFragmentInteractionListener mListener;
31+
private static final String POSITION = "POSITION";
32+
private static final String BUTTON_TEXT = "BUTTON_TEXT";
4133

4234
public OnboardingFragment() {
4335
// Required empty public constructor
@@ -53,22 +45,18 @@ public OnboardingFragment() {
5345
* @return A new instance of fragment OnboardingFragment.
5446
*/
5547
// TODO: Rename and change types and number of parameters
56-
public static OnboardingFragment newInstance(OnboardingPage onboardingPage) {
48+
public static OnboardingFragment newInstance(OnboardingPage onboardingPage, int position) {
5749
OnboardingFragment fragment = new OnboardingFragment();
5850
Bundle args = new Bundle();
5951
args.putString(TITLE, onboardingPage.getTitle());
6052
args.putString(BODY_TEXT, onboardingPage.getBodyText());
6153
args.putInt(IMAGE_RESOURCE_ID, onboardingPage.getImageResId());
54+
args.putInt(POSITION, position);
55+
args.putString(BUTTON_TEXT, onboardingPage.getButtonText());
6256
fragment.setArguments(args);
6357
return fragment;
6458
}
6559

66-
@Override
67-
public void onAttach(Context context) {
68-
super.onAttach(context);
69-
//TODO get a reference to the activity here
70-
}
71-
7260
@Override
7361
public View onCreateView(LayoutInflater inflater, ViewGroup container,
7462
Bundle savedInstanceState) {
@@ -77,74 +65,78 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
7765
// Inflate the layout for this fragment
7866
Bundle bundle = getArguments();
7967

80-
title = bundle.getString(TITLE,null);
81-
bodyText = bundle.getString(BODY_TEXT, null);
82-
imageResId = bundle.getInt(IMAGE_RESOURCE_ID,-1);
68+
/* The title which is displayed at the top of the fragment */
69+
String title = bundle.getString(TITLE, null);
70+
/* The body text which is displayed in the middle of the fragment */
71+
String bodyText = bundle.getString(BODY_TEXT, null);
72+
/* The image resource which is displayed in the middle of the fragment, above the text */
73+
int imageResId = bundle.getInt(IMAGE_RESOURCE_ID, -1);
74+
/* The position that the fragment is in adapter */
75+
final int position = bundle.getInt(POSITION, 0);
76+
/* The button text (if the user set any) */
77+
String buttonText = bundle.getString(BUTTON_TEXT, null);
8378

8479
TextView titleTextView = (TextView) view.findViewById(R.id.onboarding_fragment_title);
8580
TextView bodyTextView = (TextView) view.findViewById(R.id.onboarding_fragment_body_text);
8681
ImageView imageView = (ImageView) view.findViewById(R.id.onboarding_fragment_image);
82+
Button button = (Button) view.findViewById(R.id.onboarding_fragment_button);
8783

8884
//Set the title
89-
if (title!=null) {
85+
if (title !=null) {
9086
titleTextView.setText(title);
9187
} else {
9288
titleTextView.setVisibility(View.GONE);
9389
}
9490

9591
//Set the body text
96-
if (bodyText!=null) {
92+
if (bodyText !=null) {
9793
bodyTextView.setText(bodyText);
9894
} else {
9995
bodyTextView.setVisibility(View.GONE);
10096
}
10197

10298
//Set the image
103-
if (imageResId!=-1) {
99+
if (imageResId !=-1) {
104100
imageView.setImageResource(imageResId);
105101
} else {
106102
imageView.setVisibility(View.GONE);
107103
}
108104

105+
//Set the button text and link it to the method
106+
if (buttonText!=null) {
107+
button.setText(buttonText);
108+
button.setOnClickListener(new View.OnClickListener() {
109+
@Override
110+
public void onClick(View v) {
111+
clickOnboardingButton(position);
112+
}
113+
});
114+
} else {
115+
button.setVisibility(View.GONE);
116+
}
117+
109118
return view;
110119
}
111120

112-
// // TODO: Rename method, update argument and hook method into UI event
113-
// public void onButtonPressed(Uri uri) {
114-
// if (mListener != null) {
115-
// mListener.onFragmentInteraction(uri);
116-
// }
117-
// }
118-
//
119-
// @Override
120-
// public void onAttach(Context context) {
121-
// super.onAttach(context);
122-
// if (context instanceof OnFragmentInteractionListener) {
123-
// mListener = (OnFragmentInteractionListener) context;
124-
// } else {
125-
// throw new RuntimeException(context.toString()
126-
// + " must implement OnFragmentInteractionListener");
127-
// }
128-
// }
129-
//
130-
// @Override
131-
// public void onDetach() {
132-
// super.onDetach();
133-
// mListener = null;
134-
// }
135-
//
136-
// /**
137-
// * This interface must be implemented by activities that contain this
138-
// * fragment to allow an interaction in this fragment to be communicated
139-
// * to the activity and potentially other fragments contained in that
140-
// * activity.
141-
// * <p/>
142-
// * See the Android Training lesson <a href=
143-
// * "http://developer.android.com/training/basics/fragments/communicating.html"
144-
// * >Communicating with Other Fragments</a> for more information.
145-
// */
146-
// public interface OnFragmentInteractionListener {
147-
// // TODO: Update argument type and name
148-
// void onFragmentInteraction(Uri uri);
149-
// }
121+
private onOnboardingButtonClickListener buttonClickListener;
122+
123+
private void clickOnboardingButton(int position) {
124+
buttonClickListener.onOnboardingClick(position);
125+
}
126+
127+
@Override
128+
public void onAttach(Context context) {
129+
super.onAttach(context);
130+
buttonClickListener = (onOnboardingButtonClickListener) context;
131+
}
132+
133+
@Override
134+
public void onDetach() {
135+
super.onDetach();
136+
buttonClickListener = null;
137+
}
138+
139+
public interface onOnboardingButtonClickListener {
140+
void onOnboardingClick(int position);
141+
}
150142
}

Diff for: onboarder/src/main/java/com/jrejaud/onboarder/OnboardingPage.java

+22
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ public class OnboardingPage implements Serializable {
1414
private @DrawableRes
1515
int imageResId;
1616

17+
public interface onButtonClickedListener {
18+
void onButtonClicked();
19+
}
20+
21+
//Set these to null by default and set the value if the user sets a button
22+
private String buttonText = null;
23+
private onButtonClickedListener onButtonClickedListener = null;
24+
1725
public String getTitle() {
1826
return title;
1927
}
@@ -26,6 +34,10 @@ public int getImageResId() {
2634
return imageResId;
2735
}
2836

37+
public String getButtonText() {
38+
return buttonText;
39+
}
40+
2941
public OnboardingPage(@Nullable String title,@Nullable String bodyText) {
3042
this.title = title;
3143
this.bodyText = bodyText;
@@ -36,4 +48,14 @@ public OnboardingPage(@Nullable String title,@Nullable String bodyText, int imag
3648
this.bodyText = bodyText;
3749
this.imageResId = imageResId;
3850
}
51+
52+
public OnboardingPage.onButtonClickedListener getOnButtonClickedListener() {
53+
return onButtonClickedListener;
54+
}
55+
56+
public void setButton(String buttonText, OnboardingPage.onButtonClickedListener onButtonClickedListener) {
57+
this.buttonText = buttonText;
58+
this.onButtonClickedListener = onButtonClickedListener;
59+
}
60+
3961
}

0 commit comments

Comments
 (0)