viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
getActionBar().setSelectedNavigationItem(position);
if(position == 1)
{
new FragmentFavorites().update(getApplicationContext());
}
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
update from FragmentFavorites
public void update(Context context) {
savedFacts = com.invizorys.sexpro.util.Util.readFactsList(context);
if (savedFacts.size() > 0)
mTextView.setText(savedFacts.get(currentFactIndex));
else
mTextView.setText("you still have not saved any fact");
}
when I turn on FragmentFavorites application crashes(mTextView nullpointer), despite the fact that I find it in onCreateView()
mTextView = (TextView) rootView.findViewById(R.id.textview_fact);
Update
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_favorites, container, false);
...
mTextView = (TextView) rootView.findViewById(R.id.textview_fact);
...
return rootView;
}
When you new a FragmentFavorites instance, the onCreateView() is not called yet. You should find the FragmentFavorites instance from ViewPager instead of creating one.
Related
The problem is this:
When you first choose a picture - everything is fine. If you swipe, and press the "back" button, after you open another image then will open last image which will opened.
The correct position is passed through the Bundle. I don’t understand where to look for the error.
public class ViewPagerAdapter extends PagerAdapter {
private LayoutInflater layoutInflater;
public ViewPagerAdapter() {
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.image_fullscreen, container, false);
ImageView imageView = view.findViewById(R.id.image_fScreen);
Hit hit = hits.get(position);
Picasso.get().load(hit.getLargeImageURL())
.into(imageView);
container.addView(view);
return view;
}
#Override
public int getCount() {
return hits.size();
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view == object;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((View) object);
}
}
DialogFragment:
public class FullScreenDialogFragment extends DialogFragment {
private ArrayList<Hit> hits;
private ViewPager viewPager;
private ViewPagerAdapter viewPagerAdapter;
private TextView author;
private TextView count;
private int selectedPosition;
private Button buttonShare;
private static FullScreenDialogFragment fullScreenDialogFragment;
private int rlyPosition;
public static FullScreenDialogFragment getInstance() {
if (fullScreenDialogFragment == null) {
fullScreenDialogFragment = new FullScreenDialogFragment();
}
return fullScreenDialogFragment;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_FRAME, R.style.DialogStyle);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.image_viewpager, container, false);
viewPager = view.findViewById(R.id.viewpager);
author = view.findViewById(R.id.author);
buttonShare = view.findViewById(R.id.btnShare);
count = view.findViewById(R.id.count);
hits = (ArrayList<Hit>) getArguments().getSerializable("hits");
selectedPosition = getArguments().getInt("position");
viewPagerAdapter = new ViewPagerAdapter();
viewPager.setAdapter(viewPagerAdapter);
viewPager.addOnPageChangeListener(onViewPageChangeListener);
setItem(selectedPosition);
return view;
}
private void setItem(int pos) {
viewPager.setCurrentItem(pos, true);
setItemInfo(selectedPosition);
}
private void setItemInfo(int pos) {
count.setText((pos + 1) + " of " + hits.size());
Hit hit = hits.get(pos);
author.setText(hit.getUser());
}
ViewPager.OnPageChangeListener onViewPageChangeListener = new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
setItemInfo(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
};
I Guess the Problem is here
private void setItem(int pos) {
viewPager.setCurrentItem(pos, true);
setItemInfo(selectedPosition);
}
You might be passing the selectedPosition instead of currentPosition
Please check.
My problem was in creating DialogFramgent object.
I did it him in Singlton pattern and called it with old data.
Now in OnClick i'm created new object DialogFramget and him get new values.
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fullScreenDialog = new FullScreenDialogFragment();
fullScreenDialog.setArguments(bundle);
fullScreenDialog.show(fragmentTransaction, "");
I have Activity with LastTransactionsFragment, this fragment contains TabLayout with 3 tabs (AllTransactionsFragment , IncomesTransactionsFragment and OutgoesTransactionsFragment). All the tabs shows RecyclerView with list of transactions. User can delete row in each fragments. I need to implement if user delete row in 2nd fragment it also delete it immediately in 1st fragment. For now it delete row in main fragment and in database, but not in others fragments. It changes when i move to other and go back. All fragments look the same:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_all_transactions_list, container, false);
DatabaseHelper databaseHelper = new DatabaseHelper(getContext());
ArrayList<TransactionModel> lastTransactionsList = databaseHelper.getAllTransactionList();
Collections.reverse(lastTransactionsList);
RecyclerView recyclerView = view.findViewById(R.id.rv_allTransactionsList);
adapter = new LastTransactionsAdapter(getContext(), lastTransactionsList, false);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
return view;
}
EDIT
here is short video of the issue: https://streamable.com/398zke
EDIT 2
On Fragment with TabLayout
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if (position== 0){
IncomesTransactionsListFragment fragment = new IncomesTransactionsListFragment();
fragment.refreshRecyclerView();
} else if (position == 1){
AllTransactionsListFragment allTransactionsListFragment = new AllTransactionsListFragment();
allTransactionsListFragment.refreshRecyclerView();
} else if (position == 2){
AllTransactionsListFragment allTransactionsListFragment = new AllTransactionsListFragment();
allTransactionsListFragment.refreshRecyclerView();
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
and the method in each fragments:
public void refreshRecyclerView(){
adapter.notifyDataSetChanged();
}
EDIT 3
main fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_last_transactions, container, false);
final ViewPager viewPager = view.findViewById(R.id.vp_allTransactions);
TabLayout tabLayout = view.findViewById(R.id.tl_allTransactions);
adapter = new TransactionsViewPagerAdapter(getChildFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, tabLayout.getTabCount());
viewPager.setAdapter(adapter);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if (position == 0){
IncomesTransactionsListFragment fragment = new IncomesTransactionsListFragment();
System.out.println(0);
fragment.refreshRecyclerView();
} else if (position == 1){
AllTransactionsListFragment allTransactionsListFragment = new AllTransactionsListFragment();
System.out.println(1);
allTransactionsListFragment.refreshRecyclerView();
} else if (position == 2){
AllTransactionsListFragment allTransactionsListFragment = new AllTransactionsListFragment();
System.out.println(2);
allTransactionsListFragment.refreshRecyclerView();
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
return view;
}
}
tab fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_all_transactions_list, container, false);
DatabaseHelper databaseHelper = new DatabaseHelper(getContext());
ArrayList<TransactionModel> lastTransactionsList = databaseHelper.getAllTransactionList();
Collections.reverse(lastTransactionsList);
RecyclerView recyclerView = view.findViewById(R.id.rv_allTransactionsList);
adapter = new LastTransactionsAdapter(getContext(), lastTransactionsList, false);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
return view;
}
public void refreshRecyclerView(){
adapter.notifyDataSetChanged();
}
}
You need to add the addOnPageChangeListener() to update any fragments data from activity. for ex.
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetpxl) {
}
#Override
public void onPageSelected(int position) {
/*get your fragments instance and write one method in that fragment to refresh the list. call this method from here*/
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
Use this link for reference: https://www.legendblogs.com/blog/how-to-use-tablayout-in-viewpager-with-recyclerview/121810
I am trying to change TextView content on Sensor change but after calling TextView.setText("foo"); app crashes as if the TextView variable was null. I set up the TextView variable in onCreateView. Here is the code:
public class fragment1 extends Fragment implements SensorEventListener
{
private SensorManager mSrMgr = null;
private Sensor mLight;
private TextView page1;
protected View mView;
public fragment1()
{
// Required empty public constructor
}
public void onCreate(Bundle savedInstance)
{
super.onCreate(savedInstance);
mSrMgr = (SensorManager) getActivity().getSystemService(SENSOR_SERVICE);
mLight = mSrMgr.getDefaultSensor(Sensor.TYPE_LIGHT);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
page1 = container.findViewById(R.id.page1);
View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
this.mView = view;
return inflater.inflate(R.layout.fragment_fragment1, container, false);
}
public void onPause()
{
super.onPause();
this.unregister();
}
public void onResume()
{
super.onResume();
this.registerSensorListiner();
}
private void registerSensorListiner()
{
mSrMgr.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);
}
private void unregister()
{
mSrMgr.unregisterListener(this);
}
#Override
public void onSensorChanged(SensorEvent event)
{
Toast.makeText(getContext(),String.valueOf(event.values[0]),Toast.LENGTH_SHORT).show();
page1.setText("elo"); //here is the crashing part
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
}
}
First, inflate the layout
View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
this.mView = view;
page1 = view.findViewById(R.id.page1);
return view, false);
use view object to call findViewById method
Use this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
this.mView = view;
page1 = view.findViewById(R.id.page1);
return view;
}
Instead of this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
page1 = container.findViewById(R.id.page1);
View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
this.mView = view;
return inflater.inflate(R.layout.fragment_fragment1, container, false);
}
Why Multiple times R.layout. ?
View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
this.mView = view;
return inflater.inflate(R.layout.fragment_fragment1, container, false);
It should be One Time. At first modify here.
Note
You will get NullPointerException.
Thrown when an application attempts to use null in a case where an
object is required.
You should pass View's object .
page1 = view.findViewById(R.id.page1);
Finally
View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
page1 = view.findViewById(R.id.page1);
this.mView = view;
return view;
public class fragment1 extends Fragment implements SensorEventListener {
private SensorManager mSrMgr = null;
private Sensor mLight;
private TextView page1;
protected View mView;
public fragment1() {
// Required empty public constructor
}
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
mSrMgr = (SensorManager) getActivity().getSystemService(SENSOR_SERVICE);
mLight = mSrMgr.getDefaultSensor(Sensor.TYPE_LIGHT);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_fragment1, container, false);
page1 = mView.findViewById(R.id.page1);
return mView;
}
public void onPause() {
super.onPause();
this.unregister();
}
public void onResume() {
super.onResume();
this.registerSensorListiner();
}
private void registerSensorListiner() {
mSrMgr.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);
}
private void unregister() {
mSrMgr.unregisterListener(this);
}
#Override
public void onSensorChanged(SensorEvent event) {
Toast.makeText(getContext(), String.valueOf(event.values[0]), Toast.LENGTH_SHORT).show();
page1.setText("elo"); //here is the crashing part
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
put your findViewById code in onActivityCreated like below:
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
page1 = getActivity().findViewById(R.id.page1);
}
Sometimes when creating layouts in fragments you copy and paste xml code that has duplicate IDs. When you then call the id of the pasted resources it will try to invoke the first defined resources which is probably null at runtime. Double check if the page1 id is found only in one fragment's Xml. If not work around "findviewbyid".I hope that helps, there's little detail in your code(Error msg)
i have problem with using Butterknife libray. There is I have SearchActivity.class use ViewPager with 3 Fragment in FragmentSearchAdapter:
SearchActivity.class
mAdapter = new FragmentSearchAdapter(getSupportFragmentManager(), SearchActivity.this);
mAdapter.addFragment("search histori");
mAdapter.addFragment("search query");
mAdapter.addFragment("search result");
mPager.setAdapter(mAdapter);
mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
public void onPageSelected(int currentIndex) {
if(currentIndex==2) {
SearchResultFragment SearchResultFragment = (SearchResultFragment) mAdapter.getItem(2);
if (SearchResultFragment != null) {
SearchResultFragment.setDescription("Text 1","Text 2");
}
}
}
public void onPageScrolled(int arg0, float arg1, int arg2) { }
public void onPageScrollStateChanged(int arg0) { }
});
FragmentSearchAdapter.class
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
SearchHistoryFragment search_histori = new SearchHistoryFragment();
return search_histori;
case 1:
SearchQueryFragment search_query = new SearchQueryFragment();
return search_query;
case 2:
SearchResultFragment search_result = new SearchResultFragment();
return search_result;
}
return null;
}
Where one fragment is SearchResultFragment.Class
public class SearchResultFragment extends Fragment {
#Bind(R.id.text1)
TextView text1;
#Bind(R.id.text2)
TextView text2;
private String page_index;
private PageAlquranActivity activity;
public SearchResultFragment() {
}
public static SearchResultFragment newInstance(int page_index) {
SearchResultFragment fragment = new SearchResultFragment();
Bundle args = new Bundle();
args.putString(Variabel.page_index, String.valueOf(page_index));
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
page_index = getArguments().getString(Variabel.page_index);
}
activity = (PageAlquranActivity) getActivity();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_search_result, container, false);
ButterKnife.bind(this, v);
return v;
}
public void setDescription(String s1, String s2) {
text1.setText(s1);
text2.setText(s2);
}
#Override
public void onAttach(Context context) {super.onAttach(context);}
#Override
public void onDetach() {
super.onDetach();
}
}
My Problem : when call method SearchResultFragment.setDescription("Text 1","Text 2"); from SearchActivity, I get error :
null object reference
each TextView. This error if use ButterKnife,but if not, it's work if I declare View like this :
text1 = rootView.findViewById(R.id.text1);
text2 = rootView.findViewById(R.id.text2);
in onCreateView.
so how to solve it ? Sorry for my English. Thanks.
#AmayDiam you need to bind ButterKnife again in the called method. Pass along the dependency for the bind - the fragment view from the OnPageChangeListener
SearchActivity.class
mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
public void onPageSelected(int currentIndex)
{
if(currentIndex==2)
{
SearchResultFragment SearchResultFragment = (SearchResultFragment) mAdapter.getItem(2);
if (SearchResultFragment != null)
{
SearchResultFragment.setDescription(mPager.getChildAt(currentIndex)"Text 1","Text 2");
}
}
}
public void onPageScrolled(int arg0, float arg1, int arg2) { }
public void onPageScrollStateChanged(int arg0) { }
});
SearchResultFragment.Class
public void setDescription(View view,String s1, String s2)
{
ButterKnife.bind(this, view);
//
text1.setText(s1);
text2.setText(s2);
}
Had similar issue too, just because I've added butterknife from Android Studio's Dependency management and not by copy-pasting gradle lines from Butterknife website.
So I had to add
compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
instead of just
compile 'com.jakewharton:butterknife:8.5.1'
I am having am mainActivity which consists of 3 fragment activity in a view pager .each fragment consists of edit texts .How to get the value of the edit text when swipe fragment horizontally ? I am trying to do like this.below code
public class MainActivity extends FragmentActivity {
int value = 0;
Context context;
String a = "";
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
// Set up the action bar.
//final ActionBar actionBar = getActionBar();
// actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mAddButtonB = (Button) findViewById(R.id.addnewB);
mAddSearchV = findViewById(R.id.addnewSearch);
mAddButtonB.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mAddSearchV.setVisibility(View.GONE);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.customviewpager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
});
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
PageZero p0;
PageOne p1;
PageTwo p2;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
p0 = new PageZero();
p1 = new PageOne();
p2 = new PageTwo();
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return p0;
case 1:
return p1;
case 2:
return p2;
}
return null;
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
}
// PageZero = (Fragment1)getFragmentManager().findFragmentByTag("frag1");
public class PageZero extends Fragment {
private EditText name;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.componentname, container,
false);
name=(EditText)view.findViewById(R.id.nameTV);
name.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
a=name.getText().toString().trim();
Toast.makeText(getApplicationContext(),a,Toast.LENGTH_SHORT).show();
}
});/*(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
a=name.getText().toString().trim();
Toast.makeText(getApplicationContext(), a,Toast.LENGTH_SHORT).show();
return false;
}
});*/
return view;
}
}
public class PageOne extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.componentaddress, container,
false);
return view;
}
}
public class PageTwo extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.componentmobno, container,
false);
return view;
}
}
}
when i try to access the eddittext inside pageZero class like below
name=(EditText)view.findViewById(R.id.nameTV);
a=name.getText().toString().trim();
i am getting null value.help me out..Thanks in advance.
Change the pagezero class oncreateview code like this .....
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view =getActivity().getLayoutInflater().inflate(R.layout.componentname,null);
name=(EditText)view.findViewById(R.id.nameTV);
name.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
a=name.getText().toString().trim();
Toast.makeText(getApplicationContext(),a,Toast.LENGTH_SHORT).show();
}
return view;
});
Return the view friend and then use default layout inflater...
At the time you click the edittext field, is there any content? Maybe you should get the text in the lost focus event of the edittext field. Thus you can enter some text first...
Example:
name.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus == false){
//get text from edittext field...
}
}
});
The way you try to get the text looks right to me. At least I do it the same way.
In the PagerAdapter's 'getItem' Method you should instantiate your fragments directly:
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new PageZero();
case 1:
return new PageOne();
case 2:
return new PageTwo();
}
return null;
}
The Viewpager only calls getItem if it wants to create a new item. Unfortunatly they called the method getItem and not createItem which would be more obvious.
Get your EditText inside onClick block...
This mean change the code from:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.componentname, container, false);
name=(EditText)view.findViewById(R.id.nameTV);
name.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
a=name.getText().toString().trim();
Toast.makeText(getApplicationContext(),a,Toast.LENGTH_SHORT).show();
}
});
return view;
}
To:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.componentname, container, false);
name.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// GET EDIT TEXT HEAR:
EditText name=(EditText)view.findViewById(R.id.nameTV);
a=name.getText().toString().trim();
Toast.makeText(getApplicationContext(),a,Toast.LENGTH_SHORT).show();
}
});
return view;
}
To create a 3 different fragments with view pager you should:
1)Create FragmentActivity with view pager and view pager adapter.
public class FragmentActivity extends FragmentActivity
{
private FragmentAdapter adapter;
private ViewPager pager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
adapter = new FragmentAdapter(getSupportFragmentManager());
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
}
2)Create 3 Fragments with static instance
public final class Fragment1 extends Fragment
{
public static Fragment1 newInstance() {
return new Fragment1();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LinearLayout layout = new LinearLayout(getActivity());
// your fragment xml view
return view;
}
}
3)Fill view pager adapter with it;
public class FragmentAdapter extends FragmentPagerAdapter
{
public InstallFragmentAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return Fragment1.newInstance();
case 1:
return Fragment2.newInstance();
case 2:
return Fragment3.newInstance();
}
return null;
}
}
public class PageZero extends Fragment {
public static EditText name; //change this to public static
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.componentname, container,
false);
name = (EditText)view.findViewById(R.id.nameTV);
name.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
return view;
}
}
Then, you can access the public static EditText name (after the view for it has been generated of course) from another fragment like this.
public class PageOne extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.componentaddress, container,
false);
EditText page_one_text = PageZero.name; //you can get that variable in any fragment now, but using the class name of that fragment to access it
return view;
}
}
Cheers. Please mark this as the correct answer if it helps you. You have quite a bit of answers here
I tried below code. Working for me
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_main_explore, container, false);
ed1 = (EditText)v.findViewById(R.id.editTextTemp);
if( ed1 == null )
{
Toast.makeText(getActivity(), "ERROR UNABLE TO GET EDIT", Toast.LENGTH_LONG).show();
}
// Inflate the layout for this fragment
//return inflater.inflate(R.layout.fragment_main_explore, container, false);
return v;
}