how i place image on center of screen? - android

this is my code which place image on top senter of screen how i place image on center of screen? not top center my screen look like this http://imgur.com/65CD6
i wanna place on center of screen not op center
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/RelativeLayout01"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_gravity="center_vertical|center_horizontal"
android:background="#ffffff"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<ImageView android:id="#+id/ImageView01"
android:src="#drawable/agapplogo"
android:layout_width="wrap_content"
android:layout_marginLeft="60dip"
android:layout_height="wrap_content">
</ImageView>
<TextView android:id="#+id/TextView01"
android:text="#string/welcome"
android:layout_below="#id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</RelativeLayout>

You can try to use: android:layout_centerInParent="true" like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_centerInParent="true"
android:src="#drawable/ic_launcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>

You can either play with android:gravity="center" in <RelativeLayout>
OR
include android:centerInParent="true" inside ImageView

place android:gravity="center" for RelativeLayout

Just give your ImageView in xml this attribute:
android:layout_centerHorizontal="true"

this may happen due to RelativeLayout,try using LinearLayout

Related

Android Relative/LinearLayout always in middle, no matter if horizontal or vertical

This image http://postimg.org/image/h8akuq9gz/ should give you an idea of what I want.
No matter if the device is in vertical or horizontal position, this two "circles" must be always in the mid.
I really don't know how can I achieve this. Can you point me the direction? Thanks!
why not use gravity on root layout, like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<Button
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</LinearLayout>
Use
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
gravity="center">
<--Other contorls you want to keep in center-->
</RelativeLayout>
Can you try this: the example is for image button but you can try it as per your wish
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageButton android:id="#+id/btnFindMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/findme"></ImageButton>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:layout_gravity="center"
>
<ImageButton
android:id="#+id/img_btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/img_1">
</ImageButton>
<ImageButton
android:id="#+id/img_btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/img_2">
</ImageButton>
</RelativeLayout>

Align ImageView to bottom Right of LinearLayout

I'm triying to put an ImageView in bottom right of LinearLayout ....
I wrote the code as bellow :
style.xml:
<?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">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="90dp"
android:background="#drawable/my_background"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:src="#drawable/my_small_image" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
This code results in the ImageView 'my_small_image' at the bottom right, but I need it in the bottom left.
Anyone have any ideas about this?
I hope somebody here can help me.
Best regards and thanks in advance, Fadel.
Using LinearLayout is possible:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:orientation="vertical">
<!-- other elements in linear layout here -->
<!-- making a RelativeLayout un-desirable -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="1"
android:scaleType="fitEnd"
android:src="#drawable/your_image" />
</LinearLayout>
This will position the image at the center-bottom, for bottom-right use
android:layout_gravity="right"
Use RelativeLayout
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="90dp"
android:background="#drawable/my_background"
android:layout_alignParentBottom="true"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/my_small_image" />
</RelativeLayout>
In LinearLayout you can use -
android:layout_gravity="bottom|right"
I would recomend Using RelativeLayout but if you have to useLinearLayout i know that android:scaleType" can help center or move image, did you try android:scaleType="fitEnd" or android:scaleType="fitXY"
for more Try This

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 does not work properly

I'm using file main.xml to design UI in Android. I don't know why the last TextView (id: ImageDescription) doesn't work. If I delete ImageView tag, the last TextView will work again.
Here is my screenshot. the first in when no ImageView tag, and the second when has ImageView
As you see, when has image, I cannot see line Image descriptor: a cartoon tiger.
Here is my main.xml:
<?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" >
<TextView
android:id="#+id/imageTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/test_image_title"/>
<TextView
android:id="#+id/imageDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/test_image_date"/>
<ImageView
android:id="#+id/imageDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/test_contentDescription"
android:src="#drawable/test_image"/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/imageDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/test_image_description"/>
</ScrollView>
</LinearLayout>
thanks for help me :)
You are adding your imageDescription textview below imageview and that too your scrollview has both with and hight as fill_parent, once try this
<TextView
android:id="#+id/imageDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/test_image_date"/>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/imageDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/test_image_description"/>
</ScrollView>
<ImageView
android:id="#+id/imageDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/test_contentDescription"
android:src="#drawable/test_image"/>
I think image is too big, if it's the case try by specifying layout:weight parameter for both image and text view in proper percentage.
Try to change your XML part
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
to
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
because your ScrollView tries to be as big as your LinearLayout in height.
I have another answer for my question, I post here to whom have viewed my post and need more answers. That I use android:adjustViewBounds="true" Property :)

