How to remove a view inside a fragment
im new for android dev and fragments is really hard for me.
Basically, I am making an application that requires the user to enter their desktop (school) for 6 days a week. In addition, each fragment made a fragment for every day (that's 6 fragments), also each of these fragments has an additional item at the bottom that adds an xml layout when clicked. i figured out how to add an edittext field and two buttons (one is called data and the other is called delete) in a fragment using layout from xml file, now by clicking delete button i want this specific line to be deleted, how can i do that to do?
public class NewTimeTableMonday extends Fragment {
Context context; // context is needed to use inflater outside on create view
View view; // view needs to be passed for (find view by id)
private int unique_id = 0; // for dynamic Id allocation when New Fields are created
private final List<TextView> SUBJECT_NAME = new ArrayList<TextView>();
public NewTimeTableMonday() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_new_time_table_monday,
container, false);
context = container.getContext();
// adding two entries by default
AddNewSubjectLine(view);
AddNewSubjectLine(view);
// for button +Add Another
Button button_AddAnother = (Button) view.findViewById(R.id.AddAnother);
button_AddAnother.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AddNewSubjectLine(view);
// Adds another Subject Entry Line on the button being clicked
}
});
// for button x
Button button_crossDelete = (Button) view.findViewById(R.id.Delete);
button_crossDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
DeleteSubjectLine(v);
// Deletes that particular line when x is clicked
}
});
// For Button Done
Button button_Done = (Button) view.findViewById(R.id.Done);
button_Done.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// viewPager.setCurrentItem passes the arg0 value to getItem
// to move to page 2 when done is clicked
CreateNewTimetable.viewPager.setCurrentItem(1, true);
}
});
return view;// because OnCreatView is of type View
}
here is the addubjectline function
public void AddNewSubjectLine(View view) {
// To display another SUbject Entry Line
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View subjectLayout = inflater.inflate(R.layout.new_entry_line, null);
LinearLayout layout = (LinearLayout) view
.findViewById(R.id.MondayLayout);
layout.addView(subjectLayout);
/** Adding to list **/
// Subject Name
EditText subName = (EditText) view.findViewById(R.id.SubName);
subName.setId(unique_id);
SUBJECT_NAME.add(subName);
// Time Button
Button button_EnterTime = (Button) view.findViewById(R.id.Time);
button_EnterTime.setId(100 + unique_id);
// Delete button
Button deleteSub = (Button) view.findViewById(R.id.Delete);
deleteSub.setId(200 + unique_id);
final ScrollView scrollView = (ScrollView) view
.findViewById(R.id.scrollViewMonday);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
}, 400);
unique_id++;
}
and two used XML files
this is snippet_new_time_table_monday.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textViewmonday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/monday"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:id="@+id/linearLayoutmonday"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" >
<Button
android:id="@+id/AddAnother"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/add" />
<Button
android:id="@+id/Done"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/done" />
</LinearLayout>
<ScrollView
android:id="@+id/scrollViewMonday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textViewmonday"
android:layout_above="@+id/linearLayoutmonday"
android:layout_alignParentLeft="true" >
<LinearLayout
android:id="@+id/MondayLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginTop="50dp"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</RelativeLayout>
and xml new_line_entry
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/NewLineEntry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="17dp"
android:orientation="horizontal" >
<EditText
android:id="@+id/SubName"
android:layout_width="189dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="@string/subjectname"
android:paddingLeft="20dp" />
<Button
android:id="@+id/Time"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:layout_weight="1"
android:text="@string/time" />
<Button
android:id="@+id/Delete"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="42dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:layout_weight="1"
android:text="@string/crossdelete" />
</LinearLayout>
source to share
First of all, you must use a single XML layout and snippet implementation for one day. Just create separate instances of the snippet for each day and let the ViewPager fill it up.
I won't go into all the code, but I can definitely suggest reading Android Developers . There is also a very good video tutorial from Google on the Udacity portal.
As far as your code is concerned, as a quick fix, you can implement it like this:
Set layout and theme final
final View subjectLayout = inflater.inflate(R.layout.new_entry_line, null);
final LinearLayout layout = (LinearLayout) view
.findViewById(R.id.MondayLayout);
Add button to remove clicks to delete
deleteSub.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
layout.removeView(subjectLayout);
}
});
source to share