Dynamically add element to layout

Ricardo di Stefano

I am quite new to developing apps. Still I would have thought that this is a basic action, so if there is already a solved thread I would be OK with the link. But since I am searching for over 2 hours for this I am asking anyway:

I want to dynamically add an element to my layout every time the user clicks a button.

By now I have this:

XML (R.layout.game.xml)

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/submit_choice" 
        android:onClick="submitChoice"/>
    </LinearLayout>

Java

  public void submitChoice(View view)
    {
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText("text");

        LinearLayout ll = new LinearLayout(this);

        ll.addView(View.inflate(ll.getContext(), R.layout.game, null));
        ll.addView(textView);
        setContentView(ll);
    }

Since the XML file does not change, it only works once.

So how can I add a second text when the user clicks the button a second time (without changing the XML file)? Examples are appreciated.

Adinia

The problem comes from this line, that recreate the whole layout every time:

LinearLayout ll = new LinearLayout(this);

You should define it and setContentView(ll) outside the submitChoice function. Then on click only create and add the textView , then call ll.invalidate(); to see the changes.

Something like:

LinearLayout ll;

protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.game);
            ll = (LinearLayout) findViewById(R.id.ll_game);    
        }

// More code...

public void submitChoice(View view) {
            TextView textView = new TextView(this);
            textView.setTextSize(40);
            textView.setText("text");

            ll.addView(textView);
            ll.invalidate();
        }

where ll_game is the id you have to set in xml for your LinearLayout.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how can i add and remove element dynamically from linear layout?

From Dev

How to add relative Layout below some element dynamically

From Dev

Dynamically add layout to another layout

From Dev

How to add the layout dynamically

From Dev

Dynamically add QWebEngineView to layout

From Dev

JAXB add element dynamically

From Dev

Dynamically add/remove element

From Dev

Add a TextView dynamically in a layout ANDROID

From Dev

Add a layout dynamically, to overlapp the other layout

From Dev

Dynamically add Layout after some layout?

From Dev

Add class to dynamically added element

From Dev

Dynamically add specific attr to element

From Dev

Dynamically add element to XML file

From Dev

Add element on top of image dynamically

From Dev

Dynamically add element in JS to XUL

From Dev

Dynamically add element using angularJS

From Dev

Add element on top of image dynamically

From Dev

dynamically add attribute to child element

From Dev

How to dynamically add ImageView to layout using Adapter

From Dev

Dynamically add content to layout in MVC4

From Dev

How to add this type of layout dynamically (android)

From Dev

How to add 2 fragments dynamically in a single layout?

From Dev

Add Buttons to Relative Layout Dynamically or Extended Linear Layout android

From Dev

Add Buttons to Relative Layout Dynamically or Extended Linear Layout android

From Dev

Add element to horizontal layout without fitting to container

From Dev

How to add CSS class dynamically to html element

From Dev

How to add dynamically extended element with Polymer?

From Dev

Create div element and dynamically add child elements on it

From Dev

Add data to dynamically created element and bind event

Related Related

HotTag

Archive