How to display the clipboard copied text inside a toast - java

I'm new to Android. I want to display the copied coupon code.
Here is code:
ClipboardManager clipboard = (ClipboardManager) mCtx.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("Code", artist.getCoupon_code());
clipboard.setPrimaryClip(clip);
Toast.makeText(mCtx, "coupon code: "+clip+" is copied" , Toast.LENGTH_SHORT).show();
How to remove the red-underlined text and only display the coupon code i.e. green underlined text.
output:

The string you want may be found in
clip.getItemAt(0).getText();
or
clipboard.getPrimaryClip().getItemAt(0).getText();
or, because you are setting the clip right there, you can just use the value directly, in the string
artist.getCoupon_code()
Good luck!

The snippet below should do the tric for you.
ClipboardManager clipboard = (ClipboardManager) mCtx.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
Toast.makeText(mCtx, "coupon code: "+item.getText()+" is copied" , Toast.LENGTH_SHORT).show();
Find more info about copying and pasting from the link below.
Copy and Paste | Android developers

Related

ClickableSpan onClick not working android java

I am developing an android SMS application using java and I want to check if the received message contains a phone number. If yes, I want to make the phone number bold and underlined and when user clicks on it, I want to open a popup for options like Call, Add To Contacts etc. I have done everything except for the part where I want to show the popup when user clicks on the phone number. I tried using ClickableSpan but the onClick method never triggers. Here is what I have done so far:
String finalMsg;
String ifNumberExists = Util.extractNumber(message.getMessageBody());
String boldNum = "<b><u>" + ifNumberExists + "</u></b>";
if (message.getMessageBody().contains(ifNumberExists)) {
finalMsg = message.getMessageBody().replace(ifNumberExists, boldNum);
} else {
finalMsg = message.getMessageBody();
}
SpannableString spannableString = new SpannableString(boldNum);
spannableString.setSpan(new ClickableSpan() {
#Override
public void onClick(#NonNull View widget) {
Toast.makeText(viewMessageThreadActivity, "Text: ", Toast.LENGTH_SHORT).show();
}
}, 0, boldNum.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView messageBodyLabel = rowView.findViewById(R.id.message_body_label);
messageBodyLabel.setText(Html.fromHtml(finalMsg));
messageBodyLabel.setClickable(true);
messageBodyLabel.setMovementMethod(LinkMovementMethod.getInstance());
you are creating spannableString, but enver uses that, TextView have set Html.fromHtml(finalMsg) as a content. set this spannableString as text to get desired result (and onClick working)
messageBodyLabel.setText(spannableString.toString());

Using glide to display multiple images

For a scanning ticket app, I want to display full screen image.
The app does sequential operation. You scan your QrCode (first image), the device reach server (second image indicating to wait), then I display a third image if the ticket is validate, else I display the last one.
I'm using glide and the image path is indicated in a second activity, I transmit URI to main activity with a sharedPreferences file.
Here is the issue:
val settings = getSharedPreferences("PrefFile", 0)
if (S_mod){//priority on S_mod
uri_State_init = Uri.parse(settings.getString("mainUri", "none"))
uri_State_waiting = Uri.parse(settings.getString("waitUri", "none"))
uri_State_validated = Uri.parse(settings.getString("okUri", "none"))
uri_State_refused = Uri.parse(settings.getString("errorUri", "none"))
displayUri( uri_State_init, background)
image.setImageResource(R.drawable.empty)
}
where displayUri is
fun displayUri( uri: Uri?, dir:ImageView){//use to display image known by uri, only for full screen
Glide.with(this)
.load(uri)
.error(R.drawable.imagenotfound) // image in case of error
.override(1280, 800) // resizing if user doesn't respect the indicated dim
.centerCrop()//type of resizing
.into(dir)
}
The issue is that only the first image called by displayUri is displayed, the other call show the error image (imagenotfound)
It seem that I've not totally understood the glide extension.
If someone know about this particular issue thanks a lot!!
Here
val settings = getSharedPreferences("PrefFile", 0)
if (S_mod){//priority on S_mod
uri_State_init = Uri.parse(settings.getString("mainUri", "none"))
uri_State_waiting = Uri.parse(settings.getString("waitUri", "none"))
uri_State_validated = Uri.parse(settings.getString("okUri", "none"))
uri_State_refused = Uri.parse(settings.getString("errorUri", "none"))
displayUri( uri_State_init, background)
image.setImageResource(R.drawable.empty)
}
displayUri( uri_State_init, background)
You are just calling the display Image function with the same uri which means if you didn't passed the uri, then uri will be null and you will definitely get errors. What you can do is to send uri like this ,
val settings = getSharedPreferences("PrefFile", 0)
if (S_mod){//priority on S_mod
uri = Uri.parse(settings.getString("main_uri", "none"))
displayUri( uri_State_init, background)
image.setImageResource(R.drawable.empty)
}
Just change the value of main_uri in Shared Preferences whenever you want to change the state. You can also use onSharedPreferencesChanged Listener.

how to set value in setBackgroundResource

I want to change a picture from another activity and display it as a picture or button, but it makes me a problem when I want to run the program, I'd love to help
imageView10 = (ImageView) findViewById(R.id.imageView10);
String s;
SharedPreferences mySharedPreferences2 = enter.this.getSharedPreferences("two", Context.MODE_PRIVATE);
s = mySharedPreferences2.getString("USERNAME2", "");
s = "R.drawable.add1";
imageView10.setBackgroundResource(s);
the s value is red...

How to insert a text in EditText?

How to insert a text in EditText at the cursor position? editText.setText erases the current text and sets the new one, which is not what I want. editText.setText(editText.getText() + "my text"); inserts the text at the end, that's also not the case.
Its very easy to do this :
int start =editText.getSelectionStart(); //Get cursor position with this function
String str = "my text";//String you want to insert
editText.getText().insert(start, str); //This will get the text and insert the String str at the current position.
Hope this Helps!
are you perhaps talking about settings the hint text ?
editText.setHint("Enter Text Here");
in XML
android:hint="Enter Text Here"
If this is not what you are looking for then please alaborate the problem a bit more
Add \n before the text you want to add...
var.setText(var.getText() + "\nnewtext");

Select multiple images from Photo Gallery on Android using Intents

#See this https://stackoverflow.com/a/15029515/185022
I`m trying to select images from gallery, but i only found the way to select a single image.
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
Is there a way to select multiple images?
Create a custom gallery same like: Android custom image gallery with checkbox in grid to select multiple
First of all you need to use putExtra with your photoPickerIntent
photoPickerIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE);
Then in your on activity result you should get ClipData from Intent like this
ClipData clipData = data.getClipData();
//Where data is param intent of onActivityForResult
And iterate this clipData to get URI for specific picked image.
for (int i = 0; i < clipData.getItemCount(); i++){
Uri uri = clipData.getItemAt(i).getUri();
}
I hope this helps
Why don't you try ACTION_SEND_MULTIPLE thing. You will receive a set of Uris.
Something like
if (Intent.ACTION_SEND_MULTIPLE.equals(action))
&& Intent.hasExtra(Intent.EXTRA_STREAM)) {
ArrayList<Parcelable> list =
intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
for (Parcelable parcel : list) {
Uri uri = (Uri) parcel;
/// do things here.
}
}
Saw this code block on a google-groups post. Just try this out.
Thanks.
I think, you should implement custom gallery for multiple image pick action.
see here in details.

Categories