• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Displaying XML content within SwipeView

gazzieh

Newbie
Aug 21, 2013
22
3
Okay, so through the help on other posts I have the ability to play a video in one app and, with the added help of this site and the fact that it references the Eclipse default SwipeView, now have another app with 4 swipe view pages.

However I cannot for the life of me find out how to make the content of each page show the text I have within the xml documents (basically, I want to combine app 1 and app 2).

It seems to come down to two parts:

[HIGH] @Override
public Fragment getItem(int position)
{
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
[/HIGH]

and...

[HIGH] /**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment
{
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";

public DummySectionFragment() {}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
[/HIGH]

The site I gave suggested a case statement:

[HIGH]
  1. @Override
  2. public Fragment getItem(int position) {
  3. Fragment fragment = new Fragment();
  4. switch (position) {
  5. case 0:
  6. return fragment = new Fragment1();
  7. case 1:
  8. return fragment = new Fragment2();
  9. case 2:
  10. return fragment = new Fragment3();
  11. default:
  12. break;
  13. }
  14. return fragment;
[/HIGH]

and I tried this, creating the relevant classes. However I could not get any further. The classes remain empty.

Any help? I have an xml document for each page content so all I need the select case to do is display the relevant xml document.

Oh, and shouldn't the above select case have a break within each case? Or am I missing something in this code?
 
I had the same thought this morning; sorry for cutting short.

Okay, the solution was to keep the Select Case as before:

[HIGH]
@Override
public Fragment getItem(int position)
{
Fragment fragment = new Fragment();
switch (position)
{
case 0:
return fragment = new Fragment1();
case 1:
return fragment = new Fragment2();
case 2:
return fragment = new Fragment3();
case 3:
return fragment = new Fragment4();
default:
break;
}
return fragment;
}
[/HIGH]I still believe I should break each case since it makes no sense to flow through all of the cases but it seems to work at the moment so...

Anyway, the next part was to create the class for each of these fragments and simply to inflate the relevant XML document at that point:

[HIGH]public static class Fragment1 extends Fragment
{
VideoPlayer videoplay = new VideoPlayer();

public Fragment1() {}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.activity_page1, container, false);
return rootView;
}
}

public static class Fragment2 extends Fragment
{
public Fragment2() {}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.activity_page2, container, false);
return rootView;
}
}

public static class Fragment3 extends Fragment
{
public Fragment3() {}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.activity_page3, container, false);
return rootView;
}

}

public static class Fragment4 extends Fragment
{
public Fragment4() {}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.activity_page4, container, false);
return rootView;
}
}[/HIGH]That was it; simple really and yet took so long for me to work out!
 
Upvote 0
Okay, I decided that Steve's way was very complex but the better way and I hope I have done him justice.

I rebuilt the entire app again but this time did not use the getItem Switch I give before but use a variety of Steve's:

[HIGH]@Override
public Fragment getItem(int position)
{
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}[/HIGH]I kept with the original Eclipse name of DummySectionFragment but will change this in time.

I now created a new XML layout called fragment_main.xml:

[HIGH]<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:id="@+id/textView1"
android:textSize="18sp"
android:layout_width="400sp"
android:layout_height="400sp"
android:singleLine="false"
android:textColor="#ffffff" />

<RelativeLayout
android:id="@+id/vidViewContainer"
android:layout_marginLeft="420dp"
android:layout_width="350dp"
android:layout_height="400dp" >

<VideoView
android:id="@+id/vidView"
android:background="@drawable/back"
android:layout_width="350dp"
android:layout_height="350dp" />

<TextView
android:id="@+id/textView2"
android:textSize="15sp"
android:layout_width="fill_parent"
android:layout_height="20sp"
android:textColor="#ffff00"
android:gravity="center" />

</RelativeLayout>

</RelativeLayout>
[/HIGH]This gives me conformity of approach and so I could now ignore this file and concentrate on the Java file again.

Here I altered the final element:

[HIGH]public static class DummySectionFragment extends Fragment
{
public static final String ARG_SECTION_NUMBER = "section_number";

public DummySectionFragment() {}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView TextView = (TextView) rootView.findViewById(R.id.textView1);
TextView LabelView = (TextView) rootView.findViewById(R.id.textView2);

switch (getArguments().getInt(ARG_SECTION_NUMBER))
{
case 1:
TextView.setText(R.string.P1);
LabelView.setText(R.string.P1_label);
break;
case 2:
TextView.setText(R.string.P2);
LabelView.setText(R.string.P2_label);
break;
case 3:
TextView.setText(R.string.P3);
LabelView.setText(R.string.P3_label);
break;
case 4:
TextView.setText(R.string.P4);
LabelView.setText(R.string.P4_label);
break;
}

return rootView;
}
}[/HIGH]And that's it. This puts the text given in strings.xml onto the individual pages of the SwipeView.

Thanks everyone. This has been so much fun!
 
  • Like
Reactions: steve
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones