Skip to content

Commit

Permalink
updates for the version 1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
devendroid committed Aug 22, 2018
1 parent fb670e1 commit 1302b45
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 33 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,24 @@ Convert your TextView in ExpandableTextView with added options ReadMore/ReadLess
// OR using options to customize

ReadMoreOption readMoreOption = new ReadMoreOption.Builder(this)
.textLength(300)
.textLength(3, ReadMoreOption.TYPE_LINE) // OR
//.textLength(300, ReadMoreOption.TYPE_CHARACTER)
.moreLabel("MORE")
.lessLabel("LESS")
.moreLabelColor(Color.RED)
.lessLabelColor(Color.BLUE)
.labelUnderLine(true)
.expandAnimation(true)
.build();

readMoreOption.addReadMoreTo(textView, getString(R.string.long_desc));

```

### Known Issue

- [ ] expandAnimation not works with ListView/RecyclerView.

## License
```
Copyright 2018 Deven Singh
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
10 changes: 8 additions & 2 deletions app/src/main/java/com/devs/readmoreoptiondemo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.devs.readmoreoptiondemo;

import android.graphics.Color;
import android.media.TimedText;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
Expand Down Expand Up @@ -44,14 +45,19 @@ protected void onCreate(Bundle savedInstanceState) {
recyclerView.setAdapter(mAdapter);


// TextView tv = (TextView)findViewById(R.id.tv);
// tv.setText(getString(R.string.dummy_text));
//
// ReadMoreOption readMoreOption = new ReadMoreOption.Builder(this)
// // Optional parameters
// .textLength(300)
// // Optional parameters
// .textLength(3, ReadMoreOption.TYPE_LINE) //OR
// //.textLength(300, ReadMoreOption.TYPE_CHARACTER)
// .moreLabel("MORE")
// .lessLabel("LESS")
// .moreLabelColor(Color.RED)
// .lessLabelColor(Color.BLUE)
// .labelUnderLine(true)
// .expandAnimation(true)
// .build();
// readMoreOption.addReadMoreTo(tv, getString(R.string.dummy_text));

Expand Down
6 changes: 1 addition & 5 deletions app/src/main/java/com/devs/readmoreoptiondemo/MyAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ static class ViewHolder extends RecyclerView.ViewHolder {
}
}

// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(Context context) {
MyAdapter(Context context) {
this.context = context;
readMoreOption = new ReadMoreOption.Builder(context)
.build();
Expand All @@ -61,11 +60,8 @@ public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
return new ViewHolder(v);
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
readMoreOption.addReadMoreTo(holder.mTextView,context.getString(R.string.dummy_text));
}

Expand Down
14 changes: 12 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,22 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical">



<!-- <TextView
android:id="@+id/tv"
android:layout_margin="16dp"
tools:text="@string/dummy_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />-->


