Quick Tip: Android — Hiding Edit Text Cursor on Activity Start


In this quick tip, I’ll be showing you how to remove the automatic cursor on Edit Text’s in Android when an Activity / Fragment is launched.

This is useful when you want to remove the focus away from the Edit Texts when an application is launched.

Edit Text automatically focuses

In the caption above we can see that the Edit Text automatically focuses when the application is launched showing a cursor.

Set Cursor Visibility

Setting the Cursor to not be visible

There are two Edit Texts in the application:

  • workIntervalMinutes
  • workIntervalSeconds

To hide the cursor we implement this method for each Edit Text:

  • “setCursorVisible(false)”

This method hides the cursor in the Edit Text when the application launches.

Application launches without cursor

Now when the application launches, the automatic cursor does not appear. The user can still change the Edit Text but there isn’t a cursor available to track where the user is typing. We can reintroduce the cursor when the user clicks on the Edit Text.

Reintroduce the Cursor

Showing the cursor on user click

To reintroduce the cursor, we set an onClickListener on the Edit Text. When the user clicks on the Edit Text, cursor visibility is set to true.

Cursor is enabled after user click

The cursor reappears when the user clicks on the Edit Text and can update the text.

Thanks for reading! Please recommend this post (by clicking the ❤ ) so other people can find it.

Meanwhile, would love to hear your suggestions & comments on this article.

Christopher Coverdale is a Android Developer at Cotham Technologies, building the next generation no-code platform for mobile development. An entrepreneur and tech nut, tinkering away with IOT and Machine Learning projects.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.