GitXplorerGitXplorer
G

RichContentEditText

public
10 stars
2 forks
2 issues

Commits

List of commits on branch master.
Verified
6c87986d5e7a4bc6f7a4045e0589233d3112ec99

added screenshots

GGregoryConrad committed 6 years ago
Unverified
528565b2b6b94ff2da459e66871455b64f61047c

Merge remote-tracking branch 'origin/master'

GGregoryConrad committed 6 years ago
Unverified
e8f118782130ed003e5db59fe5f297f8e22396ba

added a file

GGregoryConrad committed 6 years ago
Verified
db0f47c32c4d2e1627d10260aca6cd4491b5bcd4

Create LICENSE

GGregoryConrad committed 6 years ago
Verified
2548ee8ee0da390edc9ddb27a9269f4961cf2069

Add files via upload

GGregoryConrad committed 6 years ago
Unverified
b309171e13a31f2bf3a05e596ccf24109a5898ef

Initial commit

committed 6 years ago

README

The README file for this repository.

RichContentEditText

A simple wrapper around AppCompatEditText that makes it easier to handle rich content

Obligatory screenshots

Screenshots

Adding to your project

Add this to your Module: app's build.gradle file under dependencies:

implementation 'com.gsconrad:richcontentedittext:1.0.0'

If that does not work, ensure jcenter() is in your project build.gradle's repositories section—it should be by default.

And if for some reason, you still can't get it to work, try using Jitpack.

Usage

For a full working example (where the screenshots came from), see the example app. You can clone this repository and open it in Android Studio to see the example app. But, here are some easy code snippets:

XML

<?xml version="1.0" encoding="utf-8"?>
<!-- Root layout can be anything. Just make sure to include xmlns:app line. -->
<android.support.constraint.ConstraintLayout 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"
    tools:context=".MainActivity">

    <!-- The RichContentEditText -->
    <!-- Notice app:allowedMimeTypes="images"; it is what accepts certain mime types
             (you can do this programmatically too) -->
    <com.gsconrad.richcontentedittext.RichContentEditText
        android:id="@+id/rich_content_edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/rich_content_edit_text_hint"
        android:inputType="text"
        app:allowedMimeTypes="images" />
</android.support.constraint.ConstraintLayout>

Java

// MainActivity.java
private void setupRichContentEditText() {
    RichContentEditText editText = findViewById(R.id.rich_content_edit_text);
    // The following line sets the listener that is called when rich content is received
    editText.setOnRichContentListener(new RichContentEditText.OnRichContentListener() {
        // Called when a keyboard sends rich content
        @Override
        public void onRichContent(Uri contentUri, ClipDescription description) {
            if (description.getMimeTypeCount() > 0) {
                final String fileExtension = MimeTypeMap.getSingleton()
                        .getExtensionFromMimeType(description.getMimeType(0));
                final String filename = "filenameGoesHere." + fileExtension;
                File richContentFile = new File(getFilesDir(), filename);
                if (!writeToFileFromContentUri(richContentFile, contentUri)) {
                    Toast.makeText(MainActivity.this,
                            R.string.rich_content_copy_failure, Toast.LENGTH_LONG).show();
                } else {
                    WebView displayView = findViewById(R.id.display_view);
                    displayView.loadUrl("file://" + richContentFile.getAbsolutePath());
                }
            }
        }
    });
}

More information

There isn't much else you need to know, as this library is very simple. If you find any issues, please file an issue. Otherwise, here are some mock code snippets of features you can use:

/**
 * Determines whether the listener is run on a background thread or on the main thread
 * True (recommended): runs the listener on a background thread to improve performance
 * False (not advised): runs the listener on the main thread
 * NOTE: if this is true, ensure you update any necessary views on the main thread
 */
public boolean runListenerInBackground

/**
 * Sets the listener that is called whenever rich content is received
 * NOTE: the listener is run on a background thread by default; see runListenerInBackground
 *
 * @param onRichContentListener the listener
 */
public void setOnRichContentListener(OnRichContentListener onRichContentListener)

/**
 * Sets the mime types to accept from a keyboard
 *
 * @param mimeTypes a string array of mime types
 */
public void setContentMimeTypes(String[] mimeTypes)

/**
 * A convenience method for allowing image insertion from a keyboard
 * Sets the appropriate mime types to accept images
 */
public void allowImageInsertion()

/**
 * A convenience method for disallowing rich content from a keyboard
 */
public void disallowRichContent()

/**
 * @return the currently allowed mime types
 */
public String[] getContentMimeTypes()