Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - removing layout programmatically

I have an activity that makes a layout programmatically from a Shared Preference using a for loop. The text views and buttons are enclosed in a linear layout. The user can input as many views as he wants. Now, the button will be a delete button. When pressed, I want to delete the linear layout the button and the other textviews are contained. How do I do this?

HERE IS 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 do take note that no id is assigned for each layout. It would help if there is a code to identify the parent of the parent of the button (The button is in a relative layout, which is in a linear layout where the linear layout must be removed by clicking the button.

like image 363
DirkJan Avatar asked May 28 '13 11:05

DirkJan


3 Answers

First find your parent layout using

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

get the child layout using

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

now to remove the whole layout you can use removeview() method as below

ll.removeView(child);

to only remove all views from the particular layout(here for eg. child) you can use

child.removeAllViews();
like image 68
karan Avatar answered Sep 24 '22 03:09

karan


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.

like image 26
vivek Avatar answered Sep 22 '22 03:09

vivek


You can remove a Child View from a parent by calling removeView(View view), for example like this :

parent.removeView(child);
like image 21
reidzeibel Avatar answered Sep 25 '22 03:09

reidzeibel