pehr.ans
pehr.ans

Reputation: 109

How to remove main layout from being in every fragment

In my MainActivity, I have "setContentView(R.layout.activity_main)". This activity_main layout contains a VideoView.

activity_main is not used in any fragment; when I switch to fragments I can't see the video (which is good), but if I tap the screen, then the video options (play,pause,etc.) pop up (not good). I'm assuming that main_activity is in the underneath every fragment because of "setContentView(R.layout.activity_main)"?

I need the video to be displayed in just a single fragment without affecting any other fragments. But, if I try to remove the VideoView from activity_main and instead identify it in a fragment layout (i.e. fragment_one), I get a NullPointerException. Again, I think this is because of "setContentView(R.layout.activity_main)". I'm thinking that views referenced in the java code must be identified (id="@+id/video_view") in activity_main?

The solution to this problem may be very simple, but I just can't seem to find any information that addresses it. I am certainly a novice, so please bear with me.

Here is my MainActivity:

public class MainActivity extends AppCompatActivity {
    //used in VideoView
    private VideoView myVideoView;
    private int position = 0;
    private ProgressDialog progressDialog;
    private MediaController mediaControls;

String[] LabelMenu = {"Home","Flight Tracker"};
@Override
//Allows the use of icons in overflow-dropdown
protected boolean onPrepareOptionsPanel(View view, Menu menu) {
    if (menu != null) {
        if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
            try {
                Method m = menu.getClass().getDeclaredMethod(
                        "setOptionalIconsVisible", Boolean.TYPE);
                m.setAccessible(true);
                m.invoke(menu, true);
            } catch (Exception e) {
                Log.e(getClass().getSimpleName(), "onMenuOpened...unable to set icons for overflow menu", e);
            }
        }
    }
    return super.onPrepareOptionsPanel(view, menu);
}
//***START OF GOTOURL REFERENCES***
public void goToUS (View view) {
    goToUrl ( "http://10.100.1.200/imsg_weather_radar/weather_radar_united_states.html");
}

public void goToSW (View view) {
    goToUrl( "http://10.100.1.200/imsg_weather_radar/weather_radar_southwest_us.html");
}

public void goToSE (View view) {
    goToUrl("http://10.100.1.200/imsg_weather_radar/weather_radar_southeast_us.html");
}

public void goToNW (View view) {
    goToUrl("http://10.100.1.200/imsg_weather_radar/weather_radar_northwest_us.html");
}

public void goToWP (View view) {
    goToUrl("http://10.100.1.200/imsg_weather_radar/weather_radar_west_pacific.html");
}

public void goToSP (View view) {
    goToUrl("http://10.100.1.200/imsg_weather_radar/weather_radar_south_pacific.html");
}

public void goToCC (View view) {
    goToUrl("http://10.100.1.200/imsg_weather_radar/weather_radar_carolina_coast.html");
}

public void goToWA (View view) {
    goToUrl("http://10.100.1.200/imsg_weather_radar/weather_radar_western_atlantic.html");
}

public void goToEU (View view) {
    goToUrl("http://10.100.1.200/imsg_weather_radar/weather_radar_eastern_us.html");
}

public void goToEGF (View view) {
    goToUrl("http://10.100.1.200/imsg_weather_radar/weather_radar_east_gulf_florida.html");
}

public void goToGM (View view) {
    goToUrl("http://10.100.1.200/imsg_weather_radar/weather_radar_gulf_of_mexico.html");
}

public void goToWGT (View view) {
    goToUrl("http://10.100.1.200/imsg_weather_radar/weather_radar_west_gulf_texas.html");
}

public void goToCI (View view) {
    goToUrl("http://10.100.1.200/imsg_weather_radar/weather_radar_caribbean_islands.html");
}

public void goToMX (View view) {
    goToUrl("http://10.100.1.200/imsg_weather_radar/weather_radar_mexico.html");
}

