How to adjust Layout backgroud image to centerInside? - android

When assigning an image as a background to a Layout(Linear || Relative) it stretched all over the Layout, but I want to make that background image to be centered inside the Layout as in the ImageButtons :
android:scaleType="centerInside"
Is there's a way to achieve that in Linear or Relative Layouts?

You should try the....
1.) take the RelativeLayout propery matchParent both. and centerInparent=true
2.) take one ImageView inside it Property WrapContent.
now take the background or src of inage view

you can use another layout LayoutB inside the LayoutA. and set the width and height to wrap_content of LayoutB..and set the image to background of LayoutB..and also set the arributs like this:
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"

This will help you
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/someImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/someImageSrc" />
</RelativeLayout>

Related

Image size - how to?

I just want to make a simple image view but when I run that app the unnecessary white background comes. I don't want to need that part. any solution.
and here is my xml file for that image view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:src="#drawable/pot1"/>
</LinearLayout>
In code use -
imgview.setScaleType(ScaleType.FIT_XY);
Or in xml Image view -
android:scaleType="fitXY"
remove the gravity attribute if you want to make it stick to the top of screen or add scaleType attribute fitCenter, cropCenter or fitXY if you want to make it fill the screen.

ImageView With Button inside

I want to do the imageView layout with the button to edit photo inside.
I think to put image as imageView background but i donĀ“t know how to do the semicircle transparency and set the icon.
Any ideas?
UPDATE
Now i have this.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="150dp"
android:layout_height="150dp"
android:background="#color/blue_bar">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/image"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#color/black"/>
<ImageButton
android:layout_alignStart="#+id/image"
android:layout_alignEnd="#+id/image"
android:src="#drawable/ic_edit_photo"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#B3ffffff"/>
</RelativeLayout>
Can i do the imageButtons background as a semicircular shape?
For that you have to make a CompoundView.
1) I would make a RelativeLayout with the imageview width and height set to match_parent, then a button with alignBottom set to true.
2) Then keep using your button normally in your Activity.
3) You can go fancier and use a CompoundView extending ViewGroup and putting the functionality of the button.
Here is a tutorial for it:
http://www.vogella.com/tutorials/AndroidCustomViews/article.html
I hope it helps.

How to make image fill RelativeLayout background without stretching

In my Android project, I am not quite sure how to make my background image fill the entirety of the RelativeLayout root element in XML, which is the size of the screen. I want to be sure that this works for all aspect ratios, so the image will clip vertically or horizontally as necessary. Does someone know how to do this easily? I've only seen questions regarding ImageViews and Buttons, but not really generic Views.
My XML file currently:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/enclosing_rl"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background"
android:fitsSystemWindows="false">
<!-- Other elements -->
</RelativeLayout>
Other than turning your image into a nine patch I don't think this is possible. What you could do instead is-
Add an ImageView as the first view in your RelativeLayout.
Set layout_centerInParent to true.
Have the layout_width and layout_height set to match_parent.
Then set scaleType to centerCrop.
That will make sure the image fills the screen without any distortion, but depending on screen size/orientation either some of the top/bottom or left/right of the image may be cut off.
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:scaleType="centerCrop"
android:src="#drawable/background" />
Any other views in the RelativeLayout will appear on top of the ImageView, as long as it is the first view in the RelativeLayout (when you are in the xml).
Create a bitmap drawable XML resource in your res/drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/background"
android:tileMode="repeat" />
Use that drawable as background instead of #drawable/background
according to this answer If you want your ImageView fill your RelativeLayout,use align parameters for ImageView.
you can put all of your views to a LinearLayout and set align parameter to your background ImageView:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/my_background"
android:scaleType="centerCrop"
android:layout_alignTop="#+id/my_views"
android:layout_alignBottom="#+id/my_views"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/my_views"
android:orientation="vertical" >
<!-- stuff -->
</LinearLayout>
</RelativeLayout>
Its smart and 100% working answer is to set the property scaleType of image view !
android:scaleType="fitCenter"

how can i give padding to images which are inside linearlayout without increasing linearlayout width in android

I am designing this layout in my android xml file
I am putting 2 linear layout 1 for the header and other for the body. so far all good. In the header liner layout i added the background image which comes perfectly fine.
Now when i am adding the right most image button for search and information, i want them to float through the header linear layout exactly as shown above. But the problem is if i put them into header linerlayout the width of the liner layout increases and both of them comes under the header layout. Is there anyway i can achieve this?
Thanks
try this combination:
<AbsoluteLayout
android:id="#+id/widget68"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="40px"
android:layout_y="35px"
>
<ImageView
android:src = "#drawable/androidlogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="124px"
android:layout_height="wrap_content"
android:text="Print Picture"
/>
</FrameLayout>
</AbsoluteLayout>
to get this:
You may also move your picture (button in this case) by padding it

How to setup background not to be stretched?

Please advice how to place an image as background for LinearLayout so this image would look as is, not stretched, titled or something.
Other solution is to define your image in a XML and refer to this XML on your background as explained here : Background Image Placement
Like that you can control your image (background) layout
Jojo's solution might work if the content of Linear layout is smaller than image height.
if i add two or edit text automatically the image starts stretching. if u still want to avoid and make the imageview at background use the below xml layout.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton android:id="#+id/imageButton1" android:src="#drawable/icon" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#null"></ImageButton>
<Button android:text="Button" android:id="#+id/button1" android:layout_width="wrap_content" android:layout_height="100px"></Button>
</RelativeLayout>
Because only image button has the src options that remains constant to width and height of image.
make android height and width of LinearLayout into wrap_content using this code in .xml file
android:layout_height="wrap_content"
android:layout_width="wrap_content"
to set background to the image use code
android:background="#drawable/image"
Now place the image in the drawable file

Categories

Resources