I have a simple imageView and I want an image to be displayed in the center of the bottom. At the moment it is displayed in the bottom right corner.
<ImageView
app:srcCompat="#android:drawable/simple_picture"
android:id="#+id/picture"
android:layout_centerHorizontal="true"
android:layout_height="300dp"
android:layout_width="330dp"
android:scaleType="fitEnd"/>
is there any way to use scaleType for bottom and center?
Thank you!
Add parent layout as Relative
<ImageView
android:id="#+id/picture"
android:layout_width="250dp"
android:layout_height="350dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="bottom"
app:srcCompat="#drawable/oldman1" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="300dp"
android:layout_height="300dp"
android:gravity="bottom|center">
<ImageView
android:id="#+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
app:srcCompat="#drawable/ic_chat" />
</LinearLayout>
Add another view as parent
You can take RelativeLayout as a parent layout and set in ImageView
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
try this ...
Look at answer for similar question. I think, using android:scaleType="matrix" can resolve problem.
If parent is linear layout then try :
android:layout_gravity="center"
If parent is relative layout then use :
android:layout_centerHorizontal="true"
I know from this question 5 years ago
But I wanted to help others
You do [[ NOT NEED TO USE scaleType ]] my friend
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom|center">
<ImageView
android:id="#+id/myimageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:src="#drawable/MyImage" />
</RelativeLayout>
Related
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>
i want to implement Image Gallery that load from url and put it into horizontal scroll view . i want to set my image view into match_parent but it doesn't work .. how to get a view like this ??
And this is my xml and view .
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_margin="12dp"
android:id="#+id/slideshow">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.android.volley.toolbox.NetworkImageView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/slide1"
android:scaleType="fitXY"
android:src="#drawable/ic_launcher"/>
<com.android.volley.toolbox.NetworkImageView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/slide2"
android:scaleType="centerInside"
android:src="#drawable/ic_launcher"/>
<com.android.volley.toolbox.NetworkImageView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/slide3"
android:scaleType="centerCrop"
android:src="#drawable/ic_launcher"/>
</LinearLayout>
</HorizontalScrollView>
Use RelativeLayout instead of LinearLayout and align the elements using rightOf
After some investigation I made to solve similar problem for myself, I came up to using ViewPager. I think, that is what you wanted to do.
I have a background image in full screen with a RelativeLayout. And other image (100x100) in an ImageView.
I want to put the ImageView in center screen. But my image shows top left.
What is the problem?
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/fondo_inicio" >
<ImageView android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:gravity="center"
android:background="#drawable/icono_inicio"/>
</RelativeLayout>
try this to your ImageView
android:layout_centerInParent="true"
Add the following to the ImageView.
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/fondo_inicio" >
<ImageView android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerInParent="true"
android:background="#drawable/icono_inicio"/>
</RelativeLayout>
<ImageView
android:id="#+id/pic_id"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter"
android:src="#drawable/pic" />
Try this
android:layout_gravity="center"
User layout_gravity and gravity doesn't work in case of RelativeLayout so just use
Layout_centerInParent = "true"
I am using this layout to make image and text in center.but its not working .i am simply using android:layout_centerHorizontal="true".why its not working i dont understand. can someone please help to solve this problem?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:background="#drawable/image_tutorial1" />
<TextView
android:textSize="14dp"
android:id="#+id/title"
android:layout_below="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/info4"
android:textColor="#000000"
android:textStyle="bold" />
</RelativeLayout>
In the layout, RelativeLayout's width is set to wrap-content, its not taking the entire available width at all. It's contents are being centered but their parent View is shrinking to fit around them.
try fill_parent for root:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
Also, a LinearLayout with orientation="vertical" and gravity="center_horizontal" will do the same, more easily.
With RelativeLayout use:
android:layout_centerInParent="true"
with
android:layout_width="wrap_content"
use [android:layout_centerHorizontal="true"] under RelativeLayout layout, then it will work
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
you using this wrong for your RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
it should be
android:layout_height="match_parent"
android:layout_width="match_parent"
I have a relative layout, and inside it i place 3 items, 2 imageviews and one scrollview.
My layout is like 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:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal"
android:baselineAligned="true"
android:background="#drawable/background">
<RelativeLayout
android:id="#+id/logosLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_margin="16dp"
android:gravity="center">
<!-- An image will be placed here -->
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:background="#33ffffff"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/up" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="8dp"
android:fadingEdgeLength="32dp"
android:scrollbars="none" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<ImageButton
android:id="#+id/hotelBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#drawable/button_background"
android:contentDescription="#string/hotelBtnDesc"
android:gravity="center"
android:src="#drawable/icon1" />
<TextView
android:id="#+id/hotelBtnDescTxtVw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="#string/hotelBtnDesc"
android:textColor="#ffffff"
android:textSize="14sp" />
<!-- more scrollview items -->
</LinearLayout>
</ScrollView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="#drawable/down" />
</RelativeLayout>
</LinearLayout>
The above code produces the view shown here:
You may note that the arrows are not aligned in the center, but are slightly moved to the right. Why is that, and how can I fix it ? Note that i have already used android:layout_centerHorizontal="true" to my imageviews.
Thank you in advance
Its problem with your
android:layout_margin="8dp"
remove it from both scrollview and imageView
and pass it to RelativeLayout direct.
or add
android:layout_marginTop="8dip"
android:layout_marginBottom="8dip"
in your RelativeLayout.
and pass
android:layout_centerHorizontal="true"
to your scrollview
they are aligned to the center if you put the marginRight into consideration, try adding android:layout_margin="8dp" to the arrows.
u need to set image view that is under the Relative layout set this image view width fill parent.
Try removing android:layout_weight="1" from your RelativeLayout and see if it works.
In your LinearLayout, the attribute android:orientation="horizontal" causing your display to be aligned horizontaly. Therefore your RelativeLayout is in center but since its shared with another layout thats why you cant note the difference. If you completely remove the first RelativeLayout (the one with id logosLayout) then i hope you'll see the second layout in center.
So, first you need to define the hierarchy of layout you require and then adjust your views accordingly.
It seems problem with your android:layout_margin="8dp" for your scrollview only and pass it to RelativeLayout direct but instead of using margin use padding = "8dp".