2

I was wondering if there was a way I can generate a video to play that's within a raw folder which is dependent on what is selected from two spinner drop-down menus? I'm trying to make a video navigation app for my school that asks the user to select a starting point for their navigation and their desired classroom. So far I've just got one video to successfully play back. Any help would be greatly appreciated!

Here's my main

public class MainActivity extends Activity {

public Spinner start, end;
private Button done;
public TextView start_display, end_display;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    addStartPoint();                                                    //Implements the start point spinner
    addEndPoint();                                                      //Implements the end point spinner
    start = (Spinner) findViewById(R.id.startingPoint_spinner);
    end = (Spinner) findViewById(R.id.endPoint_spinner);
    done=(Button)findViewById(R.id.done);
    done.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {                                   //Go to selected layout when done button is clicked
            Intent i = new Intent(MainActivity.this, SelectingItem.class);
            startActivity(i);
            start_display = (TextView)findViewById(R.id.txtStart);
            end_display = (TextView)findViewById(R.id.txtEnd);
            start = (Spinner) findViewById(R.id.startingPoint_spinner);
        };
    });
}

public void addStartPoint()
{
    Resources res = getResources();                                          //Refers to startpoint array created in strings.xml
    String[] startpoint = res.getStringArray(R.array.startpoint);
    }

public void addEndPoint()
{
    Resources res = getResources();                                         //Refers to endpoint array created in strings.xml
    String[] endpoint = res.getStringArray(R.array.endpoint);
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

SelectingItem Screen

public class SelectingItem extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.selected_layout);
    VideoView videoView = (VideoView)this.findViewById(R.id.VideoView);
    MediaController mediaController=new MediaController(this);
    mediaController.setAnchorView(videoView);
    videoView.setMediaController(mediaController);
    String uri = "android.resource://" + getPackageName() + "/" + R.raw.meb2101;                //This line will need to be changed to an if/else statement for all routes
    videoView.setVideoURI(Uri.parse(uri));
    videoView.start();
}

}

strings.xml

<string name="app_name">FlemNav</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="title_activity_display_navigation">DisplayNavigationActivity</string>
<string name="title_activity_navigation_main">NavigationMain</string>
<string name="choose_starting_point">Choose Starting Point</string>
<string-array name="startpoint">
    <item>Main Entrance</item>
    <item>Electrical Wing</item>
    <item>Steel Centre</item>
    <item>Side Entrance</item>
    <item>Cafeteria</item>
    <item>Side Door</item>
    <item>New Building</item>
</string-array>
<string-array name="endpoint">
    <item>B2101</item>
    <item>B2102</item>
    <item>B2103</item>
    <item>B2104</item>
</string-array>

1 Answers1

0

It looks like you can just add the data you need to the intent you are sending to SelectingItem.

    public class MainActivity extends Activity {
        //.....................

            @Override
            public void onClick(View v) {                                   //Go to selected layout when done button is clicked
                String startText = start.getSelectedItem().toString();
                String endText = end.getSelectedItem().toString();

                Intent i = new Intent(MainActivity.this, SelectingItem.class);
                i.putExtra("start_selection", startText);
                i.putExtra("end_selection", endText);

                startActivity(i);

                /*  Not sure what's going on here... probably not needed
                start_display = (TextView)findViewById(R.id.txtStart);
                end_display = (TextView)findViewById(R.id.txtEnd);
                start = (Spinner) findViewById(R.id.startingPoint_spinner);
                */
            };
        });
    }

And then in your SelectingItem.java:

public class SelectingItem extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.selected_layout);

    Intent intent = this.getIntent();
    String startSelection = intent.getStringExtra("start_selection");
    String endSelection = intent.getStringExtra("end_selection");

    VideoView videoView = (VideoView)this.findViewById(R.id.VideoView);
    MediaController mediaController=new MediaController(this);
    mediaController.setAnchorView(videoView);
    videoView.setMediaController(mediaController);
    String uri = null;

    if ( startSelection.equals("Main Entrance") && endSelection.equals("B2101") ){
         uri = "android.resource://" + getPackageName() + "/" + R.raw.meb2101;                //This line will need to be changed to an if/else statement for all routes
    }
    else if ( startSelection.equals("Main Entrance") && endSelection.equals("B2102") ){
         uri = "android.resource://" + getPackageName() + "/" + R.raw.meb2102; 
    }
    //..... and so on......

    if (uri != null){
       videoView.setVideoURI(Uri.parse(uri));
       videoView.start();
    }
}

For more info on what I've used here, see these posts:

How to get Spinner value?

How do I get extra data from intent on Android?

Community
  • 1
  • 1
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137