• Have Any Question?
  • contact@eitworld.com

write an application for add the edit text dynamically when user click on the button





n this application, we have added the dynamically edit text on the click event of the button, the xml file is as follows

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:id=”@+id/rl”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”.MainActivity” ><Button
android:id=”@+id/btnAdd”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentLeft=”true”
android:layout_below=”@+id/tvMessage”
android:layout_marginLeft=”22dp”
android:layout_marginTop=”48dp”
android:onClick=”xmlClick”
android:text=”Add EditText Dynamically” />

<TextView
android:id=”@+id/tvMessage”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”82dp”
android:text=”click button to add edit text” />

</RelativeLayout>

In this layout file, we have used the relative layout and provide an id so that we can use this into the java file and here we have defined a button for performing the action and used a text view to show the message.
The java file is as follows:

package com.example.buttondemo;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {
Button add;
RelativeLayout layout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

add = (Button) findViewById(R.id.btnAdd);
layout = (RelativeLayout) findViewById(R.id.rl);

add.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

EditText et = new EditText(getApplicationContext());
et.setText(“Dynamically added”);

layout.addView(et);
et.setBackgroundColor(Color.BLACK);
et.setTextColor(Color.WHITE);

}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

In the java file we have used the following pakages:

android

After that we have declared two variables of Button and RelativeLayout type as follows:
android
Now we have to register these inside the onCreate() method as follows:
android
Now we have to perform action on the add button so we have used the setOnClickListner() method. Inside this method we have created the object of the EditText and pass the application context; here we cannot pass this operator to get the current context. After that we have set the text for the edit text means when it will show on screen then what is the default text appears inside this. After that we have set the background color of the edit text by using the setBackgroundColor() and pass the color name as the parameter; after that we have set the text color of the text for the edit text by using the setTextColor() method. After that we have called the addView() method which takes one argument, here we have passed the object of the EditText. Now when we will launch the application and when we click on the button then the edit text will be added to the activity dynamically.
The output is as follows:
android
When launched
android
when clicked on button



Real Time Web Analytics