So I have a View Pager class in which I have four different fragment classes with different layouts. I have buttons on each Fragment and those buttons once clicked should take me to new activities. But the problem is once I run the application the button on the first fragment works but not the other fragments. Then again after I swipe to the other fragments and come back to the first fragment, the button which was working in the first fragment does not work anymore. There is no exception shown in the Logcat so i dont know where the error is. Can anybody please help me on this. Below is the code for my View Pager and Adapter followed by the code of one of my fragments as the codes for the other fragments are just the same with differnet button names
public class Home_Pager extends FragmentActivity {
FragmentAdapter mAdapter;
ViewPager pager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_tablet_pager);
setContentView(R.layout.activity_home_pager);
mAdapter = new FragmentAdapter(getSupportFragmentManager());
pager = (ViewPager)findViewById(R.id.pager);
pager.setAdapter(mAdapter);
}
public class FragmentAdapter extends FragmentStatePagerAdapter {
public FragmentAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = new Fragment();
switch(position){
case 0:
fragment = new Mobility();
break;
case 1:
fragment = new DesktopAIO();
break;
case 2:
fragment = new Monitor_Projector();
break;
case 3:
fragment = new SmartPhone();
break;
}
return fragment;
}
#Override
public int getCount() {
return 4;
}
}
This is the code for one of my fragment
public class Mobility extends Fragment{
View myFragmentView;
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
public static final Mobility newInstance(String message)
{
Mobility f = new Mobility();
Bundle bdl = new Bundle(1);
bdl.putString(EXTRA_MESSAGE, message);
f.setArguments(bdl);
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myFragmentView = inflater.inflate(R.layout.activity_mobility, container, false);
return myFragmentView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Button x = (Button) getActivity().findViewById(R.id.button1);
x.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent firstpage= new Intent(getActivity(),Tablet_Home.class);
getActivity().startActivity(firstpage);
}
});
}
Please guys give me a solution. Thank You
Related
I have a viewPager activity that makes multiple fragments with custom FragmentPagerAdapter.
I need to get the List<Audio> audioList and use it in my fragment.
Here is a small part of my MainActivity where I make the fragments. I do not think the rest of the MainActivity code is required in order to answer this question.
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
PagerAdapter pagerAdapter =
new PagerAdapter(getSupportFragmentManager(), MainActivity.this, audioList);
viewPager.setAdapter(pagerAdapter);
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);
// Iterate over all tabs and set the custom view
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
tab.setCustomView(pagerAdapter.getTabView(i));
}
Here is my custom FragmentPagerAdapter:
public class PagerAdapter extends FragmentPagerAdapter {
String tabTitles[] = new String[] { "Recommended", "Popular", "Rock", "Pop", "Blues", "Chill" };
Context context;
private List<Audio> audioList;
public PagerAdapter(FragmentManager fm, Context context, List<Audio> audioList) {
super(fm);
this.context = context;
this.audioList = audioList;
}
#Override
public int getCount() {
return tabTitles.length;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new BlankFragment();
case 1:
return new BlankFragment();
case 2:
return new BlankFragment();
case 3:
return new BlankFragment();
case 4:
return new BlankFragment();
case 5:
return new BlankFragment();
}
return null;
}
#Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
public View getTabView(int position) {
View tab = LayoutInflater.from(context).inflate(R.layout.custom_tab, null);
TextView tv = (TextView) tab.findViewById(R.id.custom_text);
tv.setText(tabTitles[position]);
return tab;
}
}
And I need to get the List<Audio> audioList into my Fragment here:
public class BlankFragment extends Fragment {
public BlankFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.content_view, container, false);
//Get auidoList here
return rootView;
}
I also need to get the title of the tab in the future but my guess is that I would get it similarly to the audioList. If it is not then I could also use help on getting the tab title.
Additionally if the code style is wrong then I am always happy to have some feedback on it aswell.
The traditional way of doing this is by having an instance method that moves it all via a bundle.
Like this:
public class BlankFragment extends Fragment {
public static BlankFragment newInstance(String title, ArrayList<Audio> audioList) {
BlankFragment blankFragment = new BlankFragment();
Bundle bundle = new Bundle();
bundle.putString("Title", title);
bundle.putParcelableArrayList("AudioList", audioList);
blankFragment.setArguments(bundle);
return blankFragment;
}
public BlankFragment() {
// Required empty public constructor
}
private String title;
private ArrayList<Audio> audioList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
if (bundle != null) {
title = bundle.getString("Title");
audioList = getArguments().getParcelableArrayList("AudioList");
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.content_view, container, false);
//Get auidoList here
return rootView;
}
}
Obviously your Audio class will need to implement parcelable for this to work. I found a great plugin (Android Parcelable Code Generator) in Android Studio for generating parcelable code that makes that quick and easy.
The setOnclick Listener that I have written in the onCreate method doesn't work.
The error is a null pointer exception error for button b1. I tried to initialize b1 before the onclick method but it doesn't work either.
For this code I used the Android Studio Example "TAbbed Activity".
Then I'm searching a way to use the onClickListener method in a tabbed activity.
Please tell me some solutions . Thanks
public class MainActivity extends AppCompatActivity {
public static Button b1,b2;
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
// THIS DOESNT RUN AND IT MAKES THE APP CRASH
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
b1.setText("test");
}
});
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public void newImageView(int sectionNumber, Button img, int n){
if(sectionNumber == n)
img.setVisibility(View.VISIBLE);
else
img.setVisibility(View.INVISIBLE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
b1=(Button)rootView.findViewById(R.id.b1);
newImageView(getArguments().getInt(ARG_SECTION_NUMBER),b1,1);
b2=(Button) rootView.findViewById(R.id.b2);
newImageView(getArguments().getInt(ARG_SECTION_NUMBER),b2,2);
return rootView;
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
}
return null;
}
}
}`enter code here
Put this before setting onClickListener
b1=(Button)rootView.findViewById(R.id.b1);
or set listener after you have assigned id.
You should assign an id to a button before handling its click. Otherwise, how is android supposed to figure out what button is b1.
That button is probably in your fragment's XML (R.layout.fragment_main) so you should add that onClickListener() in the onCreateView() of your PlaceholderFragment after b1=(Button)rootView.findViewById(R.id.b1);.
In any case you cant define an onClickListener() in an object that's not defined first. You first have to find that view usually by the id attribute from the XML with b1=(Button)rootView.findViewById(R.id.b1); or create one using a contructor, eg. b1= new Button(); (not suitable in your case).
I am creating swipe screens using ViewPager where i have two fragments to show using swipes inside FragmentActivity .Please consider the following code :
Fragment_One.java
public class Fragment_One extends Fragment {
private Button btnNext;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_one,null);
btnNext = (Button) v.findViewById(R.id.btnNext);
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment = new Fragment_Two();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragmentOne,fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return v;
}
}
2.Fragment_Two.java
public class Fragment_Two extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_two,null);
}
}
3. PagerAdapter.java
public class PagerAdapter extends FragmentPagerAdapter {
List<Fragment> listFragments ;
public PagerAdapter(FragmentManager fm,List<Fragment> listFragments) {
super(fm);
this.listFragments = listFragments;
}
#Override
public Fragment getItem(int i) {
return listFragments.get(i);
}
#Override
public int getCount() {
return listFragments.size();
}
}
4.MainActivity.java
public class MainActivity extends FragmentActivity{
private List<Fragment> listFragments;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewPager);
Fragment_One fragment_one = new Fragment_One();
Fragment_Two fragment_two = new Fragment_Two();
listFragments = new ArrayList<>();
listFragments.add(fragment_one);
listFragments.add(fragment_two);
PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager(),listFragments);
viewPager.setAdapter(pagerAdapter);
}
}
The above mentioned code is working fine while swiping screens .But i click on next button ,fragment is replaced with another but is displayed on the same screen.I am not able to fix the issue.Screenshot is given below :
Next button should work in the same way as we swipe the screen .Please help me to fix the issue.
Wouldn't that be easier to use:
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
viewPager .setCurrentItem(viewPage.getCurrentItem() + 1);
});
I can't understand what the issue is. I did the exact same thing previously using a list view where I had to pass results from the Parse database to a list view adapter and it worked perfectly.
#Override
protected void onPostExecute(Void result) {
// Locate the listview in listview_main.xml
viewPager = (ViewPager) findViewById(R.id.pager);
// Pass the results into ListViewAdapter.java
adapter = new ViewPagerAdapter(GalleryViewTest.this, worldpopulationlist);
// Binds the Adapter to the ListView
viewPager.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
The following line of code is what results in the error.
adapter = new ViewPagerAdapter(GalleryViewTest.this, worldpopulationlist);
Hovering over the error produces the following message:
Incompatible types.
Required: android.support.v4.view.PagerAdapter
Found:com.example.gallerypaseupload.galleryview.ViewPagerAdapter
Change this
adapter = new ViewPagerAdapter(GalleryViewTest.this, worldpopulationlist);
to
adapter = new ViewPagerAdapter(getSupportFragmentManager());
And your View Pager Adapter Look Like This
public class ViewPagerAdapter extends FragmentPagerAdapter {
private int pagerCount = 3;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
//Return the fragments
#Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return Frag1.create(i);
case 1:
return Frag2.create(i);
case 2:
return Frag3.create(i);
default:
return Frag1.create(i);
}
}
#Override
public int getCount() {
return pagerCount;
}
}
And fragment follows
public class Frag1 extends Fragment{
public static final String ARG_PAGE = "page";
public Frag1 () {
}
public static Frag1 create(int pageNumber) {
Frag1 fragment = new Frag1();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pageNumber = getArguments().getInt(ARG_PAGE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout .
rootView = (ViewGroup) inflater.inflate(R.layout.frag1, container, false);
initComponents();
return rootView;
}}
This is related to view pager transition using fragments.same way you can do the needful.
View Pager Image Slider : http://androidopentutorials.com/android-image-slideshow-using-viewpager/
My app has a Gridview. Clicking on the Gridview cell used to open an activity which was themed as a dialog in the manifest. This worked well.
To improvise the app further, I wanted to add a viewpager in the activity. So I changed SomeClass extends Activity to SomeClass extends Fragment. I created a container (which extends FragmentActivity) to hold this Fragment and I themed this container as a Dialog in the manifest:
<activity android:name="com.packagename.SomeContainer" android:theme="#style/Theme.Sherlock.Light.Dialog" />
This doesn't show the Fragment as a dialog. It shows the fragment full screen which I don't want and it doesn't resize based on the content, which it did when it was an Activity.
How should I display the container (fragmentactivity) or the fragment as a dialog which should resize according to the content and not display full-screen?
SomeContainer.java
public class SomeContainer extends SherlockFragmentActivity {
private SomeAdapter mAdapter;
private ViewPager mPager;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
overridePendingTransition(R.anim.pull_in_from_left, R.anim.hold);
setContentView(R.layout.layout_events);
getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Intent intent = getIntent();
String date_string = intent.getStringExtra("date_string");
mAdapter = new SomeAdapter(getSupportFragmentManager(), date_string);
mPager = (ViewPager) findViewById(R.id.eventspager);
mPager.setAdapter(mAdapter);
}
#Override
protected void onPause() {
overridePendingTransition(R.anim.hold, R.anim.pull_out_to_left);
super.onPause();
}
public static class SomeAdapter extends FragmentPagerAdapter {
String date;
FragmentManager fm;
public SomeAdapter(FragmentManager fm, String date) {
super(fm);
this.date = date;
this.fm = fm;
}
#Override
public int getCount() {
return 3;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return SomeFragment.newInstance(date);
case 1:
return SomeFragment1.newInstance(date);
case 2:
return SomeFragment2.newInstance(date);
default:
return null;
}
}
}
}
SomeFragment.java
public class SomeFragment extends DialogFragment {
String date_string;
public static SomeFragment newInstance(String date) {
SomeFragment someFragment = new SomeFragment();
Bundle args = new Bundle();
args.putString("date", date);
someFragment.setArguments(args);
returnsomeFragment;
}
/** Called when the activity is first created. */
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.events, container, false);
//getActivity().getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
date_string = getArguments().getString("date");
//do something
return v;
}
}