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

Apps Major Request for Help; sorry.

gazzieh

Newbie
Hi everyone.

I am new to these forums and, in fact, new to Android programming, for which I apologise for both circumstances.

I am in a little of a position right now and could really do with a lot of understanding (sympathy and forgiveness) and assistance.

I have a display panel I need to get running very quickly. The display panel will be a 7" tablet running Android (Jelly Bean) 4.2, which will have three windows viewable to the user; each comprising of text and a video.

I originally wrote the basic system using HTML5 and CSS3 but realised that the swiping action between each view would be difficult to incorporate. I did consider Flash or even converting a PowerPoint presentation but the app idea seemed a relatively simple solution (even given my previous non-experience).

I have installed Eclipse and have begun coding a little bit but cannot currently get the video working at all. I am also aware that once I get this working I will need help with each and every other step, hence the sympathy and understanding.

My activity_main is:
[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:background="#000000"
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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textColor="#ffffff" />

<VideoView
android:id="@+id/vidView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="202dp"
android:layout_marginLeft="400dp"
android:layout_toRightOf="@+id/textView1" />

</RelativeLayout>[/HIGH]I created a new java file and called it playvid.java, which contains:
[HIGH]package com.garynewport.c5app;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

public class playvid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

VideoView myVideoView = (VideoView) findViewById(R.id.vidView);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(myVideoView);
try{
String vidpath = "android.resource://" + getPackageName() + "/" + R.raw.rsr;
myVideoView.setVideoURI(Uri.parse(vidpath));
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
myVideoView.start();
}
catch (Exception e){
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT);
}

}
}[/HIGH]Now I have stolen the code from elsewhere and did some editing myself but then returned to normal when nothing worked. I have three videos that I placed in a folder called raw within the res folder inside my project area. Originally called vid1.mp4, vid2.mp4 and vid3.mp4 I changed the first one to rsr.mp4 to match the original instructions AND the code request.

I understand that the emulator may niot always work with videos so I exported the APK and installed it without issue onto the tablet but aside from the black background and the white text saying 'Hello world!" (yeah, I like the old ways), no video and lots of other bits I do not want.

Is there anyone who is willing to help me out here, knowing that this is a major request and not something I normally do but time is now my biggest issue.
 
Sure, I will help! Well looking at your code I see the issue is caused by your layout file.

Replace your VideoView with this:
[HIGH=XML]
<VideoView
android:id="@+id/vidView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="202dp"
android:layout_marginLeft="400dp"
android:layout_toRightOf="@+id/textView1" />
[/HIGH]

This should get your app playing the video. Does it?

Next I just created my own app to 1) follow along with you and 2) code cleaning for better coding practice.

Here is my MainActivity:
[HIGH=JAVA]
import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.widget.VideoView;
import android.media.AudioManager;

public class MainActivity extends Activity {

MediaPlayer mediaPlayer = new MediaPlayer();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

VideoView videoView = (VideoView) findViewById(R.id.vidView);

videoView.setMediaController(new android.widget.MediaController(this));
getWindow().setFormat(android.graphics.PixelFormat.TRANSLUCENT);
Uri myVideoUri = android.net.Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.test);
videoView.setVideoURI(myVideoUri);


mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

try {
mediaPlayer.setDataSource(getApplicationContext(), myVideoUri);

mediaPlayer.prepare();
mediaPlayer.start();
} catch (java.io.IOException e) {
e.printStackTrace();
}
videoView.requestFocus();
videoView.start();
}

@Override
public void onPause() {
mediaPlayer.stop();
super.onPause();
}
}
[/HIGH]
 
Sorry for not replying sooner but had to test it out. The video plays! Thank you.

I could not see the difference between my videoview and yours but the rest was apparent. Now I need to begin to strip this apart and work out how it works and why mine didn't but before then I need to begin to construct the first page correctly, then build the second and third page and finally work out the switch between them.

As Arnie once said: I'll be back!

And as Elvis once said: Thank you. Thank you very much.
 
Glad your video plays. Looking at this again I also don't think you need both a VideoView and a MediaPlayer... I would have to read some docs when I have more time.

For when you have more questions, please post new threads and try to make the title of the thread explain more of what the exact issue is. This will help others if they are experiencing the same issues.
 
I will do indeed.

I have everything almost in place for the first part and so will post another thread for my next issue but one more for the video.

Currently I tap the video and the play tools come up. I would like to change this so that you tap once to play and tap again to pause. I do not need the tools to appear at all. Is this possible?
 
Are you calling the menu yourself or is that build into the Android OS your using? Normal case you could just do something like this:

[HIGH=JAVA]
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
} else {
mediaPlayer.start();
}
[/HIGH]
 
