How to center in ScrollView and LinearLayout - java

Please have a look at the following code
<ScrollView 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=".DisplayResult" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/zodiac1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/aries"
/>
<TextView
android:id="#+id/orTxt"
android:text="Or"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="#+id/zodiac2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/gemini"
/>
</LinearLayout>
</ScrollView>
I need to center everything inside the LinearLayout. I must use scrollview because when the app is 100% done, there will be lot of information which cannot be viewed by a single glance, without scrolling. How can make these center? Please help!
UPDATE
use any layout inside the scroll, no prob. But I need those elements to be in the same order (one after another) and centered.

you need to set gravity as centered for linear layout
< LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal" >

This one worked for me:
<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:gravity="center">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"

Try using layout_gravity on your LinearLayout to center_horizontal:
<LinearLayout
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:orientation="horizontal"/>

<ScrollView 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=".DisplayResult"
scrollbars="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal">
<ImageView
android:id="#+id/zodiac1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/aries"
/>
<TextView
android:id="#+id/orTxt"
android:text="Or"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="#+id/zodiac2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/gemini"
/>
</LinearLayout>
</ScrollView>
#If you want to Linear layout in a center you need to write android:gravity="center_horizontal" in your linear layout.

Related

Android linearLayout children not expanding to fill parent

I have the following as a layout of mine:
<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"
tools:context=".MyActivity">
<EditText
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/bubble_b"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RatingBar
android:id="#+id/rating"
style="?android:attr/ratingBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:numStars="5"
android:rating="4"
android:stepSize="0.5"
android:layout_weight="1"/>
This makes my edittext and ratingbar be pushed up to the top and both of the heights are just 'wrap_content'. I have tried with RelativeLayouts as well and all that happens is the EditText takes up the entire screen and pushes the ratingbar off the screen.
Why are the layout weights not working as expected? If edittext weight is 3 and the ratingbar is 1 I would assume the edittext would take 75% of the screen, and so forth?
The background is a 9patch image so it should have no problem expanding either.
Thank you
I believe you want something like this
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="EditText"
android:ems="10"
android:id="#+id/editText"
android:layout_weight="0.53" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.53">
<RatingBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="#+id/ratingBar"/>
</RelativeLayout>
</LinearLayout>
use Relative Layout to center object:
<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"
tools:context=".MyActivity">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<EditText
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bubble_b"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<RatingBar
android:id="#+id/rating"
style="?android:attr/ratingBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:numStars="5"
android:rating="4"
android:stepSize="0.5"
android:layout_weight="1"/>
</RelativeLayout>
Change LinearLayout's
android:layout_height="wrap_content"
to
android:layout_height="match_parent".
Also, instead of setting "0dp" to EditText and RatingBar set it to match_parent.
[Update]
<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"
tools:context=".MyActivity">
<EditText
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1">
<RatingBar
android:id="#+id/rating"
style="?android:attr/ratingBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:numStars="5"
android:rating="4"
android:stepSize="1" />
</LinearLayout>
</LinearLayout>

XML layout 2 views in centre, one at top

I created linearlayout with two buttons in center (buttons 1 and 2), but I can't manage it to create the view 3 on top, which I need to make it fill whole width and adjust it's height by it (keep proportions). How do I create third view?
Image:
Code:
<?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="#drawable/bg"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content" >
<ImageButton
android:background="#drawable/btn_one"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1.0" />
<ImageButton
android:background="#drawable/btn_second"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1.0" />
</LinearLayout>
I would recommend you use relative layouts, they are the closest thing in android to an "absolute layout" replace the button tags with Image button tags
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</RelativeLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="215dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button3"
android:layout_below="#+id/button2"
android:layout_alignStart="#+id/button2" />
</RelativeLayout>
try this code set height and width what size of image button you need and simple look what you need.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:orientation="vertical"
android:gravity="center">
<ImageButton
android:layout_width="200dp"
android:layout_height="50dp"/>
<ImageButton
android:layout_width="200dp"
android:layout_height="50dp" />
</LinearLayout>

LinearLayout: 3 Elements, Top, Center, Bottom

I've the following xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/topFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<ImageView
android:id="#+id/imageViewTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_00" />
<TextView
android:id="#+id/textViewTest"
android:layout_below="#+id/imageViewTest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is a Test"/>
</RelativeLayout>
<FrameLayout
android:id="#+id/bottomFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal" />
</LinearLayout>
The first FrameLayout contains a fragment, which contains some buttons.
The RelativeLayout contains for example an image (changig size) and a text.
The last FrameLayout contains also a fragment, which contains either some buttons or some text or an image.
My aim is, that:
- the first FrameLayout is on top (that works)
- the elements in the RelativeLayout are centered in the middle of the screen (doesn't work)
- the last FrameLayout is on the bottom (doesn't work)
It should look like this:
My previous attempts with android:gravity="center" in the RelativeLayout and android:layout_gravity="bottom|center_horizontal" for the second FrameLayout failed.
Change the LineareLayout to Relative and use layout_centerInParent for the middle layout and alignParentBottom for the last one.
The top View will be anchored to the top by default
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> //removed orientation as it won't be needed anymore
<FrameLayout
android:id="#+id/topFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="wrap_content" // change here
android:layout_height="wrap_content"
android:layout_centernInParent="true"> // here
<ImageView
android:id="#+id/imageViewTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_00" />
<TextView
android:id="#+id/textViewTest"
android:layout_below="#+id/imageViewTest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is a Test"/>
</RelativeLayout>
<FrameLayout
android:id="#+id/bottomFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" /> //here
</RelativeLayout>
Use relative layout and use android:layout_centerHorizontal="true" instead of gravity.
EDIT
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/topFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/topFragment">
<ImageView
android:id="#+id/imageViewTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_00"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/textViewTest"
android:layout_below="#+id/imageViewTest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is a Test"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
<FrameLayout
android:id="#+id/bottomFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>

Overlaying textview with imageview linear layout

I've got a few linear layouts stacked on top of each other, the centre one has an imageview. I'm trying to get a textview on top of the imageview in the centre however I'm having trouble. I've tried with relative layout however it messes up all my other layouts. Any simple solutions to this?
EDIT ----
<?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:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="horizontal"
android:paddingLeft="50dp"
android:paddingRight="50dp" >
<ImageButton
android:id="#+id/btnBck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#null"
android:src="#drawable/ic_launcher" />
<ImageButton
android:id="#+id/btnHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#null"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="6"
android:orientation="vertical" >
<ImageView
android:id="#+id/picPreview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:contentDescription="#string/imageDesc"
android:src="#null" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="horizontal"
android:paddingLeft="50dp"
android:paddingRight="50dp" >
<ImageButton
android:id="#+id/btnAccept"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#null"
android:src="#drawable/ic_launcher" />
<ImageButton
android:id="#+id/btnDecline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#null"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
I'm trying to get the text view on top of the id/picPreview image view
LinearLayout is designed to place layouts/views in a row (linearly).
One solution would be something like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="6"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/picPreview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:contentDescription="#string/imageDesc"
android:layout_centerInParent="true"
android:src="#null" />
<TextView
android:id="#+id/picPreviewLbl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:text="Overlay" />
</RelativeLayout>
</LinearLayout>
Why not just use a TextView in place of the centered ImageView and then set the background of the text be the solid color that you are currently using? Eg.
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:text="Sample Text" ... />

Setting a button at the bottom without textview going with it

I'm having a problem with my button I have set it to the bottom succesfully but when I set it the textview I have goes down with it too all I want is the button only to be at the bottom and textview stay at its position which is center without following the position of the bottom I hope you can help me...
thanks in advance
this is my first.xml
<?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:id="#+id/relLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background2"
android:gravity="center"
android:orientation="vertical"
tools:context=".FirstActivity" >
<TextView
android:id="#+id/first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="center"
android:paddingTop="10sp"
android:text="قال (عليه السلام): (من كرمت عليه نفسه هانت عليه الدنيا)"
android:textIsSelectable="true"
android:textSize="25sp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:text="Button" />
</LinearLayout>
if you want to button at bootm then try
android:layout_gravity="bottom|center_horizontal"
instead of only gravity
in button and if you want to text center then
android:layout_gravity="center"
Simply layout gravity assign your layout at perticular position and gavity assign it's content in perticular position in own layout
The simplest solution is using FrameLayout and it's android:layout_gravity attribute.
<?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:id="#+id/relLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background2"
tools:context=".FirstActivity" >
<TextView
android:id="#+id/first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_gravity="top|center_horizontal"
android:paddingTop="10sp"
android:text="قال (عليه السلام): (من كرمت عليه نفسه هانت عليه الدنيا)"
android:textIsSelectable="true"
android:textSize="25sp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:text="Button" />
</FrameLayout>
This solves the problem but this solution is not very flexible. If you want more flexibility for future changes, you could do a similar thing using RelativeLayout instead of FrameLayout.
try this
-------
<?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:id="#+id/relLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background2"
android:orientation="vertical"
tools:context=".FirstActivity" >
<LinearLayout
android:id="#+id/lnrfirst"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_marginTop="20dp"
>
<TextView
android:id="#+id/first"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="center"
android:paddingTop="10sp"
android:text="قال (عليه السلام): (من كرمت عليه نفسه هانت عليه الدنيا)"
android:textIsSelectable="true"
android:textSize="25sp" />
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Button" />
</RelativeLayout>
</RelativeLayout>

Categories