ListView custom items graphically incorrect











up vote
0
down vote

favorite
1












I have defined a custom item in XML for my ListView and I want every one of them to have five fields: three already visible and the other two (telephone and Delete, see below) gone until a click action is performed. The execution works, but graphically it's not what I was expecting.



The item is defined as follows:



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView
android:id="@+id/txt.name"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginStart="68dp"
android:layout_marginTop="16dp"
android:gravity="center_horizontal|center_vertical"
android:text="Name"
android:textAlignment="center"
android:textSize="18sp" />

<TextView
android:id="@+id/txt.surname"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="68dp"
android:gravity="center_horizontal|center_vertical"
android:layout_toEndOf="@id/txt.name"
android:text="Surname"
android:textAlignment="center"
android:textColor="@android:color/holo_blue_bright"
android:textSize="18sp"
android:textStyle="bold" />

<TextView
android:id="@+id/txt.id"
android:layout_width="90dp"
android:layout_height="30dp"
android:layout_marginStart="144dp"
android:layout_marginTop="64dp"
android:gravity="center_horizontal|center_vertical"
android:text="ID"
android:textAlignment="center"
android:textSize="18sp" />

<TextView
android:id="@+id/txt.tel"
android:layout_width="90dp"
android:layout_height="30dp"
android:layout_marginTop="64dp"
android:layout_marginEnd="40dp"
android:layout_toEndOf="@id/txt.id"
android:gravity="center_horizontal|center_vertical"
android:text="Tel"
android:visibility="gone"
android:textAlignment="center"
android:textSize="18sp" />

<Button
android:id="@+id/btn.delete"
android:layout_width="70dp"
android:layout_height="38dp"
android:layout_below="@id/txt.id"
android:layout_marginBottom="368dp"
android:layout_toEndOf="@id/txt.id"
android:visibility="gone"
android:text="Delete"
android:textAllCaps="false" />

</RelativeLayout>


and it gets properly inflated into the ListView, but when I click on the items, the txt.tel TextView and the btn.delete Button should appear on screen. The code I wrote is



ListView list= findViewById(R.id.advanced_list);
final StudentAdapter adapter= new StudentAdapter(
this,
R.layout.custom_list_item,
m.getStudents()
//this works fine, previous code omitted
);

list.setAdapter(adapter);

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

TextView tel= view.findViewById(R.id.txt_tel);
Button delete= view.findViewById(R.id.btn_delete);

int visible= tel.getVisibility();

//doesn't work
switch (visible){
case (View.VISIBLE):
tel.setVisibility(View.GONE);
delete.setVisibility(View.GONE);
break;
case (View.GONE):
tel.setVisibility(View.VISIBLE);
delete.setVisibility(View.VISIBLE);
break;
}
}
});


Graphically, what happens is this.



enter image description here





Moreover, the item doesn't respond to other clicks anymore so I can't get them back "shrinked" with a second tap. Hope I've been clear and someone can fix both my problems.