All I have done is put in the code you so kindly gave. So, the videoview appears (I have surrounded this with a border) and when I tap on it the OS (Jelly Bean 4.2) gives me PLAY, REWIND and FORWARD centre bottom of the screen. So, you need to click the video once to get these to appear and then click the PLAY button to start the video.

I would like to have it so that the video simply plays on the first click and alternates on each click. The video is appearing in the videoView as expected so I do not have two players. I am therefore assuming that I am looking at the events for the videoView? onTouch? onClick?
 
Ok, try this code (added some comments):

[HIGH=XML]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textColor="#ffffff" />

<RelativeLayout
android:id="@+id/vidViewContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">

<VideoView
android:id="@+id/vidView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentTop="true"
android:clickable="false"
android:layout_marginTop="30dp"/>

</RelativeLayout>

</RelativeLayout>
[/HIGH]

[HIGH=JAVA]
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.VideoView;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends Activity {

VideoView videoView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

videoView = (VideoView) findViewById(R.id.vidView);
ViewGroup vidViewContainer = (ViewGroup) findViewById(R.id.vidViewContainer);

videoView.setMediaController(new android.widget.MediaController(this));
getWindow().setFormat(android.graphics.PixelFormat.TRANSLUCENT);
Uri myVideoUri = android.net.Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.test);
videoView.setVideoURI(myVideoUri);

// Needed to prevent controls from displaying and taking control.
videoView.setMediaController(null);

// Invisible onclick for the videos container to now handle controls.
vidViewContainer.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (videoView.isPlaying()) {
videoView.pause();
} else {
videoView.start();
}
}
});

videoView.requestFocus();
videoView.start();
}

@Override
public void onPause() {
videoView.stopPlayback();
super.onPause();
}

}
[/HIGH]
 
Thanks again Steve. I will go through this very shortly and come back with any issues I encounter (or simply another thank you if I have no issues).
 
Okay, I have begun to get everything in place but now have met another problem related to the video.

The code all works but I now have fragments and I reference each fragment through a Select Case:

[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]

Which calls one of the four classes I have constructed:

[HIGH] public static class Fragment1 extends Fragment
{
public Fragment1() {}

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

This all works, with each of the four pages now appearing but the video box empty.

Each of the four fragments have differing videos so I assume I should call the video routine within the relevant class but how? How do I call the correct instance of the right video whilst maintaining the swipe?
 
Each of my 4 fragment pages defines the VideoView:

[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"
android:text="@string/page1_1" />

<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"
android:clickable="false" />

<TextView
android:id="@+id/textView4"
android:textSize="15sp"
android:layout_marginTop="265dp"
android:layout_width="fill_parent"
android:layout_height="20sp"
android:textColor="#ffff00"
android:gravity="center"
android:text="@string/page1_label" />

</RelativeLayout>

</RelativeLayout>
[/HIGH]

I place the code Steve gave in the OnCreate, upload the app to my tablet and it fails to run. Investigating shows that it cannot find the VideoView vidView because it first setContentView to activity_main; which sets out the SwipeView definitions:

[HIGH]<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
tools:context=".MainActivity" />[/HIGH]

I did consider moving this to each of the 4 Fragment pages but this always generates an error and would defeat the idea of being a fragment anyway; wouldn't it?
 
Instead of creating four fragments, you could create four instances of one fragment.

For example:

[HIGH=JAVA]
public static myFragment newInstance(int position) {
MyFragment fragment = new MyFragment();

Bundle arguments = new Bundle();
arguments.putInt("num", position);
fragment.setArguments(arguments);

return fragment;
}
[/HIGH]

Then when you're fragment loads a position:

[HIGH=JAVA]
int num = getArguments().getInt("num");
Uri myVideoUri;

switch(num)
{
case 1:
myVideoUri = android.net.Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video1);
break;

case 2:
myVideoUri = android.net.Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video2);
break;

case 3:
myVideoUri = android.net.Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video3);
break;

case 4:
myVideoUri = android.net.Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video4);
break;

}

... the rest of the video code here
[/HIGH]

Let me know how you make out.
 
I think I am being very thick at the moment, so sorry for this. I first looked at your response and it made sense but as I started to compare this to my own code so far I began to lose where anything went and how anything works!

I think this is down to time pressures and so I have not yet taken the time to work out precisely when things happen and why. However, time is less now than it was and so I will need to ask my rather stupid questions; sorry.

First question: where do I place this first snippet of code you gave? I assume I can lose my [HIGH]public Fragment getItem(int position)[/HIGH] and my individual fragment classes with this single instance, but is this simply a case of replace? How does this call my one fragment XML document?

