Let us look at a Android ReadMoreTextView example.
This is a TextView that you can use if you prefer to collapse a long text. It looks like facebook's feature .
Here is demo screenshot:
Step 1: Install it
dependencies {
implementation 'kr.co.prnd:readmore-textview:x.x.x'
//implementation 'kr.co.prnd:readmore-textview:1.0.0'
}
Step 2: Add to Layout
Add it to your xml layout:
<kr.co.prnd.readmore.ReadMoreTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/long_text"
app:readMoreColor="@color/colorPrimary"
app:readMoreMaxLine="4"
app:readMoreText="…More" />
Functions
Here are the functions you can use:
toggle()
,expand()
,collapse()
: Change expand/collapse state dynamicallyisExpanded
,isCollased
,state
: Get state- ChangeListener: Observe state change using listener
Example 1: ReadMoreTextView
Here is a full example. This example will comprise the following files:
MainActivity.kt
Step 1: Create Project
- Open your
AndroidStudio
IDE. - Go to
File-->New-->Project
to create a new project.
Step 2: Add Dependencies
Install it as has been described above.
Step 3: Design Layouts
*(a). activity_main.xml
Create a file named activity_main.xml
and design it as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<kr.co.prnd.readmore.ReadMoreTextView
android:id="@+id/readMoreTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:foreground="?attr/selectableItemBackground"
android:text="@string/long_text"
app:readMoreColor="@color/colorPrimary"
app:readMoreMaxLine="4"
app:readMoreText="…더보기" />
</LinearLayout>
Step 4: Write Code
Write Code as follows:
(a). MainActivity.kt
Create a file named MainActivity.kt
Add the ReadMoreTextView in your xml layout then reference
val readMoreTextView: ReadMoreTextView = findViewById(R.id.readMoreTextView)
Attach a Change Listener to it then override the onStateChange()
callback
readMoreTextView.changeListener = object : ReadMoreTextView.ChangeListener {
override fun onStateChange(state: ReadMoreTextView.State) {
Log.d("prnd", "state: $state")
}
}
Here is the full code
package kr.co.prnd.readmore.demo
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import kr.co.prnd.readmore.ReadMoreTextView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val readMoreTextView: ReadMoreTextView = findViewById(R.id.readMoreTextView)
readMoreTextView.changeListener = object : ReadMoreTextView.ChangeListener {
override fun onStateChange(state: ReadMoreTextView.State) {
Log.d("prnd", "state: $state")
}
}
}
}
Run
Simply copy the source code into your Android Project,Build and Run.