Graham
Graham

Reputation: 113

How to use string resources in a kotlin file

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.

Upvotes: 0

Views: 2367

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006604

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

Upvotes: 1

Related Questions