1

I need to show n number of Edit text added dynamically below a static Edit text. I am adding the ET but some how it is not visible. What am doing wrong??

Here is my code:

private void addEditTextView(int numberOfViews){
    // Using layout params same as above static ET
    ViewGroup.LayoutParams layoutParams = staticEditText.getLayoutParams();

    for (int index = 0; index <= numberOfViews; index++) {
        final EditText newET = new EditText(getActivity());

        //Added below 2 lines just to make sure width and height are coming
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        newET.setLayoutParams(layoutParams);
        newET.setHint("Select ME");
        newET.setFocusable(false);
        newET.setId(index);
        newET.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO set time
                Log.d("Clicked...",newET.getId()+"");
            }
        });
        //parentLayout is Linear Layout with Vertical orientation
        parentLayout.addView(newET);
    }
}

XML code :

 <LinearLayout
        android:id="@+id/parentLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <EditText
            android:id="@+id/staticEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="3dp"
            android:background="@drawable/rectangle_bg_dark_gray_border"
            android:focusable="false"
            android:hint="staticEditText"
            android:padding="7dp" />

    </LinearLayout>

UPDATE : parentLayout.getChildCount() is coming correctly. New views are getting added but not visible!

Abhinav Tyagi
  • 5,158
  • 3
  • 30
  • 60

1 Answers1

1

Hi try adding a Scrollview

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout 
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       xmlns:android="http://schemas.android.com/apk/res/android">
       <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <LinearLayout
                  android:id="@+id/parentLayout" 
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:orientation="vertical">
                  <!-- EDITTEXT here -->
            </LinearLayout>
      </ScrollView>
 </LinearLayout>
br00
  • 1,391
  • 11
  • 21