Skip to content

Commit

Permalink
RTL support.
Browse files Browse the repository at this point in the history
  • Loading branch information
tiper committed Jun 28, 2019
1 parent 4a96be3 commit 255f9a0
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 40 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
Change Log
==========

Version 1.2.0 *(2019-06-17)*
Version 1.3.0 *(2019-06-28)*
----------------------------

* New: Added RTL support.

Version 1.2.0 *(2019-06-27)*
----------------------------

* New: Added support for custom drawables.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ dependencies {
- Showing a prompt when `spinnerMode` is `dialog`.
- Showing a bottom sheet when `spinnerMode` is `bottomsheet`.
- Custom spinner drawables.
- RTL support.

## Usage
There is a [sample](https://github.com/tiper/MaterialSpinner/tree/master/sample) provided which shows how to use the library, but for completeness, here is all that is required to get MaterialSpinner working:
Expand Down
4 changes: 2 additions & 2 deletions materialspinner/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ android {
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.sdkVersion
versionCode 120
versionName "1.2.0"
versionCode 130
versionName "1.3.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
Expand Down
97 changes: 80 additions & 17 deletions materialspinner/src/main/kotlin/com/tiper/MaterialSpinner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ import android.support.design.widget.TextInputEditText
import android.support.design.widget.TextInputLayout
import android.support.v4.content.res.ResourcesCompat
import android.support.v4.graphics.drawable.DrawableCompat
import android.support.v4.text.TextUtilsCompat
import android.support.v4.view.ViewCompat
import android.support.v7.widget.ListPopupWindow
import android.text.InputType
import android.util.AttributeSet
import android.view.Gravity
import android.view.SoundEffectConstants
import android.view.View
import android.view.View.OnFocusChangeListener
import android.view.ViewGroup
import android.view.accessibility.AccessibilityEvent
import android.widget.AdapterView
import android.widget.ListAdapter
import android.widget.ListView
import android.widget.SpinnerAdapter
import android.widget.ThemedSpinnerAdapter
import android.widget.*
import com.tiper.materialspinner.R
import java.util.*

/**
* Layout which wraps an [TextInputEditText] to show a floating label when the hint is hidden due to
Expand Down Expand Up @@ -100,6 +100,13 @@ open class MaterialSpinner @JvmOverloads constructor(
*/
var onItemClickListener: OnItemClickListener? = null

/**
* The layout direction of this view.
* {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL.
* {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
*/
private var direction = if (isLayoutRtl()) ViewCompat.LAYOUT_DIRECTION_RTL else ViewCompat.LAYOUT_DIRECTION_LTR

/**
* The currently selected item.
*/
Expand Down Expand Up @@ -153,6 +160,23 @@ open class MaterialSpinner @JvmOverloads constructor(

init {
context.obtainStyledAttributes(attrs, R.styleable.MaterialSpinner).run {
getInt(R.styleable.MaterialSpinner_android_gravity, -1).let {
if (it > -1) {
gravity = it
editText.gravity = it
} else {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
@SuppressLint("RtlHardcoded")
if (isLayoutRtl()) {
gravity = Gravity.RIGHT
editText.gravity = Gravity.RIGHT
} else {
gravity = Gravity.LEFT
editText.gravity = Gravity.LEFT
}
}
}
}
editText.isEnabled =
getBoolean(R.styleable.MaterialSpinner_android_enabled, editText.isEnabled)
editText.isFocusable =
Expand Down Expand Up @@ -254,18 +278,21 @@ open class MaterialSpinner @JvmOverloads constructor(
}

fun setDrawable(drawable: Drawable?, applyTint: Boolean = true) {
editText.setCompoundDrawablesWithIntrinsicBounds(
null,
null,
drawable?.let { DrawableCompat.wrap(drawable) }?.apply {
setBounds(0, 0, intrinsicWidth, intrinsicHeight)
if (applyTint) {
DrawableCompat.setTintList(this, colorStateList)
DrawableCompat.setTintMode(this, PorterDuff.Mode.SRC_IN)
}
},
null
)
drawable?.let { DrawableCompat.wrap(drawable) }?.apply {
setBounds(0, 0, intrinsicWidth, intrinsicHeight)
if (applyTint) {
DrawableCompat.setTintList(this, colorStateList)
DrawableCompat.setTintMode(this, PorterDuff.Mode.SRC_IN)
}
}.let {
if (isLayoutRtl()) {
Pair(it, null)
} else {
Pair(null, it)
}
}.let { (left, right) ->
editText.setCompoundDrawablesWithIntrinsicBounds(left, null, right, null)
}
}

override fun setOnClickListener(l: OnClickListener?) {
Expand Down Expand Up @@ -303,6 +330,19 @@ open class MaterialSpinner @JvmOverloads constructor(
super.setFocusableInTouchMode(focusableInTouchMode)
}

/**
* @see [android.view.View.onRtlPropertiesChanged]
*/
override fun onRtlPropertiesChanged(layoutDirection: Int) {
if (direction != layoutDirection) {
direction = layoutDirection
editText.compoundDrawables.let {
editText.setCompoundDrawablesWithIntrinsicBounds(it[2], null, it[0], null)
}
}
super.onRtlPropertiesChanged(layoutDirection)
}

/**
* Call the OnItemClickListener, if it is defined.
* Performs all normal actions associated with clicking: reporting accessibility event, playing
Expand Down Expand Up @@ -335,13 +375,36 @@ open class MaterialSpinner @JvmOverloads constructor(
prompt = context.getText(promptId)
}

/**
* Returns if this view layout should be in a RTL direction.
* @return True if is RTL, false otherwise .
*/
private fun isLayoutRtl(): Boolean {
return Locale.getDefault().isLayoutRtl()
}

/**
* Returns if this Locale direction is RTL.
* @return True if is RTL, false otherwise .
*/
private fun Locale.isLayoutRtl(): Boolean {
return TextUtilsCompat.getLayoutDirectionFromLocale(this) == ViewCompat.LAYOUT_DIRECTION_RTL
}

/**
* @see [android.support.v4.content.res.ResourcesCompat.getDrawable]
*/
private fun Context.getDrawableCompat(
@DrawableRes id: Int,
theme: Resources.Theme?
): Drawable? {
return resources.getDrawableCompat(id, theme)
}

/**
* @see [android.support.v4.content.res.ResourcesCompat.getDrawable]
*/
@Throws(Resources.NotFoundException::class)
private fun Resources.getDrawableCompat(
@DrawableRes id: Int,
theme: Resources.Theme?
Expand Down
1 change: 1 addition & 0 deletions materialspinner/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<attr name="android:textColor" />
<attr name="android:textSize" />
<attr name="android:text" />
<attr name="android:gravity" />
<attr name="srcCompat" />
<attr name="spinnerMode" format="enum">
<enum name="dialog" value="0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,31 +55,23 @@ class MainActivity : AppCompatActivity() {
material_spinner_1.selection = ListView.INVALID_POSITION
}
b1_error.setOnClickListener {
if (material_spinner_1.error.isNullOrEmpty()) {
material_spinner_1.error = "I am an error"
} else {
material_spinner_1.error = null
}
material_spinner_1.onClick()
}
b2_clear.setOnClickListener {
material_spinner_2.selection = ListView.INVALID_POSITION
}
b2_error.setOnClickListener {
if (material_spinner_2.error.isNullOrEmpty()) {
material_spinner_2.error = "I am an error"
} else {
material_spinner_2.error = null
}
material_spinner_2.onClick()
}
b3_clear.setOnClickListener {
material_spinner_3.selection = ListView.INVALID_POSITION
}
b3_error.setOnClickListener {
if (material_spinner_3.error.isNullOrEmpty()) {
material_spinner_3.error = "I am an error"
} else {
material_spinner_3.error = null
}
material_spinner_3.onClick()
}
}

private fun MaterialSpinner.onClick() {
error = if (error.isNullOrEmpty()) resources.getText(R.string.error) else null
}
}
9 changes: 5 additions & 4 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="MaterialSpinner 1"
android:hint="@string/materialspinner_1"
app:spinnerMode="bottomsheet" />

<FrameLayout
Expand Down Expand Up @@ -73,7 +73,7 @@
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginEnd="50dp"
android:hint="MaterialSpinner 2"
android:hint="@string/materialspinner_2"
android:prompt="@string/app_name"
android:src="@drawable/ic_selector_drawable"
android:textColor="#0ff"
Expand Down Expand Up @@ -106,8 +106,9 @@
style="@style/AppTheme.Widget.Design.TextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="MaterialSpinner 3"
android:prompt="@string/app_name"
android:hint="@string/materialspinner_3"
android:gravity="left"
android:prompt="@string/materialspinner"
app:spinnerMode="dialog"
app:srcCompat="@drawable/ic_selector_drawable" />

Expand Down
13 changes: 13 additions & 0 deletions sample/src/main/res/values-ar-rEG/arrays.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>الزئبق</item>
<item>فينوس</item>
<item>الأرض</item>
<item>المريخ</item>
<item>كوكب المشتري</item>
<item>زحل</item>
<item>أورانوس</item>
<item>نبتون</item>
</string-array>
</resources>
7 changes: 7 additions & 0 deletions sample/src/main/res/values-ar-rEG/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<resources>
<string name="materialspinner">مادة الدوار</string>
<string name="materialspinner_1">مادة الدوار 1</string>
<string name="materialspinner_2">مادة الدوار 2</string>
<string name="materialspinner_3">3 مادة الدوار </string>
<string name="error">3أنا خطأ</string>
</resources>
7 changes: 6 additions & 1 deletion sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<resources>
<string name="app_name">MaterialSpinner</string>
<string name="app_name" translatable="false">MaterialSpinner</string>
<string name="materialspinner">Material Spinner</string>
<string name="materialspinner_1">Material Spinner 1</string>
<string name="materialspinner_2">Material Spinner 2</string>
<string name="materialspinner_3">Material Spinner 3</string>
<string name="error">I am an error</string>
</resources>

0 comments on commit 255f9a0

Please sign in to comment.