Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4568120
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:01:51+00:00 2026-05-21T19:01:51+00:00

Because I don’t have enough reputation points to comment on previous questions yet, I

  • 0

Because I don’t have enough reputation points to comment on previous questions yet, I had to create a new one. I needed an expandable list view that didn’t take up the entire activity so I used this example to do it without ExpandableListActivity:

ExpandableList View don't expand

My slightly modified code:

public class Main extends Activity {

    ExpandableListView lv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expandable_list);
        lv = (ExpandableListView) this.findViewById(R.id.expandableListView1);
        MyExpandableListAdapter expandableAdapter = new MyExpandableListAdapter();
        lv.setAdapter(expandableAdapter);
    }

    class MyExpandableListAdapter extends BaseExpandableListAdapter {

    // Sample data set.  children[i] contains the children (String[]) for groups[i].
    private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" };
    private String[][] children = {
            { "Arnold", "Barry", "Chuck", "David" },
            { "Ace", "Bandit", "Cha-Cha", "Deuce" },
            { "Fluffy", "Snuggles" },
            { "Goldy", "Bubbles" }
    };


        public Object getChild(int groupPosition, int childPosition) {
            return children[groupPosition][childPosition];
        }

        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        public int getChildrenCount(int groupPosition) {
            return children[groupPosition].length;
        }

        public TextView getGenericView() {
            // Layout parameters for the ExpandableListView
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, 64);

            TextView textView = new TextView(Main.this);
            textView.setLayoutParams(lp);
            // Center the text vertically
            textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            // Set the text starting position
            textView.setPadding(36, 0, 0, 0);
            return textView;
        }



        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, 
                View convertView, ViewGroup parent) {


            TextView textView = getGenericView();
            textView.setText(getChild(groupPosition, childPosition).toString());
            return textView;

        }

        public Object getGroup(int groupPosition) {
            return groups[groupPosition];
        }

        public int getGroupCount() {
            return groups.length;
        }

        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            TextView textView = getGenericView();
            textView.setText(getGroup(groupPosition).toString());
            return textView;

        }

        public boolean hasStableIds() {
            return true;
        }

        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }

}

This works fine, but to make it more robust, I wanted to separate the textViews created in code to xml files (This is a good idea right?) This is where I run into some FC issues.

group_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:id="@+id/row_name"
         android:paddingLeft="50px"
         android:textSize="20px"
         android:textStyle="normal"
         android:layout_width="320px"
         android:layout_height="wrap_content"/>
</LinearLayout>

And I changed this in the java file above:

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

    TextView parentRow = (TextView) findViewById(R.id.row_name);
    parentRow.setText(getGroup(groupPosition).toString());
    return parentRow;
}

I also tried removing the LinearLayout wrapper but that didn’t fix anything. Can anyone tell me why my xml view isn’t working? Thanks.

EDIT:
Thanks Flo, new code that is working using the tutorial:

group_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/group_name"
    android:paddingLeft="50dp"
    android:textSize="30sp"
    android:textStyle="normal"
    android:layout_height="wrap_content"/>

Main:

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


        View parentView = convertView;
        if (parentView == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            parentView = vi.inflate(R.layout.group_row, null);
        }
                TextView parentText = (TextView) parentView.findViewById(R.id.group_name);
                if (parentText != null) {
                      parentText.setText(getGroup(groupPosition).toString());                     
                }

       return parentText;


    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T19:01:52+00:00Added an answer on May 21, 2026 at 7:01 pm

    Your implementation of the getGroupView() method is wrong. At the moment your searching in the activity’s layout for a element with the element R.id.row_name which doesn’t exist. I guess the line parentRow.setText(getGroup(groupPosition).toString()); throws an exception as parentRow is null.

    The correct implementation of the method with a custom row layout from a xml file is to inflate this layout. Check out this tutorial. It’s for normal ListViews and adapter but the concept of inflating the row layouts it the same.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

RESOLVED P.S.Because I don't have enough reputation to answer my own question right now
How would I use form_tag (because I don't have a model) to create a
I feel that my shop has a hole because we don't have a solid
I have a Gridview boundfield where i set ReadOnly to true because i don't
I hope you know, because I don't see it. Bonus points for whoever figures
Hey, I don't have any code because I don't know how to do this.
I have a class where I'm using __set . Because I don't want it
I have a problem with class StatefulRetryOperationsInterceptor because I don't know how to use
The title might not be clear enough because I don't know how to define
I have a little problem because I don't know whats the best way to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.