Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

action.PROCESS_TEXT activity not listed in EditText with Oreo

Tags:

android

I'm using the action_PROCESS_TEXT intent to provide a custom text selection action. On Marshmallow I am getting my action displayed on both readonly text and EditText controls and my code successfully displays/returns results.

On Oreo (Pixel 2 XL) my action is successfully displayed on read only text but I am not getting my action displayed on EditText controls, either in my own app or others. I also notice that other process text app's such as Google Translate are only randomly displayed.

Google docs are very basic for this and I'm relying on the original blog post, has something changed?

Here's my relevant manifest and activity code

Manifest

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="false"
        android:theme="@style/AppTheme">
        ...
        <activity
            android:name=".TranslateActivity"
            android:label="@string/action_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.PROCESS_TEXT"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="text/plain"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

Activity

package mynamespace

import android.app.Activity
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_translate.*

class TranslateActivity : AppCompatActivity() {

    private var readOnly = true
    val translator = Translator()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_translate)
        setTitle(R.string.app_name)

        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)

        toolbar.setNavigationOnClickListener({
            onBackPressed()
        })

        translator.loadTranslations()

        handleIntent(intent)
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)

        handleIntent(intent)
    }

    private fun handleIntent(intent: Intent) {
        if (intent.hasExtra(Intent.EXTRA_PROCESS_TEXT_READONLY)) {
            readOnly = intent.getBooleanExtra(Intent.EXTRA_PROCESS_TEXT_READONLY, false)
        }

        if (intent.hasExtra(Intent.EXTRA_PROCESS_TEXT)) {
            val text = intent.getCharSequenceExtra(Intent.EXTRA_PROCESS_TEXT).toString()

            val result: String = translator.translate(text.toLowerCase())

            if (readOnly) {
                // Display result
                textViewTranslation.text = result
            } else {
                replaceText(result)
            }
        }
    }

    private fun replaceText(replacementText: String) {
        val intent = Intent()
        intent.putExtra(Intent.EXTRA_PROCESS_TEXT, replacementText)
        setResult(Activity.RESULT_OK, intent)
        finish()
    }
...
}
like image 389
CodeChimp Avatar asked Oct 20 '25 14:10

CodeChimp


1 Answers

ACTION_PROCESS_TEXT does not work very well on Android 8.x.

The good news is that this appears to be fixed in Android P. You won't have any problems in 2025 and onwards, once all the Android O devices get retired.

like image 182
CommonsWare Avatar answered Oct 22 '25 03:10

CommonsWare