So I have it set up when the character count shows when the corresponding EditText box is of focus. And not visible when the user clicks somewhere else. but when I go into a different page and then come back to the previous page, all the character count fields pop up, but then it functions back once you click on each individual field. So I would like for those character fields to stay non-visible when I return to the page from a different page.
So here's the code that's controlling the Character Count TextView.
/** Having the Character Count Box observe the state of the corresponding EditText Box*/
itemView.date_time_add.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
if (!hasFocus) itemView.cCount1.visibility = View.GONE
else itemView.cCount1.visibility = View.VISIBLE
}
itemView.cable_run_add.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
if (!hasFocus) itemView.cCount2.visibility = View.GONE
else itemView.cCount2.visibility = View.VISIBLE
}
itemView.entrance_add.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
if (!hasFocus) itemView.cCount3.visibility = View.GONE
else itemView.cCount3.visibility = View.VISIBLE
}
itemView.door_lock_add.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
if (!hasFocus) itemView.cCount4.visibility = View.GONE
else itemView.cCount4.visibility = View.VISIBLE
}
itemView.handicap_operator_add.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
if (!hasFocus) itemView.cCount5.visibility = View.GONE
else itemView.cCount5.visibility = View.VISIBLE
}
/** A custom character count feature with an EditText and TextView */
itemView.date_time_add.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {
itemView.cCount1.visibility = View.GONE
}
override fun onTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {
if (s.isNullOrEmpty()) {
itemView.cCount1.visibility = View.GONE
} else {
itemView.cCount1.visibility = View.VISIBLE
}
}
override fun afterTextChanged(s: Editable?) {
if (!s.isNullOrEmpty()) {
itemView.cCount1.text = "${255 - s.length} Characters Remaining"
}
else {
itemView.cCount1.visibility = View.GONE
}
}
})
itemView.cable_run_add.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {
itemView.cCount2.text = ""
}
override fun onTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {
if (s.isNullOrEmpty()) {
itemView.cCount2.visibility = View.GONE
} else {
itemView.cCount2.visibility = View.VISIBLE
}
}
override fun afterTextChanged(s: Editable?) {
if (!s.isNullOrEmpty()) {
itemView.cCount2.text = "${255 - s.length} Characters Remaining"
}
else{
itemView.cCount2.visibility = View.GONE
}
}
})
itemView.entrance_add.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {
itemView.cCount3.text = ""
}
override fun onTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {
if (s.isNullOrEmpty()) {
itemView.cCount3.visibility = View.GONE
} else {
itemView.cCount3.visibility = View.VISIBLE
}
}
override fun afterTextChanged(s: Editable?) {
if (!s.isNullOrEmpty()) {
itemView.cCount3.text = "${255 - s.length} Characters Remaining"
}
else {
itemView.cCount3.visibility = View.GONE
}
}
})
itemView.door_lock_add.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {
itemView.cCount4.text = ""
}
override fun onTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {
if (s.isNullOrEmpty()) {
itemView.cCount4.visibility = View.GONE
} else {
itemView.cCount4.visibility = View.VISIBLE
}
}
override fun afterTextChanged(s: Editable?) {
if (!s.isNullOrEmpty()) {
itemView.cCount4.text = "${1200 - s.length} Characters Remaining"
}
else {
itemView.cCount4.visibility = View.GONE
}
}
})
itemView.handicap_operator_add.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {
itemView.cCount5.text = ""
}
override fun onTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {
if (s.isNullOrEmpty()) {
itemView.cCount5.visibility = View.GONE
} else {
itemView.cCount5.visibility = View.VISIBLE
}
}
override fun afterTextChanged(s: Editable?) {
if (!s.isNullOrEmpty()) {
itemView.cCount5.text = "${1200 - s.length} Characters Remaining"
}
else {
itemView.cCount5.visibility = View.GONE
}
}
})
}
}
For clarification: I want to achieve having the character count TextView to be non-visible when I return to that fragment that has them. For example. I am filling out a form that has the character count in it, I go to a different fragment, then come back to the fragment that has the form and the character count is now visible. I want to character count TextView to be invisible when i return to that fragment.
*having the character count invisible when the user returns to this fragment.
Any sugestions is appreciated. Thank you.
So I wasn't too far off from the solution. First setting up the onFocusChangeListener
properly
itemView.date_time_add.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
if (!hasFocus) itemView.cCount1.visibility = View.INVISIBLE
else itemView.cCount1.visibility = View.VISIBLE
}
Here, instaded of View.GONE
, I used View.INVISIBLE
That seemed to work best. Then I made some changes to the `addTextChangeListener.
itemView.date_time_add.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {
itemView.cCount1.visibility = View.INVISIBLE
}
@SuppressLint("SuspiciousIndentation")
override fun onTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {
if (s.isNullOrEmpty()) {
itemView.cCount1.visibility = View.INVISIBLE
} else {
if (itemView.date_time_add.hasFocus())
itemView.cCount1.visibility = View.VISIBLE
}
}
override fun afterTextChanged(s: Editable?) {
if (!s.isNullOrEmpty()) {
itemView.cCount1.text = "${255 - s.length} Characters Remaining"
}
else {
itemView.cCount1.visibility = View.INVISIBLE
}
}
})
So instead of having checking for an empty string, I used isNullOrEmpty
to check if there are characters in the String. Then changed all the visibility calls that were View.GONE
to View.INVISIBLE
and that's how I got it to work.