@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main=this;
databasesDir = this.getApplicationInfo().dataDir + "/databases/";
AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this, null, 1);
SQLiteDatabase db = admin.getReadableDatabase();
db.close();
if (Build.VERSION.SDK_INT >= 23){
if (checkPermission()){
DownloadFileFromURL d = new DownloadFileFromURL(this);
d.execute();
} else {
requestPermission();
}
}
else
{
DownloadFileFromURL d = new DownloadFileFromURL(this);
d.execute();
}
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = findViewById(R.id.view_pager);
tabLayout = findViewById(R.id.tabs);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
if (toolbar != null) {
setSupportActionBar(toolbar);
}
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(
tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
{
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
List = new String[4];
List[0] = getString(R.string.mapa);
List[1] = getString(R.string.farmacias);
List[2] = getString(R.string.favoritos);
List[3] = getString(R.string.Mas);
}
private boolean checkPermission() {
int result = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
return (result == PackageManager.PERMISSION_GRANTED);
}
private void requestPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Toast.makeText(MainActivity.this, "Write External Storage permission allows us to do store images. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_CODE) {
if (permissions.length == 1 &&
permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE) &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
DownloadFileFromURL d = new DownloadFileFromURL(this);
d.execute();
}
} else {
finish();
}
}
}
public void copyDatabase(){
OutputStream os = null;
FileInputStream is = null;
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+File.separator+"farmacias.db");
if(file.exists()){
try{
is = new FileInputStream(file);
os = new FileOutputStream(databasesDir + "FARMACIAS");
copyFile(os,is);
} catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(os != null){
os.close();
}
if(is != null){
is.close();
}
}catch(IOException e){
}
}
}
}
private void copyFile(OutputStream os, InputStream is) throws IOException{
byte[] buffer = new byte[1024];
int length;
while((length =is.read(buffer))>0){
os.write(buffer,0,length);
}
os.flush();
}
private class MyAdapter extends FragmentPagerAdapter {
MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position)
{
switch (position){
case 0 : return new FragmentMapa();
case 1 : return new FragmentFarmacias();
case 2 : return new FragmentFavoritos();
case 3 : return new FragmentMas();
}
PagerAdapter adapter = null;
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
return null;
}
@Override
public int getCount() {
return int_items;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0 :
TabLayout.Tab FragmentMapa = tabLayout.newTab();
FragmentMapa.setText(List[0]);
FragmentMapa.setIcon(android.R.drawable.ic_menu_mylocation);
tabLayout.addTab(FragmentMapa);
break;
case 1 :
TabLayout.Tab FragmentFarmacias = tabLayout.newTab();
FragmentFarmacias.setText(List[1]);
FragmentFarmacias.setIcon(R.drawable.cofbi);
tabLayout.addTab(FragmentFarmacias);
break;
case 2 :
TabLayout.Tab FragmentFavoritos = tabLayout.newTab();
FragmentFavoritos.setText(List[2]);
FragmentFavoritos.setIcon(android.R.drawable.btn_star_big_off);
tabLayout.addTab(FragmentFavoritos);
break;
case 3:
TabLayout.Tab FragmentMas = tabLayout.newTab();
FragmentMas.setText(List[3]);
FragmentMas.setIcon(android.R.drawable.ic_menu_agenda);
tabLayout.addTab(FragmentMas);
break;
}
return null;
}
}
}
EL ultimo switch duplica las pestañas o coloca pestañas invisibles asociadas al fragment de ese lugar