How to Make Image Fill Relativelayout Background Without Stretching

How to make image fill RelativeLayout background without stretching

Other than turning your image into a nine patch I don't think this is possible. What you could do instead is-

  1. Add an ImageView as the first view in your RelativeLayout.
  2. Set layout_centerInParent to true.
  3. Have the layout_width and layout_height set to match_parent.
  4. 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).

How to fill image in background without stretching?

Place your image file in Res/drawable instead of Res/mipmap.

Res/mipmap is for app launcher icons only

Try this:

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/view"
android:src="@drawable/landscape"
android:scaleType="center" />

RelativeLayout background stretches when inner view aligned to parent right

you are setting the background to relative layout... so, whole layout will have same background no matter what views it holds. Try setting "android:background="@drawable/right_bubble" to TextView instead.

EDIT (by OP after getting answer from comments):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/convoLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right" // added
android:background="@drawable/right_bubble" >

<TextView
android:id="@+id/convoBody"
android:layout_width="wrap_content"
android:layout_height="wrap_content" // removed layout_alignParentRight
android:padding="5dp"
android:text="test test test test"
android:textAppearance="?android:attr/textAppearanceMedium" />


</RelativeLayout>

My Relative layout background not stretching full screen

Try with this :

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CACACA">


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

......

Android RelativeLayout stretch background

I got it,
when add new image asset it resize the image and crop to with white borders.
Need to hard copy-paste the entire photo to the drawable folder.

Setting a background image to a view stretches my view

According to me, the problem you are facing is not a problem, it is the way how Android is used to design the layouts.

This means that you can set the height and width with 3 default constant values:

FILL_PARENT

Special value for the height or width requested by a View. FILL_PARENT means that the View wants to be as big as its parent, minus the parent's padding if any. This value is deprecated starting in API Level 8 and replaced by MATCH_PARENT.

MATCH_PARENT

Special value for the height or width requested by a View. MATCH_PARENT means that the view wants to be as big as its parent, minus the parent's padding if any. Introduced in API Level 8.

WRAP_CONTENT

Special value for the height or width requested by a View. WRAP_CONTENT means that the View wants to be just large enough to fit its own internal content, taking its own padding into account.

Now, when you are setting the View's height/width to WRAP_CONTENT, you are allowing the view to take that much size that is sufficient to show to view's content. The background image is also the View's content, hence you view will be shown of as much size as the image. That's not a problem, that's how it's shown.

Okay, but in your situation that's an issue for you because you have a background to show and view should not be stretched for that. I can suggest few ways:

  1. First and very obvious: make correctly sized images and keep them in different drawable folders.

  2. Specify the size of view not using constants, but in DP. If it becomes necessary, make different layout XML files for different sizes and keep them in layout folders.

  3. You can use a very useful thing for design layout is layout weight.

How to implement an android:background that doesn't stretch?

You should use ImageView if you don't want it to stretch.
Background images will always stretch to fit the view.
You need to set it as a Drawable to force the image aspect to the object.

Otherwise, if you are sticking with the Button idea, then you will need to force the scaling in the button to prevent the image from stretching.

Code:

onCreate(Bundle bundle) {
// Set content layout, etc up here

// Now adjust button sizes
Button b = (Button) findViewById(R.id.somebutton);
int someDimension = 50; //50pixels
b.setWidth(someDimension);
b.setHeight(someDimension);
}


Related Topics



Leave a reply



Submit