Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android dynamically add EditText in Linear Layout

I'm stuck trying to add an editText field dynamically under my existing editText fields using a button. Currently pressing the button does nothing. Here is my code:

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d4cfcd">
tools:context="com.zdowning.decisionmaker.MainActivity">


<LinearLayout
    android:id="@+id/linearLayoutMain"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:gravity="center"
        android:hint="Enter Your Question Here"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp"/>

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

    <EditText
        android:id="@+id/editText3"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:gravity="center"
        android:hint="Enter Answer #2" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:gravity="center"
        android:hint="Enter Answer #1" />

    <EditText
        android:id="@+id/editText4"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:gravity="center"
        android:hint="Enter Answer #3" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">

        <Button
            android:id="@+id/add_button"
            style="?android:attr/borderlessButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="+"
            android:textColor="@android:color/background_light"
            android:textSize="18sp"/>

        <View
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1" />

        <Button
            android:id="@+id/remove_button"
            style="?android:attr/borderlessButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="-"
            android:textColor="@android:color/background_light"
            android:textSize="18sp"
            android:layout_margin="10dp"/>
    </LinearLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    <Button
        android:id="@+id/button"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:text="DECIDE!"
        android:textColor="@android:color/background_light"
        android:textSize="18sp"
        android:layout_centerInParent="true" />
    </RelativeLayout>

    <TextView
        android:id="@+id/textView7"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_margin="20dp"
        android:textColor="@android:color/black"
        android:textSize="36sp" />

</LinearLayout>

Java:

package com.zdowning.decisionmaker;

import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ScrollView;

import java.lang.*;

import static com.zdowning.decisionmaker.R.layout.activity_main;

public class MainActivity extends AppCompatActivity {
public int numberOfLines = 3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(activity_main);
    final Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             getAnswer();
        }
    });

    final Button Add_button = (Button) findViewById(R.id.add_button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Add_Line();
        }
    });
}


public void Add_Line() {
    LinearLayout ll = (LinearLayout) 
    findViewById(R.id.linearLayoutDecisions);

    // add edittext
    EditText et = new EditText(this);
    et.setId(numberOfLines + 1);
    ll.addView(et);
    numberOfLines++;
}


public void getAnswer() {
    String[] options = new String[3];

    EditText text = (EditText)findViewById(R.id.editText2);
    options[0] = text.getText().toString();

    text = (EditText)findViewById(R.id.editText3);
    options[1] = text.getText().toString();

    text = (EditText)findViewById(R.id.editText4);
    options[2] = text.getText().toString();

    int number = (int)(Math.random() * 3);
    String answer = options[number];

    TextView answerBox = (TextView)findViewById(R.id.textView7);
    answerBox.setText(answer);
}
}

Any advice would be a huge help as I'm brand new to android studio. Coding what is above took me hours.

like image 681
JamaicanMeCode Avatar asked Apr 27 '17 19:04

JamaicanMeCode


People also ask

How to add a textview to a linearlayout dynamically in Android?

How to add a TextView to a LinearLayout dynamically in Android? This example demonstrates about How to add a TextView to a LinearLayout dynamically in Android Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

How do I create a linear layout in Android?

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout).

What is dynamic layout in Android?

Dynamic Layout containing TextViews, CheckBoxes, RadioButtons, EditTexts. Android provides us with almost all the possible attributes that we use in our XML for our View Types.

How to add and remove views in Android dynamically?

This example demonstrates how to Add and Remove Views in Android Dynamically. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 2 − Add the following code to res/layout/field.xml. Step 3 − Add the ...


2 Answers

Change your Add_line() method to :

 public void Add_Line() {
    LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayoutDecisions);
    // add edittext
    EditText et = new EditText(this);
    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    et.setLayoutParams(p);
    et.setText("Text");
    et.setId(numberOfLines + 1);
    ll.addView(et);
    numberOfLines++;
}

add the LayoutParams to the Edittext

like image 70
rafsanahmad007 Avatar answered Nov 15 '22 22:11

rafsanahmad007


This help someone who want to add dynamic TextInputLayout to linear layout.

 LinearLayout linearLayoutSubParent = new LinearLayout(this);
        linearLayoutSubParent.setOrientation(LinearLayout.HORIZONTAL);
        linearLayoutSubParent.setWeightSum(100f); // you can also add more widget by providing weight

        LinearLayout.LayoutParams linearLayoutSubParentParams =
                new LinearLayout.LayoutParams(0,
                        LinearLayout.LayoutParams.WRAP_CONTENT, 100f);
        linearLayoutSubParent.setLayoutParams(linearLayoutSubParentParams);
        linearLayoutSubParent.setPadding(0, 0, 0, 0);

        // Add TextInputLayout to parent layout first
        TextInputLayout textInputLayout = new TextInputLayout(this);
        LinearLayout.LayoutParams textInputLayoutParams = new LinearLayout.LayoutParams(0,
                LinearLayout.LayoutParams.WRAP_CONTENT, 100f);


        textInputLayout.setLayoutParams(textInputLayoutParams);
        textInputLayout.setHintTextAppearance(R.style.TextSizeHint);
        linearLayoutSubParent.addView(textInputLayout);

        // Add EditText control to TextInputLayout
        final EditText editText = new EditText(this);
        LinearLayout.LayoutParams editTextParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        editTextParams.setMargins(0, 10, 0, 0);
        editText.setLayoutParams(editTextParams);

        editText.setTextSize(TypedValue.COMPLEX_UNIT_PX,
                getResources().getDimension(R.dimen.text_size));
        editText.setHint("Enter value");
        editText.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS);
        editText.setEnabled(false);

        textInputLayout.addView(editText, editTextParams);


        linearLayoutParent.addView(linearLayoutSubParent);

 <style name="TextSizeHint" parent="TextAppearance.Design.Hint">
        <item name="android:textSize">12dp</item>
 </style>

 <dimen name="text_size">20dp</dimen>
like image 29
jessica Avatar answered Nov 15 '22 20:11

jessica