Skip to content

Commit

Permalink
Merge pull request #3 from koji-1009/fix_text_style_setting
Browse files Browse the repository at this point in the history
Fix text style setting
  • Loading branch information
koji-1009 authored Aug 9, 2020
2 parents 3dcdad7 + 196928d commit 9b658e5
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 91 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-rc1'
implementation "androidx.core:core-ktx:1.3.1"
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'com.google.android.material:material:1.2.0'

testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test:runner:1.2.0'
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

<Button
android:id="@+id/button_clear"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
Expand All @@ -30,6 +31,7 @@

<Button
android:id="@+id/button_add"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>

<item name="toolbarStyle">@style/Widget.MaterialComponents.Toolbar.Surface</item>
</style>

</resources>
6 changes: 0 additions & 6 deletions emptyrecyclerview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ android {
defaultConfig {
minSdkVersion 21
targetSdkVersion 29

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
}

Expand All @@ -17,8 +15,4 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

implementation 'androidx.recyclerview:recyclerview:1.1.0'

testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
21 changes: 0 additions & 21 deletions emptyrecyclerview/proguard-rules.pro

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ package com.dr1009.app.emptyrecyclerview

import android.content.Context
import android.graphics.Canvas
import android.os.Build
import android.util.AttributeSet
import android.view.*
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.LayoutRes
import androidx.annotation.StringRes
import androidx.annotation.StyleRes
import androidx.recyclerview.widget.RecyclerView
import com.app.dr1009.emptyrecyclerview.R
Expand Down Expand Up @@ -77,58 +82,61 @@ class EmptyRecyclerView @JvmOverloads constructor(
}

/**
* Create TextView from a string resource, Gravity, LayoutParams and Style resource,
* Create TextView from a string resource, Gravity, LayoutParams and TextAppearance resource,
* add to EmptyRecyclerView to show if the adapter is empty.
*
* @param emptyMessageResId resource id of message to show
* @param gravity [android.view.Gravity]
* @param params [ViewGroup.LayoutParams]
* @param styleRes resource id of view style
* @param textAppearanceResId resource id of TextAppearance. If null, show default appearance.
*/
@SuppressWarnings("unused")
fun setEmptyString(
emptyMessageResId: Int,
@StringRes emptyMessageResId: Int,
gravity: Int = Gravity.CENTER,
params: ViewGroup.LayoutParams? = null,
@StyleRes styleRes: Int = 0
@StyleRes textAppearanceResId: Int? = null
) {
val message = if (emptyMessageResId > 0) {
context.getString(emptyMessageResId)
} else {
null
}

setEmptyString(message, gravity, params, styleRes)
setEmptyString(context.getString(emptyMessageResId), gravity, params, textAppearanceResId)
}

/**
* Create TextView from a string, Gravity, LayoutParams and Style resource,
* Create TextView from a string, Gravity, LayoutParams and TextAppearance resource,
* add to EmptyRecyclerView to show if the adapter is empty.
*
* @param emptyMessage message to show
* @param gravity [android.view.Gravity]
* @param params [ViewGroup.LayoutParams]
* @param styleRes resource id of view style
* @param textAppearanceResId resource id of TextAppearance. If null, show default appearance.
*/
@SuppressWarnings("unused")
fun setEmptyString(
emptyMessage: String?,
gravity: Int = Gravity.CENTER,
params: ViewGroup.LayoutParams? = null,
@StyleRes styleRes: Int = 0
@StyleRes textAppearanceResId: Int? = null
) {
if (emptyMessage.isNullOrEmpty()) {
setEmptyView(null)
return
}

val textView = TextView(ContextThemeWrapper(context, styleRes)).also {
it.text = emptyMessage
it.gravity = gravity
it.layoutParams = params ?: ViewGroup.LayoutParams(
val textView = TextView(context).also { view ->
view.text = emptyMessage
view.gravity = gravity
view.layoutParams = params ?: ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)

if (textAppearanceResId != null) {
// set TextAppearance
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
view.setTextAppearance(textAppearanceResId)
} else {
view.setTextAppearance(context, textAppearanceResId)
}
}
}

setEmptyView(textView)
Expand Down

This file was deleted.

0 comments on commit 9b658e5

Please sign in to comment.