0

I am just starting to make Android apps and I want to properly use the string resources file. I have created a tabbed layout using this guide which works, but I am now trying to use the string resources for tab names. I keep getting an error though.

In my res/values/strings.xml file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    ...
    <string name="timeline_tab_name">Timeline</string>
</resources>

Now this works when I'm referencing the name in an xml file, as such.

<TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/timeline_tab_name"
        android:textSize="40sp"/>

This is MainActivity.kt

package com.*****.*****

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val fragmentAdapter = MyPagerAdapter(supportFragmentManager)
        viewpager_main.adapter = fragmentAdapter

        tabs_main.setupWithViewPager(viewpager_main)
    }
}

My error occurs in MyPagerAdapter.kt

package com.*****.*****

import android.provider.Settings.Global.getString
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter

class MyPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {

    override fun getItem(position: Int): Fragment {
        return when (position) {
            0 -> FirstFragment()
            1 -> SecondFragment()
            else -> {
                return ThirdFragment()
            }
        }
    }

    override fun getCount(): Int {
        return 3
    }

    override fun getPageTitle(position: Int): CharSequence {

        val timeline_name: String = getString(R.string.timeline_tab_name)

        return when (position) {
            0 -> timeline_name
            1 -> "Second Tab"
            else -> "Third Tab"
        }
    }
}

The error is when I'm trying to get the resource R.string.timeline_tab_name. It says this in the tooltip:

Type mismatch.
Required:  ContentResolver!
Found:     Int

I can find no information about this error, and nothing I've tried fixes it. Can anyone help me? All guides I've found for using the resources file don't get the error using the same code.

4
  • 1
    "this gives me an error" -- since there is no getPageTitle() in Activity for you to override, this would appear to not be a function in the activity itself, but instead perhaps a PagerAdapter. You might consider posting the entire file, or at the very least the entire class. Commented Mar 19, 2020 at 16:09
  • You're correct. This was in the custom pager adapter that was built in the guide I linked, my mistake. It works perfect besides the string resource. I've put the full kotlin code into my original post Commented Mar 19, 2020 at 16:17
  • Pass Context to FragmentPagerAdapter, and call getString() on that Commented Mar 19, 2020 at 16:27
  • How would I do that? Again, I'm very new to android and it's all very confusing to me. (I learned app dev in windows UWP which was way simpler to understand imo) Commented Mar 19, 2020 at 16:30

1 Answer 1

1

First, get rid of:

import android.provider.Settings.Global.getString

as it does not belong here and is confusing you.

That will change your error to be that there is no getString() function available in FragmentPagerAdapter.

Either:

  • Pass the string itself into your MyPagerAdapter, such as in its constructor, or

  • Pass a Context, such as your Activity, into your MyPagerAdapter, such as in its constructor, and call getString() on that Context

Sign up to request clarification or add additional context in comments.

2 Comments

Ok getting rid of that line did change the error as you said it would. I'm not sure how to do either of those things. It's a little hard to understand exactly what the program is doing, but the two possibilities for "Second Tab" and "Third Tab" work for titleing the tab, so I figured I could just put a string resource there.
@Graham: In your activity, you have val fragmentAdapter = MyPagerAdapter(supportFragmentManager). And, in your adapter class, you have class MyPagerAdapter(fm: FragmentManager). You can add other parameters to the constructor. So, for example, you could have class MyPagerAdapter(fm: FragmentManager, tab1Title: String) and call it as val fragmentAdapter = MyPagerAdapter(supportFragmentManager, getString(R.string.timeline_tab_name)). Or, use class MyPagerAdapter(fm: FragmentManager, context: Context) and val fragmentAdapter = MyPagerAdapter(supportFragmentManager, this).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.