2
I created a layout called with a "+" button at the end so that I would like, when clicking the button, to be added a new Linearlayout below the current layout with the same content, allowing the registration of more than one item without generating new screens.
I hardly know where to begin...
From what I understand, in the Onclick event of the "+" button, I need to create a new Linearlayout and position below the layout that contains the current button after inflating it with the content of the "address" layout using Inflate. But I can’t encode that. Could someone help me?
My Layout:
<LinearLayout
style="@style/Layout_Fragment_Port"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
style="@style/Layout_Fragment_Port"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/regpet_edt_name"
style="@style/EditText"
android:hint="@string/label_name"
android:inputType="textPersonName" />
<Spinner
android:id="@+id/regpet_spn_pettype"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/regpet_spn_petbreed"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/regpet_lbl_petsize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="@string/label_petsize"
android:visibility="invisible" />
<TextView
android:id="@+id/regpet_txt_petsize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/regpet_lbl_petsize"
android:layout_toEndOf="@+id/regpet_lbl_petsize"
android:layout_toRightOf="@+id/regpet_lbl_petsize" />
<TextView
android:id="@+id/regpet_lbl_hairsize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/regpet_lbl_petsize"
android:layout_toLeftOf="@+id/regpet_txt_hairsize"
android:layout_toStartOf="@+id/regpet_txt_hairsize"
android:text="@string/label_hairsize"
android:visibility="invisible" />
<TextView
android:id="@+id/regpet_txt_hairsize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" />
<ImageButton
android:id="@+id/regpet_btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/regpet_txt_hairsize"
android:background="@color/colorPetCareLight"
android:contentDescription="@string/img_content_description"
android:src="@mipmap/ic_delete"
android:visibility="invisible" />
<ImageButton
android:id="@+id/regpet_btn_newpet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/regpet_txt_hairsize"
android:background="@color/colorPetCareLight"
android:contentDescription="@string/img_content_description"
android:src="@mipmap/ic_new" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/regpet_frame"
style="@style/Layout_Fragment_Port"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
The event of the button:
@Override
public void onClick(View v) {
LinearLayout newlayout = new LinearLayout(getContext());
newlayout.setOrientation(LinearLayout.VERTICAL);
newlayout.setLayoutParams(new ViewGroup.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
ConfigFragment.openFragment(new RegPetFragment(), newlayout.getId(), getFragmentManager(), getString(R.string.title_reg_pet));
Toast.makeText(getContext(), "clicou no botao", Toast.LENGTH_LONG).show();
}
Contents of Configfragment.openFragment:
public static void openFragment(Fragment fragment, @IdRes int containerViewId, FragmentManager manager, String tag){
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(containerViewId, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
The error message:
Can't add fragment RegPetFragment{977258d} with tag null to container view with no id
You can add the code to your layout?
– Woss