How to compare a drawable inside an imageView with another drawable? - android

I have a drawable inside an imageView and I need to know which drawable is to modify it.
<ImageView
android:id="#+id/dice_image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/empty_dice"
tools:src="#drawable/dice_1"/>
I already tried with constant state but it's deprecated and works sometimes

I found a solution. I put a tag inside each imageview to be able to track what image was inside. Every time I changed the image I updated the tag number.

Related

ImageView doesn't appear

I have dragged an ImageView to layout and I selected the picture. But when I execute the application, It doesn't appear. What can I do to solve this ?
ImageView XML code;
<ImageView
android:layout_width="250dp"
android:layout_height="250dp"
app:srcCompat="#mipmap/idlepoor_moneys"
android:id="#+id/imageView4"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
Add this property in Your image View
android:src="#mipmap/idlepoor_moneys"
Remove the following line from your code
app:srcCompat="#mipmap/idlepoor_moneys"
As allready said change
app:srcCompat="#mipmap/idlepoor_moneys"
to
android:src="#mipmap/idlepoor_moneys"
Also I recommend to put your images in the #drawable folder. The mipmap is where app icons are stored when making an app.
app:src="#drawable/idlepoor_moneys"
You can easily find the drawable from your project map. If you can't find it you can always right click the folder to get the path to it.

How to create a filled screen ImageView

I want to create a activity that looks like the gallery or whatsapp image view
I want the image to got displayed in its normal size and allow users to zoom in and out inside the ImageView so he can see the details of the image (which will contain text so i need to have zooming)
Please help cause i need this fast
This library provides zooming ImageViews.
Here is another great library i use.
https://github.com/sephiroth74/ImageViewZoom
Simply use this in xml instead of regular ImageView like this:
<your_package_name.ImageViewTouch
android:id="#+id/image_hq"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="#string/image_hq"
android:scaleType="matrix" />

How to return an original ImageView resource?

I have an ImageView defined in layout file like:
android:src="#drawable/ic_home"
During my application workflow I change the resource several times using
iv.setImageResource(some_id);
However if something going wrong I want to get my original resource back. Here is no method like ImageView.resetToDefault() and I do not see any other way than preserve original resource id in my program and set it when I need to return image back. However I need each time modify my code if I decided to change the resource attached to image view. So if somebody knows more elegant solution, then let me know.
PS I can see similar questions unanswered, so it seems Android design lack.
Guys, what about such work around? I make two image views stackable. So my image on top is invisible by XML definition. When I need update image, I just make it visible and reload image. When I want to get image default, I just hide it. Let me try.
After the view is inflated or set, set the ImageView resource programmatically.
defaultImageId = R.drawable.ic_home;
ImageView iv = (ImageView) findViewById(R.layout.my_image_view);
iv.setImageResource(defaultImageId);
//.... sometime later when you want to reset back to the "default"
iv.setImageResource(defaultImageId);
EDIT:
If you insist on keeping the resource id in XML only, ImageView does not allow for this to in its current state. You'll have to extend it and provide the functionality yourself. See the gist below for a working example. (I have tested it).
https://gist.github.com/enragedmrt/9143603
Although Enragedmrt provided a wonderful solution, I still came with a work around as below:
<ImageView
android:id="#+id/img_orig"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_default" />
<ImageView
android:id="#+id/img_towork"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_default" />
So when I want set to default I just do iv.setVisibility(View.INVISIBLE);

How can I stretch the image in imagebutton to the whole area of the imagebutton?

Here is a picture that will help you understand my problem:
I want to stretch the image shown inside ImageButton to the whole area of the ImageButton.
As you can see, the picture with the number 1 only take about 95% of the ImageButton and you can also see the boundaries of the ImageButton.
I still want to maintain the image button style
Here's how I am using ImageView:
<ImageButton
android:id="#+id/secondActivityBbuttonDownRight"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:scaleType="fitStart"
android:src="#drawable/111" />
I think I found out what is my problem.
I need a custom component that can contain only an image and have a clicking effect.
ImageButton is ImageView.
If you want your image to fill button
than set android:scaleType="fitXY", so the image you set in android:src will be stretched to fill whole image button.
ImageButton has a background under your image and padding.
If you do not want background
set android:background="#android:color/transparent".
If you do not want padding
set android:padding="0" or set background without padding.
You can also use
android:scaleType="centerCrop"
android:padding="0dp"
android:scaleType="fitXY" tested and works correctly.
Use below code :-
<ImageButton
android:id="#+id/secondActivityBbuttonDownRight"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:scaleType="fitStart"
android:background="#drawable/111" />
You should consider creating a 9-patch image to make the image fit correctly on an ImageButton in case you need to stretch it, but you don't want the image to become distorted.
To have a button with an image that completely covers it, you can use something like this:
<ImageButton android:id="#+id/iconBtn"
android:layout_width="64dip"
android:layout_height="64dip"
android:src="#drawable/icon_button"
android:scaleType="fitCenter"
android:background="#00000000"/>
For a full tutorial on creating buttons with custom skins and the use of selectors to manage their states, you can check a tutorial I've written some time ago here (Creating a custom Android button with a resizable skin).
If your image "111" has no padding itself then simply use android:background"#drawable/111"
Just set this two attributes on the ImageButton:
android:background="?attr/selectableItemBackgroundBorderless"
android:scaleType="centerCrop"
With those, you will get the provided src to be stretched, and also if you put some specific width and height on that ImageButton you won't get a rectangle around it.

Android Image Control

I have a big problem. I want to create a control for android in which the user sees an image and over lapping this image are smaller icons/image which are positioned relative to background image.
The smaller icons can be selected.
I really do not know how to go about it.
You could do this by having a clean image as background, like this:
and then you could have TextViews within a RelativeLayout with the names for example, and on every textView you can set the attribute clickable to your method.
Something like this:
<TextView
android:id="#+id/nevada"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/Oregon"
android:layout_marginRight="50dp"
android:layout_marginTop="21dp"
android:layout_toLeftOf="#+id/Cali"
android:clickable="true"
android:onClick="showState"
android:text="#string/Nevada"
android:textColor="#color/contact_map_text_color" />
and on your activity/fragment you will have your method that will be called by the View, in the example case: "showState"
Edit: its probably not the best approach but it certainly works

Categories

Resources