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
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.
}});
im still new in android developement.
I want to refresh my fragment and getting new imageButton background in that fragment but i keep failing to do that.
This is my main activity
public class MainActivity extends AppCompatActivity {
public int QuestionNum =0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MainActivityFragment MainFrag = new MainActivityFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.content, MainFrag).commit();
}
public void Answer1(View view){
QuestionNum++;
Stage StageFrag = new Stage();
getSupportFragmentManager().beginTransaction()
.replace(R.id.content,StageFrag).commit();
}
and this is my fragment
public class Stage extends Fragment {
public Stage() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_stage, container, false);
MainActivity Main = new MainActivity();
String QuestionInd[] = getResources().getStringArray(R.array.Qind);
Integer AnswerAnimal[]={R.drawable.anjing,R.drawable.kucing....};
int QNum = Main.QuestionNum;
TextView Question = (TextView) view.findViewById(R.id.Questions);
Question.setText(QuestionInd[Main.QuestionNum]);
ImageButton Ans1 = (ImageButton) view.findViewById(R.id.Choice1);
ImageButton Ans2 = (ImageButton) view.findViewById(R.id.Choice2);
ImageButton Ans3 = (ImageButton) view.findViewById(R.id.Choice3);
ImageButton Ans4 = (ImageButton) view.findViewById(R.id.Choice4);
switch(QNum) {
case 0:
Ans1.setBackgroundResource(AnswerAnimal[0]);
Ans2.setBackgroundResource(AnswerAnimal[1]);
Ans3.setBackgroundResource(AnswerAnimal[2]);
Ans4.setBackgroundResource(AnswerAnimal[3]);
break;
case 1:
Ans1.setBackgroundResource(AnswerAnimal[8]);
Ans2.setBackgroundResource(AnswerAnimal[9]);
Ans3.setBackgroundResource(AnswerAnimal[5]);
Ans4.setBackgroundResource(AnswerAnimal[4]);
break;
}
return view;
}
}
i didnt get error in my logcat. I also try using add but the background didn't change only the fragments keep stacking. Using the attach and retach didnt work too.
Please help me and explain my mistake. i really want to learn about this. Thank you.
First of all, never do this:
MainActivity Main = new MainActivity();
explained here.
Second: Even if you considered an Android Activity as just a normal Java Class, then new MainActivity() means you are creating new instance of that class, so the instance variables will be what or how you defined them i.e public int QuestionNum =0 will remain the same.
Hence the switch statement never comes to case: 1.
What you can do is just call some method like changeImageButtons() on the StageFrag Object you have in the Activity.
I'm working on the fragment, when I click a button, it will turn to another activity, after the activity saved data to database, it will return back to the fragment, however, the fragment can't show the newest data, I need to go to the activity of the fragment, then come back to the fragment again, it will show newest data.
Here is my code of fragment. I thought that because I put the setAdapter in the onActivityCreated() method, I tried to put it in onCreate() and onCreateView(), both will return nullpointer error.
public class TodoFragment extends ListFragment {
private TodoDataSource datasource;
private Todo item;
List<Todo> todoList;
MyCustomAdapter adapt;
ListView listTask;
EditText t;
public TodoFragment(){
}
/*#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//setHasOptionsMenu(true);
datasource = new TodoDataSource(getActivity());
datasource.open();
todoList = datasource.getAllTodos();
adapt= new MyCustomAdapter(getActivity(), R.layout.todo_list, todoList);
//ListView listTask =(ListView) rootView.findViewById(android.R.id.list);
listTask.setAdapter(adapt);
}*/
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// if (activity != null) {
// Create an instance of the custom adapter for the GridView. A static array of location data
// is stored in the Application sub-class for this app. This data would normally come
// from a database or a web service.
datasource = new TodoDataSource(getActivity());
datasource.open();
todoList = datasource.getAllTodos();
adapt= new MyCustomAdapter(getActivity(), R.layout.todo_list, todoList);
//ListView listTask =(ListView) rootView.findViewById(android.R.id.list);
listTask.setAdapter(adapt);
Button button = (Button) getActivity().findViewById(R.id.simpleadd);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//String summary = t.getText().toString();
Intent i = new Intent(getActivity(), TodoDetailActivity.class);
//Bundle bundle = new Bundle();
// bundle.putString("Summary", summary);
//i.putExtras(bundle);
startActivity(i);
}
});
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_todo, container, false);
t = (EditText) rootView.findViewById(R.id.simple);
listTask = (ListView) rootView.findViewById(android.R.id.list);
return rootView;
}
#Override
public void onResume() {
datasource.open();
super.onResume();
adapt.notifyDataSetChanged();
}
#Override
public void onPause() {
datasource.close();
super.onPause();
}
}
Here is part of my another activity code.
it will update the database and return to the fragment.
confirmButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (TextUtils.isEmpty(mTitleText.getText().toString())) {
makeToast();
} else {
final String category = mCategory.getSelectedItem().toString();
final String summary = mTitleText.getText().toString();
final String description = mBodyText.getText().toString();
datasource.createTodo(category, summary, description);
setResult(RESULT_OK);
finish();
}
}
});
It looks like you aren't updating your data source todoList before calling notifyDataSetChanged() in onResume().
Edit: Clear the list first, and then re-populate. Also add logging to help track down the issue:
#Override
public void onResume() {
super.onResume();
datasource.open();
todoList.clear();
todoList.addAll(datasource.getAllTodos());
adapt.notifyDataSetChanged();
Log.d("MyApp", "Size of data: " + todoList.size() );
}
Once you have added that code, try adding a few records, and watch your logcat to see if the size goes up. You can also cycle through each record, if you post your Todo class I can give you some example code.
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.
}
});
}