Add Hide/Show EditText - android

Let me try to explain the title.
I have a manufacturer TextView that is populated from MYSQL. When a manufacturer is selected it populates the Model TextView.
Now I want to add OTHER in each model, and when it is selected an EditText appears. Something like this:
-FORD
---Mustang
---Escape
---Other
-BMW
---X5
---Z4
---Other
Now whenever Other is selected, the EditText appears. If not, then it remains hidden.
The only way I got it working is by creating a field in mysql for each model. I am hoping there is a better approach.
Here is what I have done.
enter code here
<EditText
android:id="#+id/fordTextView"
android:layout_width="0dp"
android:layout_height="130px"
android:layout_marginStart="16dp"
android:visibility="gone"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:background="#drawable/custom_progress_bar_horizontal"
android:inputType="textPersonName"
android:paddingStart="#dimen/space_12"
android:hint="#string/item_entry__model"
android:paddingLeft="#dimen/space_12"
android:paddingTop="8dp"
android:paddingEnd="#dimen/space_12"
android:paddingRight="#dimen/space_12"
android:paddingBottom="8dp"
android:textAlignment="viewStart"
android:textColor="#color/text__primary"
android:textSize="14sp"
app:font='#{"normal"}'
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/plateNumTextView" />
<EditText
android:id="#+id/bmwTextView"
android:layout_width="0dp"
android:layout_height="130px"
android:layout_marginStart="16dp"
android:visibility="gone"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:background="#drawable/custom_progress_bar_horizontal"
android:inputType="textPersonName"
android:paddingStart="#dimen/space_12"
android:hint="#string/item_entry__model"
android:paddingLeft="#dimen/space_12"
android:paddingTop="8dp"
android:paddingEnd="#dimen/space_12"
android:paddingRight="#dimen/space_12"
android:paddingBottom="8dp"
android:textAlignment="viewStart"
android:textColor="#color/text__primary"
android:textSize="14sp"
app:font='#{"normal"}'
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/fordTextView" />
Here is is the java
if (itemViewModel.holder.model_id.equals(Constants.MODEL_FORD_OTHER)){
binding.get().fordTextView.setVisibility(View.VISIBLE);
binding.get().bmwTextView.setVisibility(View.GONE);
}
if (!itemViewModel.holder.model_id.equals(Constants.MODEL_FORD_OTHER)){
binding.get().fordTextView.setVisibility(View.GONE);
binding.get().fordTextView.setText("");
}
if (itemViewModel.holder.model_id.equals(Constants.MODEL_BMW_OTHER)){
binding.get().bmwTextView.setVisibility(View.VISIBLE);
binding.get().fordTextView.setVisibility(View.GONE);
}
if (!itemViewModel.holder.model_id.equals(Constants.MODEL_BMW_OTHER)){
binding.get().bmwTextView.setVisibility(View.GONE);
binding.get().bmwTextView.setText("");
}
Is there a way of doing this by using only one EditText?

You can achieve this using single EditText, when ever click is registered for particular model Other button, set the model_id of that model as tag to the EditText. And while reading input from EditText, always check for tag to get the model_id
binding.get().modelTextView.setTag(""+itemViewModel.holder.model_id);

Related

how do I add padding in EditText hints

I have made a login page and in it there are two fields, one for username and one for password, and
I can't seem to figure out a way to add padding to hints so the hints in the field look like this
and if I add the android:paddingTop="30dp"
it looks like this
this EditText is using a background source (field_bg) and also a user icon which changes its colors when the field is selected (custom_username_field).
here is the xml code for the username field.
<EditText
android:id="#+id/usernameField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autofillHints=""
android:background="#drawable/field_bg"
android:cursorVisible="false"
android:drawableStart="#drawable/custom_username_field"
android:drawablePadding="-30dp"
android:ems="10"
android:fontFamily="#font/staatliches"
android:hint="USERNAME"
android:includeFontPadding="true"
android:inputType="textPersonName"
android:textAlignment="center"
android:textColor="#6E6E6E"
android:textSize="20sp"
android:paddingTop="30dp"
app:boxStrokeWidth="#null"
app:boxStrokeWidthFocused="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.507"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.421"
tools:ignore="TouchTargetSizeCheck" />
there is a poor solution to this problem:
android:textAlignment="center"
android:gravity="top"
tools:ignore="RtlCompat"
But in this case, the text that the user will enter will replace the hint and will be at the very top, which will not look nice. It would be possible to change the position of the text after the user has entered the text but for this you need to switch to code, not XML.
I also suspect that you plan to leave a prompt even after the user enters the text.
create hint in string.xml and then use in view
<EditText android:hint="#string/yourhint" />
You can try like this.
<EditText
android:id="#+id/usernameField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/round_10_background3"
android:drawableStart="#drawable/aa"
android:drawableTint="#color/white"
android:ems="10"
android:fontFamily="#font/caveat_semi_bold"
android:hint="USERNAME"
android:inputType="textPersonName"
android:padding="20dp"
android:textAlignment="center"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="20sp"
app:boxStrokeWidth="#null"
app:boxStrokeWidthFocused="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.507"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.421"
tools:ignore="TouchTargetSizeCheck" />

