1
0
Fork 0

Switch RadioButtonPreference to ConstraintLayout

Also ensure that RTL works properly.
master
Tiger Oakes 2019-06-25 09:22:05 -04:00 committed by Emily Kager
parent 52850feaab
commit a78d36354a
8 changed files with 68 additions and 94 deletions

View File

@ -15,7 +15,7 @@ import org.mozilla.fenix.R
class AccountAuthErrorPreference @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
attributeSetId: Int = 0
attributeSetId: Int = android.R.attr.preferenceStyle
) : Preference(context, attrs, attributeSetId) {
var email: String? = null

View File

@ -15,7 +15,7 @@ import org.mozilla.fenix.R
class AccountPreference @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
attributeSetId: Int = 0
attributeSetId: Int = android.R.attr.preferenceStyle
) : Preference(context, attrs, attributeSetId) {
var displayName: String? = null
var email: String? = null

View File

@ -15,7 +15,7 @@ import org.mozilla.fenix.R
class DefaultBrowserPreference @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
attributeSetId: Int = 0
attributeSetId: Int = android.R.attr.preferenceStyle
) : Preference(context, attrs, attributeSetId) {
private var switchView: Switch? = null

View File

@ -98,17 +98,17 @@ fun PhoneFeature.getPreferenceKey(context: Context): String {
}
}
/* In devices with Android 6, when we use android:button="@null" android:drawableStart doesn't work via xml
* as a result we have to apply it programmatically. More info about this issue https://github.com/mozilla-mobile/fenix/issues/1414
*/
/**
* In devices with Android 6, when we use android:button="@null" android:drawableStart doesn't work via xml
* as a result we have to apply it programmatically. More info about this issue https://github.com/mozilla-mobile/fenix/issues/1414
*/
fun RadioButton.setStartCheckedIndicator() {
val attr =
ThemeManager.resolveAttribute(android.R.attr.listChoiceIndicatorSingle, context)
val attr = ThemeManager.resolveAttribute(android.R.attr.listChoiceIndicatorSingle, context)
val buttonDrawable = ContextCompat.getDrawable(context, attr)
buttonDrawable.apply {
this?.setBounds(0, 0, this.intrinsicWidth, this.intrinsicHeight)
buttonDrawable?.apply {
setBounds(0, 0, this.intrinsicWidth, this.intrinsicHeight)
}
this.setCompoundDrawables(buttonDrawable, null, null, null)
setCompoundDrawablesRelative(buttonDrawable, null, null, null)
}
fun initBlockedByAndroidView(phoneFeature: PhoneFeature, blockedByAndroidView: View) {

View File

@ -5,30 +5,26 @@
package org.mozilla.fenix.settings
import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.View
import android.widget.RadioButton
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.content.res.TypedArrayUtils
import androidx.core.content.res.TypedArrayUtils.getAttr
import androidx.core.content.withStyledAttributes
import androidx.core.text.HtmlCompat
import androidx.preference.Preference
import androidx.preference.PreferenceViewHolder
import org.mozilla.fenix.R
import org.mozilla.fenix.ThemeManager
import org.mozilla.fenix.utils.Settings
class RadioButtonPreference @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
attributeSetId: Int = 0
) : Preference(context, attrs, attributeSetId) {
attrs: AttributeSet? = null
) : Preference(context, attrs) {
private val radioGroups = mutableListOf<RadioButtonPreference>()
private lateinit var summaryView: TextView
private lateinit var radioButton: RadioButton
var shouldSummaryBeParsedAsHtmlContent: Boolean = true
private var shouldSummaryBeParsedAsHtmlContent: Boolean = true
private var defaultValue: Boolean = false
private var clickListener: (() -> Unit)? = null
@ -38,38 +34,19 @@ class RadioButtonPreference @JvmOverloads constructor(
context.withStyledAttributes(
attrs,
androidx.preference.R.styleable.Preference,
TypedArrayUtils.getAttr(
context, androidx.preference.R.attr.preferenceStyle, android.R.attr.preferenceStyle
),
getAttr(context, androidx.preference.R.attr.preferenceStyle, android.R.attr.preferenceStyle),
0
) {
if (hasValue(androidx.preference.R.styleable.Preference_defaultValue)) {
defaultValue = getBoolean(
androidx.preference.R.styleable.Preference_defaultValue,
false
)
} else if (hasValue(androidx.preference.R.styleable.Preference_android_defaultValue)) {
defaultValue = getBoolean(
androidx.preference.R.styleable.Preference_android_defaultValue,
false
)
defaultValue = when {
hasValue(androidx.preference.R.styleable.Preference_defaultValue) ->
getBoolean(androidx.preference.R.styleable.Preference_defaultValue, false)
hasValue(androidx.preference.R.styleable.Preference_android_defaultValue) ->
getBoolean(androidx.preference.R.styleable.Preference_android_defaultValue, false)
else -> false
}
}
}
/* In devices with Android 6, when we use android:button="@null" android:drawableStart doesn't work via xml
* as a result we have to apply it programmatically. More info about this issue https://github.com/mozilla-mobile/fenix/issues/1414
*/
fun RadioButton.setStartCheckedIndicator() {
val attr =
ThemeManager.resolveAttribute(android.R.attr.listChoiceIndicatorSingle, context)
val buttonDrawable = ContextCompat.getDrawable(context, attr)
buttonDrawable.apply {
this?.setBounds(0, 0, this.intrinsicWidth, this.intrinsicHeight)
}
this.setCompoundDrawables(buttonDrawable, null, null, null)
}
fun addToRadioGroup(radioPreference: RadioButtonPreference) {
radioGroups.add(radioPreference)
}
@ -118,20 +95,21 @@ class RadioButtonPreference @JvmOverloads constructor(
private fun bindTitle(holder: PreferenceViewHolder) {
val titleView = holder.findViewById(R.id.title) as TextView
if (!TextUtils.isEmpty(title)) {
if (!title.isNullOrEmpty()) {
titleView.text = title
}
}
private fun bindSummaryView(holder: PreferenceViewHolder) {
summaryView = holder.findViewById(R.id.widget_summary) as TextView
if (!TextUtils.isEmpty(summary)) {
if (shouldSummaryBeParsedAsHtmlContent) {
summaryView.text =
HtmlCompat.fromHtml(summary.toString(), HtmlCompat.FROM_HTML_MODE_COMPACT)
if (!summary.isNullOrEmpty()) {
summaryView.text = if (shouldSummaryBeParsedAsHtmlContent) {
HtmlCompat.fromHtml(summary.toString(), HtmlCompat.FROM_HTML_MODE_COMPACT)
} else {
summaryView.text = summary
summary
}
summaryView.visibility = View.VISIBLE
} else {
summaryView.visibility = View.GONE

View File

@ -15,7 +15,7 @@ import org.mozilla.fenix.utils.Settings
class RadioSearchEngineListPreference @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
defStyleAttr: Int = android.R.attr.preferenceStyle
) : SearchEngineListPreference(context, attrs, defStyleAttr) {
override val itemResId: Int
get() = R.layout.search_engine_radio_button

View File

@ -31,7 +31,7 @@ import kotlin.coroutines.CoroutineContext
abstract class SearchEngineListPreference @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
defStyleAttr: Int = android.R.attr.preferenceStyle
) : Preference(context, attrs, defStyleAttr), CompoundButton.OnCheckedChangeListener, CoroutineScope {
private val job = Job()
override val coroutineContext: CoroutineContext

View File

@ -3,9 +3,9 @@
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<LinearLayout
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -16,45 +16,41 @@
android:paddingBottom="@dimen/radio_button_preference_vertical"
android:baselineAligned="false">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/radio_button"
android:layout_width="match_parent"
android:layout_height="@dimen/radio_button_preference_height"
android:background="@android:color/transparent"
android:layout_gravity="start"
android:button="@null"
android:clickable="false"
android:focusable="false"
android:drawableStart="?android:attr/listChoiceIndicatorSingle"
android:drawablePadding="@dimen/radio_button_preference_drawable_padding"/>
</LinearLayout>
<LinearLayout
<RadioButton
android:id="@+id/radio_button"
android:layout_width="wrap_content"
android:layout_height="@dimen/radio_button_preference_height"
android:background="@android:color/transparent"
android:layout_gravity="start"
android:button="@null"
android:clickable="false"
android:focusable="false"
tools:drawableStart="?android:attr/listChoiceIndicatorSingle"
android:drawablePadding="@dimen/radio_button_preference_drawable_padding"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:gravity="center|start"
android:layout_height="@dimen/radio_button_preference_height"
tools:text="Use recommended settings"
android:textAppearance="?android:attr/textAppearanceListItem"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/radio_button"/>
<TextView
android:id="@+id/widget_summary"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical">
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorSecondary"
tools:text="@string/preference_recommended_settings_summary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/radio_button"
app:layout_constraintStart_toStartOf="@+id/title" />
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:gravity="center|start"
android:layout_height="@dimen/radio_button_preference_height"
tools:text="Use recommended settings"
android:textAppearance="?android:attr/textAppearanceListItem"/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_summary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorSecondary"
tools:text="@string/preference_recommended_settings_summary"/>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>