0

I wanna change hardcoded TITLE to resource from strings.xml

How to do it properly?

    public static final CharSequence TITLE = "Home";
    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return HomeFragment.TITLE;

            case 1:
                return HistoryFragment.TITLE;

            case 2:
                return StatisticsFragment.TITLE;
        }
        return super.getPageTitle(position);
    }
}
    public static final CharSequence TITLE = R.string.title_home;
Lojma
  • 1
  • 2

1 Answers1

0

You don't need to declare PUBLIC STATIC FINAL String variable, because strings stored in resource file is unchangeable and can be accessed from anywhere at runtime.

Use the following code

@Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return context.getResources().getString(R.string.title_home);

            case 1:
                return context.getResources().getString(R.string.title_history);

            case 2:
                return context.getResources().getString(R.string.title_statistics);;
        }
        return super.getPageTitle(position);
    }
ppreetikaa
  • 1,149
  • 2
  • 15
  • 22