public void goToCA (View view) {
    goToUrl("http://10.100.1.200/imsg_weather_radar/weather_radar_central_america.html");
}

//***END OF GOTOURL REFERENCES***
private void goToUrl (String url) {
    Uri uriUrl = Uri.parse(url);
    Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
    startActivity(launchBrowser);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set the main layout of activity
    setContentView(R.layout.activity_main);
    //set the media controller buttons

    if (mediaControls == null) {
            mediaControls = new MediaController(MainActivity.this);
        }
        //initialize the VideoView
        myVideoView = (VideoView) findViewById(R.id.video_view);
        // create a progress bar while the video file is loading
        progressDialog = new ProgressDialog(MainActivity.this);
        // set a title for the progress bar
        progressDialog.setTitle("IAWS");
        // set a message for the progress bar
        progressDialog.setMessage("Loading...");
        //set the progress bar not cancelable on users' touch
        progressDialog.setCancelable(false);
        // show the progress bar
        progressDialog.show();

        try {
            //set the media controller in the VideoView
            myVideoView.setMediaController(mediaControls);
            //set the uri of the video to be played
            myVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.iaws));
        } catch (Exception e) {
            if(e.getMessage()!=null) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
    }
    myVideoView.requestFocus();
    //setOnPreparedListener in order to know when the video file is ready for playback
    myVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        public void onPrepared(MediaPlayer mediaPlayer) {
            // close the progress bar and play the video
            progressDialog.dismiss();
            //if we have a position on savedInstanceState, the video playback should start from here
            myVideoView.seekTo(position);
            if (position == 0) {
                //remove comments if you want video to start automatically
                //myVideoView.start();
            } else {
                //coming from a resumed activity, video playback will be paused
                myVideoView.pause();
            }
        }
    });

    final ActionBar actionBar = getSupportActionBar();
    // Specify that a dropdown list should be displayed in the action bar.
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    // Hide the title
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayUseLogoEnabled(false);
    //Adding an ImageView to Action Bar
    actionBar.setDisplayOptions(actionBar.getDisplayOptions()
            | ActionBar.DISPLAY_SHOW_CUSTOM);
    ImageView imageView = new ImageView(actionBar.getThemedContext());
    imageView.setScaleType(ImageView.ScaleType.CENTER);
    imageView.setImageResource(R.drawable.imsg_blue_logo_rsz);
    ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
            ActionBar.LayoutParams.WRAP_CONTENT,
            ActionBar.LayoutParams.WRAP_CONTENT, Gravity.RIGHT
            | Gravity.CENTER_VERTICAL);
    layoutParams.rightMargin = 40;
    layoutParams.bottomMargin=10;
    imageView.setLayoutParams(layoutParams);
    actionBar.setCustomView(imageView);
    // Specify a SpinnerAdapter to populate the dropdown list.
    ArrayAdapter<String> spinnerMenu = new ArrayAdapter<String>(actionBar.getThemedContext(), android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.labelMenu));
    //2nd Spinner dropdown
    /**
    Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
    mySpinner.setAdapter(new MyCustomAdapter(MainActivity.this, R.layout.row, LabelMenu));
     **/
    actionBar.setListNavigationCallbacks(spinnerMenu,
            // Provide a listener to be called when an item is selected.
            new ActionBar.OnNavigationListener() {
                public boolean onNavigationItemSelected(int position, long id) {
                    // Take action here, e.g. switching to the
                    // corresponding fragment.
                    FragmentTransaction tx = getFragmentManager().beginTransaction();
                    switch (position) {
                        case 0:
                            tx.replace(android.R.id.content, new FirstFragment());
                            break;
                        case 1:
                            tx.replace(android.R.id.content, new SecondFragment());
                            break;
                        case 2:
                            tx.replace(android.R.id.content, new ThirdFragment());
                            break;
                        case 3:
                            tx.replace(android.R.id.content, new FourthFragment());
                            break;
                        case 4:
                            tx.replace(android.R.id.content, new FifthFragment());
                            break;
                        default:
                            break;
                    }
                    tx.commit();
                    return true;
                }
            });
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    // Use onSaveInstanceState in order to store the video playback position for orientation change
    savedInstanceState.putInt("Position", myVideoView.getCurrentPosition());
    myVideoView.pause();
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    // Use onRestoreInstanceState in order to play the video playback from the stored position
    position = savedInstanceState.getInt("Position");
    myVideoView.seekTo(position);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
    super.onOptionsItemSelected(item);
    switch(item.getItemId()){
        case R.id.about:
            aboutMenuItem();
            break;
        case R.id.settings:
            settingsMenuItem();
            break;
        case R.id.search:
            searchMenuItem();
            break;
    }
    return true;
}
//Configure "about" function
private void aboutMenuItem(){
    new AlertDialog.Builder(this)
            .setTitle("App & Copyright Info")
            .setMessage("Version 1.0" + "\n" + "Property of I.M. Systems Group, Inc.")
            .setNeutralButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            }).show();{

    }
}
//Configure "settings" function
private void settingsMenuItem(){
    new AlertDialog.Builder(this)
            .setTitle("Settings")
            .setMessage("Language: English" + "\n" + "Degrees Fahrenheit Selected" + "\n" +
                    "Show Airports" + "\n" + "Speed: KM/H" )
            .setNeutralButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            }).show();{

    }
}
//Configure "search" function
private void searchMenuItem(){
    new AlertDialog.Builder(this)
            .setTitle("Search")
            .setMessage("This is an about AlertDialog")
            .setNeutralButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            }).show();{

    }

}

}

