Android Tutorial

Software Setup and Configuration

Android Studio

File Structure

Components

Core Topics

Layout

View

Button

Intent and Intent Filters

Toast

RecyclerView

Fragments

Adapters

Other UI Component

Image Loading Libraries

Date and Time

Material Design

Bars

Working with Google Maps

Chart

Animation

Database

Advance Android

Jetpack

Architecture

App Publish

App Monetization

How to create Swipe Navigation in an Android App

Swipe navigation, commonly known as swipe-based tab navigation, is typically implemented using a combination of ViewPager (or ViewPager2 in more recent Android versions) and TabLayout. Here's a step-by-step guide on how to create swipe navigation in an Android app:

Using ViewPager2 and TabLayout:

  1. Add Dependencies:

    First, make sure you have the required dependencies in your build.gradle (Module: app):

    implementation 'androidx.viewpager2:viewpager2:1.0.0'
    implementation 'com.google.android.material:material:1.4.0'
    
  2. Define the Layout:

    In your XML layout (e.g., activity_main.xml), define the TabLayout and ViewPager2:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabGravity="fill"/>
    
        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    
    </LinearLayout>
    
  3. Setup the Adapter:

    Create an adapter for the ViewPager2:

    public class ViewPagerAdapter extends FragmentStateAdapter {
    
        public ViewPagerAdapter(@NonNull FragmentActivity fragmentActivity) {
            super(fragmentActivity);
        }
    
        @NonNull
        @Override
        public Fragment createFragment(int position) {
            switch (position) {
                case 0:
                    return new FirstFragment();
                case 1:
                    return new SecondFragment();
                // add more cases for more tabs
                default:
                    return new FirstFragment();
            }
        }
    
        @Override
        public int getItemCount() {
            return 2;  // adjust this value based on the number of tabs you want
        }
    }
    
  4. Initialize and Set Up:

    In your main activity (e.g., MainActivity.java), set up the ViewPager2 and TabLayout:

    ViewPager2 viewPager = findViewById(R.id.viewPager);
    TabLayout tabLayout = findViewById(R.id.tabLayout);
    
    ViewPagerAdapter adapter = new ViewPagerAdapter(this);
    viewPager.setAdapter(adapter);
    
    // Connect the TabLayout and the ViewPager2 together
    TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(
        tabLayout, viewPager, true, (tab, position) -> {
            switch (position) {
                case 0:
                    tab.setText("First Tab");
                    break;
                case 1:
                    tab.setText("Second Tab");
                    break;
                // and so on for more tabs...
            }
        });
    
    tabLayoutMediator.attach();
    
  5. Create Fragments:

    Ensure you've created FirstFragment and SecondFragment (or whatever fragments you're using) for the content of each tab.

That's it! Now, when you run your app, you'll have a swipe navigation system set up with tabs at the top that respond to swipes left and right. You can extend this by adding more fragments and adjusting the adapter as needed.

  1. Android swipe views example:

    • Add a ViewPager to your layout file (e.g., activity_main.xml).
    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    
  2. Swipe tabs in Android app development:

    • Add tabs to your ViewPager using TabLayout for a better user interface.
    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    
    TabLayout tabLayout = findViewById(R.id.tabLayout);
    tabLayout.setupWithViewPager(viewPager);
    
  3. Code example for swipe navigation in Android:

    • Here's a basic example with a ViewPager and FragmentPagerAdapter:
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ViewPager viewPager = findViewById(R.id.viewPager);
            MyPagerAdapter pagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
            viewPager.setAdapter(pagerAdapter);
    
            TabLayout tabLayout = findViewById(R.id.tabLayout);
            tabLayout.setupWithViewPager(viewPager);
        }
    }
    
    • Create a FragmentPagerAdapter:
    public class MyPagerAdapter extends FragmentPagerAdapter {
    
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }
    
        @NonNull
        @Override
        public Fragment getItem(int position) {
            // Return the appropriate fragment based on the position
            switch (position) {
                case 0:
                    return new Fragment1();
                case 1:
                    return new Fragment2();
                // Add more cases for additional fragments
            }
            return null;
        }
    
        @Override
        public int getCount() {
            // Return the total number of fragments
            return 2;
        }
    
        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            // Return tab titles if using TabLayout
            switch (position) {
                case 0:
                    return "Tab 1";
                case 1:
                    return "Tab 2";
                // Add more cases for additional tabs
            }
            return null;
        }
    }
    
    • Create your fragments (e.g., Fragment1 and Fragment2) and design their layouts accordingly.