0

I have a viewpager tab in my app that uses text to display the title. I initially followed a tutorial to make it text, but now I want it to be icons. I've already seen answers to this question, but I'm struggling to implement them into my code.

This is the code that gives the tabs their title

  package com.khumomashapa.notes.activities

import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.astuetz.PagerSlidingTabStrip
import com.khumomashapa.notes.R
import com.khumomashapa.notes.fragments.FileViewerFragment
import com.khumomashapa.notes.fragments.RecordFragment

class AudioRecorderActivity : AppCompatActivity() {
    private var tabs: PagerSlidingTabStrip? = null
    private var pager: ViewPager? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_audio_recorder)
        pager = findViewById<View>(R.id.pager) as ViewPager
        pager!!.adapter = MyAdapter(supportFragmentManager)
        tabs = findViewById<View>(R.id.tabs) as PagerSlidingTabStrip
        tabs!!.setViewPager(pager)
        val toolbar = findViewById<View>(R.id.toolbar) as Toolbar
        toolbar.popupTheme = R.style.ThemeOverlay_AppCompat_Light
        setSupportActionBar(toolbar)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        // Handle presses on the action bar items
        return when (item.itemId) {
            R.id.action_settings -> {
                val i = Intent(this, SettingsActivity::class.java)
                startActivity(i)
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }

    inner class MyAdapter(fm: FragmentManager?) : FragmentPagerAdapter(
        fm!!
    ) {
        private val titles = arrayOf(getString(R.string.tab_title_record), getString(R.string.tab_title_saved_recordings)
        )

        override fun getItem(position: Int): Fragment {
            when (position) {
                0 -> {
                    return RecordFragment.newInstance(position)
                }
                1 -> {
                    return FileViewerFragment.newInstance(position)
                }
            }
            return null!!
        }

        override fun getCount(): Int {
            return titles.size
        }

        override fun getPageTitle(position: Int): CharSequence? {
            return titles[position]
        }
    }

    companion object {
        private val LOG_TAG = AudioRecorderActivity::class.java.simpleName
    }
}

This is what the titles currently look like

This is what my viewpager titles currenlty look like

Khumo Mashapa
  • 390
  • 1
  • 4
  • 13
  • Could [this question/answer](https://stackoverflow.com/questions/30892545/tablayout-with-icons-only) be what you're looking for? – Martin Marconcini Sep 25 '20 at 11:02
  • Yes, but I'm having a bit of trouble using those for my code. Mine is in Kotlin and the answers are in Java. I'll post my entire code this time. – Khumo Mashapa Sep 25 '20 at 11:26
  • If you have problems understanding the differences between Java and Kotlin, please ask a new question with particular problems and what you have tried and the community will help you. Also, Kotlin and Java aren't that different although I can see how the syntax may appear foreign to a new Kotlin developer. Java is, after all, the language that Kotlin compiles to (JavaBytecode in the end), so it's important as a Kotlin developer to have a decent understanding of how it operates, for some of its peculiarities, may affect Kotlin (and do so on a regular basis). – Martin Marconcini Sep 25 '20 at 11:35
  • Thank you. I understand. – Khumo Mashapa Sep 25 '20 at 11:42
  • Additionally, if you PASTE Java code into a kotlin file (and I'm not encouraging you to copy-paste what you see on StackOverflow), Android Sudio will ask you if you want to convert the code to Kotlin. It will do a _decent_ job that may help you clarify how some particular construct is made in Kotlin. From there, LINT suggestions should help you make it more kotlin-like (if needed). – Martin Marconcini Sep 25 '20 at 11:44
  • Thank you. I've worked with kotlin for so long that I've forgotten even the simplest of syntax difference. This is the only time I've encountered this issue. I also prefer not to always copy java code and convert to kotlin. I almost always know what to do, it's just that sometimes I run into problems. I'll keep giving this a look, but I know I'll fix my problem – Khumo Mashapa Sep 25 '20 at 11:54
  • I'm sure you will, if not, feel free to ask specific (and simple) questions in [Android Help](https://chat.stackoverflow.com/rooms/210228/android-help) chat. Keep in mind i'm not always there and sometimes it's empty, but if you leave a question, i'll eventually look into it (or maybe someone else will) :) – Martin Marconcini Sep 25 '20 at 11:56
  • I'll do so. Thank you once again for your help. Enjoy the rest of your day/evening – Khumo Mashapa Sep 25 '20 at 11:57

0 Answers0