William Kinaan
William Kinaan

Reputation: 28819

android how to make a button in the center of the footer of Expandablelistview

Because there is no build in footer to ExpandableListView, I have built it myself, I put a button on it. I am trying to make this button in the center of that footer.

Footer all_addresses_group_footer.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/b_all_addresses_group_footer_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="0dip"
        android:text="@string/b_edit" />

</LinearLayout>

the button on the above code is in the center, but when i added it to the ExpandableListView group, it stay at the left.

Adding Footer Programmatically

@Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        AllAddressChild child = (AllAddressChild) getChild(groupPosition,
                childPosition);
        if (childPosition == groups.get(groupPosition).getChilds().size() - 1) {
            // if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(
                    R.layout.all_addresses_group_footer, null);
            // }
            Button b_edit = (Button) convertView
                    .findViewById(R.id.b_all_addresses_group_footer_edit);
            b_edit.setText("Edit");
            b_edit.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent intent = new Intent(context, EditAddress.class);
                    ((Activity) context).startActivityForResult(intent, 104);
                }
            });
            convertView.setPadding(0, 0, 0, 20);
            return convertView;
        } else { another code

in the above code, the convertView is the view which include the footer.

In the TRUE if statement the footer is being added

what am i doing wrong please? and how to make the button on the center ?

Thanks

After a comment

enter image description here

After an ansewr

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="25dip"
        android:layout_marginTop="20dip"
        android:text="@string/tv_allAddresses"
        android:textColor="#025f7c"
        android:textSize="25dip"
        android:typeface="serif" />

    <ExpandableListView
        android:id="@+id/elv_all_addresses_addresses"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:padding="10dip" >
    </ExpandableListView>

    <Button
        android:id="@+id/b_all_addresses_addAddress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/button_selector"
        android:paddingTop="20dip"
        android:text="@string/b_addAddress"
        android:paddingRight="5dip"
        android:paddingLeft="5dip"
        android:textColor="#FFFFFF" />

</LinearLayout>

Upvotes: 2

Views: 849

Answers (2)

William Kinaan
William Kinaan

Reputation: 28819

I found the solution myself, after taking advantage of hierarchyviewer tool, the solution was making the width of the expandablelistview as fill parent. enter image description here

<ExpandableListView
        android:id="@+id/elv_all_addresses_addresses"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:padding="10dip" >
    </ExpandableListView>

Upvotes: 2

Ovidiu Latcu
Ovidiu Latcu

Reputation: 72331

You will need to set the LayoutParams for your view :

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
   LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
convertView.setLayoutParams(layoutParams);

Upvotes: 1

Related Questions