I created a custom view that has the following layout:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/hintTextLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/hintTextInputText"
android:layout_width="0dp"
android:layout_height="wrap_content"
tools:text="hint"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<ImageView
android:id="@+id/hintTextInputInputIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/hint_text_input_input_icon_margin_end"
android:scaleType="centerInside"
tools:src="@drawable/ic_eyeopen"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/hintTextInputEditText"
app:layout_constraintBottom_toBottomOf="@+id/hintTextInputEditText"/>
<TextView
android:id="@+id/hintTextInputPlaceholder"
android:layout_width="0dp"
android:layout_height="0dp"
android:lines="1"
android:gravity="top"
android:layout_marginEnd="@dimen/hint_text_input_input_placeholder_margin_end"
tools:text="placeholder"
app:layout_constraintTop_toTopOf="@+id/hintTextInputEditText"
app:layout_constraintBottom_toBottomOf="@+id/hintTextInputEditText"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@+id/hintTextInputInputIcon"/>
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/hintTextInputEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toStartOf="@+id/hintTextInputInputIcon"
app:layout_constraintTop_toBottomOf="@+id/hintTextInputText"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="@dimen/hint_text_input_input_margin_top"
android:layout_marginEnd="@dimen/hint_text_input_input_margin_end"
android:lines="1"
android:background="@null"
android:textCursorDrawable="@null"
tools:text="edit"/
</androidx.constraintlayout.widget.ConstraintLayout>
Also I defined the view in attrs.xml file:
<declare-styleable name="HintTextInputView">
<attr name="hint_text_size" format="dimension"/>
<attr name="input_text_size" format="dimension"/>
<attr name="hint_text_color" format="color"/>
<attr name="input_text_color" format="color"/>
<attr name="input_hint_text_color" format="color"/>
<attr name="android:text"/>
<attr name="android:nextFocusForward"/>
<attr name="hint_text" format="string"/>
<attr name="input_hint_text" format="string"/>
<attr name="hint_text_style" format="enum">
<enum name="normal" value="0"/>
<enum name="bold" value="1"/>
<enum name="italic" value="2"/>
</attr>
<attr name="input_text_style" format="enum">
<enum name="normal" value="0"/>
<enum name="bold" value="1"/>
<enum name="italic" value="2"/>
</attr>
<attr name="enable_input_icon" format="boolean"/>
<attr name="input_icon_enabled_image" format="integer"/>
<attr name="input_icon_disabled_image" format="integer"/>
<attr name="input_icon_tint_color" format="color"/>
<attr name="enable_borders" format="boolean"/>
<attr name="border_color" format="color"/>
<attr name="focused_border_color" format="color"/>
<attr name="border_radius" format="dimension"/>
<attr name="android:inputType"/>
</declare-styleable>
I'm also setting the attributes programmatically to the EdditText when they're defined at any layout:
private fun TextView.setFocusForward(attributes: TypedArray) {
attributes.getResourceId(R.styleable.HintTextInputView_android_nextFocusForward, 0).let {
if (it != 0) {
this.nextFocusForwardId = it
}
}
}
This is what the XML that has the views looks like:
<br.sis.smb.base.ui.HintTextInputView
android:id="@+id/name"
style="@style/DefaultEditTextBorder"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/register_company_margin_start_end"
android:layout_marginTop="@dimen/register_company_edittext_top_margin_top"
android:layout_marginEnd="@dimen/register_company_margin_space_between"
android:text="@={viewModel.name}"
android:nextFocusForward="@id/firstNickname"
app:hint_text="@string/register_edittext_name_hint"
app:input_hint_text="@string/register_edittext_name"
app:layout_constraintEnd_toStartOf="@+id/firstNickname"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.4"
/>
<br.sis.smb.base.ui.HintTextInputView
android:id="@+id/firstNickname"
style="@style/DefaultEditTextBorder"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/register_company_margin_space_between"
android:layout_marginEnd="@dimen/register_company_margin_start_end"
android:imeOptions="actionNext"
android:nextFocusForward="@+id/secondNickname"
android:text="@={viewModel.nickname}"
app:hint_text="@string/register_edittext_nickname_first"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/name"
app:layout_constraintTop_toTopOf="@+id/name"
app:layout_constraintVertical_bias="0.4"
/>
The problem is that the attribute android:nextFocusForward doesn't work at all. Even though I defined like above code, when I touch on next on my device's keyboard it doesn't lead to the right custom view but to the below custom view.
Make sure that you're not running on an emulator and on some emulator's nextfocusforward
won't work, in that case you've to use nextFocusDown
.
Also singleLine= true should be there and imeOptions =actionNext