Reputation: 968
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.
Here the XML:
<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" />
Here the code of the custom EditText:
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);
}
}
I've seen a similar post, that use the same, but I couldn't make it work.
UPDATE:
Given the quick responses, find bellow a clarification of the scenario:
Tks for the current answers, and TIA for any further answer!
Milton.
Upvotes: 1
Views: 7315
Reputation: 3506
Try do it from layout, which contain CustomEditText. For example:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.example.untitled1.CustomEditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, MyActivity"
android:id="@+id/et"
/>
MyActivity.java
public class MyActivity extends Activity {
private CustomEditText et;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et= (CustomEditText) findViewById(R.id.et);
ViewGroup.LayoutParams lp = et.getLayoutParams();
lp.width = 100;
lp.height = 100;
et.setLayoutParams(lp);
}
}
Work like a charm!
Upvotes: 1
Reputation: 4567
LayoutParams params = layout.getLayoutParams();
// Changes the height and width to the specified *pixels*
params.height = 100;
params.width = 100;
Upvotes: 0