Issues while editing an EditText during a focus change - android

I have two EditTexts for a username and password, each having the text "ENTER A USERNAME" and "ENTER A PASSWORD" respectively, as their default text.
Their text colors are a light gray. Now when they get focused they're supposed to change text colors as well as the password edittext's inputType to a password type (on top of other things but that works).
It successfully changes the text color to black at first but fails to change back and the inputType never changes.
Please suggest me where I am lacking, below is my code:
OnFocusChangeListener pwListener = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
//if it has focus then clear the text and make text black
if(((EditText) v).getText().toString().compareTo(getString(R.string.passwordField)) == 0) {
((EditText) v).setTextColor(R.color.Black);
((EditText) v).setText(R.string.blank);
((EditText) v).setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
} else if(!hasFocus) {
//if it loses focus and nothing was inputed then change back to default
if(isEmpty(((EditText) v).getText().toString())) {
((EditText) v).setTextColor(R.color.TextInput);
((EditText) v).setText(R.string.passwordField);
((EditText) v).setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
}
}
}
};
Thats the passwords edit text and this is its XML:
<EditText
android:id="#+id/passwordInput"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:layout_marginTop="5dip"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:paddingTop="15dip"
android:paddingBottom="15dip"
android:text="#string/passwordField"
android:textColor="#color/TextInput"
android:textSize="25sp"
android:gravity="center_vertical"
android:ems="10" >
</EditText>

Why you dont set the ENTER A USERNAME" and "ENTER A PASSWORD" as hint text in Edittext using
android:hint="Enter User Name"
It will automatically disappear when user clicked on Edittext.

In your Xml use Hint property to set values as default.
like:
android:hint="#string/username"
android:hint="#string/password"
Instead
android:text="#string/username"
android:text="#string/password"

Use
android:hint="#string/passwordField"
instead of
android:text="#string/passwordField"
and do same in username

Related

Android - How to get hint size of TextInputLayout programmatically when focused?

How to get hint size of Enter Email Id(mentioned in this image) ?
Text size of hint is same as the text to be inputed in TextInputLayout. To get textSize in Java do like below:
XML:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="157dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="158dp">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:id="#+id/TextInputEditText"
android:layout_height="wrap_content"
android:hint="hint" />
</android.support.design.widget.TextInputLayout>
MainActivity.java:
final TextInputEditText textInputEditText = findViewById(R.id.TextInputEditText);
textInputEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
float size = textInputEditText.getTextSize();
Log.i("TEXT_SIZE", String.valueOf(size));
}
});
If you want to change textSize you can use textInputEditText.setTextSize();
For example:
For get hint size you need to get a edittext text size like this:
EditText mEmailId= findViewById(R.id.et_email);
mEmailId.getTextSize();
String.xml
<string name="edittext_hint"><font size="15">Hint set here</font></string>
then in your XML just write
<EditText
android:id="#+id/input_msg"
android:inputType="text"
android:hint="#string/edittext_hint" />
OR
Reduce the font size on the EditText - that will reduce the size of the hint as well. i.e. android:textSize="15dp"

Hint in EditText lefts empty space on focus

I have a EditText with longer multiple lines hint and when I focus on this EditText, hint disappears but lefts empty space around new text which I don't want. I have already tried to fix it with android:gravity="bottom", but empty space still remains.
Here is the code:
<EditText
android:id="#+id/addextra_basedon_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textMultiLine"
android:hint="#string/placeholder_acting_on"
android:gravity="bottom" />
And pictures:
before
after
You can implement the behaviour of the EditText when focused like this:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
editText.setHint("");
editText.setText("");
}
}
});

EditText with "text" inputType not hiding error popup when text is changed

Here is my XML
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/barcode"
android:id="#+id/barcode"
android:inputType="number" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/name"
android:id="#+id/name"
android:inputType="text" />
I set error to both fields:
((TextView) findViewById(R.id.barcode)).setError(getString(R.string.at_least_one_field));
((TextView) findViewById(R.id.name)).setError(getString(R.string.at_least_one_field));
When i run the app and change content in barcode field it's error popup is hiding.
And when i change content in name field it's error popup is not hiding. It's hiding only when i tap on Finish button on the keyboard.
Why number and text fields have different behaviour?
I had the same problem with a "text" inputType, and found an explanation in this post https://stackoverflow.com/a/23599214/3005955
textView.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable edt) {
if( playerName.getText().length() > 0) {
playerName.setError(null);
}
}
});

Edittext in ListView lost cursor

I have a ListView contains multi EditText. Sometimes, when I scroll and focus on EditText, the cursor disappears although the keyboard is showing and I can input the character normally. The cursor just appears when I put the first character.
<EditText
android:id="#+id/et_nutri_lo"
android:background="#drawable/edit_text_corner"
android:paddingRight="#dimen/et_corner_radius"
style="#android:style/TextAppearance.Medium"
android:selectAllOnFocus="true"
android:layout_width="wrap_content"
android:layout_height="#dimen/btn_small_h"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:minEms="3"
android:maxEms="6"
android:gravity="right|bottom"
android:inputType="numberDecimal"
android:filterTouchesWhenObscured="false"
/>
Do you know what's wrong?
Thanks
Have you by any chance set an OnFocusChangeListener? If yes, try the following:
view.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
[YOUR CODE HERE]
view.dispatchWindowFocusChanged(hasFocus); // Fix for text selection handle not disappearing
}
});

Editview press softkey Return, lose focus AND hide keyboard, how?

I'm working on an Android app and I've got 2 editviews and a label. The user can enter 2 values and the label shows some calculation using input from the editviews. What I want is the following;
user enters either value with soft-keyboard
user presses "Return" softkey
editview should lose focus
the soft-keyboard should disappear
textview label should be recalculated
Now, the v.clearFocus only seems to works when there is another widget that can can get focus(?), so I've also added a dummie zero-pixel layout that can 'steal' the focus from the first editview. The Return key works now, but when the user switches focus from edit1 to edit2 by simply tapping then HideKeyboard() crashes. I've tried checking if inputMethodManager==null but that didn't help.
This all feels like I'm hacking to trick Android into doing some common UI behaviour, so I can't help but think that I'm overlooking something here.
Any help would be greatly appreciated. Btw I know this is similar to this question: How to lose the focus of a edittext when "done" button in the soft keyboard is pressed?
But I've tried that and it doesn't work.
So my layout xml is this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- Dummy control item so that first textview can lose focus -->
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<EditText
android:id="#+id/editTest1"
android:layout_width="250px"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:imeOptions="actionDone" >
</EditText>
<EditText
android:id="#+id/editTest2"
android:layout_width="250px"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:imeOptions="actionDone" >
</EditText>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test123" />
</LinearLayout>
And the source is this:
public class CalcActivity extends Activity implements OnFocusChangeListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab2_weight);
EditText testedit = (EditText) findViewById(R.id.editTest1);
testedit.setOnFocusChangeListener(this);
testedit.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
//Clear focus here from edittext
Log.d("test app", "v.clearFocus only works when there are other controls that can get focus(?)");
v.clearFocus();
}
return false;
}
});
}
public void hideSoftKeyboard() {
InputMethodManager inputMethodManager = (InputMethodManager) this.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
}
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus == false) {
Log.d("unitconverter", "onFocusChange hasFocus == false");
// update textview label
TextView bla = (TextView) findViewById(R.id.textView1);
bla.setText(String.format("%s + %s", (((EditText) findViewById(R.id.editTest1)).getText()), (((EditText) findViewById(R.id.editTest2)).getText())));
// hide keyboard
hideSoftKeyboard();
}
}
}

Categories

Resources