JuSchz
JuSchz

Reputation: 1208

maxWidth EditText in Android

I tried to apply a maxWidth on a EditText in Android. But the attributes seems to be ignored. In fact, I just want to have an EditText align in the center of the screen with a maximum size for big screen. Here my code :

<LinearLayout  
       android:layout_width="fill_parent"
       android:layout_height="40dp"
       android:paddingLeft="30dip" 
       android:paddingRight="30dip"
       android:gravity:"center" >    
       <EditText
              android:id="@+id/name"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:hint="@string/input_name"
              android:nextFocusUp="@+id/password"
              android:maxWidth="120dip" />

</LinearLayout>

Thanks for your help

Upvotes: 2

Views: 2661

Answers (3)

CSharp
CSharp

Reputation: 1583

Try this cdoe:

<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"> 
<EditText android:id="@+id/editText1" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:hint="input_name"
     android:maxWidth="120dip" />  
</LinearLayout>  

And you can try this too

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

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

Hope this will help you.

Upvotes: 1

Narendra Pal
Narendra Pal

Reputation: 6604

Try this to have edit text in the center of the screen: you have to add the layout_gravity instead of gravity like here:

      <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="40dp"
       android:paddingLeft="30dip" 
       android:paddingRight="30dip"
       android:layout_gravity="center" > 

       <EditText
              android:id="@+id/name"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:gravity="center"
              android:nextFocusUp="@+id/password"
              android:maxWidth="120dip" />

</LinearLayout>

Upvotes: 3

Daud Arfin
Daud Arfin

Reputation: 2499

Try this, wrap content will be resize automatically based on device resolution.

     <EditText
         android:id="@+id/et"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_centerHorizontal="true"
         android:layout_centerVertical="true"
         android:hint="Required"
         android:singleLine="true"
         android:typeface="monospace"
         android:maxLength="100">
    </EditText>

Upvotes: 0

Related Questions