Android center horizontal not working - android

I got this code, a linearlayout inside relativelayout... however its not centering horizontally... any idea why?
i want the linearlayout to be horizontall centered. i am filling it with images via code.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relative1"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="10"
android:background="#color/list_background_pressed" >
<LinearLayout
android:id="#+id/linear1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerHorizontal="true" >
</LinearLayout>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="HI! Welcome to My Zain"
android:textColor="#ffffff" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/text2"
android:layout_centerHorizontal="true"
android:paddingTop="20dp"
android:src="#drawable/ico_profile_1_64x64" >
</ImageView>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relative2"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="10" >
<TextView
android:id="#+id/text5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="The new Recharge Mechanism"
android:textColor="#000000" />
</RelativeLayout>
</LinearLayout>

In the LinearLayout, try setting the layout_width to "wrap_content". Right now, you're stretching it to the full width of the window, so there's nothing to center.
android:layout_width="wrap_content"

add android:gravity="center"
<RelativeLayout
android:id="#+id/relative2"
android:layout_width="match_parent"
android:layout_height="0px"
android:gravity="center"
android:layout_weight="10" >

Related

Android App 50 percent width

I am trying to build a little Android App and I need two LinearLayout side by side. (50/50)
How it looks:
http://i.stack.imgur.com/C9yCF.png
I already try to use: android:layout_width="0dp" android:layout_weight="1" but it didn't work.
This is my XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/part">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="9pt"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:baselineAligned="true"
android:background="#ff7f7f7f"
android:id="#+id/head">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="123456789"
android:id="#+id/textView"
android:paddingStart="3pt"
android:paddingLeft="3pt"
android:textColor="#ffffffff"
android:singleLine="false" />
</LinearLayout>
<LinearLayout
android:id="#+id/left"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_below="#+id/head"
android:background="#ffff0100" >
</LinearLayout>
<LinearLayout
android:id="#+id/right"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/head"
android:background="#fffcff00" />
How it should look:
http://i.stack.imgur.com/tup01.png
Have one more LinearLayout with horizontal orientation below your #+id/head LinearLayout.
Set its width to match_parent. Keep the last two LinearLayouts inside the new LinearLayout and have android:layout_weight=0.5 for both. Something like:
<RelativeLayout>
<LinearLayout> </LinearLayout>
<LinearLayout
...
android:orientation:"horizontal"/>
<LinearLayout
android:weight="0.5"
android:width="0dp"
... />
</LinearLayout>
<LinearLayout
android:weight="0.5"
android:width="0dp"
... />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
You can't use layout_weight with RelativeLayouts children. Put/Wrap both LinearLayouts in another LinearLayout.
Hey try this code...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/part"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/head"
android:layout_width="fill_parent"
android:layout_height="9pt"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#ff7f7f7f"
android:baselineAligned="true"
android:orientation="vertical" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="3pt"
android:paddingStart="3pt"
android:singleLine="false"
android:text="123456789"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#ffffffff" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/head"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/left"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffff0100"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="#+id/right"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#fffcff00"
android:orientation="vertical" />
</LinearLayout>

How to divide screen space between ImageView and TextView in RelativeLayout

I need to put ImageView and below it should be textview. I have done this by using text and image in RelativeLayout. Everything is ok , but I need to give more space (2/3) to ImageView . How can I do this ?
Thx in advance .
<RelativeLayout
android:id="#+id/rel_lay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_above="#+id/adPlaceholder" >
<ImageView
android:id="#+id/image_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/text_main"
android:layout_below="#+id/image_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/hello_world"/>
</RelativeLayout>
I think LinearLayout is more appropriate for your situation
You can use LinearLayout with android:orientation="vertical" and android:weightSum="3"
Then you can give android:layout_weight="2" to the ImageView and android:layout_weight="1" to the TextView
The final XML code after update
<LinearLayout
android:id="#+id/rel_lay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_above="#+id/adPlaceholder"
android:orientation="vertical"
android:weightSum="3" >
<ImageView
android:id="#+id/image_first"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="2"
android:src="#drawable/ic_launcher"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/text_main"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:layout_centerHorizontal="true"
android:text="#string/hello_world"/>
</LinearLayout>
Are you wanting something, or is that your image occupy more space to your text, you can do this type:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rel_lay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/adPlaceholder"
android:layout_alignParentLeft="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dip"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="#+id/image_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/text_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="#string/hello_world" />
</LinearLayout>
</RelativeLayout>
I think the best solution would be to put a LinearLayout inside ReletiveLayout and set weightsum to the inner LinearLayout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rel_lay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/adPlaceholder"
android:layout_alignParentLeft="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:weightSum="3" >
<ImageView
android:id="#+id/image_first"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/text_main"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="2"
android:text="#string/hello_world" />
</LinearLayout>
</RelativeLayout>
Also note that the layout_weight of ImageView is 1 out of 3 making in 2/3 of the parent and that of TextView is 2 out of 3 making it cover 1/3 of parent.

