Background
I have a standard Material Design button, as per below
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.AppCompatButton
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/medium_margin"
android:layout_marginRight="@dimen/medium_margin"
android:textStyle="bold"
tools:text="I am a Button"
style="@style/PrimaryColoredButton" />
The style applied to it is as follows
<style name="PrimaryColoredButton" parent="Widget.AppCompat.Button.Colored">
<item name="android:textColor">@color/white</item>
<item name="android:capitalize">sentences</item>
<item name="android:background">@drawable/primary_button_background</item>
</style>
The Problem
When the button is long pressed, the system paste button appears and allows the user to paste content into the Button's label.
Then after pasting:
I cannot see a way to stop this from an XML perspective, but it feels like this shouldn't be possible at all.
Interestingly it only happens when the long press is not on the text but the background itself.
That behavior is not exclusive to
AppCompatButton
s, it seems. It stems from the fact thatButton
is a subclass ofTextView
, and what's actually triggering that behavior in your case is thecapitalize
attribute in your custom style. When this is setto anything other than, it causesnone
TextView
's internalEditor
to become active and respond to long clicks.In fact, it appears that any attribute setting that causes the
Button
to have an input type other than the default will cause this. These attributes include - but are not limited to -capitalize
,digits
,autoText
, and, obviously, anyinputType
other thannone
. ThetextIsSelectable
attribute will cause a little havoc, too, in that theButton
will act like an un-editableEditText
. The cursor and selection handle(s) will appear, along with the editing CAB when appropriate, but no input method, and theButton
becomes un-clickable.These are clearly not attributes you would normally be setting on a
Button
, but this definitely seems like a bug to me, sinceButton
probably shouldn't, in any way, allow itself to become editable. Indeed, in certain states, pasting text, then shifting focus or clicking causes theEditor
to crash. If this is unintended behavior (the pasting, not the crashing), it's been wrongly implemented for some time, as far as I can tell.To remedy this, you should remove the
capitalize
attribute from your style, and just handle that yourself when you set the text on theButton
. Alternatively, if you don't need to respond to long clicks on yourButton
s, you could set thelongClickable
attribute in your style tofalse
.Unfortunately, simply setting the (deprecated)
editable
attribute tofalse
doesn't seem to be a fix, if combined with any of the other above-mentioned, problematic attributes.