I have a single Activity
which hosts multiple fragments through a ViewPager
. In the Activity's onCreate
method I'm using the following code to change the ActionBar
title:
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if(getSupportActionBar() != null) {
getSupportActionBar().setTitle(getResources().getString(R.string.custom_title));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
It works fine, but then I am unable to change it as soon as a Fragment
becomes visible. In order to detect it I've implemented:
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser && getContext() != null) {
((MainActivity) getContext()).editActionBarTitle("new title");
}
}
And here's the definition for editActionBarTitle
:
public void editActionBarTitle(String title) {
if(getSupportActionBar() != null) {
getSupportActionBar().setTitle(title);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
I can see that the method gets called as soon as a fragment becomes visible, but it has no effect on the ActionBar's title. What am I missing in order to edit the title correctly?
EDIT
I've also tried implementing addOnPageChangeListener
in the viewPager
, I can see that the method onPageSelected
gets called but it has no effect on the title, here's an example on MainActivity:
public class MainActivity extends BaseActivity {
private NoScrollViewPager mViewPager;
private SectionsPagerAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Toolbar initialization
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if(getSupportActionBar() != null) {
getSupportActionBar().setTitle("MainActivity");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
// ViewPager initialization
mViewPager = findViewById(R.id.containter);
setupViewPager();
}
private void setupViewPager() {
mAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mAdapter.addFragment(new Fragment1(), "Fragment 1");
mAdapter.addFragment(new Fragment2(), "Fragment 2");
mAdapter.addFragment(new Fragment3(), "Fragment 3");
mViewPager.setOffscreenPageLimit(mAdapter.getCount());
mViewPager.setAdapter(mAdapter);
// viewPager event listener
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }
@Override
public void onPageSelected(int position) {
setTitle("fragment " + position);
if(getSupportActionBar() != null) {
getSupportActionBar().setTitle("fragment " + position);
}
}
@Override
public void onPageScrollStateChanged(int state) { }
});
}
public void setViewPager(int position) { mViewPager.setCurrentItem(position); }
}
This is the custom ViewPager
class:
public class NoScrollViewPager extends ViewPager {
// Disable horizontal scrolling in ViewPager
private boolean isPagingEnabled = false;
public NoScrollViewPager(Context context) {
super(context);
}
public NoScrollViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
@SuppressLint("AndroidLintClickableViewAccessibility")
public boolean onTouchEvent(MotionEvent event) {
return this.isPagingEnabled && super.onTouchEvent(event);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
return this.isPagingEnabled && super.onInterceptTouchEvent(event);
}
public void setPagingEnabled(boolean b) {
this.isPagingEnabled = b;
}
}
And this is the custom Adapter class:
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
private FragmentManager fragmentManager;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
fragmentManager = fm;
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public Fragment getItem(int position) { return mFragmentList.get(position); }
@Override
public int getCount() { return mFragmentList.size(); }
@Override
public int getItemPosition(Object object) { return POSITION_NONE; }
}
Try this in your onCreateView method into fragment. May be it will helps you.
if(getActivity() != null){
getActivity().setTitle("Your title");
}
If you used ViewPager inside a fragment then you can use
@Override
public void setUserVisibleHint(boolean isVisible) {
super.setUserVisibleHint(isVisible);
if (isVisible) {
if(getActivity() != null){
getActivity().setTitle("Your title");
}
}
}
You can also use viewPager.addOnPageChangeListener()
In viewPager.addOnPageChangeListener()
you need to set title in your actionbar
inside onPageSelected()
which returns the current the current selected page in viewpager
SAMPLE CODE
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
if (i == 0) {
getSupportActionBar().setTitle("Fragment One");
} else if (i == 1) {
getSupportActionBar().setTitle("Fragment Two");
} else if (i == 2) {
getSupportActionBar().setTitle("Fragment three");
} else if (i == 3) {
getSupportActionBar().setTitle("Fragment four");
} else if (i == 4) {
getSupportActionBar().setTitle("Fragment Five");
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
here is the working code for me
activity code
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class HomeActivity extends AppCompatActivity {
private NoScrollViewPager mViewPager;
private SectionsPagerAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// Toolbar initialization
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Fragment One");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// ViewPager initialization
mViewPager = findViewById(R.id.containter);
findViewById(R.id.btnOne).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mViewPager.setCurrentItem(0);
}
});
findViewById(R.id.btnTwo).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mViewPager.setCurrentItem(1);
}
});
findViewById(R.id.btnThree).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mViewPager.setCurrentItem(2);
}
});
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
if (i == 0) {
getSupportActionBar().setTitle("Fragment One");
} else if (i == 1) {
getSupportActionBar().setTitle("Fragment Two");
} else if (i == 2) {
getSupportActionBar().setTitle("Fragment three");
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
setupViewPager();
}
private void setupViewPager() {
mAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mAdapter.addFragment(new Fragment1(), "Fragment 1");
mAdapter.addFragment(new Fragment2(), "Fragment 2");
mAdapter.addFragment(new Fragment3(), "Fragment 3");
mViewPager.setOffscreenPageLimit(mAdapter.getCount());
mViewPager.setAdapter(mAdapter);
// viewPager event listener
}
}
Layout
<android.support.design.widget.CoordinatorLayout 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.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal">
<Button
android:id="@+id/btnOne"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Frag 1" />
<Button
android:id="@+id/btnTwo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Frag 2" />
<Button
android:id="@+id/btnThree"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Frag 3" />
</LinearLayout>
<neel.com.demo.NoScrollViewPager
android:id="@+id/containter"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
OUTPUT
https://www.youtube.com/watch?v=ytJogOkj1zQ
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With