0

Android Studio 0.4.4

Hello,

I have the following layout which is in relative layout and for landscape view.

Basically, I have 3 EditText's and a Button. I want to display the ListView from the centre of the screen. And have the EditText's and Button width's to align to the left of the ListView.

I have managed to do this using the dp. But I don't like using the dp as this could affect other screen densities.

I have set the ListView to android:layout_marginLeft="250dp" and then set the EditText and button to android:layout_width="250dp"

Is there any way I can do this without using the dp as the width? Here is my current xml.

Many thanks for any suggestions,

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ListView
        android:id="@+id/lvFriends"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        android:layout_marginLeft="250dp">
    </ListView>

    <EditText
        android:id="@+id/etName"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:hint="Name"
        android:textSize="22sp"
        android:inputType="text"/>

    <EditText
        android:id="@+id/etPhone"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:hint="Phone"
        android:textSize="22sp"
        android:inputType="phone"
        android:layout_below="@+id/etName"/>

    <EditText
        android:id="@+id/etEmail"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:hint="Email"
        android:textSize="22sp"
        android:inputType="textEmailAddress"
        android:layout_below="@+id/etPhone"/>

    <Button
        android:id="@+id/btnAddFriend"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="Add Friend"
        android:textSize="22sp"
        android:layout_marginTop="8dp"
        android:layout_below="@+id/etEmail"/>

</RelativeLayout>
ant2009
  • 27,094
  • 154
  • 411
  • 609

3 Answers3

2

You can use layout_center.. with the relative layout,

    <ListView
        android:id="@+id/lvFriends"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:layout_centerHorizontal="true">
    </ListView>

As for the width of edit text, you could either have a drawable (different for all dpi) as a background and use wrap_content or just use the dp value but always in values/dimens
http://developer.android.com/guide/topics/resources/more-resources.html#Dimension
Load dimension value from res/values/dimension.xml from source code

Community
  • 1
  • 1
Pararth
  • 8,114
  • 4
  • 34
  • 51
  • 1
    android:layout_centerHorizontal="true" on the ListView didn't work for me. Is this because the width and layout is match_parent? – ant2009 Feb 08 '14 at 09:22
1

You can set android:layout_weight="1" to every EditText,but this property should be used in LinearLayout.And you can set android:layout_centerInParent="" in your xml.

Andy.Zhao
  • 250
  • 3
  • 16
1

you can try to put this code on your xml file.

<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" >

    <TextView                   
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:visibility="gone" />

   <LinearLayout 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentLeft="true"
       android:id="@+id/linear1"

       android:orientation="vertical">

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"


        android:ems="6"

        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>
     <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:ems="6"

        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>
     <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:ems="6"

        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

      <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">


    </Button>

    </LinearLayout>
      <ListView
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true"
        android:layout_toRightOf="@+id/linear1"

       >
    </ListView>

</RelativeLayout>
Hardik
  • 94
  • 1
  • 1
  • 11