I'd like this fragment to display the video exclusively. As it is now, the fragments will display their designated layouts just fine, but if you tap on the screen in any layout, the video options from main_activity's VideoView will pop up (that's bad). They're all pretty much coded the same way; they're only different in that they reference different layouts:

/**
 * Created by ansaripours on 8/19/2015.
 */
public class FirstFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.fragment_one, container, false);
        return (FrameLayout) inflater.inflate(R.layout.fragment_one, container, false);
    }

}

Here's my main_activity.xml. It seems the ViewVideo has to be identified here, because it is referenced in setContentView, otherwise I get a NullPointerException:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:background="#E1F4FF"
    android:paddingTop="?android:attr/actionBarSize"
    tools:context=".MainActivity">

    <!-- implementing swipeable pages
    <edu.dartmouth.cs.actiontabs.view.SlidingTabLayout
        android:id="@+id/tab"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/tab"/>
        -->
    <!--
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:padding="100"/>-->
    <!--
        <Spinner
            android:id="@+id/spinner"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" /> -->
    <!--
        <ImageView
            android:src="@drawable/rsz_sampleweather"
            android:id="@+id/cover_view"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="centerCrop"
         />
         -->
    <VideoView
        android:id="@+id/video_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"/>

</FrameLayout>

Finally, here's the layout that FirstFragment class references. It's literally the same as main_activity.xml (without the comments). However, like I said, if I remove the VideoView from main_activity.xml, I get a NullPointerException.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:background="#E1F4FF"
    android:paddingTop="?android:attr/actionBarSize"
    tools:context=".MainActivity">

    <VideoView
        android:id="@+id/video_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"/>

</FrameLayout>

Feel free to ask any questions. Any insight would be much appreciated.

Upvotes: 0

Views: 463

Answers (1)

Frank D.
Frank D.

Reputation: 1306

First of all, I recommend that you read the Android developer page on how to use Fragments: http://developer.android.com/guide/components/fragments.html.

You should be able to define a container for a fragment in your activity_main.xml and create a separate Fragment that holds your VideoView, which can be replaced by another Fragment using a FragmentTransaction.

Upvotes: 1

Related Questions