Android - software removal

I have an activity that makes the layout programmatically out of general preference using a for loop. Text views and buttons are enclosed in a linear layout. The user can enter as many views as he wants. Now the button will be the delete button. When I click, I want to delete the line layout, button and other text images contained. How to do it?

HERE MY CODE:

package com.dirkjan.myschools; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; import android.graphics.Typeface; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity extends Activity { LinearLayout subjectLeft, subjectRight; Button addSubj; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); subjectLeft = (LinearLayout) findViewById(R.id.llSubjectLeft); subjectRight = (LinearLayout) findViewById(R.id.llSubjectRight); //Load the saved subjects SharedPreferences getSubjects = getSharedPreferences("SubjectInfo_Prefs", MODE_PRIVATE); SharedPreferences.Editor editor = getSubjects.edit(); int subjectCount = getSubjects.getInt("count", 0); if (subjectCount > 0 ){ for (int i = 1; i <= subjectCount; i++){ //Set the linear layout for each subject LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); float scale = getResources().getDisplayMetrics().density; //SET BOTTOM MARGIN float margin = 5; //RESIZE MARGIN HERE! int margs = (int) (margin * scale + 0.5f); //SET PADDING IN DP float padding = 5; //RESIZE PADDING HERE! int pads = (int) (padding * scale +0.5f); llParams.setMargins(0,0,0,margs); //SETTING THE LINEARLAYOUT PARAMS ll.setLayoutParams(llParams); ll.setPadding(pads, pads, pads, pads); //SETTING THE BACKGROUND COLOR OF THE LINEAR LAYOUT String chosenColor = getSubjects.getString("chosenColor" + i, "BLUE"); if (chosenColor.equals("Green")){ ll.setBackgroundResource(R.color.HoloGreen); }else if (chosenColor.equals("Blue")){ ll.setBackgroundResource(R.color.HoloBlue); }else if (chosenColor.equals("Gray")){ ll.setBackgroundResource(R.color.HoloGray); }else if (chosenColor.equals("Orange")){ ll.setBackgroundResource(R.color.HoloOrange); }else { ll.setBackgroundResource(R.color.HoloYellow); } //ADDING THE LAYOUT TO THE APPROPRIATE CONTAINER (LEFT OR RIGHT) if (i % 2 == 1){ subjectLeft.addView(ll); } else { subjectRight.addView(ll); } //SETTING THE SUBJECT NAME TEXTVIEW TextView SubjectName = new TextView(this); SubjectName.setText(getSubjects.getString("subjectName" + i, "Error")); SubjectName.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); SubjectName.setTextSize(22); SubjectName.setTypeface(Typeface.DEFAULT_BOLD); //SETTING THE SUBJECT NUMB TEXT VIEW TextView SubjectNumber = new TextView(this); SubjectNumber.setText(getSubjects.getString("subjectNumb" + i, "Error")); SubjectNumber.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); SubjectNumber.setTextSize(16); //Creating the divider line ImageView divider = new ImageView(this); LinearLayout.LayoutParams dividerParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 2); divider.setLayoutParams(dividerParams); divider.setBackgroundResource(R.color.Black); //Add Views into the Layout ll.addView(SubjectNumber); ll.addView(SubjectName); ll.addView(divider); } } addSubj = (Button) findViewById(R.id.buttonPlusSubject); addSubj.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent toAddSubj = new Intent(MainActivity.this, AddSubjectActivity.class); startActivity(toAddSubj); finish(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 

Please note that no identifier is assigned for each layout. This will help if there is code to identify the parent of the button’s parent (the button is in the relative layout, which is in the line layout, where the line layout should be removed by clicking the button.

+4
source share
4 answers

First find the parent layout with

 ll = (LinearLayout) findViewById(R.id.main_linearlayout); 

get child layout using

 final LinearLayout child = (LinearLayout) ll.findViewById(count); 

Now, to remove the entire layout, you can use the removeview() method as shown below

 ll.removeView(child); 

to remove only all views from a specific layout (here, for example, for a child), you can use

 child.removeAllViews(); 
+15
source

You can call view.setVisiblility(View.GONE) if you want to remove it from the layout, or view.setVisibility(View.INVISIBLE) if you just want to hide it.

+10
source

Assuming your LinearLayout ID is my_linear_layout, just do it in your onClickListener:

  findViewById(R.id.my_linear_layout).setVisibility(View.GONE); 

Be sure to include the ID in your XML:

  <LinearLayout android:id="@+id/my_linear_layout" ...> </LinearLayout> 
+4
source

First send your code ...

You can remove the child view from the parent by calling removeView (view view), for example, as follows:

 parent.removeView(child); 

Hope this helps ^^

+3
source

Source: https://habr.com/ru/post/1483130/


All Articles