This code example provides an app that enables you to verify whether an Android View, and its subclasses, reacts to UI taps when any combination of these attributes is set to true or false:
tv_counter.setEnabled(true);
tv_counter.setClickable(true);
tv_counter.setFocusable(true);
tv_counter.setFocusableInTouchMode(true);
Touch the orange TextView to increment the counter.
After testing in the Eclipse ADT emulator and on an S3 device, I found the setEnabled and setClickable attributes work as expected.
However, setFocusable and setFocusableInTouchMode do not seem to have any effect when they are set to false.
After setting both of these attributes to false, the TextView continues to react to clicks both in the emulator and on a device.
You can use the code sample below to verify the bug.
I noted this in a bug description here:
https://sites.google.com/site/myrononmobileapps/reference-material/bugs
Code Example
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
TextView tv_counter;
Button btn_enable, btn_disable, btn_clear;
int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_counter = (TextView) findViewById(R.id.tv_counter);
btn_enable = (Button) findViewById(R.id.btn_enable);
btn_disable = (Button) findViewById(R.id.btn_disable);
btn_clear = (Button) findViewById(R.id.btn_clear);
tv_counter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv_counter.setText(String.valueOf(++counter));
Toast.makeText(getApplication(), "You clicked the counter.", Toast.LENGTH_SHORT).show();
}
});
btn_enable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// tv_counter.setEnabled(true);
// tv_counter.setClickable(true);
tv_counter.setFocusable(true);
tv_counter.setFocusableInTouchMode(true);
}
});
btn_disable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// tv_counter.setEnabled(false);
// tv_counter.setClickable(false);
tv_counter.setFocusable(false);
tv_counter.setFocusableInTouchMode(false);
}
});
btn_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter = 0;
tv_counter.setText(String.valueOf(counter));
}
});
}
}
XML Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv_counter"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:background="@android:color/holo_orange_light"
android:textColor="@android:color/black" />
<Button
android:id="@+id/btn_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="@string/clear"
android:textColor="@android:color/black"
/>
<Button
android:id="@+id/btn_enable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="@string/enable"
android:textColor="@android:color/black"
/>
<Button
android:id="@+id/btn_disable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="@string/disable"
android:textColor="@android:color/black"
/>
</LinearLayout>