I am having an issue where my ViewPager text does not display when I start up my application. However, the texts do end up showing when I swipe the tab. I am not sure what the problem is and I would like the text to appear as soon as my application starts. Any help would be appreciated! thanks.
Countries Activity(MAIN ACTIVITY)
package myapps.countryapp;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import static myapps.countryapp.R.id.pager;
public class CountriesActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_countries);
ViewPager viewPager = (ViewPager) findViewById(pager);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
}
public class MyAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 2;
//Create tab tiles.
private String tabTiles[] = new String[] {"Countries", "Map"};
Context context;
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if(position == 0){
Fragment fragmentList = new Countries();
return fragmentList;
}else {
Countries fragmentList = new Countries();
return fragmentList;
}
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
return tabTiles[position];
}
}
}//end of class
Countries.java(fragment file)
package myapps.countryapp;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;
/**
* A simple {@link Fragment} subclass.
*/
public class Countries extends Fragment {
public Countries() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_countries, container, false);
return view;
}
}
activity_countries(MAIN XML FILE)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="fill_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="myapps.countryapp.CountriesActivity">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.PagerTabStrip
android:layout_width="match_parent"
android:layout_height = "40dp"
android:paddingTop = "4dp"
android:paddingBottom = "10dp"
android:layout_gravity = "top"
android:id = "@+id/pagerStrip">
</android.support.v4.view.PagerTabStrip>
</android.support.v4.view.ViewPager>
fragment_countries.xml
<FrameLayout 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="myapps.countryapp.Countries">
<!-- TODO: Update blank fragment layout -->
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/MyListView">
</ListView>
</FrameLayout>