How to make EditText have maximum width but still fill parent width

I have an EditText that I want to fill the entire available horizontal width, but the width should not be greater than 200dp

This makes EditText adaptable to any screen size and still makes it look good on large screens (horizontal stretching will not look beautiful on a large screen).

How to do it in Android?

I saw that maxWidth=200dp and layoutWidth=fill_parent do not work together:

  <EditText android:id="@+id/oldpassword" android:hint="@string/youroldpassword" android:inputType="textpassword" android:layout_width="fill_parent" android:layout_height="wrap_content" android:maxWidth="250dp" /> 

If maxWidth and layoutWidth=fill_parent do not work together, then what does maxWidth mean for <? p>

In other words, if the EditBox does not change its width dynamically, then what do you need maxWidth for?

+6
source share
6 answers

You can set the width programmatically depending on the screen size of the device. as

 Display display = getWindowManager().getDefaultDisplay(); int width = display.getWidth(); EditText et = (EditText)findViewById(R.id.editText); if(width > 200) { et.setWidth(200); } else { et.setWidth(width); } 
+3
source

I have a solution, look, if you set android:layout_width="fill_parent" , it always had a width that has a parent element, but when you set android:layout_width="wrap_content" , then when entering text in EditText the width was increased size with content, now if you use android:maxWidth="250dp" with android:layout_width="wrap_content" , then it will increase its width to 250 dp when entering a value in EditText

using

 <EditText android:id="@+id/oldpassword" android:hint="@string/youroldpassword" android:inputType="textpassword" android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxWidth="250dp" />` 

or use

 <EditText android:id="@+id/oldpassword" android:hint="@string/youroldpassword" android:inputType="textpassword" android:layout_width="fill_parent" android:layout_height="wrap_content"/> 
+1
source

remove maxWidth = 200dp in layout

0
source

You can wrap text editing in a relative layout. Here is what you can do -:

 <RelativeLayout android:id="@+id/relativeLayout" android:layout_width="match_parent" android:layout_height="wrap_content" > <EditText android:id="@+id/oldpassword" android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxWidth="250dp" android:maxLines="1" /> </RelativeLayout> 
0
source

You can wrap editText in a layout (200dp wide) and make editText in match_parent.

-1
source

I solved this with android: layout_marginRight = "300dp" , the larger this value, the smaller the line width of the EditText object.

-3
source

Source: https://habr.com/ru/post/954042/


All Articles