How to use custom image instead of '*' in edittext password field?
see image:
Any answer or hint will be greatly appreciated.
The answer comes from this tutorial and it covers a behaviour when a user:
enters into the login screen, keyboard will open automatically.
tries to enter value in it then textbox background changes to textbox with star background.
tries to cancel/delete the input value by using back key on keyboard then textbox background will change to textbox without star background.
First of all you have to create two drawables:
Then, according to this approach, you have to implement addTextChangedListener method on your EditText. After that, as a parameter, you create a new instance of a TextWatcher class and you implement its methods:
etxtPin1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
if(etxtPin1.getText().toString().trim().length()==1){
etxtPin1.clearFocus();
etxtPin2.requestFocus();
etxtPin1.setBackgroundResource(R.drawable.pin_txt_bg_star);
}
}
});
Then, you have to implement setOnKeyListener and its method onKey:
this.etxtPin1.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View paramView, int paramInt, KeyEvent paramKeyEvent) {
if ((paramKeyEvent.getAction() == KeyEvent.ACTION_DOWN)&&(paramInt == 67) && (LoginActivity.this.etxtPin2.getText().length() == 0)) {
etxtPin1.requestFocus();
etxtPin1.setBackgroundResource(R.drawable.pin_txt_bg);
etxtPin1.setText("");
}
return false;
}
});
Another approach: create you own class which extends PasswordTransformationMethod.
public class MyPasswordTransformationMethod extends PasswordTransformationMethod {
#Override
public CharSequence getTransformation(CharSequence source, View view) {
return new PasswordCharSequence(source);
}
private class PasswordCharSequence implements CharSequence {
private CharSequence mSource;
public PasswordCharSequence(CharSequence source) {
mSource = source; // Store char sequence
}
public char charAt(int index) {
return '*'; // This is the important part
}
public int length() {
return mSource.length(); // Return default
}
public CharSequence subSequence(int start, int end) {
return mSource.subSequence(start, end); // Return default
}
}
};
Reference: In android how to show asterisk (*) in place of dots in EditText having inputtype as textPassword?
I think simple way is, you have to define 6-edittext in xml layout.
and set width and height with custom background resorces.
Like,
<?xml version="1.0" encoding="utf-8"?>
<solid android:color="#FFFFFF" />
<stroke
android:width="1dp"
android:color="#9999" />
<corners
android:bottomLeftRadius="1dp"
android:bottomRightRadius="1dp"
android:topLeftRadius="1dp"
android:topRightRadius="1dp" />
and read all edittext value.
I am not sure but it will help you.
Related
Problem: MyAutoCompleteTextView.**showDropDown()** will work when called by the onFocusChange but won't work when called by the onTextChanged. During my debug, the onTextChanged method gets called correctly when I want it to execute the showDropDown() according to the logging message I created, just nothing happens. I noticed there is a SpannableStringBuilder error that occurs prior, but as I recall (could be wrong) when I checked on this error in the past, this is a common error with an open ticket. I wasn't really sure it is the cause.
What I'm trying to do: ... is what everyone that asks this type of question wants to do, get the AutoCompleteTextView to show the full list anytime the AutoCompleteTextView is focused and empty (my first item is: "")
What I have tried: I've tried many "green checked" solutions on this forum, but none of them have worked at present EXCEPT when first focused. They will cause the full items list to present, but it is on the backspacing back to zero that it will not. I'm using a combination of change listeners as most suggestions have. I just really thought this would work based on the logging values and it being called at the proper time?
My logging statement: which writes when I want it to showing the method gets called when I like.
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
E/OnTextChanged: showDropDown()
start: 0
before: 1
count: 0
length: 0
My current version and effort:
ArrayAdapter<String> topicsAdapter = DBQueryTools.captureDBTopics(this);
topic.setAdapter(topicsAdapter);
topic.setOnItemClickListener((parent, view, position, id) -> {
if(!topic.getText().toString().equals("")) {
question.setText("");
rListNotes.setAdapter(null);
customSearch.setText(null);
loadNotes(captureNotes(researchDatabase.getNotesDao().getNotesOnTopic(topic.getText().toString())));
}
});
topic.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(before == 1 && count == 0 && s.length()==0) {
topic.showDropDown();
Log.e("OnTextChanged", "showDropDown() " + s + "\n" + "start: " + start + "\nbefore: " + before + "\ncount: " + count + "\nlength: " + s.length());
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
topic.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
topic.showDropDown();
Log.e("HasFocus", "showDropDown");
}
}
});
I was able to get a modified version of the InstantAutoComplete view working (combining from here and here)
public class InstantAutoComplete extends AppCompatAutoCompleteTextView {
public InstantAutoComplete(Context context) {
super(context);
}
public InstantAutoComplete(Context arg0, AttributeSet arg1) {
super(arg0, arg1);
}
public InstantAutoComplete(Context arg0, AttributeSet arg1, int arg2) {
super(arg0, arg1, arg2);
}
#Override
public boolean enoughToFilter() {
return true;
}
#Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (focused && getAdapter() != null) {
// This part was necessary to get it working when not
// inside a TextInputLayout and multiple per activity
if( !maybeShowSuggestions() ) {
post(this::maybeShowSuggestions);
}
}
}
private boolean maybeShowSuggestions() {
if( getWindowVisibility() == View.VISIBLE ) {
performFiltering(getText(), 0);
showDropDown();
return true;
}
else {
return false;
}
}
}
The XML for inserting it looks like (also worked without the TextInputLayout)
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/text_layout"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type words here"
android:layout_margin="16dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/focus_stealer" >
<com.example.autocompletetextviewdemo.InstantAutoComplete
android:id="#+id/autocomplete_text_view"
android:layout_width="match_parent"
android:completionThreshold="0"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
and for setting it up with an adapter
private static final String[] WORDS = new String[] {
"Aardvark", "Apple", "Baby", "Boron", "Carbon"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, WORDS);
AutoCompleteTextView textView = findViewById(R.id.autocomplete_text_view);
textView.setAdapter(adapter);
}
This works with multiple InstantAutoComplete dropdowns per activity, and after navigating back and forth between activities. Without the extra changes to the custom class it didn't work with multiple instances in an activity that weren't in a TextInputLayout.
I am trying to build a simple converter code(convert feet to mtr) in Android. I am able to do this but only when user clicks some button. Now I want to modify it such that it starts to convert as and when the user gives input(Something like google converter). Is there any way to do this in Android?
Thanks in advance.
Add the listener to your edittext:
yourEditText.addTextChangedListener(addTextWatcher);
Add the TextWatcher interface:
private TextWatcher addTextWatcher = new TextWatcher() {
#Override
public void onTextChanged(CharSequence sequence, int start, int before, int count) {
// here is where you could grab the contents of the edittext
// input each time a character is entered, and pass the value
// off to your unit conversion code. Careful to check for
// numerals/decimals only, or to set the proper inputType in
// your xml.
}
#Override
public void beforeTextChanged(CharSequence sequence, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable sequence) {
}
};
I'm trying to get a character count of an EditText (numberRoom). When user would insert 8 characters button should switch from state Disabled and color 0xBBFFFFFF to state Enabled and color 0xFFFFFFFF.
I've tried few method and I think the best one I've found is that one below. However button has state Enabled and color 0xFFFFFFFF even when input is empty. What's wrong there?
public class Join_room_screen extends Activity {
EditText numberRoom;
Button goToRoom;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.joinroom);
numberRoom = (EditText) findViewById(R.id.roomNumber);
goToRoom = (Button) findViewById(R.id.goToRoom);
TextWatcher watcher = new LocalTextWatcher();
goToRoom.addTextChangedListener(watcher);
updateButtonState();
}
void updateButtonState() {
boolean enabled = checkEditText(numberRoom);
goToRoom.setBackgroundColor(0xFFFFFFFF);
goToRoom.setEnabled(enabled);
}
private boolean checkEditText(EditText edit) {
return ((edit.getText().toString()).length() == 8 );
}
private class LocalTextWatcher implements TextWatcher {
public void afterTextChanged(Editable s) {
updateButtonState();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
}
}
However in properties I've
In this function the enabled variable is never used so the background colour and enabled states are always set.
void updateButtonState() {
boolean enabled = checkEditText(numberRoom);
goToRoom.setBackgroundColor(0xFFFFFFFF);
goToRoom.setEnabled(enabled);
}
I would replace with something like
void updateButtonState() {
boolean enabled = checkEditText(numberRoom);
if (enabled) {
goToRoom.setBackgroundColor(0xFFFFFFFF);
goToRoom.setEnabled(enabled);
} else {
//change them back to disabled state
}
}
You have one problem in updateButtonState(), it always sets one color to your button. I see, you have already solved that.
The other problem is that you set TextChangeListener not to an EditText, but somewhy to a Button.
The EditText should be watched.
numberRoom.addTextChangedListener(watcher);
instead of
goToRoom.addTextChangedListener(watcher);
On PC I can add a onKeyListener for a JTextField to listen keyReleased event. On Android I've used addTextChangedListener.
I have two EditText fields in my Android application. Editing one will affect the other. This will cause the program to fail in stack overflow error.
How can I listen for the phone's keyboard instead of changes in the EditText field? I don't want the program to invoke the listener because of the infinite loop caused by the listener.
Attach a onFocusChangedListener and add the TextChangedListener when a EditText has focus and remove it when it loses focus.
Something like this:
EditText1.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
((EditText) v).addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
//
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
//
}
public void afterTextChanged(Editable s) {
// affect EditText2
}
});
}
if(!hasFocus){
((EditText) v).removeTextChangedListener();
}
}
});
}
});
The same for EditText2
First of all, I would create one text change listener, something like SynchronizingWatcher and attach it to both EditTexts. Then, when you receive a text change event, before updating other text edits, just unregister old listeners, update text and enable listeners again:
class SynchronizingWatcher implements TextWatcher {
Set<EditText> synchronizedViews = new HashSet<EditText>();
public void watchView(EditText view) {
view.addTextChangedListener(this);
synchronizedViews.add(view);
}
public void afterTextChanged(Editable s) {
for (EditText editText : synchronizedViews) {
editText.removeTextChangeListener(this);
editText.setText(s); // Of course you can do something more complicated here.
editText.addTextChangeListener(this);
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Don't care.
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Don't care.
}
}
...
// Somewhere in your activity:
SyncrhonizingWatcher synchronizingWatcher = new SynchronizingWatcher();
synchronizingWatcher.watchView(myEditText1);
synchronizingWatcher.watchView(myEditText1);
Another solution: provide your own KeyListener that decorates existing KeyListener (you can get existing key listener with editText.getKeyListener() and set your decorator with editText.setKeyListener(). Your decorator would also update other edit texts in onKeyUp(). But I would try to stay away from messing with that stuff.
I am creating a comma seperated file and don't want to give the user a way to confuse the app.
Following what RoToRa said, you can delimitate the file using tabs instead.
If you do want to disallow commas, you can add a TextWatcher to modify the string before it is posted to the GUI:
EditText text;
private void foo()
{
text.addTextChangedListener(new TextWatcher()
{
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
public void afterTextChanged(Editable s)
{
// modify string here
}
});
}
Just check the addTextChangedListener(TextWatcher watcher) method - add the listener, which will check the editText field when it changes.