简体   繁体   中英

Android EditText dynamic height programmatically

i got a little problem with my EditText. I want it to automatically increase its height. I found other people asking for it: like this one

My problem is that im not using a XML-File. I just use my Java-Code:

public class CommentatorView extends LinearLayout {

//Variablen um den Taschenrechner anzupassen
final static float KLEIN = 0.75f;
final static float MITTEL = 1.0f;
final static float GROS = 1.5f;
final static float GROS_1 = 2.0f;
final static float GROS_2 = 3.0f;
final static float GROS_3 = 4.0f;


CommentatorListener clickListener;
Commentator goTo;
EditText commentField;
public CommentatorView(Commentator goTo) {
    super(goTo.getContext());

    this.setOrientation(LinearLayout.VERTICAL);
    this.goTo = goTo;
    clickListener = new CommentatorListener(this, goTo);

    commentField = new EditText(this.getContext());
    commentField.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    commentField.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);

    commentField.setMinHeight(120);
    this.addComponent();
}

private void addComponent() {

    this.addView(this.commentField);

    LinearLayout linearLayout = new LinearLayout(this.getContext());

    LayoutParams buttonParams = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
    buttonParams.weight = 1;
    buttonParams.setMargins(1, 0, 1, 0);

    Button btnSave = new Button(this.getContext());
    btnSave.setOnClickListener(this.clickListener);
    btnSave.setTypeface(null, Typeface.BOLD);
    btnSave.setTextSize(18);
    btnSave.setText("Speichern");
    btnSave.setTag("save");
    linearLayout.addView(btnSave, buttonParams);

    Button btnCancel = new Button(this.getContext());
    btnCancel.setOnClickListener(this.clickListener);
    btnCancel.setTypeface(null, Typeface.BOLD);
    btnCancel.setText("Abbrechen");
    btnCancel.setTag("no");
    btnCancel.setTextSize(18);

    linearLayout.addView(btnCancel, buttonParams);

    this.addView(linearLayout);
}

public int getWantedNumber() {
    int i = Integer.parseInt(commentField.getText().toString());
    return i;
}

public boolean isNumber() {
    try {
        int d = Integer.parseInt(commentField.getText().toString());
        Log.i("WTG", "" + d);
    } catch (NumberFormatException nfe) {
        Log.e("WTG", "Cast failed");
        return false;
    }
    return true;
}}

Is there any way to get in automatically increasing height with just my java code?

Thanks in advance!

Instead of doing

commentField.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

use MATCH_PARENT for height, if you want it to be as high as your parent ofcourse.

commentField.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

Otherwise you could use the weight.

commentField.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 0, 1));

EDIT

in case you want the edittext to wrap it's content but expand when necessary, check out this question Create a multiline EditText programatically

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM