0

I'm new to Android development.

I have Activity, to which I wish to add some GUI-elements.

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dial_details);
    if (android.os.Build.VERSION.SDK_INT >= 11)
        // Show the Up button in the action bar.
        getActionBar().setDisplayHomeAsUpEnabled(true);

    inflateNewRow();
    inflateNewRow();
}

private void inflateNewRow()
{
    LinearLayout thisLayout = (LinearLayout) findViewById(R.id.dial_details_layout);
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // Before edit!
    //inflater.inflate(R.layout.dialitem_details, thisLayout);
            // After edit, which works!
            inflater.inflate(R.layout.dialitem_details, null);
            thisLayout.addView(ll);
}

The first call to inflateNewRow adds the row, fine. The second call, seems to do nothing! No exception, no row is added.

I added the setVisibility call, just to make sure. It makes no difference.

It might seem strange to call inflateNewRow twice, but it's actually just for this example. It is to be called once in the onCreate, then from a menus click-event. This simple example recreates the problem though.

This is the little view that's to be added:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:ems="10"
    android:inputType="number" >

    <requestFocus />
</EditText>

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

into this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/dial_details_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".DialDetailsActivity" >

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="CheckBox" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="number" >

    <requestFocus />
</EditText>

</LinearLayout>

Any ideas why only the first row appears / is added?

kaze
  • 4,299
  • 12
  • 53
  • 74

1 Answers1

0

when you call this function once LinearLayout ll is created once but when you call again it already exists isn't it? so if you do not create ll and just write

inflater.inflate(R.layout.dialitem_details, thisLayout);

instead of

LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.dialitem_details, thisLayout);

you should get desired result

and there is no need to set visibility true for each item

Sourabh Saldi
  • 3,567
  • 6
  • 34
  • 57
  • Thanks for your answer. I tried your suggestion. Unfortunately it made no difference. I've updated the code in the question to your suggestion. – kaze Feb 10 '13 at 17:32
  • @kaze I misunderstood your requirement alonf wd the changes you will also need to add child to your view group here is a sample http://stackoverflow.com/questions/6661745/dynamically-adding-a-child-to-linearlayout-with-getting-each-childs-position – Sourabh Saldi Feb 10 '13 at 17:41
  • even http://stackoverflow.com/questions/6661261/how-to-add-dynamic-content-to-lineary-layout-programmatically-in-my-case should be helpful – Sourabh Saldi Feb 10 '13 at 17:46
  • Thank you. Your second link put me in the right direction to solve the problem. I'll mark your answer as correct, and update the code example so that it works! – kaze Feb 10 '13 at 18:05