What is Swipe View?
Swipe View is one type of navigation in the android app. You can swipe view on WhatsApp, Instagram, etc.
The swipe view is made by
- view pager
- tab layout
To implement this in your won app in the android studio you have to follow below steps-
1. Implement ViewPager and TabLayout in XML Code:
In the activity_xml file on which you want to show Swipe View, add the below code :
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<com.google.android.material.tabs.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabMaxWidth="0dp"
app:tabGravity="fill"
app:tabTextColor="#ffffffff"
app:tabBackground="@android:color/black"/>
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
2. Creating Simple Fragment page adapter:
Now decide the names which will appear in the tab view. Create blank fragments according to the names you decide.
Now create a java file , name it "SimpleFragmentPageAdapter". After creating this, open SimpleFragmentPageAdapter.java and delete all the code and paste the below code:
package //enter your pakage name here//;
import android.content.Context;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
public class SimpleFragmentPageAdapter extends FragmentPagerAdapter {
private final Context mContext;
private String tabTitles[] = new String[] { "item 1",
"item 2", "item 3","item 4" };
public SimpleFragmentPageAdapter(FragmentManager fm,
int behaviorResumeOnlyCurrentFragment, Context mContext) {
super(fm,behaviorResumeOnlyCurrentFragment);
this.mContext = mContext;
}
@Override
public Fragment getItem(int position) {
if (position == 0) {
return new item1Fragment();//change the name with your fragment
} else if (position == 1){
return new item2Fragment();//change the name with your fragment
} else if (position == 2) {
return new item3Fragment();//change the name with your fragment
}else {
return new item4Fragment();//change the name with your fragment
}
}
@Override
public int getCount() {
return 4;
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}
~~~~~~~~~~~~~
chenge item name with the names you have decided.
~~~~~~~~~~~~~~~~~~
3. Main Java file:
Now go to your activity java file on which the Swipe view will appear. and add the below code to your "onCreate" method:
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
// Create an adapter that knows which fragment should be shown on each page
SimpleFragmentPageAdapter adapter = new SimpleFragmentPageAdapter
(getSupportFragmentManager(),
FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT,this);
// Set the adapter onto the view pager
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
If everything will go right,
you will see the swipe view in your android app.
0 Comments