Android custom tab (CUSTOM TABS) framework for the Java class library
In Android development, tabs (TABS) are one of the frequent interface components, which allows users to switch between different option pages.Android provides the default tab implementation method, but sometimes we may need to customize the style and behavior of the tab page.To achieve this goal, we can use the Custom Tabs framework provided in the Java class library, which allows us to fully control the appearance and behavior of the tab page.
In this article, we will introduce how to use the Java class library to implement the Android custom tab (CUSTOM TABS) framework.
First of all, we need to import the JAVA library dependencies in the Android project.We can add the following code to the dependenncies block in the project's Build.gradle file:
implementation 'androidx.browser:browser:x.y.z'
> Note: Please replace "x.y.z" to the version number you want to use.
Next, we need to register Custom Tabs in AndroidManifset.xml files.Add the following code inside <Application> tags:
<activity android:name="androidx.browser.customtabs.CustomTabsActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:scheme="https" />
</intent-filter>
</activity>
After completing the above configuration, we can start writing the code of the custom tab.We first need to create a new activity (Activity) to carry Custom Tabs.In the layout file of this activity, we can customize the appearance of the tab page, such as color, style and animation.In addition, we need to create a fragment for each option page, and use ViewPager and Tablayout to manage these fragments during the event.
Below is the code of a custom tabs of an example:
public class CustomTabsActivity extends AppCompatActivity {
private CustomTabsIntent mCustomTabsIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_tabs);
// Set the tab page style
mCustomTabsIntent = new CustomTabsIntent.Builder()
.setShowTitle(true)
.setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary))
.setSecondaryToolbarColor(ContextCompat.getColor(this, R.color.colorAccent))
.build();
// Create a tab page fragment
List<Fragment> fragmentList = new ArrayList<>();
fragmentList.add(new HomeFragment());
fragmentList.add(new SettingsFragment());
// Set viewpager
ViewPager viewPager = findViewById(R.id.viewPager);
TabLayout tabLayout = findViewById(R.id.tabLayout);
TabAdapter tabAdapter = new TabAdapter(getSupportFragmentManager(), fragmentList);
viewPager.setAdapter(tabAdapter);
tabLayout.setupWithViewPager(viewPager);
// Processing tab page switching events
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
// Update the title of the tab page
getSupportActionBar().setTitle(tabAdapter.getPageTitle(position));
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
public void openUrl(String url) {
// Open the URL in CUSTOM TABS
mCustomTabsIntent.launchUrl(this, Uri.parse(url));
}
}
In the above code, we first set the style of the tab through the Customtabsintent.Builder, such as the color bar color and the color of the subtitle bar.Next, we created a Fragment list to store fragments of each option page.Then, we use ViewPager and Tablayout to manage these fragments, and handle the tab switching events through ViewPager's AdDONPAGECHANGELISTENER () method.Finally, we defined an openrl () method to open the specified URL in Custom Tabs.
In the above code, we use the Tabadapter class to manage the data. Below is the code of a sample: Tabadapter:
public class TabAdapter extends FragmentPagerAdapter {
private List<Fragment> mFragmentList;
public TabAdapter(FragmentManager fragmentManager, List<Fragment> fragmentList) {
super(fragmentManager);
mFragmentList = fragmentList;
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
// Set the title of the tab page
switch (position) {
case 0:
return "Home";
case 1:
return "Settings";
default:
return null;
}
}
}
In the above code, we inherited the Fragatentpageradapter class and implemented GetItem (), GetCount (), and GetPageTitle () methods to manage the data.In the getPageTitle () method, we set the title of each tab page.
After completing the writing of the above code, we can directly use the OpenUrl () method in the CustomtAbSactivity to open the specified URL in other places.
In this article, we introduced in detail how to use the Java class library to implement the Custom Tabs framework of Android.Not only can we customize the appearance of the tab, but we can also completely control the behavior of the tab.Through the above configuration and code, we can easily implement a custom tab page frame.I hope this article will help you use a custom tab page in Android development.