'how to remove view inside fragment
im new to android dev and fragments have been giving me a hard time.
Basically im making an app which requires the user to enter his time table (school) for 6 days a week.so ive made a fragment for each day (that is 6 fragments),also each of these fragments has an add subject button at the bottom which adds an xml layout when clicked. i have figured out how to add an edittext field and two buttons(one called data and the other called delete) , in the fragment using the layout from a xml file , now on clicking the delete button , i want that particular line to be deleted. how do i go about doing this ?
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 addsubjectline 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 the two XML files used
this is fragment_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>
Solution 1:[1]
if you want a view to not be shown, you can make it invisible, or gone. You can also delete it from a view
make it just not show
view.setVisibility(View.GONE);
view.setVIsibility(View.VISIBLE);
really delete it
parent = view.getParent();
parent.removeView(view);
Solution 2:[2]
First of all you should use single XML layout and Fragment implementation for one day. Just create separate instances of the Fragment for each day and let ViewPager to populate it.
I will not analyse entire code but I can definitely suggest to read Android Developers tutorials. There is also very good video tutorial from Google on Udacity portal.
Regarding your code, as a quick solution, you can implement it like this:
Set layout and subjectLayout final
final View subjectLayout = inflater.inflate(R.layout.new_entry_line, null);
final LinearLayout layout = (LinearLayout) view
.findViewById(R.id.MondayLayout);
Add click listener to delete button
deleteSub.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
layout.removeView(subjectLayout);
}
});
Solution 3:[3]
This is what works for me....
deleteSub.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
layout.setVisibility(GONE);
}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Martin |
| Solution 2 | Damian Petla |
| Solution 3 | Ola Abe |
