I'm having trouble displaying my image within my LinearLayout. I'm trying to get a background color of white and have an ImageView displayed in my LinearLayout and the image and the background color works find, but the image is stretched.
What's a good way to proportion the image show that it displays normal? Any ideas?
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FFFFFF">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ImageView
android:src="#drawable/day1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
</LinearLayout>
</ScrollView>
Related
This is my current layout:
I know it is black but my phone was facing down. It is supposed to be a camera preview. Anyway, whatever renders on the small one renders on the big one.
The small camera preview, takes half of the screen's height and 1/4 of screen's width (landscape mode). Now proportionally it should fill the rest of the screen ( the second half) but it doesn't. You can see the problem is that it is not using all the height, there are 2 green lines.
Here is my code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
tools:context=".MainPreviewActivity">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1" >
<View
android:id="#+id/center_dummy_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerInParent="true"/>
<FrameLayout
android:id="#+id/clean_preview_fragment_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignBottom="#id/center_dummy_view"
android:background="#color/LightGrey"/>
<android.support.v7.widget.RecyclerView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/listOptionsRecyclerView"
android:layout_alignTop="#id/center_dummy_view"
android:background="#color/LightGrey"/>
</RelativeLayout>
<FrameLayout
android:id="#+id/processed_preview_fragment_container"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:background="#color/WhiteSmoke"/>
</LinearLayout>
You can see the FrameLayout, #+id/processed_preview_fragment_container has a fill_parent layout_height value.
The fragments view is:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.apps.foo.bar.fragments.ProcessedPreviewFragment">
<ImageView
android:id="#+id/processed_preview_img_view"
android:background="#color/LimeGreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
try using below code as fragment view since imageview default property to draw image at center so it doesn't scale image according to view height and width. so you need to specify scale type for image. i have used "fitXY" in this image will cover full height and width but image may be stretch so you can change it according to your requirement
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.apps.alexs7.pointop.fragments.ProcessedPreviewFragment">
<ImageView
android:id="#+id/processed_preview_img_view"
android:background="#color/LimeGreen"
android:layout_width="fill_parent"
android:scaleType="fitXY"
android:layout_height="fill_parent" />
I kept my image size as 320*480 but it shows blank space in vertical sides,Please give some advice.Here is my xml code please suggest me..
<?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">
<ImageView
android:id="#+id/ImageView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAlignBottom="true"
android:src="#drawable/splash" />
</LinearLayout>
add attribute android:scaleType="fitXY" to imageview
I am attempting to add a tiled image as a background of the program.
The code that I am currently using in main.xml, which is crashing:
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/cartoonclouds"
android:contentDescription="#string/desc" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
</LinearLayout>
</ImageView>
However, when the ImageView is removed and the xmlns is moved to the LinearLayout, it functions fine (without, of course, the image).
I can't see any related errors in LogCat.
More information:
Using Eclipse and Android 2.2, API 8. The program runs but crashes instantly.
You can not put anything in an ImageView. It's not a layout. Use the background attribute of the LinearLayout to set the background to your image.
Another option would be to wrap everything in a RelativeLayout. Separate the ImageView and the LinearLayout. The image will fill the RelativeLayout and the LinearLayout will be on top of it.
Example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/cartoonclouds"
android:contentDescription="#string/desc" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
</LinearLayout>
</RelativeLayout>
You should not add a linear layout inside an imageview. Have a linearlayout at the top as root and then add the image view in there
I made a linear layout with a background image, a png... I don't know how to show it into the layout ( and centered ) keeping proportions... here is the code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background">
Obviously image has the same height and width of the display... Any help?? Thanks in advance
=.4.S.=
Thank you all ^^ I solved in this way:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/background"
android:layout_gravity="center"/>
<ListView android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#FFFFFF"
android:layout_weight="2"
android:drawSelectorOnTop="false"
android:layout_gravity="center"/>
</FrameLayout>
Try this related question/answer:
Android: Scale a Drawable or background image?
I haven't tried it, but it sounds similar to what you're talking about.
If you do not want your picture to be stretched, you can use BitmapDrawable with gravity set us instead of png.
Refer docs: Bitmap
Ive specified white backgrounds for pretty much all widgets and it works, except when scrolling. The background container goes black during scrolling resulting in annoying flickering.
You will need to use the setCacheColorHint() method of ListView.
Example: list.setCacheColorHint(yourBackColor);
Explanation of the issue and solution here
ScrollingCache="false" in your activity's *.xml file for the list object should do the trick, too.
I fixed this problem by making the background color of the cells the same color as the ListView, so:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FFFFFF">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"> <---- Background color of ListView
</ListView>
</LinearLayout>
and then the row:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dip"
android:background="#FFFFFF"> <--- Background color of the cell
<LinearLayout
android:id="#+id/projects_cover_row_image_layout"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:background="#444444"
android:layout_centerInParent="true"
android:layout_marginRight="8dip">
<ImageView
android:id="#+id/projects_cover_row_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:contentDescription="#string/projects_image_content"/>
</LinearLayout>
...
</RelativeLayout>
that completely got rid of the problem for me and seems like the best solution
Just put background on the top and no highlight or other bad effects simple and no need of hint or cache
in listview layout
android:background="#000000" for black background or android:background="#FFFFFF" for white
<?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:background="#000000"
android:orientation="vertical" >
<TextView
android:id="#+id/testscheck"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:layout_weight="1"
android:layout_gravity="left|center"
android:gravity="left"
android:textColor="#color/ics"
android:textSize="14sp"
/>
</LinearLayout>