Android Tutorial

Software Setup and Configuration

Android Studio

File Structure

Components

Core Topics

Layout

View

Button

Intent and Intent Filters

Toast

RecyclerView

Fragments

Adapters

Other UI Component

Image Loading Libraries

Date and Time

Material Design

Bars

Working with Google Maps

Chart

Animation

Database

Advance Android

Jetpack

Architecture

App Publish

App Monetization

SimpleExpandableListAdapter in Android with Example

SimpleExpandableListAdapter is a helper class in Android used for creating an expandable list view backed by simple arrays and Maps. The expandable list has groups (parent items) and children (sub-items under each parent).

Here's a simple example to show how to use SimpleExpandableListAdapter:

1. Layout Files:

activity_main.xml (Layout with the ExpandableListView):

<ExpandableListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/expandableListView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

group_item.xml (Layout for each group/parent):

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/groupText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:textSize="18sp" />

child_item.xml (Layout for each child):

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/childText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="32dp"
    android:paddingTop="8dp"
    android:paddingBottom="8dp"
    android:textSize="16sp" />

2. Kotlin Code:

In your MainActivity:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Sample Data
        val groups = listOf(
            mapOf("GROUP_NAME" to "Fruits"),
            mapOf("GROUP_NAME" to "Animals")
        )

        val children = listOf(
            listOf(
                mapOf("CHILD_NAME" to "Apple"),
                mapOf("CHILD_NAME" to "Banana")
            ),
            listOf(
                mapOf("CHILD_NAME" to "Cat"),
                mapOf("CHILD_NAME" to "Dog")
            )
        )

        val adapter = SimpleExpandableListAdapter(
            this,
            groups,
            R.layout.group_item,
            arrayOf("GROUP_NAME"),
            intArrayOf(R.id.groupText),
            children,
            R.layout.child_item,
            arrayOf("CHILD_NAME"),
            intArrayOf(R.id.childText)
        )

        val expandableListView: ExpandableListView = findViewById(R.id.expandableListView)
        expandableListView.setAdapter(adapter)
    }
}

In the above code:

  • groups: A list of maps, each representing a group. The key is the column name (e.g., "GROUP_NAME"), and the value is the actual data.

  • children: A list of lists of maps. The outer list corresponds to the groups, and the inner lists represent the children of those groups.

  • SimpleExpandableListAdapter parameters:

    • Context: The current context.
    • groupData: The group data.
    • groupLayout: The resource ID for the group item layout.
    • groupFrom & groupTo: Columns from groupData and view IDs to bind the data.
    • childData: The child data.
    • childLayout: The resource ID for the child item layout.
    • childFrom & childTo: Columns from childData and view IDs to bind the data.