UI-Relative layout

I am trying to create a UI using relative layout where in the UI should look as such:
In the top of the screen i have a TextView
followed by another TextView
followed by scroll view where in i have to display the text in the
scrollable format
in the end i have a imageview.
How can i achieve this.
I have tried using the combination of Relative and Linear Layout but the elements gets overlapped. Anyone have any suggestions on how to proceed?
<?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:background="#drawable/background_main" android:id="#+id/g_description">
<TextView android:id="#+id/titleBar" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#drawable/header"
android:layout_alignParentTop="true" android:gravity="center"
android:text="#string/aboutus" android:textAppearance="? android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TextView android:id="#+id/wd_name"
android:layout_width="fill_parent" android:layout_below="#id/titleBar"
android:layout_height="wrap_content" android:textStyle="bold"
android:textColor="#FFFFFF" android:textSize="18dip"
android:background="#drawable/background_main" android:gravity="center" />
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrolltext" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_below="#id/wd_name"
android:padding="5dip">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/wd_description"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textSize="14dip" android:textColor="#FFFFFF">
</TextView>
</LinearLayout>
</ScrollView>
<ImageView android:id="#+id/bottomBar" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:src="#drawable/bottom_bar"
android:layout_alignParentBottom="true" />
you can achieve it like this:
Take relative layout and put textview in it.
Take another textview and add this to it android:layout_below="id of the 1st textview".
Take scrollview and add this to it android:layout_below="id of the 2nd textview".
Take linear layout inside scrollview.
Put textview inside linear layout.
Take imageview and add this to it android:layout_below="id of the scrollview".
Now your layout is ready and if you get any error then let me know with your code....
If you have a series of elements that you want to flow down the screen, then it sounds like you just need a LinearLayout.
It goes as below. Placing a TextView inside scrollview will make it scrollable. Reset is selfexplanary.
<?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">
<TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="Below TOP TextView" android:id="#+id/TextView01" android:layout_alignParentTop="true"></TextView>
<TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="TOP TextView" android:id="#+id/TextView02" android:layout_below="#id/TextView01"></TextView>
<ScrollView android:layout_height="wrap_content" android:layout_width="fill_parent" android:fillViewport="true" android:layout_below="#id/TextView02">
<TextView android:id="#+id/textView1" android:layout_height="wrap_content" android:text="Scrollable textViewafkasjf;laksjflaskjf;lasjf;alsfjal;sfj;aslfj;aslfja;slfj;aslkfj;asldfj;aslkfjasl;dfj;a asfj aslfj asldfjk as;lfjk asfjka;sldf jl;asj df;asdfjasfj aslfj asljf asjfl;asfasj fasj f;asj flas jf;lasjf;asjkf;las f;asj df;as jf;lasjf ;asjf;asjf;asjf;asjf;asjf;jasf;asfja s;dfa;sf asf jf asf a; sldfja;slfj;asldfjk" android:layout_width="fill_parent"></TextView>
</ScrollView>
<ImageView android:src="#drawable/icon" android:layout_height="wrap_content" android:id="#+id/imageView1" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_width="fill_parent"></ImageView>
</RelativeLayout>
It works fine with setting android:orientation="vertical" for each text view.

Categories

Resources