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 that Button
is a subclass of TextView
, and what's actually triggering that behavior in your case is the capitalize
attribute in your custom style. When this is set to anything other than none
, it causes TextView
's internal Editor
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, any inputType
other than none
. The textIsSelectable
attribute will cause a little havoc, too, in that the Button
will act like an un-editable EditText
. The cursor and selection handle(s) will appear, along with the editing CAB when appropriate, but no input method, and the Button
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, since Button
probably shouldn't, in any way, allow itself to become editable. Indeed, in certain states, pasting text, then shifting focus or clicking causes the Editor
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 the Button
. Alternatively, if you don't need to respond to long clicks on your Button
s, you could set the longClickable
attribute in your style to false
.
Unfortunately, simply setting the (deprecated) editable
attribute to false
doesn't seem to be a fix, if combined with any of the other above-mentioned, problematic attributes.