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)
}
Related
I'm want to enable button when repeatPasswordInputEditText is not empty, I tried to enable it with android:enabled="#{!repeatPasswordInputEditText.text.toString().isEmpty()}" but it doesn't work, why ? I'm also calling binding.lifecycleOwner = this in onCreateView
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/uRepeatPassword"
android:layout_width="384dp"
android:layout_height="75dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:hint="#string/repeat_password_hint"
app:passwordToggleEnabled="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/uNewPassword">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/repeatPasswordInputEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:text="#{passwordChangeViewModel._repeatPassword}"
android:background="#color/white"
app:passwordToggleEnabled="false"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:id="#+id/changePasswordButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Change Password"
android:enabled="#{!repeatPasswordInputEditText.text.toString().isEmpty()}"
app:layout_constraintTop_toBottomOf="#+id/uRepeatPassword">
</com.google.android.material.button.MaterialButton>
You cannot listen to changes in EditText text inside the XML itself.
In your code, you have
android:text="#{passwordChangeViewModel._repeatPassword}"
Seems like you are trying to use two-way data-binding here where _repeatPassword is a MutableLiveData<String>. This won't work because it is just one-way right now. You need to replace # with #= to make it two way.
Now that you have two-way data binding working, you can use the value of this live data to enable/disable your button:
android:enabled="#{!_repeatPassword.empty}"
If you are not using two-way data binding, you will have to put the logic in your Activity/Fragment:
repeatPasswordInputEditText.doAfterTextChanged { text ->
changePasswordButton.enabled = text.isNotEmpty()
}
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);
I am getting problem in data binding. In the application, the default value of TextView is not showing. TextView is just blank.
XML File
<TextView
android:id="#+id/tvDoctorName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text='#{doctorsInfo.name , default="FetchingData"}'
android:textAppearance="#style/SuperHeadingText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Use this:
android:text='#{doctorsInfo.name, default="FetchingData"}'
OR
android:text="#{doctorsInfo.name, default=`FetchingData`}"
Instead of:
android:text='#{doctorsInfo.name , default="FetchingData"}'
Use this
android:text='#{doctorsInfo.name, default="FetchingData"}'
I have an EditText set as android:inputType="number" and have a hint string set on it. The issue is that when running the app the text 0 is added to the edittext and the hint is not displaying.
The android:text value is not set. I have also tried to set it to empty but without any effect.
Any clue on how to solve this?
Edit: I did try and do as said in the answer in comment but doesn't work.
The code is as follows:
<EditText
android:id="#+id/txtEngineCapacity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#drawable/maptextbox"
android:ems="10"
android:fontFamily="#font/lato_light"
android:hint="#string/vehicleCC"
android:inputType="number"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/txtChassisNumber" />
This is my button in activity_main.xml
<Button
android:text="#string/fs"
android:layout_width="154dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/textView"
android:layout_marginEnd="12dp"
android:layout_marginRight="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp" android:layout_marginBottom="32dp"
app:layout_constraintBottom_toTopOf="#+id/imageView"
app:layout_constraintHorizontal_bias="0.451"
app:layout_constraintVertical_bias="0.069" android:id="#+id/button2"
style="#style/Widget.AppCompat.Button"
android:background="#android:color/holo_green_dark"
android:onClick="flowerpage"/>
This is the button being created I believe in the Mainactivity.kt
fun flowerpage(view: activity2) {
}
I am new to Kotlin, however, I used to HTML where you could connect two web pages together via HTML link, however, this doesn't seem to be as simple.
button2.setOnClickListener {flower_button()}
this shows compiler error. am I missing an import..??
With kotlin you just need to give your button an id in the XML, then you can do this:
btn_id_you_gave.setOnClickListener {doSomething()}
private fun doSomething() {...}
You don't need to do the OnClick thing in the XML you want to take max advantage of Kotlin.