+ 1

How can realized btn logic?

Hello for everyone! I have 2 TextView in my activity_main.xml First TextView (text) serves as a text, the second TextView (btn) serves as a button. The text include huge text includes 20 lines, but i need to make it look short(3 lines) on the layout. For example: | It some text on 20 lines. It some text on 20 lines. It some text on 20 lines... | The btn which under the first TextView with the name "Read more" should open the text in full size and renamed itself "Read less". When we click again on this btn we need to transform the name of btn from "Read less" to "Read more" and collapse expanded text to 3 lines. All this logic must be executed an unlimited number of times when this button is pressed. So, what i need to write in MainActivity.kt for btn?

31st May 2021, 7:12 AM
Andrey Rudnev
Andrey Rudnev - avatar
12 Respuestas
+ 2
Keep flag = false globally if(!flag) { flag = true setText = "Read less" setLength = synopsis.length() } else { flag = false setText = "Read more" setLength = 3 } It should look something like this. Note, this is an abstract overview to give you an idea, not the actual code.
31st May 2021, 9:14 AM
Abhishek Tandon
Abhishek Tandon - avatar
+ 2
just add: else { btnRead.text = "Read less" flag = true } Looking at the code, I can't tell what `synopsis` is, you can make the changes to `synopsis.maxLines` accordingly
31st May 2021, 8:41 AM
Abhishek Tandon
Abhishek Tandon - avatar
+ 2
You don't need another listener inside the first if statement. Each time you press the button, the listener function is automatically called. You don't have to handle the multiple iterations yourself. Just write the code for a single iteration for each flag = true and false = false respectively.
31st May 2021, 8:55 AM
Abhishek Tandon
Abhishek Tandon - avatar
+ 2
You're welcome. I'm glad it worked 😄
31st May 2021, 9:20 AM
Abhishek Tandon
Abhishek Tandon - avatar
+ 1
P.S.: I am more familiar with Java, so pardon me for my lack of knowledge in Kotlin.
31st May 2021, 8:43 AM
Abhishek Tandon
Abhishek Tandon - avatar
+ 1
Infinity Thanks a lot! I wrote the next code a few moment later: btnRead.setOnClickListener { if (flag == true){ btnRead.text = "Read less" synopsis.maxLines = 20 flag = false } else { btnRead.text = "Read more" synopsis.maxLines = 3 flag = true } } It's working fine 😁
31st May 2021, 9:17 AM
Andrey Rudnev
Andrey Rudnev - avatar
0
@Infinity , I find the solution: var flag = false btnRead.setOnClickListener { btnRead.text = "Read less" synopsis.maxLines = synopsis.length() flag = true if (flag == true){ btnRead.setOnClickListener { btnRead.text = "Read more" synopsis.maxLines = 3 flag = false } } } But it work only works once, how to make this logic work as many times as I need to press the button?
31st May 2021, 8:33 AM
Andrey Rudnev
Andrey Rudnev - avatar
0
Infinity Synopsis is my TextView which need expand and shrink
31st May 2021, 8:46 AM
Andrey Rudnev
Andrey Rudnev - avatar
0
Infinity i dont what i doing wrong, but your 'else' body doesn't work 😥
31st May 2021, 8:54 AM
Andrey Rudnev
Andrey Rudnev - avatar
0
Infinity sorry for my stupid, I'm a beginner in programming. Can you provide an example, how it will be seen?!
31st May 2021, 9:03 AM
Andrey Rudnev
Andrey Rudnev - avatar
0
You can use OnCklickListener
4th Jul 2021, 2:23 PM
Игорь Орешков
Игорь Орешков - avatar