share|improve this question


























    up vote
    0
    down vote

    favorite
    1












    I have defined a custom item in XML for my ListView and I want every one of them to have five fields: three already visible and the other two (telephone and Delete, see below) gone until a click action is performed. The execution works, but graphically it's not what I was expecting.



    The item is defined as follows:



    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
    android:id="@+id/txt.name"
    android:layout_width="120dp"
    android:layout_height="40dp"
    android:layout_marginStart="68dp"
    android:layout_marginTop="16dp"
    android:gravity="center_horizontal|center_vertical"
    android:text="Name"
    android:textAlignment="center"
    android:textSize="18sp" />

    <TextView
    android:id="@+id/txt.surname"
    android:layout_width="120dp"
    android:layout_height="40dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="68dp"
    android:gravity="center_horizontal|center_vertical"
    android:layout_toEndOf="@id/txt.name"
    android:text="Surname"
    android:textAlignment="center"
    android:textColor="@android:color/holo_blue_bright"
    android:textSize="18sp"
    android:textStyle="bold" />

    <TextView
    android:id="@+id/txt.id"
    android:layout_width="90dp"
    android:layout_height="30dp"
    android:layout_marginStart="144dp"
    android:layout_marginTop="64dp"
    android:gravity="center_horizontal|center_vertical"
    android:text="ID"
    android:textAlignment="center"
    android:textSize="18sp" />

    <TextView
    android:id="@+id/txt.tel"
    android:layout_width="90dp"
    android:layout_height="30dp"
    android:layout_marginTop="64dp"
    android:layout_marginEnd="40dp"
    android:layout_toEndOf="@id/txt.id"
    android:gravity="center_horizontal|center_vertical"
    android:text="Tel"
    android:visibility="gone"
    android:textAlignment="center"
    android:textSize="18sp" />

    <Button
    android:id="@+id/btn.delete"
    android:layout_width="70dp"
    android:layout_height="38dp"
    android:layout_below="@id/txt.id"
    android:layout_marginBottom="368dp"
    android:layout_toEndOf="@id/txt.id"
    android:visibility="gone"
    android:text="Delete"
    android:textAllCaps="false" />

    </RelativeLayout>


    and it gets properly inflated into the ListView, but when I click on the items, the txt.tel TextView and the btn.delete Button should appear on screen. The code I wrote is



    ListView list= findViewById(R.id.advanced_list);
    final StudentAdapter adapter= new StudentAdapter(
    this,
    R.layout.custom_list_item,
    m.getStudents()
    //this works fine, previous code omitted
    );

    list.setAdapter(adapter);

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    TextView tel= view.findViewById(R.id.txt_tel);
    Button delete= view.findViewById(R.id.btn_delete);

    int visible= tel.getVisibility();

    //doesn't work
    switch (visible){
    case (View.VISIBLE):
    tel.setVisibility(View.GONE);
    delete.setVisibility(View.GONE);
    break;
    case (View.GONE):
    tel.setVisibility(View.VISIBLE);
    delete.setVisibility(View.VISIBLE);
    break;
    }
    }
    });


    Graphically, what happens is this.



    enter image description here





    Moreover, the item doesn't respond to other clicks anymore so I can't get them back "shrinked" with a second tap. Hope I've been clear and someone can fix both my problems.










    share|improve this question
























      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      I have defined a custom item in XML for my ListView and I want every one of them to have five fields: three already visible and the other two (telephone and Delete, see below) gone until a click action is performed. The execution works, but graphically it's not what I was expecting.



      The item is defined as follows:



      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent">


      <TextView
      android:id="@+id/txt.name"
      android:layout_width="120dp"
      android:layout_height="40dp"
      android:layout_marginStart="68dp"
      android:layout_marginTop="16dp"
      android:gravity="center_horizontal|center_vertical"
      android:text="Name"
      android:textAlignment="center"
      android:textSize="18sp" />

      <TextView
      android:id="@+id/txt.surname"
      android:layout_width="120dp"
      android:layout_height="40dp"
      android:layout_marginTop="16dp"
      android:layout_marginEnd="68dp"
      android:gravity="center_horizontal|center_vertical"
      android:layout_toEndOf="@id/txt.name"
      android:text="Surname"
      android:textAlignment="center"
      android:textColor="@android:color/holo_blue_bright"
      android:textSize="18sp"
      android:textStyle="bold" />

      <TextView
      android:id="@+id/txt.id"
      android:layout_width="90dp"
      android:layout_height="30dp"
      android:layout_marginStart="144dp"
      android:layout_marginTop="64dp"
      android:gravity="center_horizontal|center_vertical"
      android:text="ID"
      android:textAlignment="center"
      android:textSize="18sp" />

      <TextView
      android:id="@+id/txt.tel"
      android:layout_width="90dp"
      android:layout_height="30dp"
      android:layout_marginTop="64dp"
      android:layout_marginEnd="40dp"
      android:layout_toEndOf="@id/txt.id"
      android:gravity="center_horizontal|center_vertical"
      android:text="Tel"
      android:visibility="gone"
      android:textAlignment="center"
      android:textSize="18sp" />

      <Button
      android:id="@+id/btn.delete"
      android:layout_width="70dp"
      android:layout_height="38dp"
      android:layout_below="@id/txt.id"
      android:layout_marginBottom="368dp"
      android:layout_toEndOf="@id/txt.id"
      android:visibility="gone"
      android:text="Delete"
      android:textAllCaps="false" />

      </RelativeLayout>


      and it gets properly inflated into the ListView, but when I click on the items, the txt.tel TextView and the btn.delete Button should appear on screen. The code I wrote is



      ListView list= findViewById(R.id.advanced_list);
      final StudentAdapter adapter= new StudentAdapter(
      this,
      R.layout.custom_list_item,
      m.getStudents()
      //this works fine, previous code omitted
      );

      list.setAdapter(adapter);

      list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

      TextView tel= view.findViewById(R.id.txt_tel);
      Button delete= view.findViewById(R.id.btn_delete);

      int visible= tel.getVisibility();

      //doesn't work
      switch (visible){
      case (View.VISIBLE):
      tel.setVisibility(View.GONE);
      delete.setVisibility(View.GONE);
      break;
      case (View.GONE):
      tel.setVisibility(View.VISIBLE);
      delete.setVisibility(View.VISIBLE);
      break;
      }
      }
      });


      Graphically, what happens is this.



      enter image description here





      Moreover, the item doesn't respond to other clicks anymore so I can't get them back "shrinked" with a second tap. Hope I've been clear and someone can fix both my problems.










      share|improve this question













      I have defined a custom item in XML for my ListView and I want every one of them to have five fields: three already visible and the other two (telephone and Delete, see below) gone until a click action is performed. The execution works, but graphically it's not what I was expecting.



      The item is defined as follows:



      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent">


      <TextView
      android:id="@+id/txt.name"
      android:layout_width="120dp"
      android:layout_height="40dp"
      android:layout_marginStart="68dp"
      android:layout_marginTop="16dp"
      android:gravity="center_horizontal|center_vertical"
      android:text="Name"
      android:textAlignment="center"
      android:textSize="18sp" />

      <TextView
      android:id="@+id/txt.surname"
      android:layout_width="120dp"
      android:layout_height="40dp"
      android:layout_marginTop="16dp"
      android:layout_marginEnd="68dp"
      android:gravity="center_horizontal|center_vertical"
      android:layout_toEndOf="@id/txt.name"
      android:text="Surname"
      android:textAlignment="center"
      android:textColor="@android:color/holo_blue_bright"
      android:textSize="18sp"
      android:textStyle="bold" />

      <TextView
      android:id="@+id/txt.id"
      android:layout_width="90dp"
      android:layout_height="30dp"
      android:layout_marginStart="144dp"
      android:layout_marginTop="64dp"
      android:gravity="center_horizontal|center_vertical"
      android:text="ID"
      android:textAlignment="center"
      android:textSize="18sp" />

      <TextView
      android:id="@+id/txt.tel"
      android:layout_width="90dp"
      android:layout_height="30dp"
      android:layout_marginTop="64dp"
      android:layout_marginEnd="40dp"
      android:layout_toEndOf="@id/txt.id"
      android:gravity="center_horizontal|center_vertical"
      android:text="Tel"
      android:visibility="gone"
      android:textAlignment="center"
      android:textSize="18sp" />

      <Button
      android:id="@+id/btn.delete"
      android:layout_width="70dp"
      android:layout_height="38dp"
      android:layout_below="@id/txt.id"
      android:layout_marginBottom="368dp"
      android:layout_toEndOf="@id/txt.id"
      android:visibility="gone"
      android:text="Delete"
      android:textAllCaps="false" />

      </RelativeLayout>


      and it gets properly inflated into the ListView, but when I click on the items, the txt.tel TextView and the btn.delete Button should appear on screen. The code I wrote is



      ListView list= findViewById(R.id.advanced_list);
      final StudentAdapter adapter= new StudentAdapter(
      this,
      R.layout.custom_list_item,
      m.getStudents()
      //this works fine, previous code omitted
      );

      list.setAdapter(adapter);

      list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

      TextView tel= view.findViewById(R.id.txt_tel);
      Button delete= view.findViewById(R.id.btn_delete);

      int visible= tel.getVisibility();

      //doesn't work
      switch (visible){
      case (View.VISIBLE):
      tel.setVisibility(View.GONE);
      delete.setVisibility(View.GONE);
      break;
      case (View.GONE):
      tel.setVisibility(View.VISIBLE);
      delete.setVisibility(View.VISIBLE);
      break;
      }
      }
      });


      Graphically, what happens is this.



      enter image description here





      Moreover, the item doesn't respond to other clicks anymore so I can't get them back "shrinked" with a second tap. Hope I've been clear and someone can fix both my problems.







      android android-studio






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 at 17:01









      davide m.

      306




      306
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted











          The execution works, but graphically it's not what I was expecting.




          You set an attribute android:layout_marginBottom="368dp" for the Button, that's why the list item gets so huge.




          Moreover, the item doesn't respond to other clicks anymore so I can't get them back "shrinked" with a second tap




          This happens because the expanded item contains a (visible) Button: if you leave out the Button, then the item is "shrinkable". See for example this post on possible workarounds



          Please note also that you're likely to encounter problems when scrolling because you change the ListView items "from outside" (just test it with a large set of students). ListView items should only be changed in getView() and based on the current data for each position. So you should keep track of the expanded state of the rows in the Adapter, e.g. using a List<Boolean> or better (for performance) a SparseBooleanArray, and onClick() change the corresponding data and call notifyDatasetChanged()






          share|improve this answer























          • First, i really don't know the origin of that marginBottom... Sure must have been a design mistake. Second, thanks for the useful resource and for resolving my issue!
            – davide m.
            Nov 22 at 22:28


















          up vote
          0
          down vote













          Change the root layout's height attribute from "match_parent" to "wrap_content"



          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"> ... </RelativeLayout>





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435522%2flistview-custom-items-graphically-incorrect%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted











            The execution works, but graphically it's not what I was expecting.




            You set an attribute android:layout_marginBottom="368dp" for the Button, that's why the list item gets so huge.




            Moreover, the item doesn't respond to other clicks anymore so I can't get them back "shrinked" with a second tap




            This happens because the expanded item contains a (visible) Button: if you leave out the Button, then the item is "shrinkable". See for example this post on possible workarounds



            Please note also that you're likely to encounter problems when scrolling because you change the ListView items "from outside" (just test it with a large set of students). ListView items should only be changed in getView() and based on the current data for each position. So you should keep track of the expanded state of the rows in the Adapter, e.g. using a List<Boolean> or better (for performance) a SparseBooleanArray, and onClick() change the corresponding data and call notifyDatasetChanged()






            share|improve this answer























            • First, i really don't know the origin of that marginBottom... Sure must have been a design mistake. Second, thanks for the useful resource and for resolving my issue!
              – davide m.
              Nov 22 at 22:28















            up vote
            1
            down vote



            accepted











            The execution works, but graphically it's not what I was expecting.




            You set an attribute android:layout_marginBottom="368dp" for the Button, that's why the list item gets so huge.




            Moreover, the item doesn't respond to other clicks anymore so I can't get them back "shrinked" with a second tap




            This happens because the expanded item contains a (visible) Button: if you leave out the Button, then the item is "shrinkable". See for example this post on possible workarounds



            Please note also that you're likely to encounter problems when scrolling because you change the ListView items "from outside" (just test it with a large set of students). ListView items should only be changed in getView() and based on the current data for each position. So you should keep track of the expanded state of the rows in the Adapter, e.g. using a List<Boolean> or better (for performance) a SparseBooleanArray, and onClick() change the corresponding data and call notifyDatasetChanged()






            share|improve this answer























            • First, i really don't know the origin of that marginBottom... Sure must have been a design mistake. Second, thanks for the useful resource and for resolving my issue!
              – davide m.
              Nov 22 at 22:28













            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted







            The execution works, but graphically it's not what I was expecting.




            You set an attribute android:layout_marginBottom="368dp" for the Button, that's why the list item gets so huge.




            Moreover, the item doesn't respond to other clicks anymore so I can't get them back "shrinked" with a second tap




            This happens because the expanded item contains a (visible) Button: if you leave out the Button, then the item is "shrinkable". See for example this post on possible workarounds



            Please note also that you're likely to encounter problems when scrolling because you change the ListView items "from outside" (just test it with a large set of students). ListView items should only be changed in getView() and based on the current data for each position. So you should keep track of the expanded state of the rows in the Adapter, e.g. using a List<Boolean> or better (for performance) a SparseBooleanArray, and onClick() change the corresponding data and call notifyDatasetChanged()






            share|improve this answer















            The execution works, but graphically it's not what I was expecting.




            You set an attribute android:layout_marginBottom="368dp" for the Button, that's why the list item gets so huge.




            Moreover, the item doesn't respond to other clicks anymore so I can't get them back "shrinked" with a second tap




            This happens because the expanded item contains a (visible) Button: if you leave out the Button, then the item is "shrinkable". See for example this post on possible workarounds



            Please note also that you're likely to encounter problems when scrolling because you change the ListView items "from outside" (just test it with a large set of students). ListView items should only be changed in getView() and based on the current data for each position. So you should keep track of the expanded state of the rows in the Adapter, e.g. using a List<Boolean> or better (for performance) a SparseBooleanArray, and onClick() change the corresponding data and call notifyDatasetChanged()







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 22 at 18:22

























            answered Nov 22 at 18:12









            0X0nosugar

            6,96631742




            6,96631742












            • First, i really don't know the origin of that marginBottom... Sure must have been a design mistake. Second, thanks for the useful resource and for resolving my issue!
              – davide m.
              Nov 22 at 22:28


















            • First, i really don't know the origin of that marginBottom... Sure must have been a design mistake. Second, thanks for the useful resource and for resolving my issue!
              – davide m.
              Nov 22 at 22:28
















            First, i really don't know the origin of that marginBottom... Sure must have been a design mistake. Second, thanks for the useful resource and for resolving my issue!
            – davide m.
            Nov 22 at 22:28




            First, i really don't know the origin of that marginBottom... Sure must have been a design mistake. Second, thanks for the useful resource and for resolving my issue!
            – davide m.
            Nov 22 at 22:28












            up vote
            0
            down vote













            Change the root layout's height attribute from "match_parent" to "wrap_content"



            <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"> ... </RelativeLayout>





            share|improve this answer

























              up vote
              0
              down vote













              Change the root layout's height attribute from "match_parent" to "wrap_content"



              <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"> ... </RelativeLayout>





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                Change the root layout's height attribute from "match_parent" to "wrap_content"



                <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"> ... </RelativeLayout>





                share|improve this answer












                Change the root layout's height attribute from "match_parent" to "wrap_content"



                <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"> ... </RelativeLayout>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 at 17:33









                Nandakumar

                12




                12






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435522%2flistview-custom-items-graphically-incorrect%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Trompette piccolo

                    How do I get these specific pathlines to nodes?

                    What visual should I use to simply compare current year value vs last year in Power BI desktop