Lojma
Lojma

Reputation: 1

static final CharSequence to R.string.xxx how to do it properly?

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;

Upvotes: 0

Views: 231

Answers (1)

ppreetikaa
ppreetikaa

Reputation: 1247

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);
    }

Upvotes: 0

Related Questions