I'm new to Android and am trying to make a simple checklist app. I am testing adding textviews dynamically to the activity using an xml template for the textview. The problem is when I try to inflate more than one view it creates the textviews but only sets the text to the last view created.
Here is the code in the activity:
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.main_linear_layout);
TextView textView1 = (TextView)getLayoutInflater().inflate(R.layout.textview_template, linearLayout).findViewById(R.id.textview_template);
TextView textView2 = (TextView)getLayoutInflater().inflate(R.layout.textview_template, linearLayout).findViewById(R.id.textview_template);
TextView textView3 = (TextView)getLayoutInflater().inflate(R.layout.textview_template, linearLayout).findViewById(R.id.textview_template);
textView1.setText("Take out trash.");
textView2.setText("Wash windows.");
textView3.setText("Why won't you work?");
Here is the main layout I am trying to insert the textviews into:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/main_linear_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:clipToPadding="false"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#EEEEEE"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:elevation="2dp"
android:background="#FFFFFF">
<EditText android:id="@+id/new_task_text_edit"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/text_edit_hint"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_add"
android:onClick="addTask"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="4dp">
</LinearLayout>
</LinearLayout>
Here is the template code for the textview (textview_template.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="@+id/textview_template"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="6dp"
android:paddingBottom="6dp"
android:textColor="#000000"
android:textSize="20dp"
android:background="@drawable/item_box" />
</LinearLayout>
This is what is showing:
I've tried setting the text after each inflate but before the next inflate but the results are the same. What am I doing wrong?
Actually all your textView1
,textView2
,textView3
are pointing to the same UI, coz you are inflating with the same id and you are searching immediately.
Id's must be different, and for such thing that you are coding better use recyclerview
and adapter for it, coz above coding can be better.