Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set layout height of an edittext programmatically
    text
    copied!<p>I'm trying to set the height of a custom EditText component programmatically, without success. I have a custom class that inherit from EditText, to set a custom font. I want to update the height and padding of the EditText, but all my intents don't update it. Only when I set the height in the XML file, the height is updated.</p> <p><strong>Here the XML:</strong></p> <pre><code>&lt;example.ui.customcontrols.CustomEditText android:id="@+id/txtUsername" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="2dip" android:layout_marginTop="2dip" android:inputType="text" android:singleLine="true" /&gt; </code></pre> <p><strong>Here the code of the custom EditText:</strong></p> <pre><code>public class CustomEditText extends EditText { public CustomEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.init(); } public CustomEditText(Context context, AttributeSet attrs) { super(context, attrs); this.init(); } public CustomEditText(Context context) { super(context); this.init(); } public void init() { UIHelper.setTypeface(this); this.setTextSize(17); // This is not working. this.setLayoutParams(newLayoutParams(LayoutParams.FILL_PARENT, 10)); this.setPadding(this.getPaddingLeft(), 0, this.getPaddingRight(), 0); } } </code></pre> <p>I've seen a <a href="https://stackoverflow.com/questions/4274819/how-to-dynamically-set-edittext-height">similar post</a>, that use the same, but I couldn't make it work.</p> <p><strong>UPDATE:</strong></p> <p>Given the quick responses, find bellow a clarification of the scenario:</p> <ol> <li>I've a lot of EditText, that are added dynamically in the activity layout. I need to do that from the custom control, rather from the activity. I'm looking for the right event in the EditText (like onDraw, onPredraw, etc), in order to read and set the layoutparams. Let me know if you find the proper event to be overrided. Tks again! </li> <li>getLayoutParams return null in the constructor of the view (EditText). So, i think that the solution may come addressing the right event, when the Layout be instantiated.</li> <li>So far I've tried and read about a lot of the events, like onDraw, onLayout, onFinishInflate, etc. But the param is ignored, or trows an exception.</li> </ol> <p>Tks for the current answers, and TIA for any further answer!</p> <p>Milton.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload