Problem with Android Layout - android

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"

Related

Two linear layouts in a layout resource file but only one displays

I have already seen the same question but the answer did not held me that much. I am new to android app development and I have the following problem. From the two linear layouts only the first one displays. Can anyone help? Thank you in advance!
<?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"
android:background="#drawable/home"
tools:context="dmst.allamoda.Login"
android:weightSum="1">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/linearLayout1"
android:background="#drawable/rectangle"
android:gravity="top|right">
<Button
android:background="#drawable/mybutton"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/button_home"
android:id="#+id/home"
android:textColor="#ffffd1"
android:onClick="signupControl"
android:layout_gravity="center"/>
<Button
android:background="#drawable/mybutton"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/button_profile"
android:id="#+id/profile"
android:textColor="#ffffd1"
android:onClick="signupControl"
android:layout_gravity="center"/>
<ImageView
android:id="#+id/logo_image"
android:background="#drawable/allamodalogo"
android:layout_width="160dp"
android:layout_height="100dp"
android:scaleType="fitXY"
android:gravity="right"
android:adjustViewBounds="true"
android:maxHeight="50dp"
android:maxWidth="50dp"
android:minHeight="50dp"
android:minWidth="50dp"
android:layout_gravity="right"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/linearLayout2"
android:background="#drawable/post">
</LinearLayout>
</LinearLayout>
replace your second linear layout with this code below
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"
android:orientation="vertical"
android:id="#+id/linearLayout2"
android:background="#drawable/post">

How to display image in center across resolutions

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.

How to center a view using RelativeLayout?

I wanted to know how to center a View between two other views (or between a view and the parent edge) using RelativeLayout.
For example, if I have the following...
How do I vertically center the Button between the ImageView and the bottom of the screen using RelativeLayout?
I'm looking for a solution where...
the Button is not stretched in any way
there are no nested layouts
And I'm trying to do this in the XML layout (not programmatically).
You can use following:
<RelativeLayout
android:layout_below="#id/theImageView"
android:align_parentBottom="true"
android:layout_width="match_parent"
android:layout_height="200dp" >
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="onClickButton"
android:textSize="20sp"
android:text="Go"/>
</RelativeLayout>
There's unfortunately no easy way to do this. You can either nest layouts, or do it at runtime. The simplest way is to nest layouts:
<LinearLayout
android:width="match_parent"
android:height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_top_image"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="#string/my_button_label"/>
</RelativeLayout>
</LinearLayout>
This puts the image at the top. Below that, the layout_height=0 and layout_weight=1 attributes on the RelativeLayout cause it to take up all the remaining space. You can then center the button in the RelativeLayout. You can play with padding on the button to get it to the size you want.
Use below XMl code for that, it will solve your problem.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/mLlayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/mImgView1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="#drawable/ic_launcher" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="2" >
<Button
android:id="#+id/Btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Dipak" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
use this android:layout_centerInParent="true" in 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"
android:weightSum="1.0" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.8"
android:background="#00ff00" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.2" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button" />
</RelativeLayout>
</LinearLayout>
#Gus you can create center view between two other view... this is not exact you must try with
this way....
<?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" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#00ccFF"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:layout_width="59dp"
android:layout_height="59dp"
android:layout_alignRight="#+id/linearLayout1"
android:layout_alignTop="#+id/linearLayout1"
android:layout_marginRight="-21dp"
android:layout_marginTop="-21dp"
android:background="#FFccFF"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
I think what you just need is to allign the imageview as you would normally done and align the button using layout_below , no need for hardcoding nor using nested views use android:gravity="center" on the button you're done
<RelativeLayout
android:layout_below="#id/theImageView"
android:align_parentBottom="true"
android:layout_width="match_parent"
android:layout_height="200dp" >
<ImageView
android:id="#+id/imgview"
android:layout_width="match_parentmat"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imgview"
android:gravity="center"
android:onClick="onClickButton"
android:textSize="20sp"
android:text="Go"/>
</RelativeLayout>
<?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" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:src="#drawable/wood" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageView1"
android:layout_below="#+id/imageView1"
android:layout_marginLeft="101dp"
android:layout_marginTop="53dp"
android:text="Image Button" />
</RelativeLayout>

Display 4 Pictures in the middle

Hy!
I want to have have a layout like this:
The problem is i don't know what to use:
Gridview?
TableLayout?
LinearLayout with PictureViews?
The Pictures should be oriented in the middle of the screen(horicontal & vertikal)
Please add some code example in your answers.
thx
one way can do this type of layout
<?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" android:gravity="center_horizontal|center_vertical">
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content">
<ImageView android:layout_width="150dp" android:layout_height="150dp"
android:layout_weight="1" android:background="#drawable/bg_img"
android:layout_margin="5dp"/>
<ImageView android:layout_width="150dp" android:layout_height="150dp"
android:layout_weight="1" android:background="#drawable/bg_img"
android:layout_margin="5dp"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content">
<ImageView android:layout_width="150dp" android:layout_height="150dp"
android:layout_weight="1" android:background="#drawable/bg_img"
android:layout_margin="5dp"/>
<ImageView android:layout_width="150dp" android:layout_height="150dp"
android:layout_weight="1" android:background="#drawable/bg_img"
android:layout_margin="5dp"/>
</LinearLayout>
</LinearLayout>
i used this image download from here, image is too big but this was test only you can use your images just replace the background src in imageview in each or as per your requirement.
and you get the result like this way
Please use below linearlayout with Imageview..Main trick is hot use weight in linearlayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:id="#+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1">
<ImageView android:id="#+id/imageView1" android:layout_width="fill_parent" android:layout_weight="1" android:layout_height="fill_parent" android:background="#drawable/icon"></ImageView>
<ImageView android:id="#+id/imageView2" android:layout_weight="1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#drawable/icon"></ImageView>
</LinearLayout>
<LinearLayout android:id="#+id/linearLayout2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1">
<ImageView android:id="#+id/imageView3" android:layout_width="fill_parent" android:layout_weight="1" android:layout_height="fill_parent" android:background="#drawable/icon"></ImageView>
<ImageView android:id="#+id/imageView4" android:layout_width="fill_parent" android:layout_weight="1" android:layout_height="fill_parent" android:background="#drawable/icon"></ImageView>
</LinearLayout>
</LinearLayout>

One button on left side other on the right

I know there are questions like this but most the time it seems their problem is they didn't align the Layouts as fill parent. Well I did so I am not sure why my buttons still won't do it. Could it be because of all the layouts I use?
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/face">
<RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:gravity="center">
<ImageView android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:id="#+id/eyes" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#drawable/eyes1" />
<LinearLayout android:layout_marginTop="50dp" android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="horizontal">
<Button android:id="#+id/eyesback" android:layout_width="50dp" android:layout_height="50dp" />
<Button android:id="#+id/eyesforward" android:layout_height="50dp" android:layout_width="50dp" android:layout_gravity="right"></Button>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
Its probably something simple I missed too. Thanks for any help.
try this one
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/face">
<RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:gravity="center">
<ImageView android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:id="#+id/eyes" android:background="#drawable/eyes1" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<RelativeLayout android:layout_marginTop="50dp" android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="horizontal">
<Button android:id="#+id/eyesback" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentLeft="true"/>
<Button android:id="#+id/eyesforward" android:layout_height="50dp" android:layout_width="50dp" android:layout_gravity="right" android:layout_alignParentRight="true"></Button>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
I have simply change teh LinearLayout into Relative layout and set properties to one button to get alighted left another one to right.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/face">
<RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:gravity="center">
<ImageView android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:id="#+id/eyes" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#drawable/eyes1" />
<Button android:id="#+id/eyesback" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentLeft="true"/>
<Button android:id="#+id/eyesforward" android:layout_height="50dp" android:layout_width="50dp" android:layout_gravity="right" android:layout_alignParentRight="true"></Button>
</RelativeLayout>
</LinearLayout>
try it out
Check this: this is suitable example for your requirement.
http://androidcodesnips.blogspot.com/2011/09/showing-buttons-on-either-side-of.html

Categories

Resources