I'm having a bit of trouble having the ViewPager displaying and running properly with a single fragment class.
So a bit of background. The Activity itself is supposed to allow users to answer questions in a survey. Each question consists of a question title, an input box, and a submit button. I've made a layout and a corresponding fragment (QuestionFragment) class to hold all of this.
My idea is to have the ViewPager hold a bunch of QuestionFragment's and the user will be able to swipe to the questions they would like to answer or edit. Basically each page will be using a QuestionFragment, but each will contain a unique question.
However my implementation doesn't look like its working out. The first page in the activity will have the UI set up (page 1) but the rest of the pages will have the default layout xml applied.
Note: at the moment I'm trying to set up the UI with the index number. You can ignore the Question[] array and any reference to it as the UI doesnt use it at this time. However I do try to set the typeface, which only works on the first page. You can take a look at the screenshots at the bottom, "How many moons does Saturn have" is the default xml in the layout. I've also noticed that the textview displays question 9, instead of 1.
Here is the fragment
public class SurveyTakerFragment extends Fragment {
private Question question;
private int index;
private TextView tv_question_title;
private EditText et_sms_response;
private Button btn_submit_response;
private SharedPreferences sharedPrefs;
private Typeface typeface;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_question, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setup();
}
void setup(){
Activity activity = getActivity();
tv_question_title = (TextView) activity.findViewById(R.id.tv_question_title);
et_sms_response = (EditText) activity.findViewById(R.id.et_sms_response);
btn_submit_response = (Button) activity.findViewById(R.id.btn_submit_response);
tv_question_title.setTypeface(typeface);
tv_question_title.setText("Question: " + index);
//TODO: Set question title.
//TODO: Pre-fill previous answer if any.
btn_submit_response.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String response = et_sms_response.getText().toString();
//TODO: Submit response.
}
});
}
public void setQuestion(Question question, int index){
this.question = question;
this.index = index;
}
public void setSharedPrefs(SharedPreferences sharedPrefs){
this.sharedPrefs = sharedPrefs;
}
public void setTypeface(Typeface typeface){
this.typeface = typeface;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
}
Here is the adapter:
public class SurveyTakerAdapter extends FragmentPagerAdapter{
private Question[] questions;
private SharedPreferences sharedPrefs;
private Typeface typeface;
public SurveyTakerAdapter(FragmentManager fm, Question[] questions,
SharedPreferences sharedPrefs, Typeface typeface) {
super(fm);
this.questions = questions;
this.sharedPrefs = sharedPrefs;
this.typeface = typeface;
}
#Override
public Fragment getItem(int index) {
SurveyTakerFragment surveyTakerFragment = new SurveyTakerFragment();
surveyTakerFragment.setQuestion(questions[index], index);
surveyTakerFragment.setSharedPrefs(sharedPrefs);
surveyTakerFragment.setTypeface(typeface);
return surveyTakerFragment;
}
#Override
public int getCount() {
return questions.length;
}
#Override
public CharSequence getPageTitle(int position) {
return "Question: " + (position + 1);
}
}
Pager Activity
public class SurveyTaker extends FragmentActivity{
private final String APP_DATA = "appData";
private SurveyTakerAdapter surveyTakerAdapter;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_survey);
Question[] questions = new Question[10];
FragmentManager fragmentManager = getSupportFragmentManager();
SharedPreferences sharedPrefs = getSharedPreferences(APP_DATA, MODE_PRIVATE);
Typeface robot = Typeface.createFromAsset(getAssets(), "Roboto-Thin.ttf");
surveyTakerAdapter = new SurveyTakerAdapter(fragmentManager, questions, sharedPrefs, robot);
viewPager = (ViewPager) findViewById(R.id.vp_survey_taker);
viewPager.setOffscreenPageLimit(10);
viewPager.setAdapter(surveyTakerAdapter);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int index) {
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
}
I'm assuming this is the case because I'm using a single fragment to populate all the pages in the viewpager, but I don't know how else to go about this. Is my design bad? Or is there something im missing?
Any help would be greatly appreciated!
Thanks!
It looks like the R.id.tv_question_title, R.id.tv_question_title and R.id.tv_question_title views are part of your R.layout.fragment_question layout. You should be referencing those views through your SurveyTakerFragment instance using getView(), not through the Activity via getActivity(). You are also duplicating effort in onActivityCreated() and setup().
Replace your onActivityCreated() and setup() implementations with this:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setup();
}
void setup(){
View view = getView();
tv_question_title = (TextView) view.findViewById(R.id.tv_question_title);
et_sms_response = (EditText) view.findViewById(R.id.et_sms_response);
btn_submit_response = (Button) view.findViewById(R.id.btn_submit_response);
tv_question_title.setTypeface(typeface);
tv_question_title.setText("Question: " + index);
//TODO: Set question title.
//TODO: Pre-fill previous answer if any.
btn_submit_response.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String response = et_sms_response.getText().toString();
//TODO: Submit response.
}
});
}
Related
I have a ViewPager with 3 fragments in it, each fragment has a next and prev. button, and you can also swipe left and right to navigate through fragments. and although I'm able to get the input from each fragment when the next button is clicked, I sill can't figure out a way to get the input when the user scrolls between fragments.
this is the code in my first fragment:
public class signupfrag1 extends Fragment {
private TextInputEditText fName, lName;
public Spinner spncountry, spncity;
private Button next1;
private String vfName, vlName, vcountry, vcity;
private String method;
signupfrag1Listener activityCommander;
public interface signupfrag1Listener{
public void getDataFromFrag1(String fName, String lName, String countryName, String cityName);
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try{
activityCommander = (signupfrag1Listener) context;
}catch (ClassCastException e){
throw new ClassCastException(context.toString());
}
}
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.signupfrag1, container, false);
fName = (TextInputEditText) view.findViewById(R.id.edtFName);
lName = (TextInputEditText) view.findViewById(R.id.edtLName);
spncountry = (Spinner) view.findViewById(R.id.cmbCountry);
spncity = (Spinner) view.findViewById(R.id.cmbCities);
next1 = (Button) view.findViewById(R.id.btnNext2);
next1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
vfName = fName.getText().toString();
vlName = lName.getText().toString();
if(vfName.equals("") || vfName.length()==1){
fName.setError("please enter a correct first name!");
}
else if(vlName.equals("") || vlName.length()==1){
lName.setError("Please enter a correct first name!");
}
else
startFrag2(view);
}
});}
public void startFrag2(View view){
activityCommander.getDataFromFrag1(vfName, vlName, vcity, vcountry);
}
this is the code from my main activity:
public class Main2Activityforfragments extends AppCompatActivity implements signupfrag1.signupfrag1Listener, signupfrag2.signupfrag2Listener {
private ViewPager fragsViewPager;
private SectionsPagerAdapter1 sectionsPagerAdapter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2_activityforfragments);
fragsViewPager = (ViewPager) findViewById(R.id.fragsViewPager);
sectionsPagerAdapter1 = new SectionsPagerAdapter1(getSupportFragmentManager());
fragsViewPager.setAdapter(sectionsPagerAdapter1);
}
//this get called when the next button is clicked
#Override
public void getDataFromFrag1(String fName, String lName, String cityName, String countryName) {
if(fragsViewPager.getCurrentItem() == 0){
fragsViewPager.setCurrentItem(1);}
}
if you are in the first fragment then create ref. of interface and call its method and pass the values on Click what you want and it is needed to implement Two interface in SecondFragment. call its method in the First Fragment. you can create interface Two in SecondFragment
Two Second=new SecondFragment(); // it will be called on click of firstFragment which pass the value.
Second.two("Value one","Value two");
interface Two{
void two(String value,String value); // implement this inteface in SecondFragment and call it in FirstFragment
}
When you reach to second you get the value from the first fragment and continue with the third fragment as same.
I have an activity that has 2 buttons (Previous, Next) and a text view in between the two. Below that is a viewpager.
I would like to change the textview to the title of whatever page they are on.
Here is the class where it all happens:
public class MyWorkout extends BaseActivity {
NonSwipeableViewPager vp;
TextView txtViewTitle;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_workout);
mToolBar = activateToolbar();
setUpNavigationDrawer();
getSupportActionBar().setTitle("My Workout");
Intent intent = getIntent();
String workouttitle = intent.getStringExtra("workout");
switch(workouttitle) {
case "w29" : {
getSupportActionBar().setTitle("My Workout - Workout 29");
break;
}
case "w30" : {
getSupportActionBar().setTitle("My Workout - Workout 30");
break;
}
}
Button btnPrev = (Button) findViewById(R.id.btnPrevious);
Button btnNext = (Button) findViewById(R.id.btnNext);
vp = (NonSwipeableViewPager) findViewById(R.id.view_pager);
txtViewTitle = (TextView) findViewById(R.id.txtViewTitle);
this.addPages(vp);
//THIS IS WHAT I HAVE TRIED
if (vp.getCurrentItem() == 0) {
txtViewTitle.setText("Week One, Monday");
}
if (vp.getCurrentItem() == 1) {
txtViewTitle.setText("Week One, Wednesday");
}
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MoveNext();
}
});
btnPrev.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MovePrevious();
}
});
}
public void MoveNext() {
//it doesn't matter if you're already in the last item
vp.setCurrentItem(vp.getCurrentItem() + 1);
}
public void MovePrevious() {
//it doesn't matter if you're already in the first item
vp.setCurrentItem(vp.getCurrentItem() - 1);
}
private void addPages(NonSwipeableViewPager pager) {
MyFragPagerAdapter adapter = new MyFragPagerAdapter(getSupportFragmentManager());
Intent intent = getIntent();
String workout = intent.getStringExtra("workout");
Bundle args = new Bundle();
args.putString("workout", workout);
Week1Mon week1MonFragment = new Week1Mon();
week1MonFragment.setArguments(args);
adapter.addPage(week1MonFragment);
Week1Wed week1WedFragment = new Week1Wed();
week1WedFragment.setArguments(args);
adapter.addPage(week1WedFragment);
Week1Fri week1Fri = new Week1Fri();
week1Fri.setArguments(args);
adapter.addPage(week1Fri);
Week2Mon week2MonFragment = new Week2Mon();
week2MonFragment.setArguments(args);
adapter.addPage(week2MonFragment);
Week2Wed week2WedFragment = new Week2Wed();
week2WedFragment.setArguments(args);
adapter.addPage(week2WedFragment);
Week2Fri week2Fri = new Week2Fri();
week2Fri.setArguments(args);
adapter.addPage(week2Fri);
pager.setAdapter(adapter);
}
}
The part I have tried:
if (vp.getCurrentItem() == 0) {
txtViewTitle.setText("Week One, Monday");
}
if (vp.getCurrentItem() == 1) {
txtViewTitle.setText("Week One, Wednesday");
}
This sets the textview to Week One, Monday. Unfortunately it doesn't change when the user hits the Next button. I'm also hoping theres a better way, as I don't want to "assume" that the current item is that day of the week, because every week is different. I also need to figure out how to set the current page when the app is opened, because it saves the position is shared pref and I have to load it in the class above, and somehow set the page to a current page (preferably by the title)
Here is MyFragPagerAdapter:
public class MyFragPagerAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> pages = new ArrayList<>();
public MyFragPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return pages.get(position);
}
#Override
public int getCount() {
return pages.size();
}
public void addPage(Fragment f) {
pages.add(f);
}
#Override
public CharSequence getPageTitle(int position) {
return pages.get(position).toString();
}
}
Thanks for any help, let me know if you need to see more code!
You can use,
viewPager.addOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
public void onPageSelected(int position) {
// Check if this is the page you want.
}});
please am sorry to ask this simple question, but have been battling with this problem for the past one week. I will appreciate if anyone can solve this problem for me by coding inside the onclick of the button listener which when clicked it share the content in the fragment or the adapter that extends fragmentStatePageAdapter.
My Activity
public class MainActivity extends AppCompatActivity {
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final Adapter devicePageAdaptor = new Adapter(getSupportFragmentManager(), getApplicationContext());
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(devicePageAdaptor);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
The Fragment
public class fragment extends Fragment {
public static final String ProverbKey = "proverbKey" ;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_device, container, false);
Bundle bundle = getArguments();
if (bundle != null) {
String proverb = bundle.getString(ProverbKey);
setValues(view,proverb);
}
return view;
}
private void setValues(View view, String proverb) {
TextView textView = (TextView) view.findViewById(R.id.textView_proverb);
textView.setText(proverb);
}
}
The Adapter that extends fragmentStateAdapter
public class Adapter extends FragmentStatePagerAdapter {
String[] proverb;
public Adapter(android.support.v4.app.FragmentManager fm,Context context) {
super(fm);
Resources resources = context.getResources();
proverb = resources.getStringArray(R.array.proverb);
}
#Override
public android.support.v4.app.Fragment getItem(int position) {
Bundle bundle = new Bundle();
bundle.putString(fragment.ProverbKey, proverb[position]);
fragment deviceFragment = new fragment();
deviceFragment.setArguments(bundle);
return deviceFragment;
}
#Override
public int getCount() {
return proverb.length;
}
}
And my String Array in each fragment which is named proverb
<string-array name="proverb">
<item>A young idler, an old beggar.</item>
<item>Action speaks louder than words.</item>
<item>A hungry man is an angry man.</item>
<item>A living dog is better than a dead lion.</item>
<item>A hedge between keeps friendship green.</item>
<item>A heavy purse gives to a light heart.</item>
<item>A handful of patience is worth more than a bushel of brains</item>
<item>A guilty conscience needs no accuser.</item>
<item>A growing youth has a wolf in his belly.</item>
<item>A good name is better than a good face.</item>
<item>A good beginning makes a good end.</item>
<item>A drop of ink may make a million think.</item>
<item>A dry March, a wet April and a cool May may fill barn and cellar and bring much hay.</item>
</string-array>
//Global Variable
String[] proverb;
//Init on onCreate()
proverb = resources.getStringArray(R.array.proverb);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = viewPager.getCurrentItem();
String itemToShare = proverb[pos];
share(itemToShare);
}
});
private void share(String item) {
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_SUBJECT, "Title Of The Post");
share.putExtra(Intent.EXTRA_TEXT, item);
startActivity(Intent.createChooser(share, "Sharing Item!"));
}
Try this and update me if works
I have this error nullpointerexception, when i do getText().toString() from EditTex:
public class SendMessActivity extends SherlockFragmentActivity {
private EditText tEmail;
private Button sendButton;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send_mess_layout);
tEmail = (EditText)findViewById(R.id.editEmailTo);
sendButton = (Button)findViewById(R.id.btn_sendmess);
endButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//String textEmail = tEmail.getText().toString(); //nullpointerexception
Editable textEmail1Editable = tEmail.getText(); //nullpointerexception
String textEmail = textEmail1Editable.toString()
Log.d(DEBUGTAG, "SENDING START:::::::: " + textEmail);
}
});
}}
Please, tell me how to do it
UPDATE Q
David, thank you, for your surmise,the problem was really in the my tangled Layouts
I had all 4 levels of nesting LinearLayouts.
After I left the simplified scheme and only 2 levels I have, all began to work
You need to call setContentView(your_layout.xml) so it knows what layout to use. Without setting the layout, all of your calls to findViewById(...) that try to find the views in your layout will return null.
public class SendMessActivity extends SherlockFragmentActivity {
private EditText tEmail;
private Button sendButton;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(your_layout.xml); //set your activity layout here.
tEmail = (EditText)findViewById(R.id.editEmailTo);
sendButton = (Button)findViewById(R.id.btn_sendmess);
endButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String textEmail = tEmail.getText().toString(); //nullpointerexception
Log.d(DEBUGTAG, "SENDING START:::::::: " + textEmail);
}
});
}}
If you have no assigned text then I would assume that the EditText retrieval of the text would be a null entry and you are attempting to make a string of that null entry.
I am planning on having 3 fragmentlists contained within one activity. The goal is you select the talk option from the first list, then it transitions to the run list based on what you clicked in talk list, and then in the run list, based on what you click it will transition to the final eat list. Should this be happening in the fragments themselves(like i have it) or calling the activity to handle passing data back and forth to the fragments?
public class OptionsActivity extends Activity {
protected TalkFragment talk;
protected RunFragment run;
protected EatFragment eat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
talk = new TalkFragment();
run = new RunFragment();
eat = new EatFragment();
}
}
public class TalkFragment extends ListFragment {
private Cursor mCursor;
int mCurCheckPosition = 0;
#Override
public void onActivityCreated(Bundle savedState) {
super.onActivityCreated(savedState);
}
#Override
public void onListItemClick(ListView l, View v, int pos, long id) {
mCurCheckPosition = pos;
// We can display everything in-place with fragments.
// Have the list highlight this item and show the data.
getListView().setItemChecked(pos, true);
// Check what fragment is shown, replace if needed.
RunFragment run_frag = (RunFragment) getFragmentManager().findFragmentById(R.id.fragment_run);
if (run_frag == null || run_frag.getShownIndex() != pos) {
run_frag = RunFragment.newInstance(pos);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.details, details);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
}
}
This is obviously just snippits, but you get the idea. If I do it this way, I am not really sure how to pass certain parameters to fragment properly. Ideally, the RunFragment would know what to display based on the id of the item clicked in the TalkFragment. Should these be going through the Activity instead?
The way I typically do it is have the Activity be the traffic cop for handling the fragments. Your onListItemClick implementation could just tell the Activity what it wants to do:
public class OptionsActivity extends Activity {
protected TalkFragment talk;
protected RunFragment run;
protected EatFragment eat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
talk = new TalkFragment();
run = new RunFragment();
eat = new EatFragment();
}
public void showRunFragment() {
showFragment(R.id.fragment_run);
}
public void showEatFragment() {
showFragment(R.id.fragment_eat);
}
public void showFragment(int fragmentId) {
// Check what fragment is shown, replace if needed.
...
}
}
public class TalkFragment extends ListFragment {
private Cursor mCursor;
int mCurCheckPosition = 0;
#Override
public void onActivityCreated(Bundle savedState) {
super.onActivityCreated(savedState);
}
#Override
public void onListItemClick(ListView l, View v, int pos, long id) {
mCurCheckPosition = pos;
// We can display everything in-place with fragments.
// Have the list highlight this item and show the data.
getListView().setItemChecked(pos, true);
getActivity().showRunFragment()
}
}