abdul
abdul

Reputation: 157

How to stop layout from taking all width

I have this activity and I need to stop filling all width of textview and button. Currently these are shown in centre but filling all width. How to stop that and make width fixed? Please see layout below.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="1280dp"
    android:layout_height="match_parent"
    android:gravity="center_vertical|center_horizontal"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Login" >

    <TextView
        android:id="@+id/lblUserName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerHorizontal="true"
        android:text="@string/UserName" />

    <EditText
        android:id="@+id/txt4UserName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/lblUserName"
        android:layout_below="@+id/lblUserName"
        android:ems="10"
        android:inputType="text|textFilter|textNoSuggestions" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/lblPassword"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/lblUserName"
        android:layout_below="@+id/txt4UserName"
        android:layout_marginTop="20dp"
        android:text="@string/Password" />

    <EditText
        android:id="@+id/txt4Password"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/lblPassword"
        android:layout_below="@+id/lblPassword"
        android:ems="10"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/btnForLogin"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="50dip"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/txt4Password"
        android:layout_alignRight="@+id/txt4Password"
        android:layout_below="@+id/txt4Password"
        android:layout_marginTop="30dp"
        android:maxLength="50"
        android:onClick="Verification"
        android:text="@string/Login" />

</RelativeLayout>

Required highlighted in red enter image description here

Upvotes: 0

Views: 87

Answers (4)

Onur A.
Onur A.

Reputation: 3027

change width of your parent layout to wrap_content, if it doesn't change anything then try to put some margin to left and right of your parent layout as your choice

<RelativeLayout 
...
android:layout_width="wrap_content"
...>

or

<RelativeLayout 
...
android:layout_marginLeft="someValue"
android:layout_marginRight="someValue"
...>

Upvotes: 1

David N
David N

Reputation: 519

I think you could use

android:layout_centerInParent="true" 

in your relative layout

Upvotes: 1

Ryhan
Ryhan

Reputation: 1885

Change your EditText's width to wrap_content & your RelativeLayout's width to match_parent.

<RelativeLayout 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_vertical|center_horizontal"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Login" >

<EditText
        android:id="@+id/txt4UserName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/lblUserName"
        android:layout_below="@+id/lblUserName"
        android:ems="10"
        android:inputType="text|textFilter|textNoSuggestions" >



 <EditText
    android:id="@+id/txt4Password"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/lblPassword"
    android:layout_below="@+id/lblPassword"
    android:ems="10"
    android:inputType="textPassword" />

Upvotes: 1

Shruti
Shruti

Reputation: 5591

You have mentioned

android:layout_width="1280dp"

in your relative layout

set it to

android:layout_width="wrap_content"

and in both the edittexts you have specified layout_width as fill_parent .. I think this is causing the problem

try setting edittext's width to whatever you want or simply to wrap_content

and then your layout will not occupy whole screen

Upvotes: 1

Related Questions