image from blog.smartthings.com


Just earlier working on an app, I realized I needed to display tabbed information to users in a dialog box. This is quite different from the usual Tabbed Activity.

Dependency

The first step is to make sure you have "'com.android.support:design" in your build.gradle file.

Layouts

Next is to create the layout xml files for the custom dialog and your fragment. Your custom dialog may look like this:

dialog_sample.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:orientation="vertical" android:layout_width="match_parent"  
   android:layout_height="match_parent">  
   <android.support.design.widget.TabLayout  
     android:id="@+id/tabLayout"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     />  
   <android.support.v4.view.ViewPager  
     android:id="@+id/masterViewPager"  
     android:layout_width="match_parent"  
     android:layout_height="match_parent"  
     />  
 </LinearLayout>  

And for the purpose of this tutorial, we will use a minimal Fragment, like the one indicated below:

fragment_sample.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:orientation="vertical" android:layout_width="match_parent"  
   android:layout_height="match_parent">  
   <TextView  
     android:id="@+id/textView"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content" />  
 </LinearLayout>  

Implementation

Next up is, building the java classes for the dialog, the fragment adapter, and the fragments.

Here is a sample implementation of the viewpager's adapter:

CustomAdapter.java
 public class CustomAdapter extends FragmentPagerAdapter {  
   List<Fragment> mFragmentCollection = new ArrayList<>();  
   List<String> mTitleCollection = new ArrayList<>();  
   public CustomAdapter(FragmentManager fm) {  
     super(fm);  
   }  
   public void addFragment(String title, Fragment fragment)  
   {  
     mTitleCollection.add(title);  
     mFragmentCollection.add(fragment);  
   }  
   //Needed for  
   @Override  
   public CharSequence getPageTitle(int position) {  
     return mTitleCollection.get(position);  
   }  
   @Override  
   public Fragment getItem(int position) {  
     return mFragmentCollection.get(position);  
   }  
   @Override  
   public int getCount() {  
     return mFragmentCollection.size();  
   }  
 }  

Note: you must override getTitle for the Tabs to have proper names.



Below is also a sample fragment for illustration purposes, we'll keep it simple for now:

CustomFragment.java

 public class CustomFragment extends Fragment {  
   private String mText = "";  
   public static CustomFragment createInstance(String txt)  
   {  
     CustomFragment fragment = new CustomFragment();  
     fragment.mText = txt;  
     return fragment;  
   }  
   @Override  
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
     View v = inflater.inflate(R.layout.fragment_sample,container,false);  
     ((TextView) v.findViewById(R.id.textView)).setText(mText);  
     return v;  
   }  
 }  


Go ahead and create your dialog's class:

TabbedDialog.java
  
 public class TabbedDialog extends DialogFragment {  
   TabLayout tabLayout;  
   ViewPager viewPager;  
   @Nullable  
   @Override  
   public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {  
     View rootview = inflater.inflate(R.layout.dialog_sample,container,false);  
     tabLayout = (TabLayout) rootview.findViewById(R.id.tabLayout);  
     viewPager = (ViewPager) rootview.findViewById(R.id.masterViewPager);  
     CustomAdapter adapter = new CustomAdapter(getChildFragmentManager());  
     adapter.addFragment("Boy",CustomFragment.createInstance("John"));  
     adapter.addFragment("Girl",CustomFragment.createInstance("Stacy"));  
     adapter.addFragment("Robot",CustomFragment.createInstance("Aeon"));  
     viewPager.setAdapter(adapter);  
     tabLayout.setupWithViewPager(viewPager);  
     return rootview;  
   }  
 }  


And there you have it, it displays similarly to what's shown below:

Full source can be found here: https://github.com/kunmi/BlogTutorials/tree/master/TabbedDialog




You may have experienced many times when you move between countries; your google language is determined from the IP of where you're browsing from. Here is how you can enforce your language of choice without having to use translate feature of chrome.

If one the google site you're on, there's no language switch in site, modify the url to and add the quey hl=(2 letter language name) e.g hl=en. If there are queries already before it, then &hl=en, if it's first, ?hl=en.

e.g
https://console.developers.google.com/apis/dashboard?hl=fr
https://www.google.com/adsense/signup?subid=ww-ww-org-signup&signup=rs&hl=en

You may have experienced this problem a couple of time, where your SD Card decides to go haywire on you and the only available option on your device is Erase Everything (Format). Here are some simple fixes that may help.

Internal SD Card Reader Available: 

Try with SD Lock off:

if your laptop has an internal SD Reader, there exist two quick fixes that usually works.
insert you SD-Card in an SD-Card Adapter, and see if Windows detects it, if Windows does. Follow the following steps.


The first step is to note the assigned drive letter; These can be seen from This PC / My Computer / Disk Management.

  • Go to start, click on run (alternatively Window key + R) enter cmd.
    • or type in Start, Command Prompt.
  • Type chkdsk /f  /r /x driverletter: 
    • i.e drive letter is the letter assigned to your drive. e.g if the letter e is assigned to your drive, then sample input may look like this:      chkdsk /f /r /x e:     
if your card is not detected or after detection, you cannot access it or get these error messages:
Cannot open volume for direct access 
The type of the file system is RAW.
CHKDSK is not available for RAW drives.
The type of the file system is FAT32.
Error in IOCTL call.
Then the below solution may help out.

Try with Write Lock on

Sometimes, trying your sdcard with write lock on does the magic, although you don't get to run a checkdisk on your sdcard, but you get the opportunity to backup your files before attempting the format option on your phone. (As at time of writing, I just had similar problem and this worked).