I'm trying to implement emojis in a way that I automatically replace things like ":)" with their emoji equivalent. And I'm not really sure how to do that, I found a table of emoji UTF codes, but I'm not sure on how I'm supposed to programmatically put them into a EditText :/
inputField=(EditText)temp.findViewById(R.id.inputField);
inputField.addTextChangedListener(new TextWatcher() {
boolean justChanged=false;
#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(justChanged) {
justChanged=false;
return;
}
Log.d(s.toString(), s.toString());
if(s.toString().contains(":)")) {
justChanged=true;
inputField.setText(s.toString().replace(":)", "UTF CODE HERE?"));
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
The simplest way is to achieve this is to encode your source code as UTF-8, and paste your desired emoji into the code. You will then need to pass -encoding UTF-8 to javac. The emoji will then be converted to its Unicode point on compilation.
E.g.
inputField.setText(s.toString().replace(":)", "😁"));
Alternatively, you can use UTF-16 Unicode point literals within Java strings, using the \uXXXX notation. From a suitable Unicode reference site, such as, http://www.fileformat.info/info/unicode/block/emoticons/list.htm, you can get the UTF-16 type encoding or the complete Java escape sequence.
E.g.
inputField.setText(s.toString().replace(":)", "\uD83D\uDE00"));
Related
I would like to prevent the users of my Android application to copy and paste data from my application to anywhere else but within the application itself. Given that the clipboard is one of the more common ways for exposing sensitive data, I am wondering if there is any way to limit the scope of the clipboard, so that it can only be used within the application?
I've already created a solution to prevent copy/paste in the application's text components (TextView, EditText...), but I am looking for a more efficient approach to this problem. I've been thinking about clearing the clipboard on exiting the application, but I don't want to do that, since the user might have important information he/she wants to keep in the clipboard.
Has anyone else faced a similar situation before? Do you have any ideas on how to do this?
Thanks!
This is not a full solution but you could limit the use of the copy and paste to one time use.
This way the sensitive data is not lingering.
public Class ClipboardUtils{
public static clear(Context context){
ClipboardManager clipBoard = (ClipboardManager) context.getSystemService(CLIPBOARD_SERVICE);
ClipData data = ClipData.newPlainText("", "");
clipBoard.setPrimaryClip(data);
}
}
With an edit text you could do something like this
EditText et = (EditText) mView.findViewById(R.id.yourEditText);
et.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count > 2)
ClipboardUtils.clear(getContext());
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void afterTextChanged(Editable s) {}
});
sources
Clearing Clipboard Data in Android
Android intercept paste\copy\cut on editText
I am new to Android and need some help.
I have a listView with some edittext fields and buttons and store the information of the edittext in a room database.
Looks like this: https://imgur.com/a/NTpMUmY
Now, when I change the input of field A to, let's say AAAA, and click on the "button", the room database has to update the row.
My question is when exactly do I call the update command, and how does room know which row has changed? I don't want to update to whole table.
Let me know if I should explain it in another way.
Ok, I found a solution which works for me.
TextWatcher textWatcher = 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) {
}
#Override
public void afterTextChanged(Editable s) {
Dao_db.update(s.toString());
}
};
viewHolder.bearbeiten_et.addTextChangedListener(textWatcher);
I use this Textwatcher (didn't know that something like this exist) and update my db when the text changed. :)
I am trying to generate a regular expression in Android which can satisfy following conditions:
Edit text can accept:
Alphabet only
Combination of Alphabet and number
Combination of Alphabet and special character
Should not accept:
a. Only Number
b. Only Special character
I tried alot but still, i didn't get any meaningful link. Please try to save my day.
I tried with regular expression (?!^\d+$)^.+$") which only valid alphanumric requirement. I am looking for such regular expression which fullfil my requirement.
It's really simple bro... just clear EditText if the text inside it doesn't have an alphabet .. as you have said ...
Should not accept a. Only Number b. Only Special character
TextWatcher mTextWatcher = 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) {
boolean atleastOneAlpha = s.toString().matches(".*[a-zA-Z]+.*");
if (!atleastOneAlpha) {
editText.setText("");
}
}
#Override
public void afterTextChanged(Editable s) {
}
};
mTargetEditText.addTextChangedListener(mTextWatcher);
I need regex that will allow only Latin characters, digits and all other symbols(but not whitespace)
thanks!
UPDATE:
private boolean loginPassHasCorrectSymbols(String input){
if (input.matches("[A-Za-z0-9\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\>\=\?\#\[\]\{\}\\\^\_\`\~]+$")){
return true;
}
return false;
}
I hope I got them all.
"[A-Za-z0-9\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\>\=\?\#\[\]\{\}\\\\\^\_\`\~]+$"
Edit: I forgot that in Java, the regexes are also strings, so you need to actually escape each \ given in the string using another \. I hope I didn't miss any now.
"[A-Za-z0-9\\!\\\"\\#\\$\\%\\&\\'\\(\\)\\*\\+\\,\\-\\.\\/\\:\\;\\<\\>\\=\\?\\#\\[\\]\\{\\}\\\\\\^\\_\\`\\~]+$"
How about everything not a whitespace?
"^\S+$"
I did this and it works for me .
Either you can block whitespace by mentioning it on Edittext, or you can block on editetext.addtextChangeListner too by pragmatically .
1>
android:digits="0,1,2,3,4,5,6,7,8,9,*,qwertzuiopasdfghjklyxcvbnm,_,-"
2>
etNewPassword.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 (etNewPassword.getText().toString().contains(" ")) {
etNewPassword.setText(etNewPassword.getText().toString().replace(" ", ""));
int iLength = etNewPassword.getText().toString().length();
etNewPassword.setSelection(iLength);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
Let me know if any concern.
For find any symbol except whitespace, you can use this code. I hope you find it useful
public static boolean hasAnySymbolExceptWhitespace(String string){
return Pattern.matches("(?=.*[^a-zA-Z0-9] ).*", string);
}
Smooth Kotlin solution which allows English letters with some default symbols. Cyrillic or any other language symbols won't be allowed.
//Allows english with usual symbols
private fun hasNonAllowedSymbols(input: String) : Boolean {
val regex = "[a-zA-Z0-9\\s-#,/~`'!#$%^&*()_+={}|;<>.?:\"\\[\\]\\\\]*"
val pattern = Pattern.compile(regex)
return !pattern.matcher(input).matches()
}
I'm new to android & I'm trying to write an application for a project.
I need to check whether the user has entered 7 numbers followed by one alphabet in edittext.
Example: 0000000x
How should I do that? TIA! :)
Probably the best approach would be to use a TextWatcher passed into the addTextChangedListener() method of the EditText. Here is an example use:
editText.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable e) {
String textFromEditView = e.toString();
validateText(textFromEditView);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//nothing needed here...
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//nothing needed here...
}
});
I will leave the implementation of the validateText(String) method as an exercise for the reader, but I imagine it should be easy enough. I would either use:
A simple Regular Expression.
Or since this case is easy enough, checking that the length of the string is 8, and reviewing each character. There is a simple utility class to inspect the characteristics of characters. Character.isDigit(char) and Character.isLetter(char)
OnKeyListener listens to every key stroke in the view. you can use that to check whether the user has entered what he is supposed.
eg : if the no of char entered is 7 then
check if it follows the reqd expression format.
There is a Class called Pattern in Android in that you can give Regular Expression to match your Requirements try this follwoing code i think it may work
Pattern p = Pattern.compile( "{7}" );
Matcher m = p.matcher(String.valueOf(edittext));
This will be true only if 7 characters are there in the Text box and then you can use some menthods like "Character.isDigit(char) and Character.isLetter(char)"