<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_margin="8dp"
Expand Down
2 changes: 1 addition & 1 deletion readmoreoption/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ android {
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
versionName "1.0.1"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,69 +15,120 @@

package com.devs.readmoreoption;

import android.animation.LayoutTransition;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
* Created by ${Deven} on 6/1/18.
*/
public class ReadMoreOption {

private static final String TAG = ReadMoreOption.class.getSimpleName();
public static final int TYPE_LINE = 1;
public static final int TYPE_CHARACTER = 2;

// required
private Context context;
// optional
private int textLength;
private int textLengthType;
private String moreLabel;
private String lessLabel;
private int moreLabelColor;
private int lessLabelColor;
private boolean labelUnderLine;
private boolean expandAnimation;

private ReadMoreOption(Builder builder){
this.context = builder.context;
this.textLength = builder.textLength;
this.textLengthType = builder.textLengthType;
this.moreLabel = builder.moreLabel;
this.lessLabel = builder.lessLabel;
this.moreLabelColor = builder.moreLabelColor;
this.lessLabelColor = builder.lessLabelColor;
this.labelUnderLine = builder.labelUnderLine;
this.expandAnimation = builder.expandAnimation;
}

public void addReadMoreTo(final TextView textView, final String text){

if(text.length() <= textLength) {
if(textLengthType==TYPE_CHARACTER) {
if (text.length() <= textLength) {
textView.setText(text);
return;
}
}
else {
// If TYPE_LINE
textView.setLines(textLength);
textView.setText(text);
return;
}

SpannableString ss = new SpannableString(text.substring(0, textLength) + "... "+ moreLabel);
ClickableSpan clickableSpan = new ClickableSpan() {
textView.post(new Runnable() {
@Override
public void onClick(View view) {
addReadLess(textView, text);
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(labelUnderLine);
ds.setColor(moreLabelColor);
public void run() {

int textLengthNew = textLength;

if(textLengthType==TYPE_LINE) {


if (textView.getLayout().getLineCount() <= textLength) {
textView.setText(text);
return;
}

ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) textView.getLayoutParams();

String subString = text.substring(textView.getLayout().getLineStart(0),
textView.getLayout().getLineEnd(textLength - 1));
textLengthNew = subString.length() - (moreLabel.length()+4+(lp.rightMargin/6));
}

SpannableString ss = new SpannableString(text.substring(0, textLengthNew) + "... "+ moreLabel);
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View view) {
addReadLess(textView, text);
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(labelUnderLine);
ds.setColor(moreLabelColor);
}
};
ss.setSpan(clickableSpan, ss.length() - moreLabel.length(), ss.length() , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && expandAnimation) {
LayoutTransition layoutTransition = new LayoutTransition();
layoutTransition.enableTransitionType(LayoutTransition.CHANGING);
((ViewGroup)textView.getParent()).setLayoutTransition(layoutTransition);
}

textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
};
ss.setSpan(clickableSpan, ss.length() - moreLabel.length(), ss.length() , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ss)
;
textView.setMovementMethod(LinkMovementMethod.getInstance());
});


}

private void addReadLess(final TextView textView, final String text ) {
textView.setMaxLines(Integer.MAX_VALUE);
SpannableString ss = new SpannableString(text + " "+ lessLabel);
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
Expand All @@ -97,8 +148,7 @@ public void updateDrawState(TextPaint ds) {
}
};
ss.setSpan(clickableSpan, ss.length() - lessLabel.length(), ss.length() , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ss)
;
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
}

Expand All @@ -107,18 +157,28 @@ public static class Builder {
private Context context;
// optional
private int textLength = 100;
private int textLengthType = ReadMoreOption.TYPE_CHARACTER;
private String moreLabel = "read more";
private String lessLabel = "read less";
private int moreLabelColor = Color.parseColor("#ff00ff");
private int lessLabelColor = Color.parseColor("#ff00ff");
private boolean labelUnderLine = false;
private boolean expandAnimation = false;

public Builder(Context context){
this.context = context;
}

public Builder textLength(int length){
/**
* @param length can be no. of line OR no. of characters - default is 100 character
* @param textLengthType ReadMoreOption.TYPE_LINE for no. of line OR
* ReadMoreOption.TYPE_CHARACTER for no. of character
* - default is ReadMoreOption.TYPE_CHARACTER
* @return Builder obj
*/
public Builder textLength(int length, int textLengthType){
this.textLength = length;
this.textLengthType = textLengthType;
return this;
}

Expand Down Expand Up @@ -147,6 +207,16 @@ public Builder labelUnderLine(boolean labelUnderLine){
return this;
}

/**
* @param expandAnimation either true to enable animation on expand or false to disable animation
* - default is false
* @return Builder obj
*/
public Builder expandAnimation(boolean expandAnimation){
this.expandAnimation = expandAnimation;
return this;
}

public ReadMoreOption build(){
return new ReadMoreOption(this);
}
Expand Down

0 comments on commit 1302b45

Please sign in to comment.