SergioL08
SergioL08

Reputation: 121

Set expandablelistview item's height programmatically

I have an expandablelistview and I have put a background for the groups item, but I would like to adjust the height. I have already set the height into xml file (50dp) but it doesn't works (the real height isn't 50 dp, but it's taller), so I am thinking to do it programmatically.

enter image description here

Here you are a piece of my code to explain it

grouplayout_index.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/LinearLayout_index"
      android:background="@drawable/bg_capitolo_retina"
      android:paddingBottom="10dp"
      android:paddingTop="10dp"
      android:layout_height="50dp"             ---------------------//height = 50 dp
      android:layout_marginTop="10dp"
      android:layout_marginBottom="10dp"
      android:layout_width="match_parent" >

    <ImageView android:id="@+id/freccia_indice" android:src="@drawable/freccia_o"
        android:clickable="false"
        android:layout_height="28dp"
        android:layout_width="28dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="10dp" 
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:paddingLeft="10dp"  />

    <TextView android:id="@+id/tvGroup"  
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="10dp"
        android:paddingLeft="50dp" />
</LinearLayout> 

Index.java

  public class Index extends  ExpandableListActivity {
        .....
           IndexAdapter   mAdapter = new IndexAdapter .....
          explistview.setAdapter(mAdapter);
}

IndexAdapter.java

   public class IndexAdapter  extends BaseExpandableListAdapter{

     ....various methods......
   public View getGroupView(int groupPosition, boolean isExpanded, View  convertView,ViewGroup parent) {

    if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             convertView =   infalInflater.inflate(R.layout.grouplayout_index, null);

    }      
         TextView textView = (TextView) convertView.findViewById(R.id.tvGroup);
         textView.setTextAppearance(context, R.style.textViewStyleGroup);
         textView.setText("textview");

            /* I have tried this code but it doesn't works.
           LinearLayout ll = (LinearLayout)   convertView.findViewById(R.id.LinearLayout_indice);
           ll.setLayoutParams(new LinearLayout.LayoutParams(30,50));
*/              
   }

Can someone help me, please?

Upvotes: 0

Views: 11118

Answers (5)

user3472840
user3472840

Reputation: 1

If you are using databinding and while expanding if the expandablelist view is not adjusting the height then I would suggest to call binding.executependingBinding()

Upvotes: 0

Ram Mandal
Ram Mandal

Reputation: 1959

Use Like this

ExpandableAdapter adapter = new 


ExpandableAdapter(Fragment_Test.this.getActivity(), parentList, childList);
        ExpandableListView exp = new ExpandableListView(Fragment_Test.this.getActivity());
        exp.setGroupIndicator(null);
        exp.setSelector(getResources().getDrawable(R.drawable.selectedcorner));
        exp.setVerticalScrollBarEnabled(true);
        exp.setScrollbarFadingEnabled(false);
    exp.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT));

Upvotes: 0

kalidoss rajendran
kalidoss rajendran

Reputation: 640

Use this code to set both parent and child height

expListView.setChildDivider(getResources().getDrawable(R.color.white)); expListView.setDivider(getResources().getDrawable(R.color.white));
expListView.setDividerHeight(20);

Upvotes: 0

pankaj716
pankaj716

Reputation: 59

I have successfully implemented group indicator at right side of the screen without any issue by adding extra imageview to group layout, as below:-

Expandable Adapter method 

@Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
    {
        View row;

        if (convertView != null)
        {
            row = convertView;
        }
        else
        {
            row = LayoutInflater.from(getContext()).inflate(R.layout.group_row, null));
        }


        final ImageView indicator = (ImageView)row.findViewById(R.id.indicator);
        indicator.setSelected(isExpanded);
        return row;
    }

group layout  

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="8dp" 
    android:background="#fff">


    <TextView
        android:id="@+id/lblListHeader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingLeft="3dp"
        android:paddingRight="20dp"
        android:textSize="16dp"
        android:text="Header"
        android:lineSpacingExtra="-4dp"
        android:textColor="@color/title_heading" />

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        >
    </LinearLayout>

     <ImageView 
         android:id="@+id/imageview_list_group_indicator"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginRight="7dp"
        android:src="@drawable/groupindicator1"/>
</LinearLayout>

selector xml for image

<?xml version="1.0" encoding="utf-8"?>


 <selector xmlns:android="http://schemas.android.com/apk/res/android" >
       <item android:drawable="@drawable/arrow_up" android:state_selected="true" />
    <item android:drawable="@drawable/arrow_down" android:state_selected="false" />
    <item android:drawable="@drawable/arrow_down"  />
</selector>

Upvotes: 0

joates
joates

Reputation: 1103

The problem with the extra height is likely the result of the layout_marginTop and layout_marginBottom set on the LinearLayout. A layout like the following may meet your need:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout_index"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/shape_data_background"
android:paddingBottom="10dp"
android:paddingTop="10dp" >

<ImageView
    android:id="@+id/freccia_indice"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:paddingLeft="10dp"
    android:src="@drawable/arrow_right" />

<TextView
    android:id="@+id/tvGroup"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:text="textview" />

</LinearLayout>

Unless you have a requirement for rows that are exactly 50dp, it's usually worked best for me to let the rows in a list view size themselves using layout_height="wrap_content".

Upvotes: 1

Related Questions