androidandroid-alertdialogabout-box

Can we use asset html files in Alert dialog?


Currently i am using string to show text in alert dialog , is there a way to use assets html file directly without using layout and show alert dialog like this code

 private void About() {
    AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle(getString(R.string.about));
    alertDialog.setMessage(getString(R.stringabout));
    alertDialog.setIcon(R.drawable.ic_launcher);
    alertDialog.setButton(DialogInterface.BUTTON_NEUTRAL,
            getString(R.string.lbl_dialog_close),
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                   close
                }
            });
    alertDialog.show();
}

Solution

  • try this way,

     try {
                InputStream is = getAssets().open("yourhtmlfile.txt");
    
                // We guarantee that the available method returns the total
                // size of the asset...  of course, this does mean that a single
                // asset can't be more than 2 gigs.
                int size = is.available();
    
                // Read the entire asset into a local byte buffer.
                byte[] buffer = new byte[size];
                is.read(buffer);
                is.close();
    
                // Convert the buffer into a string.
                String text = new String(buffer);
    
                // Finally stick the string into the text view.
                TextView tv = (TextView)findViewById(R.id.text);
                tv.setText(text);
            } catch (IOException e) {
                // Should never happen!
                throw new RuntimeException(e);
            }