There is a separate edit view:

<com.sai.android.features.common.widget.CustomEditText android:id="@+id/nameView" android:gravity="right" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/transparent" android:textColor="@color/other_black" app:fontType="regular" android:textSize="@dimen/text_18"/> 

By default, the text is inserted into it, after which I can delete it and write the desired one. How is it possible to make it so that by clicking on the text, which is the default, it is immediately removed and I could write without deleting anything manually?

  • 3
    When View gets the focus, check the text that is in it, if it is equivalent to the default one - delete it. - post_zeew

3 answers 3

  EditText txtEdit = (EditText) findViewById(R.id.edittxt); txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { // code to execute when EditText got focus } } }); 

    If I understand the question correctly, then I need to add the hint EditText property.

     android:hint="" 

    This is essentially a hint, which is displayed until the user has clicked on it.

      I did something similar in my project not so long ago, only on the cotlin, but for clarity, I think an example would suit:

       nameView.onFocusChangeListener = OnFocusChangeListener { v, hasFocus -> if (hasFocus && nameView.text.toString() == getString(R.string.nameView)) { nameView.text.clear() } } 

      Here, we compare the same way if your text is in focus and the text matches the required one, after which we clean the field.