Bug - Unnable to change supportActionBar title

167 views Asked by At

I'm trying to change supportActionBar title from a method called from a network callback. Even though I call it from inside Main Thread, title doesn't change! If I try in any other method it does.

My layout:

  <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/userProfile"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:contentDescription="@string/str_act_main_user_list_item_user_profile_picture" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

MainActivity:

class ActivityMain : BaseActivity(), ContractMainView {

    @Inject
    lateinit var mPresenter: ContractMainPresenter

    private var mUser: User? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        App.component!!.inject(this)
        setSupportActionBar(toolbar)
        initPageView()
        setUpProfile()
        mPresenter.init()

    }

    override fun getContext(): AppCompatActivity {
        return this
    }

    override fun getView(): BaseView? {
        return this
    }

    override fun getPresenter(): BasePresenter? {
        return mPresenter
    }

    private fun initPageView() {
        container.adapter = SectionsPagerAdapter(supportFragmentManager)
        container.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                if (position == 0 && positionOffset > 0.95) {
                    app_bar.setExpanded(false)
                }
            }

            override fun onPageSelected(position: Int) {
                if (position == 0) {
                    app_bar.setExpanded(true)
                }
            }
        })
    }

    override fun fillUserInterface(user: User) {
        mUser = user
        runOnUiThread {
            println("changing title to: ${user.name}")
            supportActionBar!!.title = "This is a Test"
        }
    }

    private fun setUpProfile() {
        Picasso.with(this)
                .load("http://images.mentalfloss.com/sites/default/files/borat_primary.jpg")
                .fit()
                .centerCrop()
                .into(findViewById(R.id.userProfile) as ImageView)
    }
}

The called method is: fillUserInterface(user: User). No matter what I don, title gets never change from it. Any help is appreciated, thanks!

1

There are 1 answers

0
Miha_x64 On

Use Activity.setTitle(CharSequence) instead of Toolbar.setTitle(CharSequence).