2

I have been reading about how to dynamically add an EditText field to my Linear Layout, every-time a user clicks a TextView (which has an onClick Listener attached).

I have had some mild success - I know that the EditText field is being created because when the button is clicked, all other elements move up as if something is being added to the screen.

My problem is that the EditText aren't visible and I haven't a clue why that is, so any help would be appreciated.

The the app isn't crashing so nothing to add in terms of the StackTrace and Log, as far as the app is concerned, everything is being created.

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/facebookBlue"
    android:orientation="vertical"
    android:weightSum="1"
    tools:context="com.test.practise.AddTeamMembers">


    <android.support.design.widget.TextInputEditText
        android:id="@+id/tv_teamNames"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="@string/teamName"
        android:textColor="@android:color/background_light"
        android:textColorLink="@android:color/background_light"
        android:textSize="30sp"
        android:textStyle="bold" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.26"
        android:orientation="vertical"
        android:weightSum="1">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="0.47"
            android:gravity="center"
            android:text="Enter Player Names Below!"
            android:textColor="@android:color/background_light"
            android:textSize="24sp" />

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.16"
        android:orientation="vertical"
        android:weightSum="1">

        <EditText

            android:id="@+id/et_team_name1"
            android:layout_width="232dp"
            android:layout_height="37dp"
            android:layout_centerVertical="true"
            android:layout_gravity="center"
            android:background="@android:color/background_light"
            android:ems="10"
            android:hint="Team Name"
            android:imeOptions="actionDone"
            android:inputType="text"
            android:paddingLeft="70dp"
            android:singleLine="true"
            tools:layout_editor_absoluteX="76dp"
            tools:layout_editor_absoluteY="188dp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.16"
        android:orientation="vertical"
        android:weightSum="1">

        <EditText
            android:id="@+id/et_team_name2"
            android:layout_width="232dp"
            android:layout_height="37dp"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center"
            android:background="@android:color/background_light"
            android:ems="10"
            android:hint="Team Name"
            android:imeOptions="actionDone"
            android:inputType="text"
            android:paddingLeft="70dp"
            android:singleLine="true"
            tools:layout_editor_absoluteX="76dp"
            tools:layout_editor_absoluteY="188dp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.16"
        android:orientation="vertical"
        android:weightSum="1">

        <EditText
            android:id="@+id/et_team_name3"
            android:layout_width="232dp"
            android:layout_height="37dp"
            android:layout_gravity="center"
            android:background="@android:color/background_light"
            android:ems="10"
            android:hint="Team Name"
            android:imeOptions="actionDone"
            android:inputType="text"
            android:paddingLeft="70dp"
            android:singleLine="true"
            tools:layout_editor_absoluteX="76dp"
            tools:layout_editor_absoluteY="188dp" />

    </LinearLayout>

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

        >
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.07"
        android:orientation="vertical"
        android:weightSum="1">

        <TextView
            android:id="@+id/tv_add_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="+ Add Name"
            android:textColor="@android:color/background_light"
            android:textSize="16dp" />

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:weightSum="1">

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@color/facebookBlue"
            android:gravity="center"
            android:text="Ready to join!"
            android:textColor="@android:color/background_light" />

     </LinearLayout>


     </LinearLayout>

     </ScrollView>

Below is the AddTeamMembers Class that calls the above XML

 public class AddTeamMembers extends Fragment implements 
 View.OnClickListener 
 {

private SharedPreferences pref;
private TextView tv_teamNames, tv_add_name;
private LinearLayout mLayout;






//The below method must be overridden in order to implement a fragment - 
  this changes the lifecycle method
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, 
   Bundle savedInstanceState) {


    View view = inflater.inflate(R.layout.activity_add_team_members, 
    container, false);
    initViews(view);
    return view;


   }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
    // 0, Sets Shared pref mode to private
    pref = getActivity().getPreferences(0);
    tv_teamNames.setText(pref.getString(Constants.Team_Name, ""));

   }


   private void initViews(View view) {

    tv_teamNames = (TextView) view.findViewById(R.id.tv_teamNames);
    tv_add_name = (TextView) view.findViewById(R.id.tv_add_name);

    mLayout = (LinearLayout) view.findViewById(R.id.editTextGroupLayout);

    tv_add_name.setOnClickListener(this);



  }

  @Override
   public void onClick(View view) {

    switch (view.getId()) {

        case R.id.tv_add_name:
            createEditTextView();
            break;


      }

   }

   @TargetApi(Build.VERSION_CODES.M)
   public void createEditTextView() {

    try{

        //dynamically create new EditText when user clicks to add another 
          name
        //target user using API 22 and above (lollipop and above)
        EditText editTextView = null;
        if (android.os.Build.VERSION.SDK_INT >= 
        android.os.Build.VERSION_CODES.M) {
            editTextView = new EditText(getContext());
        }else{
            Toast.makeText(getActivity(), "App is not supported on this 
        device",
                    Toast.LENGTH_LONG).show();
        }
        editTextView.setGravity(Gravity.CENTER);

        LinearLayout.LayoutParams params = new 
        LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT, 1);

        editTextView.setLayoutParams(params);

        mLayout.addView(editTextView);



        }catch(Exception e){
        Log.d(TAG, "Failed to create new edit text");
    }
   }
   }

[![Before Button is clicked][1]][1]
[![I have clicked 2 add 3 EditText here to make it obvious whats happening]
[2]][2]


[1]: https://i.stack.imgur.com/DGoPd.png
[2]: https://i.stack.imgur.com/3LWcY.png
Display name
  • 730
  • 2
  • 8
  • 23
  • Can you clarify to which linear layout you are adding an editText? – A. Steenbergen Jul 25 '17 at 16:29
  • Sure, I am adding the EditText to the Linear Layout ID editTextGroupLayout – Display name Jul 25 '17 at 16:33
  • A few things right off the bat: You are using a LinearLayout with height match_parent in a scrollView, this does not make sense, since the LinearLayout will just have the same height as the scrollView and will never scroll. Also you are using linearLayouts with only one child, this is bad for performance, you can use a FrameLayout instead. As for the other problem, let me have a look. – A. Steenbergen Jul 25 '17 at 16:35
  • You are adding a EditText with width set to wrap_content, which, when it is empty will be zero. Change it to match_parent where you set the editText's LayoutParams. Please confirm that it works so I can write a full answer. – A. Steenbergen Jul 25 '17 at 16:38
  • Hi, yes you're correct! Thanks for the tips - how would you recommend would be the best way to match the height and width of the EditText above it and to also leave the same amount of space between each dynamic EditText, just like the I have achieved with the static EditText? – Display name Jul 25 '17 at 16:55
  • Don't worry, I have managed to accomplish my second question, although the spacing between each EditText is varying every-time a new EditText is added. But that's a problem to solve another day! – Display name Jul 25 '17 at 19:55
  • Have a look at https://stackoverflow.com/questions/15128652/how-to-add-vertical-divider-to-a-horizontal-linearlayout (android:showDividers), you can use this to add spaces between views too, just don't give your dividers a background color – A. Steenbergen Jul 26 '17 at 09:43

0 Answers0