I'm working on an Android Studio app and I would like to hava an indicator of in which fragment user is. There is 4 pages, each one with some fragments and I would like to highlight the fragment where user is (the yellow bar)
Actually, the yellow bar don't move when fragment change. Is it possible to make changement automatic or have I to programm the bar for each fragment ? I don't know if it can help, but I use androidx and ViewPager2
public void setUp(final FormActivity context, final Toolbar toolbar, final Spinner spinner, final DrawerLayout drawerLayout, final ViewPager2 pager, final SlidingTabLayout tabs, FragmentActivity supportFragmentActivity) {
this.drawerLayout = drawerLayout;
this.pager = pager;
this.tabs = tabs;
this.progressBar = view.findViewById(R.id.progressBar);
In the .xml file :
<pack.myrhs.views.NVHeaderTextView
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginEnd="@dimen/drawer_side_margin
android:layout_marginRight="@dimen/drawer_side_margin"
android:layout_marginBottom="@dimen/drawer_divider_margin"
android:text="loading..."
android:textColor="@color/textIconsColor"
android:visibility="gone" />
And that's the NVHeaderTextView class:
public class NVHeaderTextView extends AppCompatTextView {
public NVHeaderTextView(Context context, AttributeSet attrs) {
super(context, attrs);
if(isInEditMode())
return;
String fontName = "fonts/Roboto-Medium.ttf";
Typeface typeface = Typeface.createFromAsset(context.getAssets(), fontName);
setTypeface(typeface);
setTextSize(14);
setTextColor(Color.argb((int) (255 * 1.00), 255, 255, 255));
int flags = getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG
| Paint.ANTI_ALIAS_FLAG;
setPaintFlags(flags);
}
}
UPDATE
I'm using SlidingTabLayout, so the TabLayoutMediator doesn't work (it seems to me). I'm now, using the setViewPager() method:
@Override
protected void onPostExecute(Void params) {
pager.setAdapter(adapter);
tabs.setViewPager(pager);
if (adapter != null)
adapter.notifyDataSetChanged();
progressBar.setVisibility(View.GONE);
drawerLayout.closeDrawers();
cancel(true);
}
Thanks for advance.