Skip to content

Commit

Permalink
Add support for centerTitle and useLightFont
Browse files Browse the repository at this point in the history
  • Loading branch information
André Sousa committed Oct 29, 2018
1 parent 0af2f9a commit 7e6027e
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 1 deletion.
4 changes: 3 additions & 1 deletion demo/src/main/res/layout/activity_demo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/white"/>
android:background="@android:color/white"
app:superToolbar_useLightFont="true"
app:superToolbar_centerTitle="true"/>

<android.support.v7.widget.RecyclerView
android:id="@+id/items_list"
Expand Down
36 changes: 36 additions & 0 deletions lib/src/main/java/com/andrefrsousa/supertoolbar/KExtensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2018 André Sousa
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.andrefrsousa.supertoolbar

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

//region VIEW

internal fun ViewGroup.inflate(layoutId: Int): View {
return LayoutInflater.from(context).inflate(layoutId, this, false)
}

//endregion
70 changes: 70 additions & 0 deletions lib/src/main/java/com/andrefrsousa/supertoolbar/SuperToolbar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,24 @@
package com.andrefrsousa.supertoolbar

import android.content.Context
import android.graphics.Typeface
import android.os.Build
import android.support.v4.view.ViewCompat
import android.support.v7.widget.AppCompatTextView
import android.support.v7.widget.Toolbar
import android.util.AttributeSet
import android.view.Gravity
import android.view.animation.DecelerateInterpolator

private const val DURATION = 250

open class SuperToolbar : Toolbar {

private lateinit var titleView: AppCompatTextView

private var isElevationShown = false
private var centerTitle = false
private var useLightFont = false
private var animationDuration = 0L
private var toolbarElevation = 0f

Expand All @@ -57,6 +65,8 @@ open class SuperToolbar : Toolbar {
with(context.obtainStyledAttributes(attrs, R.styleable.SuperToolbar, defStyleAttr, 0)) {
animationDuration = getInt(R.styleable.SuperToolbar_superToolbar_animationDuration, DURATION).toLong()
isElevationShown = getBoolean(R.styleable.SuperToolbar_superToolbar_showElevationAtStart, false)
centerTitle = getBoolean(R.styleable.SuperToolbar_superToolbar_centerTitle, false)
useLightFont = getBoolean(R.styleable.SuperToolbar_superToolbar_useLightFont, false)
recycle()
}

Expand All @@ -73,6 +83,66 @@ open class SuperToolbar : Toolbar {
if (!isElevationShown) {
ViewCompat.setElevation(this, 0f)
}

// Add a custom title view
if (centerTitle || useLightFont) {
titleView = inflate(R.layout.super_toolbar_title) as AppCompatTextView

if (useLightFont) {
titleView.typeface = Typeface.SANS_SERIF
}

if (centerTitle) {
val layoutParams = titleView.layoutParams as Toolbar.LayoutParams
layoutParams.gravity = Gravity.CENTER
addView(titleView, layoutParams)

} else {
addView(titleView)
}
}
}

override fun setTitle(resId: Int) {
if (::titleView.isInitialized) {
titleView.setText(resId)
return
}

super.setTitle(resId)
}

override fun setTitle(title: CharSequence?) {
if (::titleView.isInitialized) {
titleView.text = title
return
}

super.setTitle(title)
}

override fun setTitleTextAppearance(context: Context?, resId: Int) {
if (::titleView.isInitialized) {
if (hasMinimumSdk(Build.VERSION_CODES.M)) {
titleView.setTextAppearance(resId)

} else {
titleView.setTextAppearance(context, resId)
}

return
}

super.setTitleTextAppearance(context, resId)
}

override fun setTitleTextColor(color: Int) {
if (::titleView.isInitialized) {
titleView.setTextColor(color)
return
}

super.setTitleTextColor(color)
}

//region PUBLIC METHODS
Expand Down
30 changes: 30 additions & 0 deletions lib/src/main/java/com/andrefrsousa/supertoolbar/Utils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2018 André Sousa
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.andrefrsousa.supertoolbar

import android.os.Build

internal fun hasMinimumSdk(minimumSdk: Int): Boolean {
return Build.VERSION.SDK_INT >= minimumSdk
}
7 changes: 7 additions & 0 deletions lib/src/main/res/layout/super_toolbar_title.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.AppCompatTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"/>
2 changes: 2 additions & 0 deletions lib/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<declare-styleable name="SuperToolbar">
<attr name="superToolbar_animationDuration" format="integer"/>
<attr name="superToolbar_showElevationAtStart" format="boolean"/>
<attr name="superToolbar_centerTitle" format="boolean"/>
<attr name="superToolbar_useLightFont" format="boolean"/>
</declare-styleable>

</resources>

0 comments on commit 7e6027e

Please sign in to comment.