samuel kathungu
samuel kathungu

Reputation: 1

Adding views in Android from Java source

I'm working on an app that requires the user to either increase or decrease the number of edit texts during application run-time (eg, in the default Android contacts app).

Someone please assist with some Java source code

Upvotes: 0

Views: 38

Answers (1)

Che Jami
Che Jami

Reputation: 5151

Assume we have a container defined in an xml layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container" android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

Now we can do this:

ViewGroup container = (ViewGroup) findViewById(R.id.container);
EditText editText = new EditText(this);
container.addView(editText);

Note: The container doesn't even have to be defined via xml. We can also create the container dynamically and use setContentView. It also does not have to be the root ViewGroup of your layout.

Upvotes: 3

Related Questions