TextInputLayout having prefix and AutoCompleteTextView

I have a TextInputLayout that contains an AutoCompleteTextView. The idea is that when the user starts typing the mobile number, some automatic suggestions should come. Since it is specifically for phone numbers, a fixed prefix "+91" is required.
app:prefixText="+91" is used for this purpose. It does show the prefix but it gets mixed with the hint and doesn't come properly (as it comes when EditText is used instead of AutoCompleteTextView). Checkout the images to see expected vs actual look.
XML Layout file:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AddMob">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textInpLayoutMob"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="300dp"
android:layout_height="60dp"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
android:hint="Mobile No."
app:counterMaxLength="10"
app:endIconMode="clear_text"
app:helperTextTextColor="#color/purple_700"
app:layout_constraintBottom_toTopOf="#+id/textInpLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:prefixText="+91"
app:prefixTextColor="#color/purple_700">
<AutoCompleteTextView
android:id="#+id/autoCompleteTextViewMob"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="number"
android:maxLength="10" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Two-way databinding screen orentation change changes value

I'm using two-way databinding.
I have some data inside my Room databse and depending on what user writes in edittext showing that value in textview.
When I rotate screen, value gets updated. At fist its empty string then back to how it was. I know this is normal behavior of activity lifecycle.
So I would like to know the best way to get rid of this problem.
Thanks in advance.
EditText:
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/edtOddelek"
android:text="#={viewModel.loginOddelek}"
android:layout_marginEnd="8dp" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/textView3"
app:layout_constraintBottom_toBottomOf="#+id/textView3"
app:layout_constraintStart_toStartOf="#+id/edtGeslo" style="#style/adoEditTextStyle"/>
TextView:
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/txtOddelek"
android:layout_marginTop="8dp"
android:text="#{viewModel.oddelek}"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="12dp"
android:textColor="#color/orangeDark"
app:layout_constraintTop_toBottomOf="#+id/edtOddelek"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="12dp"/>
ViewModel:
val loginOddelek = NonNullMutableLiveData<String>("")
val oddelek:LiveData<String> = Transformations.switchMap(loginOddelek){
settingsModel.getSifrPr(it)
}

Placing a label in an EditText with specific design

I need to rewrite an iOS app for Android. The layout should look alike and I struggle with this issue:
I want to have a label on the left side of an EditText. It should stick there and don't let the EditText text overwrite the label:
I have tried using the interface builder in Android Studio like this :
The problem is, that the EditText's text does not hide behind the label.
Any ideas?
Edit:
Since my question was unclear:
I want a label inside an EditText.
It should stick to the left and have a margin to the input of EditText.
Try using below code in your xml and check, this will align your label text with password field where user can enter the password.
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/editbox_background"
android:layout_margin="#dimen/padding_medium">
<TextView
android:id="#+id/label"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:text="Password"
android:layout_marginTop="#dimen/padding_medium"
android:gravity="center_vertical"
android:paddingLeft="#dimen/padding_xsmall"
android:textSize="22sp"
android:textColor="#android:color/holo_red_dark"
app:layout_constraintHeight_default="wrap"
android:paddingStart="#dimen/padding_xsmall"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintWidth_default="wrap"/>
<com.google.android.material.textfield.TextInputEditText
android:layout_width="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="#id/label"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginStart="4dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:background="#null"
android:padding="4dp"
android:imeOptions="actionNext"
android:lines="1"
android:inputType="textPassword"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Note: I have used androidx controls as I am using new material design library. If you are using support library you would need to replace them with controls from support library.

EditText hidden behind Keyboard

I am having an issue with EditText as when I entered lines more than it is being shown on the screen then EditText is hidden behind keyboard if I try to edit line which is below the visible area of EditText but If I edit any of those line which is in visible area it adjust the view for keyboard properly. I am not understanding what mistake I am doing?
I have used adjustPan as softInputMode.
Please have a look at this video to better understand what I want to say:
EditTextIssueVideo
EditText Code:
<EditText
android:id="#+id/notes_et"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:ems="10"
android:gravity="top|left"
android:hint="Notes"
android:inputType="textMultiLine"
android:padding="10dp"
android:maxLines="5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/time_tp"
app:layout_constraintVertical_bias="0.202" />
It is probably because you have made your activity full screen. Try not making it full screen.
Check your AndroidManifest if you set
android:windowSoftInputMode="|adjustResize|adjustPan"
in any activity just remove it.

Categories

Resources