0

I have developed an Health App in Android, and i have an Activity where the first question is a Yes/No answer (i make with 2 checkboxes), depending the Yes/No answer the activity shows diferents EditText to complete. I really know how to hide EditText and how to show when the user click the checkboxes, but the question is if exist a correct design pattern to do this? i read the Material Desing web , but i didnt find nothing about this. It is correct way to do? Or i must enable/disable the EditTexts.

  • `android:visibility="invisible"` and `visible` when needed. Can also set it programmatically. – Vucko Aug 15 '16 at 22:28

1 Answers1

0

If you only need to set a few EditText's, your way is right.

OPTION A

Imagine that "foo()" returns which is the EditText that you have to show.

In your layout:

<EditText
 android:id="@+id/edit1"
 android:visibility="GONE"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"/>

<EditText
 android:id="@+id/edit2"
 android:visibility="GONE"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"/>

....

Now, in your code:

...
switch(foo()){
  case 1: //You have to show the first EditText
    EditText edit1 = (EditText)findViewById(R.id.edit1);
    edit1.setVisibility(View.VISIBLE);
    break;
  case 2: //You have to show the second EditText
    EditText edit2 = (EditText)findViewById(R.id.edit2);
    edit2.setVisibility(View.VISIBLE);
    break;
  ....
 }

OPTION B

Another choice would be adding the EditText programatically, something like:

EditText editText = new EditText(context); // Pass it an Activity or   Context
editText.setLayoutParams(new LayoutParams(..., ...)); // Pass two args; must be LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, or an integer pixel value.
myLayout.addView(editText);

In this case, you dont need to add the EditText's in the layout file, you are going to add it dynamically only if you need it!

Generating Edit Text Programatically in android

Hope it helps!

Community
  • 1
  • 1
David Corral
  • 4,085
  • 3
  • 26
  • 34
  • Thanks, maybe my question is a litlee confused, I already know how to do that. The question is if this method is correct in in desing of android. – Pablo Pautasso Aug 15 '16 at 22:39
  • As I said in my answer if you only are going to have a couple of EditText's, it is right. But if you want to control a more specific situation, check the option B that is more efficent. – David Corral Aug 15 '16 at 22:42