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?
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
instead of
you should get desired result
and there is no need to set visibility true for each item