How to check which image is set to the activity's view? - android

How can I check what image is on my activity (programmatically)? I want to make a condition: if I have lets say img1, then I want to show a Toast "img1", and if I have img2, then I want to show a Toast "img2". I know there is something like setImageDrawable(getResources().getDrawable(R.drawable.img2), which does setting an image.

you can use the setTag function associated with a view to hold this information, as in view.setTag("img1") and you can get back the value as in (String)view.getTag()

you want to test if your imageView contains an image, then u want to set another
use this if it helps
if(imgView.getDrawable().equals(this.getResources().getDrawable(R.id.img1))) {
//affiche your Toast Here ... Toast ( "img1 displayed ..").show();
Toast.makeText(this,"image 1 dispayed ...",3000).show();
}
else if(imgView.getDrawable().equals(this.getResources().getDrawable(R.id.img2))) {
//affiche your Toast Here ... Toast ( "img2 displayed ..").show();
Toast.makeText(this,"image 2 dispayed ...",3000).show();
}
is that what you want to do ??

Related

Change image after click on ImageView

everyone. After clicking on an ImageView, I would like to change it depending on what kind of drawable is currently displayed. To do this, I compare the currently set image in the ImageView with one from the drawable folder. Unfortunately, it is currently the case that only the else branch is executed. The image changes after the first click, but then it no longer switches. What am I doing wrong?
bookmark_site.setOnClickListener{
vibrator.vibrate(50);
var draw = bookmark_site.drawable.constantState
if(draw == ContextCompat.getDrawable(this,R.drawable.ic_baseline_bookmark_filled_24)?.constantState)
{
bookmark_site.setImageDrawable(resources.getDrawable(R.drawable.ic_outline_bookmark_border_outline_24))
}
else{
//Only this two is ever executed
bookmark_site.setImageDrawable(resources.getDrawable(R.drawable.ic_baseline_bookmark_filled_24))
}
}
you can't base on constantState, its not desired to compare drawables as there is a different instance for every drawable, thus your if isn't true never ever. in fact there is no way for getting resource of drawable already set for ImageView. most convenient way would be to keep some boolean flag outside onClick method informing that image is switched or not. optionally you may keep this flag "inside" ImageView using setTag(...) method
bookmark_site.setOnClickListener{
vibrator.vibrate(50);
val switched = bookmark_site.tag != null
bookmark_site.setImageResource(
if (switched) R.drawable.ic_outline_bookmark_border_outline_24
else R.drawable.ic_baseline_bookmark_filled_24
)
bookmark_site.tag = if (switched) null else "SWITCHED"
}
you should keep category or tag along with the image in the model class and then change the image on the basis of that tag or category.

Image icon within Toast maketext message

I am beginning learning some android mobile development and have created a notepad app through some tutorials and am now wanting to customise it a little.
I currently have a Toast maketext message that displays when a user saves a new note. The code is as follows:
if(Utilities.saveNote(this, new Note(mNoteCreationTime, title, content)))
Toast.makeText(this, "Swag Note has been saved", Toast.LENGTH_SHORT).show();
What I am wanting to do is add a small icon at both ends of this toast message.
Is there a relatively simple way of achieving this?
Toasts cannot have icons. You can create a custom Toast with ImageViews in it (example). However, there might be an unicode symbol that suits your purpose: in that case, you can just paste it.
Edit:
To make the linked example work, you'd just have to add a View for the image with id "toast_image", which will be invoked this way:
ImageView image = (ImageView) layout.findViewById(R.id.toast_image);
Show Toast as you do it now:
Toast.makeText(this, "Swag Note has been saved", Toast.LENGTH_SHORT).show();
Show MyToast from linked example:
MyToast.show(this, "Swag Note has been saved", false);

Android gallery onItemClick show larger image

I've just finished copying the android walkthrough for the gallery (http://developer.android.com/resources/tutorials/views/hello-gallery.html) and was wondering how I would go about having the thumbnail photo scale into a larger one within the onClickItem method.
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(ProjectPreview.this, "" + position, Toast.LENGTH_SHORT).show();
}
Currently it just shows the position as depicted in the Toast. Could someone point me in the right direction or show me a simple way?
Thanks in advance.
EDIT: Added this method and called it in the onItemClick sending it the position. And it works.
public void showLarger(int position){
ImageView image = (ImageView) findViewById(R.id.iv1);
image.setLayoutParams(new Gallery.LayoutParams(200, 100));
image.setScaleType(ImageView.ScaleType.FIT_XY);
image.setImageResource(mImageIds[position]);
}
Use a Dialog with a layout which contains an imageview, and set its source case the position you have and with the size you want.
I would use a Dialog. You can create a new activity and have an ImageView in its layout, and disguise that Activity as a Dialog. See this question for how to do all that: Android Image Dialog/Popup
As mentioned above, you can use a Dialog to display the larger image or you can just fire an intent that would start a regular Activity(You will have to add the dialog Theme to your Activity if you do it as described as above).
You'd have to do something like call v.getItemID(position) and store that in a String, then pass that into your next Activity.
Either way, you'll have to pass in some data to your Activty that will host the ImageView, and then use that data(path, uri, etc) to inflate your larger ImageView. Just don't forget to add the theme to your manifest if you want use the Dialog route.

Animating a toast in android?

Am trying to animate my toast but have been unsuccessful so far. This is what I have in my onCreate():
ImageView image = new ImageView(this);
image.setImageResource(R.drawable.icon);
Animation move = new TranslateAnimation(0,100, 0, 100);
move.setDuration(2000);
move.setFillEnabled(true);
move.setFillAfter(true);
image.startAnimation(move);
Toast toast = new Toast(this);
toast.setView(image);
toast.show();
Have tried placing startAnimation() after displaying the toast, but that doesn't work either.
Toast notifications use a system template to determine how they are displayed. Short of modifying the Android source code, you can not create a special animation for your Toast message.
However, if possible you might be able to create a normal view and use that instead. One common method is to create a new Activity that has a transparent background with your modal toast message animated and displayed however you want.

Android -- Is there a way to rotate a toast 90 degrees?

Can't think of any more info to provide. Is there a way?
As hackbod said, you would have to have a custom view to display the toast.
I found a few classes for you that rotates the label for you: VerticalLabelView and CustomTextView
I chose to use the latter, and had this code working in my own app:
// Creating a new toast object
Toast myToast = new Toast(MyActivity.this);
// Creating our custom text view, and setting text/rotation
CustomTextView text = new CustomTextView(MyActivity.this);
text.SetText("Hello World!");
text.SetRotation(-90, 120, 90);
myToast.setView(text);
// Setting duration and displaying the toast
myToast.setDuration(Toast.LENGTH_SHORT);
myToast.show();
Not an easy way. You can supply your own view to display the toast, so you can make a view that rotates its content.

Categories

Resources