In my app I programmatically set start and end selection on EditText via setSelection(int start, int end) method, but user doesn't see selection markers. How can I show markers programmatically?
When I said marker I mean this:
I found answer. Hope it will help someone. Need use method show when need show pins and in onSelectionChange need use hidePins, because pins don't hide after used showPins.
public void showPins() {
Class TV = TextView.class;
try {
Field GetEditor = TV.getDeclaredField("mEditor");
GetEditor.setAccessible(true);
Object editor = GetEditor.get(this);
Method GetInsController = editor.getClass().getDeclaredMethod("getSelectionController");
GetInsController.setAccessible(true);
Object insController = GetInsController.invoke(editor);
if (insController != null) {
Method Show = insController.getClass().getDeclaredMethod("show");
Show.setAccessible(true);
Show.invoke(insController);
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public void hideTwoPins() {
Class TV = TextView.class;
try {
Field GetEditor = TV.getDeclaredField("mEditor");
GetEditor.setAccessible(true);
Object editor = GetEditor.get(this);
Method GetInsController = editor.getClass().getDeclaredMethod("getSelectionController");
GetInsController.setAccessible(true);
Object insController = GetInsController.invoke(editor);
if (insController != null) {
Method hide = insController.getClass().getDeclaredMethod("hide");
hide.setAccessible(true);
hide.invoke(insController);
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
custom method
/**
* Method allocates filtering substring in all contacts yellow color,
* that satisfy the user's search
* #param inputText - DisplayName
* filtText - filtering Text
* #return String with allocating substring (Spannable)
*/
public static Spannable changeBackgroungFiltText(CharSequence inputText, String filtText, int color) {
Spannable str = null;
if(inputText != null)
{
String inputStr = inputText.toString();
String inputLowerCaseStr = inputStr.toLowerCase();
String filtLowerCaseStr = filtText.toLowerCase();
// Spannable str = new SpannableStringBuilder(inputStr);
str = new SpannableStringBuilder(inputStr);
if (filtText.length() != 0)
{
int indexStart = 0;
while (true)
{
int indexCur = inputLowerCaseStr.indexOf(filtLowerCaseStr, indexStart);
if (indexCur != -1) {
int start = indexCur;
int end = indexCur + filtText.length();
int flag = Spannable.SPAN_EXCLUSIVE_EXCLUSIVE;
str.setSpan(new ForegroundColorSpan(color),start, end, flag);
//str.setSpan(new BackgroundColorSpan(highlightColor), start, end, flag);
indexStart = indexCur + 1;
} else {
return str;
}
}
} else {
return str;
}
}
return str;
}
Here is how to show the left or right pin,
based on the code of the second post (thanks a lot by the way).
public static void showSelectionPins(TextView textView, boolean left, boolean right) {
Class TV = TextView.class;
try {
Field GetEditor = TV.getDeclaredField("mEditor");
GetEditor.setAccessible(true);
Object editor = GetEditor.get(textView);
Method GetInsController = editor.getClass().getDeclaredMethod("getSelectionController");
GetInsController.setAccessible(true);
Object insController = GetInsController.invoke(editor);
if (insController != null) {
Method Show = insController.getClass().getDeclaredMethod("show");
Show.setAccessible(true);
Show.invoke(insController);
}
/**
* since the {#code mStartHandle} and {#code mEndHandle} are created lazily<br/>
* we must access them after {#code show()} has been called.
*/
if (!left) {
Field SelectionStartHandleView = insController.getClass().getDeclaredField("mStartHandle");
SelectionStartHandleView.setAccessible(true);
Object startHandleView = SelectionStartHandleView.get(insController);
Method handleViewHideMethod = startHandleView.getClass().getSuperclass().getDeclaredMethod("hide");
handleViewHideMethod.setAccessible(true);
handleViewHideMethod.invoke(startHandleView);
}
if (!right) {
Field SelectionEndHandleView = insController.getClass().getDeclaredField("mEndHandle");
SelectionEndHandleView.setAccessible(true);
Object endHandleView = SelectionEndHandleView.get(insController);
Method handleViewHideMethod = endHandleView.getClass().getSuperclass().getDeclaredMethod("hide");
handleViewHideMethod.setAccessible(true);
handleViewHideMethod.invoke(endHandleView);
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
Here is how to show the insertion pin,
also based on the code of the second post (thanks a lot by the way).
public static void showInsertionPin(TextView textView) {
Class TV = TextView.class;
try {
Field GetEditor = TV.getDeclaredField("mEditor");
GetEditor.setAccessible(true);
Object editor = GetEditor.get(textView);
Method GetInsertionPointCursorController = editor.getClass().getDeclaredMethod("getInsertionController");
GetInsertionPointCursorController.setAccessible(true);
Object insController = GetInsertionPointCursorController.invoke(editor);
if (insController != null) {
Method hide = insController.getClass().getDeclaredMethod("show");
hide.setAccessible(true);
hide.invoke(insController);
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
Related
I would like to ask how to call my decryption method from one activity to another(Adapter).
MessageActivity.java here is my decryption method:
private String AESDecriptionMetod(String string) throws UnsupportedEncodingException {
byte[] EncriptedByte = string.getBytes("ISO-8859-1");
String decryptedString = string;
byte[] decryption;
try {
decipher.init(cipher.DECRYPT_MODE, secretKeySpec);
decryption = decipher.doFinal(EncriptedByte);
decryptedString = new String(decryption);
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
return decryptedString;
}
MessageAdapter.java where I want to call decryption method:
#Override
public void onBindViewHolder(#NonNull MessageAdapter.ViewHolder holder, int position) {
Chat chat = mChat.get(position);
stringMessage = chat.getMessage().toString();
stringMessage = stringMessage.substring(1, stringMessage.length()-1);
String[] stringMessageArrey = stringMessage.split(", ");
Arrays.sort(stringMessageArrey);
String[] stringFinal = new String[stringMessageArrey.length];
for(int i = 0; i<stringMessageArrey.length; i++){
String[] stringKeyValue = stringMessageArrey[i].split("=", 2);
try {
stringFinal[i]= AESDecriptionMetod(stringKeyValue[1]);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return stringFinal.toString();
}
holder.show_message.setText(stringFinal);
if (imageurl.equals("default")){
holder.profile_image.setImageResource(R.mipmap.ic_launcher);
} else {
Glide.with(mContext).load(imageurl).into(holder.profile_image);
}
}
And how to implement stringFinal in holder.show_message.setText( ??? );
Thanks for any answer!
I have already gone through related questions on stack but all in vain. I am not getting callback in Invitation Listener. Any help will be appreciated.
MultiUserChatListener
MultiUserChatManager.getInstanceFor(connection).addInvitationListener(new InvitationListener() {
#Override
public void invitationReceived(XMPPConnection conn, MultiUserChat room, String inviter, String reason, String password, Message message) {
String nickname = AppSharedPereference.getInstance(mContext).getUserName();
try {
mchat.join(nickname);
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
}
});
}
sendInvitation Method
public void sendInvites(ArrayList<ContactData.Usercontactsdetail> usercontactsdetail) {
try {
for (int i = 0; i < usercontactsdetail.size(); i++) {
String userID = usercontactsdetail.get(i).getContactid();
String user = userID + "#" + connection.getHost();
mchat.invite(user,
userID + "_user");
}
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}finally {
usercontactsdetail = null;
}
try {
mchat.sendMessage("New group created");
} catch (SmackException.NotConnectedException e1) {
e1.printStackTrace();
}
}
I want to use progress dialog in function in Android Studio. This form will appear until the code is executed:
protected boolean encryptFile() {
/*
* set input file name
*/
String encryptFileName = mEditText.getText().toString();
/*
* set hint
*/
String hint = mHintText.getText().toString();
if(hint.isEmpty()){hint=null;}
/*
* set password
*/
String password = mPassText.getText().toString();
/*
* select encrypted file name
*/
String encryptedFileName = mEditText.getText().toString()+".enc";
if(hint!=null){
encryptedFileName = mEditText.getText().toString() + "(hint-" +hint+ ").enc";
}
/*
* Encrypt
*/
try {
AESCrypt crypt = new AESCrypt(password);
crypt.encrypt(2, encryptFileName, encryptedFileName);
return true;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (GeneralSecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public void encrypt(int version, String fromPath, String toPath)
throws IOException, GeneralSecurityException {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(fromPath));
debug("Opened for reading: " + fromPath);
out = new BufferedOutputStream(new FileOutputStream(toPath));
debug("Opened for writing: " + toPath);
encrypt(version, in, out);
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
in quickblock sample chat, trying to add block and unblock for user 1-1 was unable to include ALL of the desired items .Only one user was adding to list always this is my code
privacyListsManager = QBChatService.getInstance().getPrivacyListsManager();
privacyListsManager.addPrivacyListListener(privacyListListener);
privacyListsManager.setPrivacyListAsActive("public");
QBPrivacyList list_all = null;
try {
list_all = privacyListsManager.getPrivacyList("public");
Log.e("lists", "privacyCheck: " + list_all + list_all.getItems());
privacyListsManager.setPrivacyList(list_all);
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
}
and on click of block, private void blockUser() {
List<Integer> userIds = qbChatDialog.getOccupants();
Log.e("userid ", "blockUser: " + id);
QBPrivacyList list = new QBPrivacyList();
list.setName("public");
ArrayList<QBPrivacyListItem> items = new ArrayList<QBPrivacyListItem>();
QBPrivacyListItem item1 = new QBPrivacyListItem();
item1.setAllow(false);
item1.setType(QBPrivacyListItem.Type.USER_ID);
item1.setValueForType(String.valueOf(id));
items.add(item1);
list.setItems(items);
try {
privacyListsManager.setPrivacyList(list);
privacyListsManager.setPrivacyListAsDefault("public");
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
}
privacyListListener = new QBPrivacyListListener() {
#Override
public void setPrivacyList(String listName, List<QBPrivacyListItem> listItem){
Log.e("block", "setPrivacyList: " );
}
#Override
public void updatedPrivacyList(String listName) {
Log.e("unblock", "setPrivacyList: " );
}
};
}
The problem is `QBPrivacyList always used to be override with the block click values but I want the previous data to be saved.
I need to know if there has been set an OnTouchListener to a view.
Following code works fine below api level 15.
public static boolean isSetOnTouchListener_v8(View v) {
try {
Class<View> clazz = (Class<View>) Class.forName("android.view.View");
Field f = clazz.getDeclaredField("mOnTouchListener");
f.setAccessible(true);
if (f.get(v) == null) {
return false;
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return true;
}
Now I need the same method for api level 15 and above.
I tried the following code, but it's not working. Does anyone have any suggestions?
public static boolean isSetOnTouchListener_v15(View v) {
try {
Object listenerInfo = getListenerInfo(v);
if (listenerInfo != null) {
Class<? extends Object> clazz = listenerInfo.getClass();
Field f = clazz.getDeclaredField("mOnTouchListener");
f.setAccessible(true);
if (f.get(listenerInfo) == null) return false;
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
return true;
}
private static Object getListenerInfo(View v) {
Field f = null;
try {
f = View.class.getDeclaredField("mListenerInfo");
f.setAccessible(true);
return (Object) f.get(v);
} catch (Exception e) {
return null;
}
}