Android ImageView layout alignment

How can change my layout to make image 1 more like image 2? I don't mind if it's the text that moves up or the image that moves up.
my layout file is like so :-
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:orientation="vertical" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/imp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/stats_chr" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right|center_vertical"
android:singleLine="true"
android:text="AA" />
</LinearLayout>
Accepted answer does not works for me. Here is my solution
Add android:scaleType="fitStart" to the ImageView.
Try this..
Use android:gravity="top" for LinearLayout or android:layout_gravity="top" for ImageView
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:gravity="top"
android:layout_weight="0.6"
android:orientation="vertical" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:src="#drawable/imp" />
</LinearLayout>

Android: Horizontal scrollview issue

I am facing layout issues while using the horizontal scrollview to scroll images with checkboxes
below is my layout
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
>
<LinearLayout
android:id="#+id/mygallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
</LinearLayout>
</HorizontalScrollView>
i am using this scrollview in a listview which is in another layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/albumsearch"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<ListView
android:id="#+id/albumdetails"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:paddingBottom="5dp" >
</ListView>
</LinearLayout>
below is the layout with my image and checkbox
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<CheckBox
android:id="#+id/selectedImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/icon"
android:layout_centerHorizontal="true"
android:layout_marginBottom="72dp"
android:layout_marginTop="20dp"
android:checked="true" />
<ImageView
android:id="#+id/icon"
android:layout_width="72dp"
android:layout_height="75dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
but when i am running this code in small screen devices i can see only half image, how to get full image, any help is appreciated.
Solved: After Changing my layout with checkbox and imageview to linearlayout and used weight attribute
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="left"
android:orientation="vertical"
android:layout_weight="1" >
<CheckBox
android:id="#+id/selectedImg"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:checked="true"
android:layout_weight="1"
android:gravity="center" />
<ImageView
android:id="#+id/icon"
android:layout_width="72dp"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center" />
</LinearLayout>

LinearLayout being pushed off the screen

So for my landscape layout of a page, I want to have a sort of horizontal layout. However, my FrameLayout including my ListView goes off the screen, even though it's set to android:layout_width="match_parent". Here's a screenshot of what I mean:
The blue line is the outline of where the FrameLayout is going, when it is set to match parent.
Here's my XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/contactlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:background="#drawable/brushed_metal_background_dark"
android:orientation="vertical"
tools:context=".ContactList" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/headerbackground"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1"
android:scaleType="center"
android:src="#drawable/homeheader7" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:paddingRight="3sp"
android:text="#string/version"
android:textColor="#FFFFFF"
android:textStyle="italic" >
</TextView>
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:onClick="setOptions"
android:src="#drawable/menu" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical" >
<ImageButton
android:id="#+id/report"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#null"
android:gravity="left"
android:onClick="report"
android:scaleType="center"
android:src="#drawable/submit_report" />
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:adjustViewBounds="true"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:scaleType="center"
android:src="#drawable/divider_ver" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:scaleType="center"
android:src="#drawable/schoolfeed2" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="100"
android:background="#drawable/background_overlay_dark"
android:duplicateParentState="false"
android:fadingEdge="horizontal"
android:paddingBottom="9dp"
android:paddingLeft="16dp"
android:paddingRight="10dp"
android:paddingTop="7dp" >
<ListView
android:id="#+id/schoolFeed"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="2dp"
android:footerDividersEnabled="false"
android:headerDividersEnabled="true"
android:scrollbarDefaultDelayBeforeFade="50000"
android:scrollbarStyle="outsideInset"
android:scrollbarThumbVertical="#drawable/scrollbar_vertical_thumb"
android:scrollbarTrackVertical="#drawable/scrollbar_vertical_track" >
</ListView>
</FrameLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Any help is appreciated! Thanks,
I would recommend you that, instead of using match_parent, use wrap_content, becuase, actually, whats happening is that the framelayout has the same width of the screen, so, as it isn't at the left of the screen, it goes off.

Categories

Resources