Second question: When it DOES call my XML fragment document, how would this be presented? Currently my textViews are drawing their text from STRINGS.XML through the use of page1_1, page2_1, page2_2 and so on. How would I maintain this through a single fragment instance?

Third question: do I simply place the Switch statement within the onCreate class?

I am sorry for the barrage of questions but I am learning (and unlearning) with your help so thank you.

To (hopefully) assist, my MainActivity.java currently reads:

[HIGH]package com.garynewport.c5_advert;

import java.util.Locale;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.VideoView;

public class MainActivity extends FragmentActivity implements ActionBar.TabListener
{

public static class Fragment1 extends Fragment
{

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

SectionsPagerAdapter mSectionsPagerAdapter;

ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);


VideoView videoView = (VideoView)findViewById(R.id.vidView);
videoView.setVideoPath("/res/raw/vid1.mp4");
videoView.start();

// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
{
@Override
public void onPageSelected(int position)
{actionBar.setSelectedNavigationItem(position);}
});

// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++)
{
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
{
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {}

/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter
{

public SectionsPagerAdapter(FragmentManager fm) {super(fm);}

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

@Override
public int getCount() {return 4;}

@Override
public CharSequence getPageTitle(int position)
{
Locale l = Locale.getDefault();
switch (position)
{
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
case 3:
return getString(R.string.title_section4).toUpperCase(l);
}
return null;
}
}
}
[/HIGH]
 
If you use separate fragments like you are just put the video code in the onCreate of the fragment. I was just showing you a more manageable way to handle fragments.
 
This is where I am having the problem though, I cannot find out where the onCreate is where I can put the video instructions.

I assumed it was in the inidvidual FragmentX classes but I get a warning about putting non-static commands within static classes and any changes simply create greater problems.

I cannot find out where to put this code at all!

[HIGH]package com.garynewport.sinclair_c5;

import java.util.Locale;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {

public static class Fragment1 extends Fragment
{

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

/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;

/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);

// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});

// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}

/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

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

@Override
public int getCount() {
// Show 3 total pages.
return 4;
}

@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
case 3:
return getString(R.string.title_section4).toUpperCase(l);
}
return null;
}
}

