I've got a EditText with default text on it.
Now when a user clicks on that EditText the default text should be changed to something.
What I've got is:
How can I fix this that it is done from the first time?
This is my xml:
<EditText
android:id="@+id/step"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="5dp"
android:hint="@string/hnt_message"
android:text="Default text"
android:maxLength="@integer/max_free_msg_length"
android:maxLines="2"
/>
This is my fragment code:
private EditText editMessage;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mContentView = inflater.inflate(R.layout.fragment_step, container, false);
ButterKnife.inject(this, mContentView);
editMessage = (EditText) mContentView.findViewById(step);
editMessage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clearEditMessageText();
}
});
}
I know I could use butterknife to for this button but I did and that didn't worked.
Set focusable on EditText to false. First click gains focus to EditText.
<EditText
android:id="@+id/step"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="5dp"
android:hint="@string/hnt_message"
android:text="Default text"
android:maxLength="@integer/max_free_msg_length"
android:maxLines="2"
android:focusable="false"
/>
UPDATE:
Ok, in your case you can do it with onFocusChangeListener
instead of onClickListener
.
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if (b) {
//do something
}
}
});
I had this issue with an editText where I wanted the player to enter a character name. I didn't like using a hint as the hint is also put in the popup keyboard window, I wanted that initially blank. So I initially added the name request in the editText. I wanted that erased when first clicked. The first click triggered the popup keyboard, but not 'setOnClickListener' to erase the field. I solved the issue by calling a performClick action on the 'setOnFocusChangeListener'.
This still seems a bit of a hack to me, but changing focusable or focusableInTouchMode to false as has been suggested prevents the popup keyboard from showing.
charnameEditText.setText("Enter Name Here");
....
charnameEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
view.performClick();
}
}
});
//Normally not called by first click
charnameEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
charnameEditText.setText("");
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With