Background fit LinearLayout - android

I want the background picture not to be deformed, but everytime I try, ImageView appears in front of other UI elements.
I already tried with an image view android:src and scaleType=center,
and when I try with another layout, then I can't make the picture fit.

try settings scaleType=matrix, if that didnt help either then there is issues with intrinsic padding , i will post customImageview Then.
cheers!

Try this:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:contentDescription="#string/xxxx"
android:src="#drawable/xxxx" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/xxxx"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/today"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="xxx"
android:textSize="xxsp" />
</RelativeLayout>
</LinearLayout>

Related

Trying to draw an arbitrary background image

I've already seen many tutorials trying to put an image as background of my main activity. Tested many ways, and now I'm with this:
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:src="#drawable/library"
android:scaleType = "centerCrop"
android:contentDescription="#string/bckgrndimg" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical"
>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="370dp"
android:layout_marginLeft="0dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true">
</ListView>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/nuevo_libro"
android:id="#+id/nuevoLibro"
android:layout_above="#android:id/list"
android:layout_centerHorizontal="true"
android:layout_marginBottom="42dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/biblioteca_virtual"
android:id="#+id/textView2"
android:layout_above="#+id/nuevoLibro"
android:layout_centerHorizontal="true"
android:layout_marginBottom="31dp" />
</RelativeLayout>
</merge>
Ok, this should go ok, shouldn't it? My "library" image is a 900x600 PNG image, but it just doesn't show up on the screen.
Am I missing something?
Thank you!
Do you need an Image View?
You could do something like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/library.."
...
>
...
</RelativeLayout>
Change Background color of RelativeLayout
android:background="#ffffff"
To
android:background="#android:color/transparent"

ImageView cannot be centered in a RelativeLayout

I've already tried adding layout_gravity, and gravity, and centerInParent.. but the image view still shows up at the left upper corner of the screen..Am I missing something?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="#+id/noHistroySign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:gravity="center"
android:background="#drawable/sign"
/>
....
I have tested your layout. Your ImageView is already in center. Its correct.
I think there is a problem in android:background="#drawable/sign" perhaps sign image is not displaying correctly or not found or not in drawable folder.
Try to change different image and other image will be in center perfectly.
Put this code in your imageview
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
In RelativeLayout using below property we centered ImageView or other child view.Just add below line in ImageView property.
android:layout_centerVertical="true"
remove
android:layout_gravity="center"
android:gravity="center"
and write
android:src="#drawable/sign"
inplace of
android:background="#drawable/sign"
Try this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/sign" />
</RelativeLayout>
Or try this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sign" />
</RelativeLayout>

Android place an image view between two views

I need to create a layout as shown in the image.
The rounded button with the arrow needs to be exactly between the blue and the gray background.
I'm having difficulties placing it without specifying the margins precisely, which is something I don't want to do because there is no guarantee it will look good on all resolutions and devices.
I would appreciate an xml sample for that
Thanks!
Use the desired drawable.. hope it works.. you can set width and height according to your need.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#1b96d9"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#e6e6e6"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
<ImageView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/arrow" />
</FrameLayout>
</LinearLayout>

Android how to overlap an ImageView on top of a LienearLayouy so that they have the same size?

Hi and thanks for any suggestion.
I need to put an ImageView to overlapp over a LinearLayout.
I have tried this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/background"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="#+id/backgroundimage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/gradient_box"
android:layout_gravity="center"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical"
android:layout_gravity="center">
But this is the result I get (the ImageView shrinks to a single line)
I have tried using RelativeLayout, this is the result I get.
CLOSE, but the ImageView now expands to fill all the available space and does not "wrap content" as I need to.
The desired result would be like this:
PS: I cannot use android:background="image" because i need to change it at runtime.
You can use a Relativ Layout that contains the LinearLayout and then align the ImageView with the LinearLayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/background"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/linear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical"
android:layout_gravity="center">
<ImageView
android:id="#+id/backgroundimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/gradient_box"
android:layout_alignLeft="#id/linear"
android:layout_alignRight="#id/linear"
android:layout_alignTop="#id/linear"
android:layout_alignBottom="#id/linear"
android:layout_gravity="center"
android:scaleType="fitCenter" />
If you just want to set the image as a background you can just set a background image for the linear layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/background"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/blue" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical"
android:background="#drawable/gradient_box"
android:layout_gravity="center">
PS: I cannot use android:background="image" because i need to change it at runtime.
Why not? Use setBackgroundResource.

Android layout (TextView)

I'm new to android development and have some doubts with regards to layout design. I have the following codes but its not working as i wanted. I would like to have the image view display a photo i have taken and below it, a textbox for user to input data. However, at this moment, the image takes up the whole screen and I could not locate my textbox. Please kindly advice. Thanks!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/linear">
<ImageView android:id="#+id/imageView1"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:contentDescription="#string/desc"/>
<EditText
android:id="#+id/editTextSimple1"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<requestFocus />
</EditText>
</LinearLayout>
Use RelativeLayout instead of LinearLayout and USe below xml code instead of your xml code, it will solve your problem.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/editTextSimple1"
android:contentDescription="desc" />
<EditText
android:id="#+id/editTextSimple1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<requestFocus />
</EditText>
</RelativeLayout>
Change ImageView layout height to layout_height="wrap_content"
With layout_height="fill_parent", the image will take all the space.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/linear">
<ImageView android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/desc"/>
<EditText
android:id="#+id/editTextSimple1"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<requestFocus />
</EditText>
</LinearLayout>
The problem is
android:layout_height ="fill _parent"
This makes it occupy the whole parent space I.e. The whole screen
Either set the height to a specific value of dp,px etc. Or use
android:layout_height="wrap_content"
If view is small enough to fit on screen
<ImageView android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="#string/desc"/>
Add weight to your ImageView like this. It will work.

Categories

Resources