How to disable stretching of image in ImageView? - java

There are some ImageView and image for this view. ImageView is 100px/100px, but image is 50px/50px. I wouldn't like the image fills full space in ImageView, i.e. I want that the ImageView contains my image in center of ImageView without stretching. How can I do it?
UPDATE:
Sorry, I've made a mistake. My ImageView is 50px/50px, and my image is 100px/100px. What do you think about it?

Add to your ImageView:
android:scaleType="center"
Note that in the Android Studio xml layout preview if you use the android:background="#drawable/my_image" you will still see your image streached (In run time it won't streach).
But if you still want to see your ImageView background (not streached) also in the IDE preview use android:src="#drawable/my_image" instead of background property.
(in any case the scaleType should be center)

You need to set the android:scaleType attribute on your image view. If the image is bigger than your view, either use centerInside or one of the fitXY, fitStart, fitEnd options.
You have all the info you need on the Android developer documentation for the ImageView class.

set the android:scaleType with proper value, you can give centerCrop.
e.g. android:scaleType="centerCrop"

In ImageView we have a property called scaleType that is shown below in the code.
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button2"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="94dp"
android:scaleType="center"
android:src="#drawable/ic_launcher" />
It will not scale your image.

Related

I do not fit the image I received from the user in Android studio fully into the imageview

enter image description here
this is my application. I want the images in this recycler view to fill the whole screen. As in Instagram (I set the width of the Image View to match parent)
enter image description here
I want it to look like this
try setting your imageview width to 0dp and if this doesn't work then try setting scaleType to fitXY.
Let me know if this works or not!
Try using adjustViewBounds attribute, also you can use scaleType attribute if you want to scale the image:
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/ic_launcher_background" />

Placing an image in button but it does not show image

I created a button and I used android:background="#drawable/background" to change the image however it only displays the default design of a button (purple). How do I set it to the image I have saved in my drawable folder?
I know there is Image Button but I wanted to write a text on the button, but there is no feature where I can write a text on top if I used Image Button
As you have mentioned, you can't put a text if you have used ImageButton.
As far as I understood, you want to have view which has a text on above of image and image on bottom of the text.
If you use android:background feature on Button, button's background will be your drawable image and it won't be as you have expected.
If you are going to use Button for sure, you can use android:drawableBottom feature to show your drawable image on below of your text. And you can basically add button text by using android:text feature.
My advice to you, you should use TextView instead of Button for that case. Because if you do it with the Button as I have mentioned above, your button will have background and you can make transparent background for your button by using below code.
<Button
android:id="#+id/buttonMoreTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:drawableBottom="#drawable/ic_baseline_home_24"
android:text="#string/app_name"
android:textColor="#color/black" />
This code will make your Button has a transparent background.
android:background="#android:color/transparent"
But this is a little bit hacky solution. If it won't have background and if you do not have to use Button in your case for sure, you can use TextView for that and basically to show text and drawable on bottom, you can code your TextView basically as below.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
app:drawableBottomCompat="#drawable/ic_baseline_home_24" />
Note: These are very simple xml examples to solve your main problem about showing image on bottom of your text, in same view. If you need anything more, let me know and I would be glad to help.

Android - How to Resize/Reposition ImageView According to Parent?

What's the recommended approach for ensuring an ImageView button is the correct size according to its parent?
I have a VideoView set to "match_parent" (screen width) and an ImageView over the VideoView that is 60dp x 60dp, which is fine on the screen I'm testing.
Problem is, if I rotate the phone and the VideoView is much smaller, the ImageView is no longer proportional and in the correct position. Same issue is true for larger screen sizes such as tablets.
I want the ImageView to always be the same proportional size and appear in the same position relative to the VideoView (its parent).
You should consider using layout_sum in android:
Since I don't know how you are exactly coding this, I can only provide assumptions based code. I am assuming that you would want to proportionately align an VideoView and ImageView in a parent. (Assuming you required 30% of the space to be occupied by the ImageView and the rest 70% of the space to be occupied by the VideoView).
You can code the XML file as follows:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10"
android:orientation="vertical"
android:layout_gravity="center">
<VideoView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="7"/>
</LinearLayout>
This will embed the VideoView and ImageView into the LinearLayout (Parent view).
Try to use RelativeLayout,wrapContent and avoid using hardCoded sizes.
Or better define a separate layout for Landscape and Portrait View that may solve your issue.
Even if your issue is not solved try using this library i frequently use it in my projects-SDP
I ended up doing this programmatically using ViewTreeObserver GlobalLayout listener. It works great and I only need it for one view on the screen, so not a big deal. Button appears in the correct position relative to its parent size even with the phone is rotated.
var topPadding = Convert.ToInt32(_videoView.Height * .25);
_buttonLayout.SetPadding(_buttonLayout.PaddingLeft, topPadding, _buttonLayout.PaddingRight, _buttonLayout.PaddingBottom);

Is it possible to make android:drawableLeft fill the parent on a Button View?

So I've defined a simple Button in xml like so:
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_margin="5dp"
android:gravity="center"
android:text="#string/home_button_student"
android:textSize="#dimen/abc_text_size_headline_material"
android:onClick="buttonClick"
android:id="#+id/home_button_student"
android:drawableLeft="#drawable/ic_action_emo_cool"/>
I have several of these in a LinearLayout and as expected they fill the screen, each taking up an equal amount of space, each one looking similar to this: http://i.imgur.com/fMNldop.jpg
The problem I'm having here is as you can see the drawable is only the same height as the text, I'd like to know if there's a way to make the drawable scale to the height of the button itself WITHOUT using a workaround like creating my own Button using another LinearLayout, I'd like to stick to using a Button.
I have different versions of the image in my res folder under the correct folders (xxhdpi, xhdpi, and so on) but it only seems to want to use the smallest one.
EDIT: It seems like this may be due to the image supplied being too small, as I said just above I have the image in a whole range of sizes, how can I tell android to use a larger one on a larger screen instead of defaulting to the smaller one? I thought that was the whole point of having multiple sizes in the first place.
The drawable will only be the size the bitmap is. Just provide larger image and it should be fine.
For that you need to change your layout_height from fill_parent to wrap_content like this
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center"
android:text="#string/home_button_student"
android:textSize="#dimen/abc_text_size_headline_material"
android:onClick="buttonClick"
android:id="#+id/home_button_student"
android:drawableLeft="#drawable/ic_action_emo_cool"/>
If you want a button real big in height, then make sure you have an equal heighted drawable image.and also there's no need to give weight here,
you can use your custom layout for that like drawable left or right whatever you want just use linear layout put in this imageview and button for the view. so you can put image that you wants

How to correctly scale an ImageView in a widget?

I have two images overlayed on top of another in a widget layout. The top image is supposed to rotate above the background image. Both are ImageViews. As you can see, the inside circle is too small (check xkcd.com/now to see what the layout should look like) . I tried resizing the file itself, which didn't change anything, I tried using mat.postScale, which only gave me an outOfMemory error. I'm using matrix.postRotate to rotate the inside circle. Since I'm using RemoteViews (it's a widget), I'm very limited in what I can do. So how can I resize the inside circle by 115%? (oh and I can't use fixed values because it has to change sizes if the widget is resized)
And here is the XML layout:
<ImageView
android:contentDescription = "#string/app_name"
android:id="#+id/imagef"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"/>
<ImageView
android:contentDescription = "#string/app_name"
android:id="#+id/nowView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:layout_centerInParent="true"/>
Screenshot of what it looks like now:

Categories