/**
* 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]

You may notice that the package name has changed; this is the reason for the long delay in replying since I had a problem that I could not solve in the earlier package that caused it to fail to run, so I rebuilt it from scratch. Good to know I can though. :)
 
steves way is a quicker cleaner way to do it, but a little tougher.

the way you are doing it, you need to add onCreate in each fragments class.

does it play video for you with that current code? it looks like it would load up the same video for each fragment, but i haven't setup a clone in eclipse to check it out.

EDIT-- you posted right as i was typing this, so it can be ignored for now.
? is it

Cannot make static reference to non-static method "findViewById'(int) from the type Activity
?
 
I pasted the wrong part so here's the right part for this one.

Again, thanks Steve (I have rudely assumed I can use your shortened name, I hope this is okay).

Here I added to my fragment code as follows:

[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);
final VideoView videoView = (VideoView) rootView.findViewById(R.id.vidView);
ViewGroup vidViewContainer = (ViewGroup) rootView.findViewById(R.id.vidViewContainer);
videoView.setMediaController(null);

switch (getArguments().getInt(ARG_SECTION_NUMBER))
{
case 1:
TextView.setText(R.string.P1);
LabelView.setText(R.string.P1_label);
videoView.setVideoPath("android.resource://com.garynewport.sinclair_c5/" + R.raw.vid1);
break;
case 2:
TextView.setText(R.string.P2);
LabelView.setText(R.string.P2_label);
videoView.setVideoPath("android.resource://com.garynewport.sinclair_c5/" + R.raw.vid2);
break;
case 3:
TextView.setText(R.string.P3);
LabelView.setText(R.string.P3_label);
videoView.setVideoPath("android.resource://com.garynewport.sinclair_c5/" + R.raw.vid3);
break;
case 4:
TextView.setText(R.string.P4);
LabelView.setText(R.string.P4_label);
videoView.setVideoPath("android.resource://com.garynewport.sinclair_c5/" + R.raw.vid4);
break;
}

vidViewContainer.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{if (videoView.isPlaying()) {videoView.pause();} else {videoView.start();}}
});

return rootView;
}
}
[/HIGH]

I can now tap to play and then tap to pause. I only now need to stop the video when swiped but this is a MAJOR step forward!

Again, thanks everyone and particularly Steve for his help.
 
I have to run so I didn't get a chance to review your code but here is a quick setup I created for you to check out. This is using thirdparty lib: https://github.com/astuetz/PagerSlidingTabStrip
...but you can use really any tab setup.. the point is in the fragment and activity.

Activity:
[HIGH=JAVA]
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;
import com.astuetz.viewpager.extensions.PagerSlidingTabStrip;

public class MainActivity extends FragmentActivity {

static String packageName;
private SectionsPagerAdapter pagerAdapter;
private ViewPager pager;
protected PagerSlidingTabStrip tabs;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Setup the pager with an adapter and defaults.
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new SectionsPagerAdapter(getSupportFragmentManager()));
pagerAdapter = ((SectionsPagerAdapter) pager.getAdapter());

packageName = getPackageName();
getWindow().setFormat(android.graphics.PixelFormat.TRANSLUCENT);

tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);

// Bind the tab strip to the adapter.
tabs.setViewPager(pager);
// Custom hack for drop-down menu on tab.
// Used to pause image loader during tab change.
tabs.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i2) {
}

@Override
public void onPageSelected(int i) {

//MyVideoFragment currentFragment = ((MyVideoFragment)(pagerAdapter).mCurrentPrimaryItem);
//currentFragment.getVideoView().start();

}

@Override
public void onPageScrollStateChanged(int state) {
}
});

}


protected static String[] CONTENT;

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
public Fragment mCurrentPrimaryItem = null;

public MyVideoFragment[] mFragments;
private int mCount;

public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
CONTENT = new String[]{"Video One", "Video Two", "Video Three", "Video Four"};
mCount = CONTENT.length;
mFragments = new MyVideoFragment[mCount];
}

@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
Fragment fragment = (Fragment)object;
if (fragment != mCurrentPrimaryItem) {
if (mCurrentPrimaryItem != null) {
mCurrentPrimaryItem.setMenuVisibility(false);
}
if (fragment != null) {
fragment.setMenuVisibility(true);
}
mCurrentPrimaryItem = fragment;
}
}

@Override
public Fragment getItem(int position) {

if (mFragments[position] == null) {
mFragments[position] = MyVideoFragment.newInstance(position);
}
return mFragments[position];
}

@Override
public int getCount() {
return mCount;
}

@Override
public CharSequence getPageTitle(int position) {
return CONTENT[position % CONTENT.length];
}

}
[/HIGH]

Separate Fragment:
[HIGH=JAVA]
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.VideoView;

public final class MyVideoFragment extends Fragment {

private int num;
private VideoView videoView;
private ViewGroup vidViewContainer;
private Uri myVideoUri = null;

public static MyVideoFragment newInstance(int position) {
MyVideoFragment fragment = new MyVideoFragment();

Bundle arguments = new Bundle();
arguments.putInt("num", position);
fragment.setArguments(arguments);

return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_video, container, false);
num = getArguments().getInt("num");
videoView = (VideoView) root.findViewById(R.id.vidView);
vidViewContainer = (ViewGroup) root.findViewById(R.id.vidViewContainer);
setupVideo();
return root;
}

public VideoView getVideoView() {
return videoView;
}

public void setupVideo() {

switch(num)
{
case 0:
myVideoUri = Uri.parse("android.resource://" + getActivity().getPackageName() + "/" + R.raw.test);
break;

case 1:
myVideoUri = Uri.parse("android.resource://" + getActivity().getPackageName() + "/" + R.raw.test);
break;

case 2:
myVideoUri = Uri.parse("android.resource://" + getActivity().getPackageName() + "/" + R.raw.test);
break;

case 3:
myVideoUri = Uri.parse("android.resource://" + getActivity().getPackageName() + "/" + R.raw.test);
break;

}

videoView.setMediaController(new android.widget.MediaController(getActivity()));
videoView.setVideoURI(myVideoUri);

// Needed to prevent controls from displaying and taking control.
videoView.setMediaController(null);

// Invisible onclick for the videos container to now handle controls.
vidViewContainer.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (videoView.isPlaying()) {
videoView.pause();
} else {
videoView.start();
}
}
});

}

}
[/HIGH]

Layouts:
[HIGH=XML]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >

<com.astuetz.viewpager.extensions.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dip"
android:layout_alignParentTop="true"/>


<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tabs"/>

</RelativeLayout>
[/HIGH]

[HIGH=XML]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textColor="#ffffff" />

<RelativeLayout
android:id="@+id/vidViewContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">

<VideoView
android:id="@+id/vidView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentTop="true"
android:clickable="false"
android:layout_marginTop="30dp"/>

</RelativeLayout>

</RelativeLayout>
[/HIGH]
 
A quick glance shows I was not far off! :)

I will takje a longer look once I get past this time constraint but thank you.

The one issue I still have is that the video does not stop when the view is changed; so I could potentially have 4 videos playing.

I'll work on this but need now to also focus on locking this tablet down.

Again, thank you so much!
 
Back
Top Bottom