Reputation: 331
I have been trying to set a large string (25k+ characters) to a TextView
for a while now but I've been getting a "STRING_TOO_LARGE" output.
I read somewhere that setting the string in run time might help solve that problem, so I modified my code but that didn't seem to help. I also tried setting the resource id directly in setText()
but that didn't do anything as well.
I use the following method in my Activity
to show the terms_dialog
:
private void showTermsPopup() {
Dialog termsDialog = new Dialog(this);
termsDialog.setContentView(R.layout.terms_dialog);
termsDialog.getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);
// Cancel the dialog if you touch the background
termsDialog.setCanceledOnTouchOutside(true);
// Add the terms string to the dialog
TextView termsText = termsDialog.findViewById(R.id.termsTextView);
String terms = getResources().getString(R.string.terms);
termsText.setText(terms);
// Show the dialog
termsDialog.show();
}
This is the terms_dialog.xml
file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:padding="8dp"
android:scrollbarSize="0dp">
<TextView
android:id="@+id/termsTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
</android.support.constraint.ConstraintLayout>
And this is the result I am getting (sensitive info whited out).
Upvotes: 6
Views: 10343
Reputation: 2327
At this situation you can take 2 TextView like below code and devide your text(tems or privacy policy) in to two parts and load into both TextView.
<ScrollView
android:id="@+id/SCROLLER_ID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/_20sdp"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/TEXTVIEW_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:linksClickable="true"
android:text="FIRST_HALF_TEXT"
android:textColor="@color/color_text_color"
android:textColorLink="@color/ic_blue_gray_500"
android:textSize="@dimen/_12ssp" />
<TextView
android:id="@+id/TEXTVIEW_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:linksClickable="true"
android:visibility="gone"
android:text="SECOND_HALF_TEXT"
android:textColor="@color/color_text_color"
android:textColorLink="@color/ic_blue_gray_500"
android:textSize="@dimen/_12ssp" />
</LinearLayout>
</ScrollView>
Upvotes: 0
Reputation: 331
As pointed out by the link that @HB. gave me, you cannot have a string that is larger than 32,767 bytes (encoded in UTF-8) in your APK file.
So, what I did, was to create a txt file in the assets folder named terms.txt
and I put the string in it. Then, using the following function I converted the file to a String:
private String getTermsString() {
StringBuilder termsString = new StringBuilder();
BufferedReader reader;
try {
reader = new BufferedReader(
new InputStreamReader(getAssets().open("terms.txt")));
String str;
while ((str = reader.readLine()) != null) {
termsString.append(str);
}
reader.close();
return termsString.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
And I just assigned it to the TextView
.
Upvotes: 15
Reputation: 111
use android: ellipsize to accommodate the text
<TextView
android:id="@+id/termsTextView"
android:ellipsize="middle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Regards!
Upvotes: -3