Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying large TextViews efficiently in Android DialogFragments

I am attempting to show some licensing information for my Android application using a DialogFragment that contains a TextView wrapped inside of a ScrollView. In specific I am using the String generated from the GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo() method. The String generated here contains a large amount of text and when I pass the required information to the Dialog to be displayed the UI thread freezes for about 3-4 seconds before finally showing the dialog. Is there a more efficient way to display large amounts of text within a DialogFragment?

Here is the code where the Dialog is instantiated and shown.

if(googlePlayServicesLicense != null) {

   // Google Play Service License Info
    TitleParagraphViewModel googlePlayInfo = 
    new TitleParagraphViewModel("Google Play Services", 
    googlePlayServicesLicense);

    infoList.add(googlePlayInfo);
}

// Create license dialog and show
TitleParagraphListDialog dialog = 
TitleParagraphListDialog.newInstance("Licenses", infoList);

// Show the dialog
dialog.show(getSupportFragmentManager(), "dialog");

Here is the code for my DialogFragment

public class TitleParagraphListDialog extends DialogFragment {
private static final String DIALOG_TITLE_EXTRA_KEY = "dialog_title";
private static final String DIALOG_INFO_LIST_EXTRA_KEY = "info_list";
private String mDialogTitle;
private ArrayList<TitleParagraphViewModel> mInfoList;
private TextView mInformation;

public static TitleParagraphListDialog newInstance(String title, ArrayList<TitleParagraphViewModel> infoList) {
    TitleParagraphListDialog dialog = new TitleParagraphListDialog();
    Bundle args = new Bundle();
    args.putString(DIALOG_TITLE_EXTRA_KEY, title);
    args.putParcelableArrayList(DIALOG_INFO_LIST_EXTRA_KEY, infoList);
    dialog.setArguments(args);
    return dialog;
}

@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle args = getArguments();
    mDialogTitle = args.getString(DIALOG_TITLE_EXTRA_KEY) != null ? args.getString(DIALOG_TITLE_EXTRA_KEY) : "Information";
    mInfoList = (ArrayList<TitleParagraphViewModel>) (args.getParcelableArrayList(DIALOG_INFO_LIST_EXTRA_KEY) != null ? args.getParcelableArrayList(DIALOG_INFO_LIST_EXTRA_KEY) : new ArrayList<TitleParagraphViewModel>());
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();

    View dialogView = inflater.inflate(R.layout.title_paragraph_dialog, null);
    TextView dialogTitle = (TextView) dialogView.findViewById(R.id.dialog_title);
    mInformation = (TextView) dialogView.findViewById(R.id.information);

    dialogTitle.setText(mDialogTitle);

    dialogTitle.setTypeface(Typeface.createFromAsset(getActivity()
            .getAssets(), "fonts/Roboto-Light.ttf"));

    mInformation.setTypeface(Typeface.createFromAsset(getActivity()
            .getAssets(), "fonts/Roboto-Light.ttf"));

    for(TitleParagraphViewModel info : mInfoList) {
        mInformation.append(Html.fromHtml("<b>" + info.getTitle() + "</b>")
                + "\n\n" + info.getParagraph() + "\n\n");
    }

    builder.setView(dialogView)
    .setPositiveButton("Done", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            TitleParagraphListDialog.this.getDialog().cancel();
        }
    });
    return builder.create();
}

}

I would like to know a better way to handle displaying large amounts of text. I tried using a ListView but performance was worse. The GooglePlayServices license is just one of many licenses that I may need to display in my app.

If you are wondering about the TitleParagraphViewModel here is the code for it.

public class TitleParagraphViewModel implements Parcelable {
private String title, paragraph;

public TitleParagraphViewModel(String title, String paragraph) {
    this.title = title;
    this.paragraph = paragraph;
}

public void setTitle(String title) {
    this.title = title;
}

public void setParagraph(String paragraph) {
    this.paragraph = paragraph;
}

public String getTitle() {
    return this.title;
}

public String getParagraph() {
    return this.paragraph;
}

// Parceling Part
public TitleParagraphViewModel(Parcel in) {
    readFromParcel(in);
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeStringArray(new String[] { this.getTitle(), this.getParagraph()});
}

private void readFromParcel(Parcel in) {
    String[] stringData = new String[2];
    in.readStringArray(stringData);
    this.setTitle(stringData[0]);
    this.setParagraph(stringData[1]);
}

@SuppressWarnings("rawtypes")
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
    @Override
    public Object createFromParcel(Parcel in) {
        return new TitleParagraphViewModel(in);
    }

    @Override
    public Object[] newArray(int size) {
        return new Object[size];
    }

};
}
like image 200
Wayne J. Avatar asked Dec 15 '25 13:12

Wayne J.


1 Answers

I ended up splitting the long text into an array of individual lines and then used appcompat's RecyclerView to display that array of lines.

Here's the code:

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.android.gms.common.GoogleApiAvailability;

public class GoogleLicenseDialogFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Activity activity = getActivity();
        AlertDialog.Builder builder = new AlertDialog.Builder(activity)
                .setPositiveButton(R.string.button_close, null);

        GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
        String licenseInfo = googleApiAvailability.getOpenSourceSoftwareLicenseInfo(activity);

        if (licenseInfo != null) {
            Context context = getDialogContext(activity);
            RecyclerView recyclerView = createRecyclerView(context);
            recyclerView.setAdapter(new LongMessageAdapter(context, licenseInfo));
            builder.setView(recyclerView);
        }

        return builder.create();
    }

    private Context getDialogContext(Context context) {
        TypedValue outValue = new TypedValue();
        int resId = android.support.v7.appcompat.R.attr.dialogTheme;
        context.getTheme().resolveAttribute(resId, outValue, true);
        int themeId = outValue.resourceId;
        return themeId == 0 ? context : new ContextThemeWrapper(context, themeId);
    }

    private RecyclerView createRecyclerView(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        @SuppressLint("InflateParams")
        RecyclerView recyclerView = (RecyclerView) inflater.inflate(R.layout.recycler_dialog, null);

        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));

        return recyclerView;
    }

    private static final class LongMessageAdapter
            extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        private final Context context;
        private final String[] lines;

        public LongMessageAdapter(Context context, String message) {
            this.context = context;
            this.lines = message.split("\\n");
        }

        @Override
        public int getItemCount() {
            return lines.length;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            TextView textView = new TextView(context);
            textView.setTextAppearance(context, R.style.TextAppearance_AppCompat_Subhead);
            return new RecyclerView.ViewHolder(textView) {
            };
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ((TextView) holder.itemView).setText(lines[position]);
        }
    }
}

recycler_dialog.xml:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="?attr/dialogPreferredPadding"
    android:paddingRight="?attr/dialogPreferredPadding"
    android:paddingTop="@dimen/abc_dialog_padding_top_material"
    android:scrollbars="vertical"/>
like image 73
devconsole Avatar answered Dec 18 '25 12:12

devconsole



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!