Skip to content

Commit

Permalink
fixed runtime title change issue
Browse files Browse the repository at this point in the history
  • Loading branch information
RaviKoradiya committed Jan 11, 2018
1 parent c5fdeb4 commit 261f0be
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.text.Editable
import android.text.TextWatcher
import android.view.Menu
import com.ravikoradiya.toolbarcentertitle.databinding.ActivityMainBinding


class MainActivity : AppCompatActivity() {

lateinit var binding: ActivityMainBinding;
lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -23,6 +25,34 @@ class MainActivity : AppCompatActivity() {
supportActionBar?.setDisplayShowHomeEnabled(true)

binding.switch1.setOnCheckedChangeListener { compoundButton, b -> binding.isTitleInCenter = b }

binding.editTitle.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(p0: Editable?) {

}

override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

}

override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
binding.toolbar.title = p0
}
})

binding.editSubTitle.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(p0: Editable?) {

}

override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

}

override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
binding.toolbar.subtitle = p0
}
})
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
Expand Down
33 changes: 32 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<variable
name="isTitleInCenter"
type="Boolean" />

</data>

<android.support.design.widget.CoordinatorLayout
Expand Down Expand Up @@ -55,11 +56,41 @@
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:checked="true"
android:text="Title In Center"
app:layout_constraintBottom_toTopOf="@+id/editTitle"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="420dp" />

<EditText
android:id="@+id/editTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:ems="10"
android:gravity="center"
android:hint="Title"
android:inputType="textPersonName"
android:lines="1"
app:layout_constraintBottom_toTopOf="@+id/editSubTitle"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<EditText
android:id="@+id/editSubTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:ems="10"
android:gravity="center"
android:hint="Sub Title"
android:inputType="textPersonName"
android:lines="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
Expand Down
93 changes: 80 additions & 13 deletions library/src/main/java/com/ravikoradiya/library/CenterTitle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,28 @@ package com.ravikoradiya.library

import android.databinding.BindingAdapter
import android.support.v7.widget.Toolbar
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import android.view.ViewTreeObserver
import android.view.ViewTreeObserver.OnGlobalLayoutListener
import android.widget.ImageButton
import android.widget.TextView


object CenterTitle {

lateinit var titleWatcher: TextWatcher
lateinit var subTitleWatcher: TextWatcher
lateinit var globalLayoutListener: OnGlobalLayoutListener
@JvmStatic
var isCenter = true

@BindingAdapter("centerTitle")
@JvmStatic
fun centerTitle(toolbar: Toolbar, textAlignment: Boolean) {
fun centerTitle(toolbar: Toolbar, flagCenter: Boolean) {

isCenter = flagCenter

// title
val fieldTitle = Toolbar::class.java.getDeclaredField("mTitleTextView")
Expand All @@ -29,9 +40,53 @@ object CenterTitle {
fieldNav.isAccessible = true
val mNavButtonView: ImageButton? = fieldNav.get(toolbar) as ImageButton?


mTitleTextView?.let {

//
//removing observer for avoid recursive call
if (::globalLayoutListener.isInitialized)
removeAllListners(mTitleTextView, mSubtitleTextView, globalLayoutListener, titleWatcher, subTitleWatcher)

// view tree observer for listen any layout changes
globalLayoutListener = object : OnGlobalLayoutListener {

override fun onGlobalLayout() {
removeAllListners(mTitleTextView, mSubtitleTextView, globalLayoutListener, titleWatcher, subTitleWatcher)
centerTitle(toolbar, isCenter)
}
}

titleWatcher = object : TextWatcher {
override fun afterTextChanged(p0: Editable?) {
mTitleTextView.requestLayout()
removeAllListners(mTitleTextView, mSubtitleTextView, globalLayoutListener, titleWatcher, subTitleWatcher)
centerTitle(toolbar, isCenter)
}

override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}

override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

}
}

subTitleWatcher = object : TextWatcher {
override fun afterTextChanged(p0: Editable?) {
mSubtitleTextView?.requestLayout()
removeAllListners(mTitleTextView, mSubtitleTextView, globalLayoutListener, titleWatcher, subTitleWatcher)
centerTitle(toolbar, isCenter)
}

override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}

override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

}
}

//mesure all views
mTitleTextView.measure(0, 0)
mNavButtonView?.measure(0, 0)
mSubtitleTextView?.measure(0, 0)
Expand Down Expand Up @@ -63,7 +118,7 @@ object CenterTitle {

// position title
mTitleTextView.run {
if (textAlignment) left = leftSideMarginTitle
if (isCenter) left = leftSideMarginTitle
else left = mNavButtonView?.measuredWidth.orZero()
right = Math.min(toolbar.measuredWidth - leftSideMarginTitle, menuIconsMargin)
layoutParams.width = Math.min(toolbar.measuredWidth - leftSideMarginTitle, menuIconsMargin) - leftSideMarginTitle
Expand All @@ -72,25 +127,37 @@ object CenterTitle {

// position sub title
mSubtitleTextView?.run {
if (textAlignment) left = leftSideMarginSubTitle
if (isCenter) left = leftSideMarginSubTitle
else left = mNavButtonView?.measuredWidth.orZero()
right = Math.min(toolbar.measuredWidth - leftSideMarginSubTitle, menuIconsMargin)
layoutParams.width = Math.min(toolbar.measuredWidth - leftSideMarginSubTitle, menuIconsMargin) - leftSideMarginSubTitle
text = text
}

val vto = mTitleTextView.getViewTreeObserver()

// add a view tree observer so that we can center the title every time view tree is updated
vto.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
// remove listener to prevent recursive calls
mTitleTextView.getViewTreeObserver().removeOnGlobalLayoutListener(this)
centerTitle(toolbar, textAlignment)
}
})
val vto = mTitleTextView.getViewTreeObserver()
vto.addOnGlobalLayoutListener(globalLayoutListener)

// add text watcher for listen text change
mTitleTextView.addTextChangedListener(titleWatcher)

// add text watcher for listen text change
mSubtitleTextView?.addTextChangedListener(subTitleWatcher)

}
}


private fun removeAllListners(titleView: TextView,
subTitleView: TextView?,
titleObserver: OnGlobalLayoutListener,
titleTextWatcher: TextWatcher,
subTitleTextWatcher: TextWatcher) {
titleView.getViewTreeObserver().removeOnGlobalLayoutListener(titleObserver)
titleView.removeTextChangedListener(titleTextWatcher)
subTitleView?.removeTextChangedListener(subTitleTextWatcher)
}
}

fun Int?.orZero(): Int = this ?: 0
Expand Down

0 comments on commit 261f0be

Please sign in to comment.