This example provides a basic usage of SimpleExpandableListAdapter. Depending on the requirements, more advanced customizations might be needed, for which creating a custom adapter would be the right approach.

  1. Implementing SimpleExpandableListAdapter in Android example code:

    • Description: Introduces the SimpleExpandableListAdapter class for creating expandable lists in Android.
    • Example Code (Java):
      // Define group and child data
      List<Map<String, String>> groupData = new ArrayList<>();
      List<List<Map<String, String>>> childData = new ArrayList<>();
      
      // Set group and child data to the adapter
      SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
          this,
          groupData,
          android.R.layout.simple_expandable_list_item_1,
          new String[] { "GroupTitle" },
          new int[] { android.R.id.text1 },
          childData,
          android.R.layout.simple_expandable_list_item_2,
          new String[] { "ChildTitle" },
          new int[] { android.R.id.text1 }
      );
      
      // Set adapter to ExpandableListView
      ExpandableListView expandableListView = findViewById(R.id.expandableListView);
      expandableListView.setAdapter(adapter);
      
  2. Using SimpleExpandableListAdapter with ExpandableListView in Android:

    • Description: Demonstrates using SimpleExpandableListAdapter with an ExpandableListView to display group and child data.
    • Example Code (XML):
      <ExpandableListView
          android:id="@+id/expandableListView"
          android:layout_width="match_parent"
          android:layout_height="match_parent"/>
      
  3. Customizing SimpleExpandableListAdapter layout in Android:

    • Description: Customizes the layout used by SimpleExpandableListAdapter for both group and child items.

    • Example Code (XML):

      <!-- Custom layout for group items -->
      <LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content">
      
          <TextView
              android:id="@+id/customGroupText"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"/>
      </LinearLayout>
      
      <!-- Custom layout for child items -->
      <LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content">
      
          <TextView
              android:id="@+id/customChildText"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"/>
      </LinearLayout>
      
      // Specify custom view IDs
      int[] groupTo = {R.id.customGroupText};
      int[] childTo = {R.id.customChildText};
      
      // Create SimpleExpandableListAdapter with custom layouts
      SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
          this,
          groupData,
          R.layout.custom_group_layout,
          new String[] { "GroupTitle" },
          groupTo,
          childData,
          R.layout.custom_child_layout,
          new String[] { "ChildTitle" },
          childTo
      );
      
  4. Handling data binding with SimpleExpandableListAdapter in Android:

    • Description: Illustrates data binding using SimpleExpandableListAdapter for efficient data display.
    • Example Code (Java):
      // Use SimpleExpandableListAdapter.ViewBinder for data binding
      SimpleExpandableListAdapter.ViewBinder viewBinder = (groupPosition, isExpanded, convertView, parent) -> {
          // Handle data binding for group items
          if (groupPosition % 2 == 0) {
              // Customize the appearance of even group items
              // Example: convertView.setBackgroundColor(Color.YELLOW);
              return true;
          }
          return false;
      };
      
      // Set the view binder to the SimpleExpandableListAdapter
      adapter.setViewBinder(viewBinder);
      
  5. Adding images to SimpleExpandableListAdapter in Android:

    • Description: Adds image elements to SimpleExpandableListAdapter for displaying images in both group and child items.
    • Example Code (Java):
      // Add image data to the map
      groupItem.put("GroupImage", R.drawable.group_icon);
      childItem.put("ChildImage", R.drawable.child_icon);
      
      // Specify additional keys for the images
      String[] groupFrom = { "GroupTitle", "GroupImage" };
      String[] childFrom = { "ChildTitle", "ChildImage" };
      
      // Specify additional view IDs for the images
      int[] groupTo = { R.id.text1, R.id.groupImageView };
      int[] childTo = { R.id.text1, R.id.childImageView };
      
  6. Creating a SimpleExpandableListAdapter for grouped data in Android:

    • Description: Adapts SimpleExpandableListAdapter for displaying grouped data in an expandable format.
    • Example Code (Java):
      // Set data for groups and children
      List<Map<String, String>> groupData = new ArrayList<>();
      List<List<Map<String, String>>> childData = new ArrayList<>();
      
      // Set data to the adapter
      SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
          this,
          groupData,
          android.R.layout.simple_expandable_list_item_1,
          new String[] { "GroupTitle" },
          new int[] { android.R.id.text1 },
          childData,
          android.R.layout.simple_expandable_list_item_2,
          new String[] { "ChildTitle" },
          new int[] { android.R.id.text1 }
      );
      
  7. SimpleExpandableListAdapter with multiple data sets in Android:

    • Description: Utilizes SimpleExpandableListAdapter with multiple data sets for complex data display.
    • Example Code (Java):
      // Define multiple data sets for groups and children
      List<Map<String, String>> groupData1 = new ArrayList<>();
      List<Map<String, String>> groupData2 = new ArrayList<>();
      List<List<Map<String, String>>> childData1 = new ArrayList<>();
      List<List<Map<String, String>>> childData2 = new ArrayList<>();
      
      // Set multiple data sets to the adapter
      List<List<Map<String, String>>> allChildData = new ArrayList<>();
      allChildData.add(childData1);
      allChildData.add(childData2);
      
      SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
          this,
          groupData1,
          android.R.layout.simple_expandable_list_item_1,
          new String[] { "GroupTitle" },
          new int[] { android.R.id.text1 },
          allChildData,
          android.R.layout.simple_expandable_list_item_2,
          new String[] { "ChildTitle" },
          new int[] { android.R.id.text1 }
      );
      
  8. Dynamic data loading with SimpleExpandableListAdapter in Android:

    • Description: Dynamically loads data into SimpleExpandableListAdapter to update the displayed content.
    • Example Code (Java):
      // Clear existing data
      groupData.clear();
      childData.clear();
      
      // Add new data
      Map<String, String> newGroupItem = new HashMap<>();
      newGroupItem.put("GroupTitle", "New Group");
      groupData.add(newGroupItem);
      
      // Notify adapter of data change
      adapter.notifyDataSetChanged();
      
  9. Updating data in SimpleExpandableListAdapter in Android:

    • Description: Updates data in SimpleExpandableListAdapter and notifies the adapter to refresh the view.
    • Example Code (Java):
      // Modify existing data
      Map<String, String> groupItemToUpdate = groupData.get(0);
      groupItemToUpdate.put("GroupTitle", "Updated Group");
      
      // Notify adapter of data change
      adapter.notifyDataSetChanged();
      
  10. Expanding and collapsing groups with SimpleExpandableListAdapter in Android:

    • Description: Implements methods to programmatically expand and collapse groups in SimpleExpandableListAdapter.
    • Example Code (Java):
      // Expand a group
      expandableListView.expandGroup(groupPosition);
      
      // Collapse a group
      expandableListView.collapseGroup(groupPosition);
      
  11. Styling SimpleExpandableListAdapter items in Android:

    • Description: Applies styles to SimpleExpandableListAdapter items for a visually appealing interface.

    • Example Code (XML):

      <!-- Apply styles to the custom layout or default layout -->
      <style name="CustomText">
          <item name="android:textSize">18sp</item>
          <item name="android:textColor">#FF0000</item>
      </style>
      
      <!-- Apply styles to TextView in the custom layout -->
      <TextView
          android:id="@+id/customGroupText"
          style="@style/CustomText"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"/>
      
  12. Alternative to SimpleExpandableListAdapter in Android:

    • Description: Explores alternatives to SimpleExpandableListAdapter, such as using custom adapters or RecyclerView.
    • Example Code (Java):
      // Explore using custom adapters or RecyclerView for more complex scenarios
      
  13. Handling group and child click events with SimpleExpandableListAdapter:

    • Description: Handles group and child item click events for user interaction.
    • Example Code (Java):
      // Set group click listener
      expandableListView.setOnGroupClickListener((parent, v, groupPosition, id) -> {
          // Handle group item click
          // Return true if you want to prevent the group from being expanded/collapsed
          return false;
      });
      
      // Set child click listener
      expandableListView.setOnChildClickListener((parent, v, groupPosition, childPosition, id) -> {
          // Handle child item click
          return true;
      });