While I was working with an Android application for a customer, there was a use case to disable a button at runtime and enable it after some action. This is straightforward in Android. Below I show the XML for the button, how to reference it in your activity, and how to enable/disable it based on network status using a small NetworkUtil helper (link provided).
Button XML
Use a standard Button in your layout (example):
<Button
android:id="@+id/about_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp"
android:text="@string/about_label" />
Reference the Button in your Activity
In onCreate(), find the button and keep a reference. Using the Button type makes intentions clearer and provides button-specific APIs:
Button aboutButton = findViewById(R.id.about_button);
Note: on older Android APIs you may need a cast, e.g. Button aboutButton = (Button) findViewById(R.id.about_button);, but with modern SDKs the generic signature is available.
Enable and disable the Button
You can enable or disable a View (including Button) using setEnabled(true/false):
- To enable:
aboutButton.setEnabled(true);
- To disable:
aboutButton.setEnabled(false);
Disabling a button via setEnabled(false) also prevents it from receiving click events. For improved UX you may also want to change the visual appearance (alpha, color) so users can see the disabled state:
aboutButton.setEnabled(false);
aboutButton.setAlpha(0.5f); // visual cue
// when enabling back:
aboutButton.setEnabled(true);
aboutButton.setAlpha(1f);
Remember to make UI changes on the main thread.
Enable based on network status (using NetworkUtil)
I use a small helper class NetworkUtil to check network connectivity. You can download it here:
Create an instance and call getConnectivityStatus with a Context. Example usage (corrected and formatted):
NetworkUtil net = new NetworkUtil();
int status = net.getConnectivityStatus(getApplicationContext());
if (status != 0) {
aboutButton.setEnabled(true);
aboutButton.setAlpha(1f);
} else {
aboutButton.setEnabled(false);
aboutButton.setAlpha(0.5f);
}
Notes:
- The original article had a typo
setEnabled(flase); ensure you usefalse. - The meaning of status values depends on NetworkUtil implementation. Keep your app logic consistent with that helper's return values.
Accessibility and UX considerations
- When disabling interactive elements, ensure assistive technologies can convey the change. Consider adding content descriptions for non-textual controls and ensuring focus behavior is appropriate.
- Adjust visual indicators (alpha, color) so users can perceive the disabled state.
Alternatives and further improvements
- For production apps consider using Android's ConnectivityManager and NetworkCallback APIs to observe connectivity changes rather than polling.
- In modern architectures, expose connectivity status via LiveData, Flow, or other observable streams and observe from the Activity/Fragment to update UI reactively.
NetworkUtil download link (same as above): https://sites.google.com/site/karjolblogs/downloads/NetworkUtil.java?attredirects=0&d=1
That’s it. Happy Coding..





