0

I have a xml file named socio_form_organization.xml that have two layout's. This xml file is inflated in a layout named content how we can see by the code below

socio_form_organization.xml

  <LinearLayout     
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/sfo_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/background_tile"
    android:orientation="vertical" >

  <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/sfo_layout2"
      android:layout_width="wrap_content"
      android:layout_height="51dp"
      android:background="@drawable/background_tile"
      android:baselineAligned="true"
      android:orientation="horizontal">

      <Button
          android:id="@+id/sfo_btOrganization"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginBottom="2dp"
          android:layout_marginLeft="5dp"
          android:layout_marginRight="5dp"
          android:layout_marginTop="2dp"
          android:hint="@string/sfo_btOrganization" />

      <EditText
          android:id="@+id/sfo_etEmpresa"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginBottom="2dp"
          android:layout_marginTop="2dp"
          android:layout_weight="0.14"
          android:hint="@string/sfo_etEmpresa"
          android:inputType="textMultiLine"
          android:scrollHorizontally="false" />


      <ImageButton
          android:id="@+id/sfo_ivRemove"
          style="?android:attr/buttonStyleSmall"
          android:layout_height="60dp"
          android:layout_width="50dp"
          android:layout_marginTop="2dp"
          android:layout_marginBottom="2dp"
          android:layout_marginRight="5dp"
          android:onClick="onClick"
          android:background="@android:color/transparent"
          android:src="@drawable/tb_no_delete" />

  </LinearLayout>

    <EditText
        android:id="@+id/sfo_etTitulo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="65dp"
        android:layout_marginRight="55dp"
        android:layout_marginTop="2dp"
        android:layout_weight="0.14"
        android:hint="@string/sfo_etTitulo"
        android:inputType="textMultiLine"
        android:scrollHorizontally="false"  />

</LinearLayout>

Layout where is inflated the socio_form_organization.xml

 <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/se_contentOrganization"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2dp"
        android:background="@drawable/background_tile"
        android:orientation="vertical" >

        <include
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="70dp"
            layout="@layout/socio_form_organization" />

In my class i do the inflation

    LayoutInflater inflaterOrganization=
    (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout organization =    
    (LinearLayout)inflaterOrganization.inflate(R.layout.socio_form_organization, null);
    LinearLayout lLayoutOrganization;
    lLayoutOrganization = (LinearLayout)findViewById(R.id.se_contentOrganization);
    lLayoutOrganization.addView(organization);

But now á need to remove the two layout inside the content when i click in ImageButton but i can't.

I try with

    View toRemove = (View) view.getParent();
    ViewGroup vg = (ViewGroup)findViewById(R.id.se_content);
    vg.removeView(toRemove);

But only works when I have one layout. In this case I have two layout and don't work.

I do not know if I could make you understand. Anyone can help me? Thanks

Ricardo Filipe
  • 435
  • 4
  • 8
  • 24
  • Are you getting any error? If so, please post the logcats. – Sam Jul 17 '12 at 16:36
  • Hy Sam.I don't have errors. Simply when i click in the imagebutton to remove the content inflated nothing happens and the content is not removed. – Ricardo Filipe Jul 17 '12 at 16:43

1 Answers1

0

You are referencing two ids that do not exist in the layouts you provided:

lLayoutOrganization = (LinearLayout)findViewById(R.id.se_contentOrganization);

Where is se_contentOrganization? Did you mean se_contentEmail?

And when you try to remove the views:

ViewGroup vg = (ViewGroup)findViewById(R.id.se_content);

Where is se_content? Or did you mean se_contentEmail? Or sfo_layout?

Addition

Try this:

LinearLayout parent = (LinearLayout) findViewById(R.id.se_contentOrganization);
LinearLayout child = (LinearLayout) findViewById(R.id.sfo_layout);
parent.removeView(child);

If you are using socio_form_organization.xml multiple times then try:

LinearLayout child = (LinearLayout) view.getParent().getParent(); 
LinearLayout parent = (LinearLayout) child.getParent(); 
parent.removeView(child);
Sam
  • 86,580
  • 20
  • 181
  • 179
  • Sorry the mistakes sam. Already upgraded. Is not se_contentEmail but yes se_contentOrganization. – Ricardo Filipe Jul 17 '12 at 16:46
  • Ok, which LinearLayout are you trying to remove? `se_contentOranization`? – Sam Jul 17 '12 at 16:47
  • I need to remove thw two linearLayouts. android:id="@+id/sfo_layout2" and android:id="@+id/sfo_layout2" . When i try when click in the image button View toRemove = (View) view.getParent(); ViewGroup vg = (ViewGroup)findViewById(R.id.se_contentOrganization); vg.removeView(toRemove); don't work – Ricardo Filipe Jul 17 '12 at 16:49
  • (Hehe, I assume you meant `sfo_layout` and `sfo_layout2`.) My latest answer should do that, let me know if it works for you. – Sam Jul 17 '12 at 16:52
  • Sam if you can see my answer below I apreciate :) :) – Ricardo Filipe Jul 18 '12 at 09:15
  • Sam I'm succeed. I have to remove the two layout's separate and not simultaneously. View toRemoveOrganization = (View) view.getParent(); View toRemoveOrganization2 = (View) toRemoveOrganization.getParent(); ViewGroup teste = (ViewGroup)findViewById(R.id.se_contentOrganization); teste.removeView(toRemoveOrganization); teste.removeView(toRemoveOrganization2); – Ricardo Filipe Jul 18 '12 at 09:41