Viewpager in FragmentActivity themed as Dialog - android

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;
}
}

Related

Pass object from activity to fragment

I am new to Android.Its my first application.I have tried searching on Internet but everyone tried to pass simple data from activity to fragment.I want to pass an whole object from activity to fragment.
I know there is one solution around is just use serialize & de-serialize & put the data in Bundle.
Instead trying serialize & deserialize,I created a non-static method in my activity & trying to getActivity() in fragment & then calling that method but it returns me null.Can anybody help me with what I am doing wrong.
Below is my code:-
BaseDefaultActivity is my own customized class which extends AppCompatActivity.
public class EventDetailActivity extends BaseDefaultActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setBaseContentView(R.layout.activity_event_detail);
ButterKnife.bind(this);
initialiseDataFromIntend();
DummyObject obj=new DummyObj();
setDataObject(obj);
// Get the ViewPager and set it's PagerAdapter so that it can display items
viewPager.setAdapter(new EventDetailFragementAdapter(getSupportFragmentManager(), EventDetailActivity.this));
// Give the TabLayout the ViewPager
tabLayout.setupWithViewPager(viewPager);
}
}
public class BaseDefaultActivity extends AppCompatActivity implements BaseActivity {
private Object dataObj;
public void setDataObject(Object obj) {
this.dataObj = obj;
}
public Object getDataObj() {
return this.dataObj;
}
}
public class EventDetailFragementAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 3;
private String tabTitles[] = new String[]{"Tab1", "Tab2", "Tab3"};
private Context context;
public EventDetailFragementAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
#Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new EventDetailFragement();
break;
case 1:
fragment = new EventBasicInfoFragement();
break;
default:
fragment = new EventDetailFragement();
break;
}
return fragment;
}
#Override
public int getCount() {
return PAGE_COUNT;
}
public class EventDetailFragement 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.event_detail_fragement, container, false);
TextView textView = (TextView) view;
textView.setText("AAAAAAA #" + (SearchEventResponseObject) ((BaseDefaultActivity) getActivity()).getDataObj());
return view;
}
}

Buttons on ViewPager Fragments

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

Can't swipe to next item inside my detail activity after implementing viewpager

I am trying to swipe item(from listview) in my detail activity using viewpager.
I have 20 items in my listview (data parsed from json) Instead of swiping the next/previous item , It's only the same item that I swipe (20 times).
here is my DetailActivity.
public class DetailsActivity extends FragmentActivity {
Article feed;
int pos;
private DescAdapter adapter;
private ViewPager pager;
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.item_details);
pos = getIntent().getExtras().getInt("pos");
adapter = new DescAdapter(getSupportFragmentManager());
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
pager.setCurrentItem(pos);
}
public class DescAdapter extends FragmentStatePagerAdapter {
public DescAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return 20;
}
public Fragment getItem(int position) {
return DetailFragment.newInstance(position);
}
}
And my detailFragment
public class DetailFragment extends Fragment {
static DetailFragment newInstance(int position) {
DetailFragment f = new DetailFragment();
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.detail_fragment, container, false);
String displaytitle= getActivity().getIntent().getStringExtra("title");
TextView titleF = (TextView)view.findViewById(R.id.title);
titleF.setText(displaytitle);
...
return view;
}
What am I doing wrong?
You are not using the correct argument:
You send the argument "pos2" but you retrieve the argument "title".
#Override
public Fragment getItem(int position) {
DetailFragment frag = new DetailFragment();
Bundle bundle = new Bundle();
bundle.putInt("fPos",position);
frag.setArguments(bundle);
return frag;
}

error on calling getSupportFragmentManager() from activity

I am trying to implement Swipe view using fragment.
But its very important for me to extend simple activity rather than fragment activity.
I am getting error on getSupportFragmentManager() in the code written below:
public class CollectionDemoActivity extends Activity {
DemoCollectionPagerAdapter mDemoCollectionPagerAdapter;
ViewPager mViewPager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_collection_demo);
mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mDemoCollectionPagerAdapter);
}
public static class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
public DemoCollectionPagerAdapter(FragmentManager fm) {
super(fm);
//int x=10;
}
#Override
public Fragment getItem(int i) {
Fragment fragment = new DemoObjectFragment();
Bundle args = new Bundle();
args.putInt(DemoObjectFragment.ARG_OBJECT, i + 10);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
// For this contrived example, we have a 100-object collection.
return 100;
}
#Override
public CharSequence getPageTitle(int position) {
return "OBJECT " + (position + 1);
}
}
public static class DemoObjectFragment extends Fragment {
public static final String ARG_OBJECT = "object";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_collection_object, container, false);
Bundle args = getArguments();
((TextView) rootView.findViewById(android.R.id.text1)).setText(
Integer.toString(args.getInt(ARG_OBJECT)));
return rootView;
}
}
}
The error is pretty self-explanatory. getSupportFragmentManager() is only defined for the FragmentActivity class. No matter how much you want or need to call that method, it simply doesn't exist for plain Activities.
See also: Difference between Activity and FragmentActivity and Difference between Fragment and FragmentActivity

Android ViewPager, switching from fragment to another fragment

I have a viewpager with 2 pages, on each fragment i put a button to switching fragment,
but if i change orientation switching doesn't work.
For switching fragment I using my OnChangePageButtonClick interface
Why is this happening?
ViewPager Activity:
public class ViewPagerMusic extends FragmentActivity implements OnChangePageButtonClick {
private ViewPager vp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_pager_music);
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(super.getSupportFragmentManager(), fragments);
vp = (ViewPager)findViewById(R.id.viewpager);
vp.setAdapter(viewPagerAdapter);
}
private class ViewPagerAdapter extends FragmentStatePagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch(position) {
case 0:
MainPage mainPage = new MainPage();
mainPage.setOnChengeButtonListener(ViewPagerMusic.this);
return mainPage;
case 1:
PlaylistPage playlistPage = new PlaylistPage();
playlistPage.setOnChengeButtonListener(ViewPagerMusic.this);
return playlistPage;
}
return null;
}
#Override
public int getCount() {
return 2;
}
}
#Override
public void selectPage(int page) {
vp.setCurrentItem(page);
}
}
my Frgamnets:
public class MainPage extends Fragment {
public MainPage() {
setRetainInstance(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.music_main_page, container, false);
ImageButton btnPlaylist = (ImageButton)v.findViewById(R.id.btnGoPlaylist2);
btnPlaylist.setOnClickListener(onButtonListener);
return v;
}
private OnClickListener onButtonListener = new OnClickListener() {
#Override
public void onClick(View view) {
onChangePageButtonClick.selectPage(1);
}
};
public void setOnBackButtonListener(OnChangePageButtonClick onChangePageButtonClick) {
this.onChangePageButtonClick = onChangePageButtonClick;
}
private OnChangePageButtonClick onChangePageButtonClick;
}
Playlist fragment is similar to MainPage fragment.
Your fragments are storing a reference to the ViewPagerMusic activity. However, after rotation, the activity is destroyed and recreated, so the fragments now contain a reference to the old activity.
Instead of storing a reference to the activity, you can call the activity's method from the fragment like this:
((ViewPagerMusic) getActivity()).selectPage(1);
just create this method in your view pager
#Override
public void selectPage(int page) {
vp.setCurrentItem(page);
}
and then call this in your onclick method
((ViewPagerMusic) getActivity()).selectPage(1);

Categories

Resources