How to display image in center across resolutions - android

I have a layout in which one of the inside doesn't come in center, It happens only in few resolution devices, whereas in few it comes perfectly in center. Below is code and screenshot
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:gravity="center"
android:src="#drawable/thank" android:paddingTop="10dip"
android:paddingBottom="10dip"/>
Code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/splash_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:gravity="center" android:orientation="vertical"
android:background="#drawable/background">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:gravity="center_horizontal"
android:src="#drawable/logo" android:paddingTop="10dip"
android:paddingBottom="10dip" />
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:gravity="center"
android:src="#drawable/thank" android:paddingTop="10dip"
android:paddingBottom="10dip"/>
<com.teleca.sam.ui.Copyright
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:gravity="bottom|center"/>
</RelativeLayout>
</LinearLayout>

If you are using a LinearLayout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_gravity="center_horizontal"
android:src="#drawable/ic_launcher" />
</LinearLayout>
If you are using a RelativeLayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:src="#drawable/ic_launcher"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>

because your ImageView is in a RelativeLayout, you should use
android:layout_centerInParent="true"
instead of
android:gravity="center"
in your ImageView tag

RelativeLayout may be the reason for this. Try Linear Layout instead.

Related

How to align image and text exactly equal when using in gridview android?

Hi I am using Gridview to display images with text below by using custom adapter. But problem is when I fetch image and display, align gets changing and it's not proper.Here is my code:
<?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">
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:numColumns="3" />
</RelativeLayout>
gridview.row:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical" >
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/brand_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"/>
<TextView
android:id="#+id/brand_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/brand_picture"
android:gravity="center_vertical"
android:text="TextView"
android:background="#color/blue"
android:textColor="#FFFFFF" />
How to achieve the expected output with same image size and text below that.
Expected output is:
But my output looks like this.
Thanks.
try this..hope it works for u..
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/brand_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/brand_name"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/brand_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/brand_picture"
android:background="#color/blue"
android:gravity="center_vertical"
android:text="TextView"
android:textColor="#ffffff" />
On setting Wrap content the widget adjusts its height/width parameters to the given content size.so
change width of image view and text view from wrap content and set same width for both image view and text view.
eg:android:layout_width="70dp"
To get text in cente of text view you have to set
android:gravity="center"
xml:ROW
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical" >
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#333333">
<ImageView
android:id="#+id/brand_picture"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:background="#499999"
android:src="#drawable/ic_launcher"/>
<TextView
android:id="#+id/brand_name"
android:layout_width="90dp"
android:layout_below="#+id/brand_picture"
android:gravity="center"
android:text="AQUA LUNG"
android:background="#445657"
android:textColor="#FFFFFF" />
Add property,
android:layout_centerHorizontal="true"
to your ImageView as well as TextView. It will work.
You can try this layout:
Actually i have replaced your relative layout with linear layout, but it works just like what you want.
<LinearLayout
android:id="#+id/RelativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/brand_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:layout_gravity="center" />
<TextView
android:id="#+id/brand_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/blue"
android:text="TextView"
android:textColor="#FFFFFF" />
</LinearLayout>
Hope this resolves your issue....
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000">
<ImageView
android:id="#+id/brand_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"/>
<TextView
android:id="#+id/brand_name"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/brand_picture"
android:layout_alignRight="#+id/brand_picture"
android:layout_below="#+id/brand_picture"
android:background="#445657"
android:gravity="center"
android:text="WHATEVER"
android:textColor="#FFFFFF" />
</RelativeLayout>
Or You can try
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical" >
<LinearLayout
android:id="#+id/RelativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/brand_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:layout_gravity="center" />
<TextView
android:id="#+id/brand_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:text="TextView"
android:textColor="#FFFFFF" />
</LinearLayout>
</LinearLayout>
Hopw this helps you

Bottom align a textview with an ImageView in a GridView

