1

I am fairly new to the Android SDK, but I have got stuck in today and managed to produce an app with three tabs.

The only problem is that my Map Fragment is covering the Action Bar and the tabs. I'm sure this is due to something fairly noob-ish but I can't figure it out. Any help would be much appreciated!

The screenshots below show my swiping to the left

enter image description here

MapFragment.java:

public class MapFragment extends Fragment {
    private MapView mMapView;
    private GoogleMap googleMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // inflate and return the layout
        View v = inflater.inflate(R.layout.fragment_location_info, container, false);
        mMapView = (MapView) v.findViewById(R.id.mapView);
        mMapView.onCreate(savedInstanceState);

        mMapView.onResume();// needed to get the map to display immediately

        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }

        googleMap = mMapView.getMap();
        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

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

    @Override
    public void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }
}

fragment_location_info.xml:

<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"
    tools:context="uk.findmybus.findmybus.MapFragment">

    <com.google.android.gms.maps.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

MainActivity.java:

public class MainActivity extends AppCompatActivity {
    public static FragmentManager fragmentManager;

    private TabLayout tabLayout;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        fragmentManager = getSupportFragmentManager();

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(fragmentManager);
        adapter.addFragment(new UpdatesFragment(), "Updates");
        adapter.addFragment(new MapFragment(), "Map");
        adapter.addFragment(new TimetablesFragment(), "Timetables");
        viewPager.setAdapter(adapter);
    }

    @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_map, 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
}

activity_map.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabGravity="fill" />
    </android.support.design.widget.AppBarLayout>

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

</android.support.design.widget.CoordinatorLayout>
jskidd3
  • 4,609
  • 15
  • 63
  • 127
  • @QuantumTiger Yes, but after further investigation and re-asking the question in more detail: http://stackoverflow.com/questions/33982527/stop-fragments-going-over-actionbar-in-viewpager – jskidd3 Dec 04 '15 at 22:04
  • Ah, cool. That is a really helpful explanation. Will have to try that myself. Glad you got it fixed. – QuantumTiger Dec 04 '15 at 22:20
  • @QuantumTiger Thanks very much, have given your answer an up vote for effort and time! – jskidd3 Dec 04 '15 at 22:21

1 Answers1

1

I think the problem is that the ViewPager is occupying the full system window

Try changing layout_height of ViewPager to 0dp and/or enclose everything inside the CoordinatorLayout in a vertical linear layout. And add android:layout_weight="1"

If you are using AndroidStudio the layout preview pane is really helpful for showing the outline boundaries of each element in your layout.

QuantumTiger
  • 970
  • 1
  • 10
  • 22
  • Woo! So all the tabs show as you'd expect now, but the map is invisible. Could you please help me get around this? – jskidd3 Nov 28 '15 at 20:21
  • Also add layout_weight=1 to the ViewPager? – QuantumTiger Nov 28 '15 at 20:27
  • Ok so I have set the width to `match_parent`, height to `0dp` and `layout_weight` to 1 on the ViewPager and the map remains invisible. – jskidd3 Nov 28 '15 at 20:29
  • Did you try the linearLayout? – QuantumTiger Nov 28 '15 at 20:31
  • So do I basically have to put the ViewPager inside of a LinearLayout tag? Sorry I am a noob :) – jskidd3 Nov 28 '15 at 20:34
  • You shouldn't have to, no. But I've yet to find the ideal way of getting the viewpager to be the right height. See my answer to the following for a layout I know should work. http://stackoverflow.com/questions/33940835/coordinatorlayout-leaves-empty-bottom-space/33940960#33940960 – QuantumTiger Nov 28 '15 at 20:37