Image scale in TextView - android

I use TextView and drawableRight:
<TextView
android:id="#+id/text1_2"
android:drawableRight="#drawable/star"
android:layout_width="match_parent"
android:layout_height="20dp"
android:textColor="#color/grey_500"
android:textSize="#dimen/category_bottom_text_size" />
But the image does not scale according to the textView's height and just crops. I tried different solutions, such as using a bitmap file for this image and making the image .9, but the result was the same.

Try to used.. textview in image scale
android:scaleX="0.5"
android:scaleY="0.5"

Related

How to Put an image and text overlapped when the TextView height is wrap content?

I am working in an Android studio project. Here I am used an imageview and an TextView. I want to put some text on am image.
There are different texts taken from a database. The size of the texts vary.
The problem is when the size of the texts are too long it exceeds the size of the image, thus exceeds the image. The image size I have given is fixed. And the size of the TextView is wrap content.
Is there any way to make it possible that whatever the size of the texts, the image size will match accordingly? (I can use different images in case the size of the texts varies too much).
Thank you in advance.
You can use a relativeLayout to achieve the desired result. Make the imageView alignLeft and alignRight to the textView, so that it will match the width of the textView. Use scaleType = "fitXY" to fit the image src to the width of the imageView.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignLeft="#id/text_view"
android:layout_alignRight="#id/text_view"
android:scaleType="fitXY" />
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some text here" />
</RelativeLayout>

Fitting image width to match parent and adjust height according to aspect ratio in ImageView

I have an ImageView in my xml -
<ImageView
android:id="#+id/image"
android:layout_centerHorizontal="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
I am using Glide library to fetch image from an url and set that into the ImageView.
All i want is to match the width of image with parent width and then adjust the height of image without affecting the aspect ratio.
if the image dimension is very less, it should be expanded to match parent width and if image dimension are more than screen size it should be shrinked to match parent width.
ImageView img = (ImageView) findViewById("image");
Glide.with(this).load("url here").into(img);
Which scaleType should i use ?
Is it possible in XML or it needs java code ?
Thanks in advance. :)
It is possible from the xml. I achieved it using the following code.
<ImageView
android:id="#+id/ivImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:adjustViewBounds="true"/>
Scale XY scales the image using FILL and adjust view bounds preserves the aspect ratio.
I do not think you need center horizontal property.
After this I called a photo loader library to just download the image into the image view.
It helps me. Just use in imageView xml
android:adjustViewBounds="true"
And add override in glide:
GlideApp.with(context)
.load(url)
.override(Resources.getSystem().getDisplayMetrics().widthPixels)
.into(imageView)
use android:scaleType:fitXY
<ImageView
android:id="#+id/image"
android:layout_centerHorizontal="true"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
and java code
Glide.with(this).load(url)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(img);
You may use ConstraintLayout for this purpose.
You can set the view size to a ratio such as 16:9 if at least one of the view dimensions is set to "match constraints" (0dp)
As quoted from android developers. Also you can refer this medium article for reference on how to achieve this ratio thing.

ImageDrawable not set in desire imageview size

I'm trying to set drawable at ImageView where I'm generation drawable by image bitmap of intrinsicWidth and height 160 and imageview width is 36dp but still image not setting in 36dp imageview.
Image is looking more smaller than imageview.
Please help me.
<ImageView
android:id="#+id/img_doctor"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_centerVertical="true"
android:scaleType="centerCrop" />
Drawable drawable11 = new BitmapDrawable(getResources(),myBitmap);
Log.e("", ""+drawable11.getIntrinsicWidth()+"::"+drawable11.getIntrinsicHeight());
imgView.setImageDrawable(drawable11);
Here I'm getting 160::160 in logcat.
if you have bitmap already then use imgView.setImageBitmap(myBitmap) instead of converting bitmap to drawable.

How to set bitmap according to the image size?

I am trying to set bitmap to imagebutton but image size change
My Image xml is
<ImageButton
android:id="#+id/button_ChoosePicFIrst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/choose_pic"
android:tag="PIC1" />
i want to set image size accoring to the "#drawable/choose_pic" size
after choosing image from gallery i am convertin it to bitmap and it change the imagebutton size please give me some solution ?
//for that use ImageView with src
<ImageView
android:id="#+id/button_ChoosePicFIrst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="#drawable/choose_pic"
android:tag="PIC1" />
As you are using ImageButton you need to set your image in property android:src="" not as background of your ImageButton.
So set your image in src property and remove background as below:
<ImageView
android:id="#+id/button_ChoosePicFIrst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="#drawable/choose_pic"
android:tag="PIC1" />
EDITED:
Difference between android:background" and android:src"
android:background exists for all the view. As the name suggests this is what is going to be there in the background.
android:src exists for ImageViews and its subclasses. You can think of this as the foreground. Because ImageView is a subclass of View you even have android:background for that.
If you set an image to be the background of your ImageView, then the image will scale to whatever size the ImageView is. Other than that, src is a foreground image and background is a background image.
The src to an ImageView has additional features:
different scaling types
adjustViewBounds for setting bounds to match image dimensions
some transformations such as alpha-setting
And more that you may find in the docs.

ImageView re-size while setting Image after Cropping

I follow this http://www.androidhub4you.com/2012/07/how-to-crop-image-from-camera-and.html to crop image.
I have 4-5 ImageView in a collage frame.In All Imageview i have set a default image.On click on particular imageview, I am selecting Image from gallery then crop the Image. Now I have to set the cropped Image in my ImageView. So after setting the cropped Image my ImageView get re-size, and because of it other ImageView's size also differ. I want while i am setting the cropped image in Imageview my ImageView must not re-size.
The size of resulted cropped Image may different so I took the ImageView Size before cropping, And after cropping I am scaling result bitmap as the same size of my Image view.
Coding sample is given blow.
I am getting height and width of my Imageview like this
viewhight = tempView.getMeasuredHeight();
viewWidth = tempView.getMeasuredWidth();
After cropping Image I scaled Image according to height and width of my view like this
Bitmap photobitmap1 = Bitmap.createScaledBitmap(photobitmap, viewWidth,viewhight, true);
After this i changed it into drawable like this and set it on my Imageview.
Drawable drawble = new BitmapDrawable(getResources(),photobitmap1);
tempView.setBackground(drawble);
but on setting the hight and width of my view get resized and because of this near Image view also get resized why I don't know.because i converted my imageview in the same size of view.
I also tried this to scale bitmap :
Bitmap photobitmap1 = Bitmap.createScaledBitmap(photobitmap, (viewWidth-30), (viewhight-30), true);
and this to set bitmap :
tempView.setImageBitmap(photobitmap1);
tempView.setBackgroundColor(Color.TRANSPARENT);
but nothing worked.
my xml code is like this:
<LinearLayout
android:id="#+id/fst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/frame"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingLeft="85dp"
android:paddingRight="85dp"
android:paddingTop="10dp"
>
<ImageView
android:id="#+id/viw_1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="2.5dp"
android:layout_weight="1"
android:background="#drawable/rec"
/>
<ImageView
android:id="#+id/viw_2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="2.5dp"
android:background="#drawable/rec"
/>
</LinearLayout>
can any one help me please .....
Are you setting a fixed height and width for the imageview in the XML ?
Example:
<ImageView
android:layoutWidth="60dp"
android:layoutHeight="60dp" />

Categories

Resources