So far this is the result of my layout
I am trying to put the textview at the bottom of my ImageView (not below it). The textview should BottomAlign with the ImageView.
The image of my ImageView is a square. If the content of the textview turns out to be longer that the width of my ImageView, the content should continues to the next line.
At the moment i don't care if the content of the ImageView does not scaled proportionally (i.e. gets stretched, etc).
This is the XML that i have:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<ImageView
android:id="#+id/iconItem"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignBottom="#+id/textItem"
android:scaleType="fitXY"
android:padding="0dip"
android:src="#drawable/shout_ic"/>
<TextView
android:id="#id/textItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="11sp"
android:text="Motorcycles"
android:textColor="#FFF"
android:background="#8888"/>
</RelativeLayout>
How can i achieve the result i want?
EDIT 1:
I think i am making a progress... (but not exactly like what i have in mind
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<ImageView
android:id="#+id/iconItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:padding="0dip"
android:src="#drawable/thumbqoo_launcher_ic"/>
<TextView
android:id="#+id/textItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignBottom="#+id/iconItem"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="11sp"
android:text="Motorcycles"
android:textColor="#FFF"
android:background="#8888"/>
</RelativeLayout>
Change your parent layout to FrameLayout with orientation vertical like this
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="#+id/iconItem"
android:layout_width="80dp"
android:layout_height="80dp"
android:padding="0dip"
android:scaleType="fitXY"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#8888"
android:gravity="center"
android:layout_gravity="bottom"
android:text="Motorcycles"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#FFF"
android:textSize="11sp" />
</FrameLayout>
remove this line android:layout_alignBottom="#+id/textItem" from your imageview and try adding line android:layout_alignBottom="#+id/iconItem"in your TextView
set width of ImageView to width of your row layout
http://i.stack.imgur.com/HUY4o.png Check the sample image...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/album_item"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/cover"
android:layout_width="148dp"
android:layout_height="148dp"
android:src="#drawable/empty_photo" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/cover"
android:background="#70000000"
android:padding="6dp" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="12sp"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="#+id/iconItem"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignBottom="#+id/textItem"
android:padding="0dip"
android:scaleType="fitXY"
android:src="#drawable/shout_ic" />
<TextView
android:id="#id/textItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#8888"
android:gravity="center"
android:text="Motorcycles"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#FFF"
android:textSize="11sp" android:layout_alignBottom="#id/iconItem"/>
</RelativeLayout>

Android textview on imageview

I am trying to place an TextView on a ImageView in the bottom center of the RelativeLayout.
I am able to place the TextViewin the center but not able to place it in the bottom .
Can anyone guide me.
here is my XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/back"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/eventimage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:background="#000000">
</ImageView>
<TextView
android:id="#+id/text98"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerVertical="true"
android:singleLine="true"
android:textColor="#color/white"
android:text="LAST"
android:textStyle="bold"
android:textSize="18sp"/>
</RelativeLayout>
on the textView use the following attributes android:layout_centerHorizontal="true" instead of android:layout_centerVertical="true" and add android:alignParentBottom=true". Also look at the relative layout options to understand how to work with it.
try
<TextView
android:layout_alignParentBottom="true"
...
You could try using something like
android:layout_alignBaseline (#id/eventimage);
and add some padding on the bottom later
You can use this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/back"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/eventimage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:background="#000000">
</ImageView>
<TextView
android:id="#+id/text98"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:singleLine="true"
android:textColor="#color/white"
android:text="LAST"
android:layout_alignParentBottom="true"
android:textStyle="bold"
android:textSize="18sp"/>
</RelativeLayout>
Add this to your TextView:
android:layout_alignBottom="#id/eventimage"
Use this code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/back"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/eventimage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:background="#000000">
</ImageView>
<TextView
android:id="#+id/text98"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentBottom="true"
android:alignBottom="#+id/eventimage"
android:singleLine="true"
android:textColor="#color/white"
android:text="LAST"
android:textStyle="bold"
android:textSize="18sp"/>
</RelativeLayout>
Hope it will help you.
Try the following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/back"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/eventimage"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:background="#000000">
</ImageView>
<TextView
android:id="#+id/text98"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:singleLine="true"
android:textColor="#color/white"
android:text="LAST"
android:textStyle="bold"
android:textSize="18sp"/>
</RelativeLayout>
This layout works according to your requirement .by setting the gravity bottom of the linear layout ,its chilld view is automatically aligned at center- bottom position
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/back"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/ic_launcher"
android:gravity="bottom">
<TextView
android:id="#+id/text98"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center"
android:singleLine="true"
android:text="LAST"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#android:color/white"/
>
</LinearLayout>
android:layout_alignParentBottom to move the control to the bottom of the screen.

View is overlapped by another view with match_parent and wrap_content on Android

I want to create a view with two parts:
1. Avatar, which is always displayed at right/center_vertial on screen.
2. Name, which is always displayed at left on screen (do not overlap on Avatar).
<?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="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/username" />
</LinearLayout>
<LinearLayout
android:id="#id/avatar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="right|center_vertical"
android:orientation="vertical" >
<ImageView
android:id="#+id/avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:scaleType="center"
android:src="#drawable/useravatar" />
</LinearLayout>
</LinearLayout>
The problem is when Name is too long, it overlaps on Avatar. How can I fix this problem?
You should use RelativeLayout and a android:layout_toLeftOf="#id/avatar" attribute for the TextView.
You're better off using a Relative Layout for this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/avatar"
android:text="Some User Name" />
<ImageView
android:id="#+id/avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_gravity="right|center_vertical"
android:scaleType="center"
android:src="#drawable/thumbr" />
</RelativeLayout>

Problem with Android Layout

I'm having a very hard time getting the 2 buttons in the 2nd child linearlayout to display as I want them to... I want them centered, on the same row but they appear left-aligned and on the same row. Can anyone help me with this?
<?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:background="#color/black"
android:orientation="vertical">
<LinearLayout
android:id="#+id/splash"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageLogo1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:cropToPadding="true"
android:scaleType="fitXY"
android:src="#drawable/isi_logo"
android:paddingLeft="50sp"
android:paddingRight="50sp"
android:paddingTop="20sp"/>
<ImageView
android:id="#+id/imageLogo2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:cropToPadding="true"
android:scaleType="fitXY"
android:src="#drawable/sa_logo"
android:paddingLeft="100sp"
android:paddingRight="100sp"
android:paddingBottom="20sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<Button
android:id="#+id/buttonScores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="100sp"
android:text="#string/scores"/>
<Button
android:id="#+id/buttonStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="100sp"
android:text="#string/test"/>
</LinearLayout>
</LinearLayout>
Try using android:gravity="center" instead of android:layout_gravity="center"

Categories

Resources