So I have these values (Long and Double) in three textfields. They represent time, pace, and distance.
Now, to prevent user error I want to make them choose the values using numberPickers. I want to replace those textfields with those pickers.
This is what I have:
val time = timeTxtField as TextView
val distance = distanceTxtField as TextView
val pace = paceTxtField as TextView
var resetRunningBtn = clearBtn
val pickerMinutes = numberPicker as NumberPicker
val pickerSeconds = numberPickerSeconds as NumberPicker
pickerMinutes.minValue = 0
pickerMinutes.maxValue = 59
pickerMinutes.wrapSelectorWheel = false
pickerSeconds.minValue = 0
pickerSeconds.maxValue = 60
pickerSeconds.wrapSelectorWheel = false
calculateBtn.setOnClickListener {
when {
time.text.isEmpty() && (distance.text.isNotEmpty() && pace.text.isNotEmpty()) ->
calculatePace(null, distance.text.toString().toDouble(), pace.text.toString())
distance.text.isEmpty() && (time.text.isNotEmpty() && pace.text.isNotEmpty()) ->
calculatePace(time.text.toString(), null, pace.text.toString())
pace.text.isEmpty() && (time.text.isNotEmpty() && distance.text.isNotEmpty()) ->
calculatePace(time.text.toString(), distance.text.toString().toDouble(), null)
else -> {
Toast.makeText(this, "Please check fields",
Toast.LENGTH_SHORT).show()
}
}
}
As you can see I have the pickers set and all that. I tried to call something like .value but it didn't work.
This is my layout, so you can have a bit of understanding of the concept.
Any tips? :)
Thanks
EDIT XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.reecreate.woderator2.Controller.RunningCalculatorActivity">
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Running Pace Calculator"
android:textSize="24sp"
android:textStyle="bold"
android:inputType="numberDecimal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/distanceTxtField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="28dp"
android:ems="10"
android:hint="distance..."
android:inputType="number|numberDecimal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/radioType" />
<EditText
android:id="#+id/timeTxtField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="time mm:ss"
android:inputType="time"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/distanceTxtField" />
<EditText
android:id="#+id/paceTxtField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="pace mm:ss 10:21"
android:inputType="time"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/timeTxtField" />
<RadioGroup
android:id="#+id/radioType"
android:layout_width="280dp"
android:layout_height="56dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView5">
<RadioButton
android:id="#+id/milesDistanceRadioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="140dp"
android:layout_marginTop="16dp"
android:text="Miles"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toRightOf="#id/kmDistanceRadioBtn"
app:layout_constraintTop_toBottomOf="#id/textView5" />
<RadioButton
android:id="#+id/kmDistanceRadioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:text="KM"
app:layout_constraintEnd_toStartOf="#+id/milesRadioBtn"
app:layout_constraintHorizontal_bias="0.694"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RadioGroup>
<RadioGroup
android:id="#+id/paceRadioGroup"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.294"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/paceTxtField">
<RadioButton
android:id="#+id/milesPaceRadioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="140dp"
android:layout_marginTop="16dp"
android:text="MILES PACE"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toRightOf="#id/kmDistanceRadioBtn"
app:layout_constraintTop_toTopOf="parent" />
<RadioButton
android:id="#+id/kmPaceRadioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:text="KM PACE"
app:layout_constraintEnd_toStartOf="#+id/milesDistanceRadioBtn"
app:layout_constraintHorizontal_bias="0.694"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RadioGroup>
<Button
android:id="#+id/calculateBtn"
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/colorAccent"
android:text="Calculate"
android:textColor="#android:color/background_light"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/result"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:background="#drawable/border"
android:hint="Result"
android:textAlignment="center"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/paceRadioGroup" />
<Button
android:id="#+id/clearBtn"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:background="#color/colorAccent"
android:text="CLEAR"
android:textColor="#android:color/background_light"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/result" />
<NumberPicker
android:id="#+id/numberPicker"
android:layout_width="64dp"
android:layout_height="119dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.407"
app:layout_constraintTop_toBottomOf="#+id/radioType" />
<NumberPicker
android:id="#+id/numberPickerSeconds"
android:layout_width="64dp"
android:layout_height="119dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/numberPicker" />
EDIT VALUE LISTENER
I added this, but not doing anything
var pickedValue: Int = pickerMinutes.value
time.setText(Integer.toString(pickedValue))
................
when {
pickerMinutes.isClickable -> time.setText(Integer.toString(pickedValue))
}
numberPicker.setOnValueChangedListener { pickerMinutes, oldVal, newVal ->
//store values in variable for use later
}
A few general Kotlin things to note: I see you are setting variables and casting the the number pickers. You don't need to do this. In Kotlin, with the correct import, something like: kotlinx.android.synthetic.main.my_layout_file.*, you can simply call the xml by its id without casting, like so: numberPicker. That goes for your other items as well, such as the TextViews.
The other thing is that you shouldn't have to call Integer.toString(pickedValue). You can just call pickedValue.toString().
Lastly, since I am unable to see exactly how you are testing the app, my suggestion would be to do the following:
Create variables to store the picker values:
var numPickerVal = 0;
var secondsPickerVal = 0;
Then set a change listener for each of the pickers:
numberPicker.setOnValueChangedListener { picker, oldVal, newVal ->
numPickerVal = newVal
}
numberPickerSeconds.setOnValueChangedListener { picker, oldVal, newVal ->
secondsPickerVal = newVal
}
Related
I'm creating a View Stub for my login screen, however I have a question. When I try to set an setOnClickListener on my login button in my viewstub_login.kt file, the button action won't go through. Do I need to change my extension on my Kotlin file, or do I need to put my setOnClickListener in the onStart() function? Thank you!
viewstub_login.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#BF090909">
<androidx.cardview.widget.CardView
android:id="#+id/loginCardView"
android:layout_width="0dp"
android:layout_height="300dp"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
app:cardBackgroundColor="#color/main_purple"
app:cardCornerRadius="8dp"
app:cardElevation="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/logoLoginImageView"
android:layout_width="90dp"
android:layout_height="35dp"
android:layout_marginTop="12dp"
android:contentDescription="#string/login_logo_image"
android:scaleType="fitXY"
android:src="#drawable/skedaddle_services_"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/emailTextInputLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="24dp"
android:textColorHint="#color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/logoLoginImageView">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/emailInputTextView"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#drawable/textview_underlinr"
android:drawableStart="#drawable/outline_mail_white_24"
android:drawablePadding="5dp"
android:hint="#string/e_mail"
android:textColor="#color/white"
android:textColorHint="#color/white" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/passwordTextInputLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="24dp"
android:hint="#string/password"
android:textColorHint="#color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/emailTextInputLayout"
app:passwordToggleEnabled="true"
app:passwordToggleTint="#color/white">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/passwordTextInputView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/textview_underlinr"
android:drawableStart="#drawable/outline_lock_white_24"
android:drawablePadding="5dp"
android:hint="#string/password"
android:inputType="textPassword"
android:textColor="#color/white"
android:textColorHint="#color/white" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="#+id/forgotPasswordButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:background="#android:color/transparent"
android:text="#string/forgot_password"
android:textAlignment="textEnd"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="12sp"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/passwordTextInputLayout" />
<Button
android:id="#+id/cancelButton"
android:layout_width="120dp"
android:layout_height="48dp"
android:background="#color/black"
android:text="#android:string/cancel"
android:textAllCaps="false"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/secondLoginButton"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/forgotPasswordButton" />
<Button
android:id="#+id/secondLoginButton"
android:layout_width="120dp"
android:layout_height="48dp"
android:layout_marginStart="32dp"
android:background="#color/main_teal"
android:text="#string/log_in"
android:textAllCaps="false"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="#+id/cancelButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/cancelButton"
app:layout_constraintTop_toTopOf="#+id/cancelButton"
tools:ignore="TextContrastCheck" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
viewstub_login.kt
class LoginViewStub : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.viewstub_login)
val emailTextView = findViewById<TextInputEditText>(R.id.emailInputTextView)
val passwordTextView = findViewById<TextInputEditText>(R.id.passwordTextInputView)
val forgetPasswordButton = findViewById<Button>(R.id.forgotPasswordButton)
val loginButton = findViewById<Button>(R.id.secondLoginButton)
val cancelButton = findViewById<Button>(R.id.cancelButton)
val emailText = emailTextView.text
val passwordText = passwordTextView.text
loginButton.setOnClickListener {
if (emailText.isEmpty(emailTextView.text) || passwordText.isEmpty(passwordTextView.text)) {
val dialogBuilder = AlertDialog.Builder(this)
dialogBuilder.setMessage("Please fill out all fields.").setCancelable(false).setPositiveButton("Okay", DialogInterface.OnClickListener {
dialog, id -> finish()
}).setNegativeButton("Cancel", DialogInterface.OnClickListener {
dialog, id -> dialog.cancel()
})
val alert = dialogBuilder.create()
alert.show()
}
}
cancelButton.setOnClickListener {
}
forgetPasswordButton.setOnClickListener {
}
}
}
private fun Boolean.isEmpty(str: CharSequence?): Boolean {
return str == null || str.length == 0
}
click listener is correct, but inside the block, your code is not correct
because emailText and passwordText is not null, as you have defined above, thus the if condition returning false and your code just going over it
android studio 3.6
I need to show custom view when click on map's marker:
implementation 'com.google.android.gms:play-services-maps:17.0.0'
snippet to show marker info window
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
mMap.setInfoWindowAdapter(object : GoogleMap.InfoWindowAdapter {
override fun getInfoWindow(marker: Marker): View? {
return null
}
override fun getInfoContents(marker: Marker): View? {
val markerLatLng = marker.position
val selectGazStattion =
gazStationsList.first { it.latitude == markerLatLng.latitude && it.longitude == markerLatLng.longitude }
val customView = layoutInflater.inflate(
R.layout.map_marker_info_content_layout, null
)
val textView : TextView = customView.findViewById(R.id.addressValueTextView)
textView.setText(selectGazStattion.address)
return customView
}
})
here map_marker_info_content_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/titleContainerLayout"
android:layout_width="0dp"
android:layout_height="#dimen/min_height"
android:layout_marginStart="#dimen/half_default_margin"
android:layout_marginTop="#dimen/default_margin"
android:layout_marginEnd="#dimen/half_default_margin"
android:background="#drawable/bottom_border_bg"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/titleTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/half_default_margin"
android:gravity="center|start"
android:text="Title"
android:textColor="#android:color/black"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/stateTtextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/open"
android:textColor="#android:color/holo_green_light"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/titleTextView"
app:layout_constraintStart_toStartOf="#+id/titleTextView"
app:layout_constraintTop_toBottomOf="#+id/titleTextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/containerAgentInfo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/half_default_margin"
android:background="#drawable/bottom_border_bg"
app:layout_constraintEnd_toEndOf="#+id/titleContainerLayout"
app:layout_constraintStart_toStartOf="#+id/titleContainerLayout"
app:layout_constraintTop_toBottomOf="#+id/titleContainerLayout">
<TextView
android:id="#+id/addressLabelTextView"
style="#style/mapMarkerInfoItemLabelTextStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/half_default_margin"
android:layout_marginEnd="#dimen/half_default_margin"
android:text="#string/address_colon"
app:layout_constraintEnd_toStartOf="#+id/addressValueTextView"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/addressValueTextView"
style="#style/mapMarkerInfoItemLabelTextStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="end"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/addressLabelTextView"
app:layout_constraintTop_toTopOf="#+id/addressLabelTextView" />
<TextView
android:id="#+id/workingHoursLabelTextView"
style="#style/mapMarkerInfoItemLabelTextStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/half_default_margin"
android:text="#string/working_hours_colon"
app:layout_constraintEnd_toEndOf="#+id/addressLabelTextView"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="#+id/addressLabelTextView"
app:layout_constraintTop_toBottomOf="#+id/addressLabelTextView" />
<TextView
android:id="#+id/workingHoursValueTextView"
style="#style/mapMarkerInfoItemLabelTextStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="end"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="#+id/addressValueTextView"
app:layout_constraintTop_toTopOf="#+id/workingHoursLabelTextView" />
<TextView
android:id="#+id/phoneLabelTextView"
style="#style/mapMarkerInfoItemLabelTextStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/half_default_margin"
android:text="#string/phone_colon"
app:layout_constraintEnd_toEndOf="#+id/addressLabelTextView"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="#+id/workingHoursLabelTextView"
app:layout_constraintTop_toBottomOf="#+id/workingHoursLabelTextView" />
<TextView
android:id="#+id/phoneValueTextView"
style="#style/mapMarkerInfoItemLabelTextStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="end"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="#+id/addressValueTextView"
app:layout_constraintTop_toTopOf="#+id/phoneLabelTextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/containerServices"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/half_default_margin"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/titleContainerLayout"
app:layout_constraintStart_toStartOf="#+id/titleContainerLayout"
app:layout_constraintTop_toBottomOf="#+id/containerAgentInfo">
<TextView
android:id="#+id/servicesTextView"
style="#style/mapMarkerInfoItemLabelTextStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/half_default_margin"
android:layout_marginEnd="#dimen/half_default_margin"
android:text="#string/services_colon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.yarolegovich.discretescrollview.DiscreteScrollView
android:id="#+id/agetServiceDiscreteScrollView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:dsv_orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/servicesTextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
But when I click on marker I get this:
Why not show my custom view?
P.S. If I use LinearLayout then success show info window. But with androidx.constraintlayout.widget.ConstraintLayout not work
I have a view that uses constraint layout. The view has an edit text and expects the soft keyboard input to show up. There are two buttons pinned to the bottom of the screen(Previous and Next). When the soft keyboard opens up, the two buttons hide behind the keyboard. However, I want the layout to resize so that the buttons are above the soft keyboard and not behind it.
I have the following in my manifest.
<activity
android:name=".activity.SomeActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize">
</activity>
I have looked at various other posts and tried a few different things like, adjustPan. I have set
android:fitsSystemWindows="true"
But both of them did not work.
My Constraint layout looks like this.
<?xml version="1.0" encoding="utf-8"?>
<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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorLibWhite"
android:focusable="true"
android:focusableInTouchMode="true"
tools:context=".ui.main.question.QuestionFragment">
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="8dp"
android:layout_marginTop="4dp"
android:background="#color/colorLibWhite"
android:indeterminate="false"
android:progressDrawable="#drawable/progress_bar_background"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="#+id/sectionHeader"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="#color/colorLibLighterGray"
android:ellipsize="end"
android:maxLines="2"
android:paddingStart="16dp"
android:paddingTop="8dp"
android:paddingEnd="16dp"
android:paddingBottom="8dp"
android:text="new section"
android:textColor="#color/colorLibGray"
android:textSize="12sp"
android:visibility="gone"
app:layout_constraintBottom_toTopOf="#+id/questionLabel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/progressBar"/>
<TextView
android:id="#+id/questionLabel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:text="#string/placeholder"
android:textAlignment="center"
app:layout_constraintBottom_toTopOf="#+id/inputContainerCL"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/sectionHeader"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/inputContainerCL"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="#+id/prevNextContainer"
app:layout_constraintTop_toBottomOf="#+id/questionLabel">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/inputContainer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="#+id/hintText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:text="Hint text goes here"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/inputContainer"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/prevNextContainer"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginBottom="16dp"
android:background="#drawable/primary_button"
android:text="#string/question_previous"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<Button
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="16dp"
android:background="#drawable/primary_button"
android:text="#string/question_next"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
EditText is added programatically.
var editText = CustomEditText(context)
editText.apply {
id = View.generateViewId()
layoutParams = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT)
setSingleLine(true)
imeOptions = EditorInfo.IME_ACTION_DONE
val questionType = question.validation?.type
if (questionType == Validation.Type.INTEGER) {
inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_SIGNED
} else if (questionType == Validation.Type.NUMBER) {
inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_SIGNED or InputType.TYPE_NUMBER_FLAG_DECIMAL
} else if (questionType == Validation.Type.MRN) {
inputType = InputType.TYPE_CLASS_NUMBER
filters = arrayOf(InputFilter.LengthFilter(9))
} else if (questionType == Validation.Type.ZIP_CODE) {
inputType = InputType.TYPE_CLASS_PHONE
filters = arrayOf(InputFilter.LengthFilter(10))
} else if (questionType == Validation.Type.PHONE) {
inputType = InputType.TYPE_CLASS_NUMBER
editText.setSelection(editText.text?.length!!)
editText.addTextChangedListener(PhoneFormatterTextWatcher(editText))
}
tag = TAG_FOCUS_FIRST
}
editText.setText(question.value ?: "")
if (question.validation?.type == Validation.Type.ZIP_CODE) {
editText.addTextChangedListener(ZipCodeTextWatcher(editText, question, updateCallback))
} else {
editText.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
//
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
//
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
question.value = if (s != null && s.isNotBlank()) s.toString() else null
updateCallback()
}
}
)
}
editText.setOnEditorActionListener() { v, actionId, event ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
doneCallback()
true
} else {
false
}
}
return editText
Here is the code that adds this view to input container
inputContainer.addView(editText)
Where is your EditText view?.I think you must learn about Constraint Layout. how ever you can try this xml layout code..
Add your styles for views.
<?xml version="1.0" encoding="utf-8"?>
<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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
tools:context=".ui.main.question.QuestionFragment">
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="411dp"
android:layout_height="7dp"
android:indeterminate="false"
android:progress="40"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/sectionHeader"
android:layout_width="99dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:maxLines="2"
android:paddingStart="16dp"
android:visibility="gone"
android:paddingTop="8dp"
android:paddingEnd="16dp"
android:paddingBottom="8dp"
android:text="new section"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/progressBar" />
<TextView
android:id="#+id/questionLabel"
android:layout_width="379dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="questionLabel"
android:layout_marginRight="8dp"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/sectionHeader" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/inputContainerCL"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintBottom_toTopOf="#+id/prevNextContainer"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/questionLabel">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/inputContainer"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
app:layout_constraintBottom_toTopOf="#+id/hintText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" >
<EditText
android:id="#+id/editText2"
android:layout_width="0dp"
android:layout_height="0dp"
android:ems="10"
android:gravity="top"
android:text="text"
android:inputType="textMultiLine"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="#+id/hintText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:text="Hint text goes here"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/prevNextContainer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginBottom="8dp"
android:text="question_previous"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="16dp"
android:text="question_next"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
this is the preview
I have the following list of checkboxes and TextInputLayouts
<CheckBox
android:text="#string/has_lession"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/hasLeassionCheckBox"
android:layout_marginTop="16dp" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="32dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="32dp"
app:layout_constraintHorizontal_bias="1.0"
android:onClick="onCheckboxClicked"/>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="32dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="32dp"
android:hint="#string/lesion_comments" app:layout_constraintHorizontal_bias="0.0"
android:layout_marginTop="16dp" app:layout_constraintTop_toBottomOf="#+id/hasHadLessionCheckBox"
android:visibility="gone" android:id="#+id/lesionCommentsField">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="false"/>
</com.google.android.material.textfield.TextInputLayout>
<CheckBox
android:text="#string/has_had_lession"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/hasHadLessionCheckBox" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="32dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="32dp" android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="#+id/hasLeassionCheckBox"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:text="#string/uses_tobaco"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/tobacoCheckBox" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="32dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="32dp" android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="#+id/lesionCommentsField"
android:onClick="onCheckboxClicked"/>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="32dp"
android:layout_marginEnd="32dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"
android:hint="#string/years_of_tobacco_use"
app:layout_constraintTop_toBottomOf="#+id/tobacoCheckBox" android:id="#+id/yearsOfTobacoUseField"
android:visibility="gone">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:inputType="number"/>
</com.google.android.material.textfield.TextInputLayout>
<CheckBox
android:text="#string/uses_alcohol"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/alcoholCheckbox" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="32dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="32dp" android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="#+id/yearsOfTobacoUseField"
android:onClick="onCheckboxClicked"/>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/alcoholYearsField" app:layout_constraintStart_toEndOf="#+id/drinksPerWeek"
app:layout_constraintHorizontal_bias="0.5" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="32dp" android:layout_marginStart="8dp"
app:layout_constraintTop_toTopOf="#+id/drinksPerWeek"
app:layout_constraintBottom_toBottomOf="#+id/drinksPerWeek" android:hint="#string/years_alcohol_used"
android:visibility="gone">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/drinksPerWeek" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHorizontal_bias="0.5" app:layout_constraintEnd_toStartOf="#+id/alcoholYearsField"
android:layout_marginStart="32dp" android:layout_marginEnd="8dp" android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="#+id/alcoholCheckbox" android:hint="#string/drinks_per_week"
android:visibility="gone">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:inputType="number"/>
</com.google.android.material.textfield.TextInputLayout>
<CheckBox
android:text="#string/history_of_cancer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/cancerCheckBox" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="32dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="32dp" android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="#+id/drinksPerWeek"
android:onClick="onCheckboxClicked"/>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/cancerCountField" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHorizontal_bias="0.5" app:layout_constraintEnd_toStartOf="#+id/cancerYearsField"
app:layout_constraintTop_toTopOf="#+id/cancerYearsField"
app:layout_constraintBottom_toBottomOf="#+id/cancerYearsField" android:layout_marginStart="32dp"
android:layout_marginEnd="8dp" android:visibility="gone"
android:hint="#string/cancer_count">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/cancerYearsField"
app:layout_constraintStart_toEndOf="#+id/cancerCountField" app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="#+id/cancerCheckBox" android:layout_marginStart="8dp"
android:layout_marginEnd="32dp" android:visibility="gone"
android:hint="#string/years_since_cancer">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"/>
</com.google.android.material.textfield.TextInputLayout>
<CheckBox
android:text="#string/history_of_oral_pain"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/oralPainCheckBox" android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="#+id/cancerCountField"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="32dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="32dp"
android:onClick="onCheckboxClicked"/>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/painSeverityField" android:hint="#string/pain_severity"
app:layout_constraintStart_toEndOf="#+id/yearsOfOralPainField"
app:layout_constraintHorizontal_bias="0.5" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp" app:layout_constraintTop_toBottomOf="#+id/oralPainCheckBox"
android:layout_marginStart="8dp" android:layout_marginEnd="32dp" android:visibility="gone">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/yearsOfOralPainField"
android:hint="#string/years_of_oral_pain" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHorizontal_bias="0.5" app:layout_constraintEnd_toStartOf="#+id/painSeverityField"
app:layout_constraintTop_toTopOf="#+id/painSeverityField"
app:layout_constraintBottom_toBottomOf="#+id/painSeverityField" android:layout_marginStart="32dp"
android:layout_marginEnd="8dp" android:visibility="gone">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:text="#string/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/nextPatientButton" android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="#+id/painSeverityField" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="32dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
The TextInputLayouts are shown and hidden based on if cirtan checkboxes are clicked or not. Here is the code that handles that:
fun onCheckboxClicked(view: View) {
if (view is CheckBox) {
val checked: Boolean = view.isChecked
val test = view.id
when (view.id) {
R.id.hasLeassionCheckBox->{
if(checked){
lesionCommentsField.visibility = View.VISIBLE
}
else{
lesionCommentsField.visibility = View.GONE
}
}
R.id.tobacoCheckBox->{
if(checked){
yearsOfTobacoUseField.visibility = View.VISIBLE
}
else{
yearsOfTobacoUseField.visibility = View.GONE
}
}
R.id.alcoholCheckbox->{
if(checked){
drinksPerWeek.visibility = View.VISIBLE
alcoholYearsField.visibility = View.VISIBLE
Log.i("PATIENT ACTIVITY", "DRINKS PER WEEK VIEW: ${drinksPerWeek.visibility}")
}
else{
drinksPerWeek.visibility = View.GONE
alcoholYearsField.visibility = View.GONE
}
}
R.id.cancerCheckBox-> {
if (checked) {
cancerYearsField.visibility = View.VISIBLE
cancerCountField.visibility = View.VISIBLE
} else {
cancerYearsField.visibility = View.GONE
cancerCountField.visibility = View.GONE
}
}
R.id.oralPainCheckBox->{
if(checked){
yearsOfOralPainField.visibility = View.VISIBLE
painSeverityField.visibility = View.VISIBLE
}
else{
yearsOfOralPainField.visibility = View.GONE
painSeverityField.visibility = View.GONE
}
}
}
}
}
The way this is supposed to work is that certain TextInputLayouts are displayed when the user selects certain checkboxes. For example: if the user selects the has_lession checkbox, it is supposed to display the lesionCommentsField TextInputLayout.
The first time the user goes through this form, this code worked perfectly. For all the checkboxes and TextInputLayouts. The second time the user is on this form, the checkboxes that were clicked the last time around don't make their corresponding TextInputLayouts visible. For example: if the user selects the has_lession checkbox last time they used the form and selects the same checkbox this time around the lesionCommentsField is not displayed.
The other checkboxes that were not clicked in the previous use of the form work just fine, it is only the checkboxes that were used in previous use of the form that misbehaves.
The issue goes away when the app is restarted.
I have debugged and checked that the fields do get set to visible, they are just not displayed on the screen.
I have this inside a fragment and the checkbox code is in its parent activity. Not sure if this makes a difference.
How would I go about debugging and resolving this?
Editing my answer, I just understood multiple views can be visible at the same time.
Why are you not writing this separately for all the checkbox onCheckChange method instead of onClick?
hasLeassionCheckBox.setOnCheckedChangeListener { compoundButton, isChecked ->
if (isChecked) {
lesionCommentsField.visibility = View.VISIBLE
} else {
lesionCommentsField.visibility = View.GONE
}
}
Currently I have a problem that has been driving me insane. Basically I have a recyclerview that display a list of cards. The problem is whenever I ran my app no cards will be shown. I've already check my size and it's correct. I've been trying to fix this for hours.Have a look on my code,
card_list.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="8dp"
android:gravity="center_horizontal"
android:padding="25dp"
card_view:cardBackgroundColor="#color/cardview_light_background"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
card_view:cardUseCompatPadding="true">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.Guideline
android:id="#+id/guideline_horizontal_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="103dp"
tools:layout_editor_absoluteY="192dp"
tools:layout_editor_absoluteX="8dp" />
<ImageView
android:id="#+id/offer_image"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:contentDescription=""
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/job_card_title"
style="#style/ShadowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="Job Title"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="#+id/guideline_horizontal_top"
android:layout_marginTop="8dp"
app:layout_constraintHorizontal_bias="0.502"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<ImageView
android:id="#+id/right_image"
style="#style/ShadowText"
android:layout_width="130dp"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/job_card_apply"
tools:ignore="ContentDescription"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/job_card_title"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/job_card_company_name"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<ImageView
android:id="#+id/left_image"
android:layout_width="130dp"
android:layout_height="40dp"
android:layout_alignTop="#+id/right_image"
android:layout_centerHorizontal="true"
android:layout_gravity="center_vertical"
android:src="#drawable/job_card_reject"
tools:ignore="ContentDescription"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/job_card_title"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/job_card_company_name"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<android.support.constraint.Guideline
android:id="#+id/guideline_horizontal_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="261dp"
tools:layout_editor_absoluteY="350dp"
tools:layout_editor_absoluteX="8dp" />
<TextView
android:id="#+id/job_card_company_name"
style="#style/ShadowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="Company Name"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="28sp"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/guideline_horizontal_bottom"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<ImageView
android:id="#+id/job_card_image_location"
android:layout_width="28dp"
android:layout_height="28dp"
card_view:srcCompat="#drawable/ic_location_on_black"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="#+id/offer_image"
app:layout_constraintRight_toLeftOf="#+id/job_card_company_location"
app:layout_constraintHorizontal_bias="1.0"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/guideline3"
android:layout_marginStart="8dp" />
<TextView
android:id="#+id/job_card_company_location"
style="#style/ShadowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="11dp"
android:layout_toEndOf="#id/job_card_image_location"
android:text="Company Location"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="22sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/guideline3"
app:layout_constraintLeft_toLeftOf="#+id/job_card_image_location"
app:layout_constraintRight_toRightOf="#+id/offer_image"
app:layout_constraintTop_toTopOf="#+id/guideline_horizontal_bottom"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintHorizontal_bias="0.21" />
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/guideline3"
app:layout_constraintGuide_begin="308dp"
android:orientation="horizontal"
tools:layout_editor_absoluteY="397dp"
tools:layout_editor_absoluteX="8dp" />
<ImageView
android:id="#+id/job_card_image_job_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_weight="1"
card_view:srcCompat="#drawable/ic_action_work"
app:layout_constraintRight_toLeftOf="#+id/job_card_job_type"
android:layout_marginRight="0dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintTop_toTopOf="#+id/guideline3"
android:layout_marginTop="8dp" />
<TextView
android:id="#+id/job_card_job_type"
style="#style/ShadowText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:layout_toEndOf="#id/job_card_image_job_type"
android:text="Job Type"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="22sp"
android:textStyle="bold"
app:layout_constraintTop_toTopOf="#+id/guideline3"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintVertical_bias="0.014"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
adapter.class
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
cardPosition = position - 2;
Log.d("Details Before : ","" + position);
if(v == null){
LayoutInflater inflater = LayoutInflater.from(context);
v = inflater.inflate(R.layout.card_list,parent,false);
}
randomImage.add(R.drawable.background_image_1);
randomImage.add(R.drawable.background_image_2);
randomImage.add(R.drawable.background_image_3);
randomImage.add(R.drawable.background_image_4);
randomImage.add(R.drawable.background_image_5);
randomImage.add(R.drawable.background_image_6);
randomImage.add(R.drawable.background_image_7);
randomImage.add(R.drawable.background_image_8);
Collections.shuffle(randomImage);
ImageView imageView = (ImageView) v.findViewById(R.id.offer_image);
//imageView.setImageResource(R.drawable.image_test);
//Picasso.with(context).load(R.drawable.job_card_background_1).transform(new BlurTransformation(context)).into(imageView);
Glide.with(context)
.load(randomImage.get(0))
.into(imageView);
TextView jobTitle = (TextView) v.findViewById(R.id.job_card_title);
TextView companyName = (TextView) v.findViewById(R.id.job_card_company_name);
TextView companyLocation = (TextView) v.findViewById(R.id.job_card_company_location);
TextView jobType = (TextView) v.findViewById(R.id.job_card_job_type);
ImageView imageLocation = (ImageView) v.findViewById(R.id.job_card_image_location);
ImageView imageJobType = (ImageView) v.findViewById(R.id.job_card_image_job_type);
jobTitle.setText(cardData.get(position).getJobTitle());
companyName.setText(cardData.get(position).getCompanyName());
/**
* If condition checking for data [company location]
*/
if((cardData.get(position).getCity().isEmpty() || cardData.get(position).getCity() == null) &&
(cardData.get(position).getState().isEmpty() || cardData.get(position).getState().equals("null"))){
companyLocation.setVisibility(View.INVISIBLE);
imageLocation.setVisibility(View.INVISIBLE);
}
else if((!cardData.get(position).getCity().isEmpty() || !cardData.get(position).getCity().equals("null")) &&
(cardData.get(position).getState() == null || cardData.get(position).getState().equals("null")))
companyLocation.setText(cardData.get(position).getCity());
else if((cardData.get(position).getCity().isEmpty() || cardData.get(position).getCity().equals("null")) &&
(!cardData.get(position).getState().isEmpty() || !cardData.get(position).getState().equals("null")))
companyLocation.setText(cardData.get(position).getState());
else
companyLocation.setText(cardData.get(position).getCity() + ", " + cardData.get(position).getState());
/**
* If condition checking for data [job type]
*/
if((cardData.get(position).getJobType().isEmpty() || cardData.get(position).getJobType().equals("null")) &&
(cardData.get(position).getJobPosition().isEmpty() || cardData.get(position).getJobPosition().equals("null"))){
jobType.setVisibility(View.INVISIBLE);
imageJobType.setVisibility(View.INVISIBLE);
}
else if((!cardData.get(position).getJobType().isEmpty() || !cardData.get(position).getJobType().equals("null")) &&
(cardData.get(position).getJobPosition().isEmpty() || cardData.get(position).getJobPosition().equals("null")))
jobType.setText(cardData.get(position).getJobPosition());
else if((cardData.get(position).getJobType().isEmpty() || cardData.get(position).getJobType().equals("null")) &&
(!cardData.get(position).getJobPosition().isEmpty() || !cardData.get(position).getJobPosition().equals("null")))
jobType.setText(cardData.get(position).getJobPosition());
else
jobType.setText(cardData.get(position).getJobType() + ", " + cardData.get(position).getJobPosition());
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("Layer type: ", Integer.toString(v.getLayerType()));
Log.i("Hardware Accel type:", Integer.toString(View.LAYER_TYPE_HARDWARE));
}
});
return v;
}
Is not your card view layout name should be card_list.xml instead of activity_job_list_card_view
in
v = inflater.inflate(R.layout.activity_job_list_card_view,parent,false);
First of all to use adapter for CardView and RecyclerView RecyclerView.Adapter is best use and make sure are you inflating correct layout in your adapter.