Android ExpandableList Move Item From Group to Another on Click

I needed an expandable list that would let us move item from a group to another. So I wrote this simple tutorial. It’s nothing difficult to achieve on your own, but I wanted to add this code here in case someone will need it. So, we will create an ExpandableList with items that can be moved to a new desired group when they are clicked (it’s not a drag’n drop example :P).

1. Create a new simple project in Android

2. Go to res – layout – activity_main.xml and add an ExpandableListView like this:

3. Now, create 2 new xml files in res – layout. Name the first one as list_item_parent.xml and the other one as list_item_child.xml. Below is the code for both.

list_item_parent.xml

list_item_child.xml

4. Now, we have to create a ”model” class for displaying the groups. So, go to the java package, right click on your project package and create a new Java class. Name it Parent. Below is the code for this class:

Parent.java

5. Now, we have to create another java class for the ExpandableListView’s adapter. We will name this class MyCustomAdapter.

MyCustomAdapter.java

6. Now go to the MainActivity class (the activity that was created automatically when you created the project) and add the following code:

MainActivity.java

And that’s it. Now, whenever you click on an item, it will automatically be moved to a new group that was hardcoded as “Group 25 (Hardcoded)” and which is at the bottom of the list. The group is created only once, when no items were added to it. After that, the items will be just moved to this group.

I hope I will enjoy it and feel free to post comments or suggestions 😀

Android ExpandableListView Add Footer Dynamically

Hi, recently I ran into a problem with footer view on an ExpandableListView. I wanted to add a footer view only when I would have needed it. I tried many ways to achieve this, but the only way that worked, in my case, was to add the footer and just after it to remove it, and only after this “trick” was done, I could add the footer anywhere in the code.

So I will show in this tutorial how I did this. It’s very simple 🙂

1. First you have to follow the steps from Android ExpandableListView Example, but when you will get to “MyActivity” class you will have to ignore it and follow the steps from this tutorial. So, you must create all the classes from that tutorial except “MyActivity” which will be created here.

2. Create a new xml file under res-layout, called “footer_view.xml”. The code is below:

3. Now create the “MyActivity” class:

So that’s it. The footer will appear after 3 seconds (3000 ms) on the screen. You can also try to make the footer gone at first (before setAdapter) and in the thread to make it visible. If it works this way, I recommend this approach. But for some reason, in my case it didn’t work, so I had to use the approach described in this tutorial.

I hope that this tutorial will help anybody who ran into the same problem as me 🙂

ExpandableListView, how to hide indicator

If you use an ExpandableListView and want to hide the group indicator (that arrow that appears on the left) you need to put this in your xml file where the ExpandableListView is created:

Below is a complete example of an xml file:

For more info about groupIndicator from Android’s ExpandableListView, visit the official documentation here.

Android ExpandableListView Scroll to Expanded Group Position

Once I had this problem: I had an ExapandableListView with groups and children. When I wanted to expand a group the list was scrolling to the end every time. After a little search I’ve figured it out what the problem was. In my xml file where my ExpandableListView was created, I had this attribute:

So I changed it to this:

And the problem was solved. When I expanded a group from the ExpandableListView, the list was scrolling to the group I had expanded.

So if it happens to you to have this problem too, please consider this solution even if you don’t have the transriptMode attribute, just put it in your xml code as disabled, it might work for you too 🙂

Android Broadcast Receiver Example with Parcelable

In this tutorial I will show you how to use Broadcast Receiver and how to use Parcelable too. To do this, we will create an ExpandableList which will have parents and children(they will contain only their names). The children will be an arrayList of strings and because we want to pass this array with an Intent we will have to implement Parcelable. But you will understand it better later 🙂 So let’s start:

In order to use Broadcast you have to follow some steps:

Step 1: USE PARCELABLE to send complex data(e.g ArrayList)

Now, I want to tell you which are the general steps for implementing a Parcelable into the class where you have the data which must be sent:

  • The class that contains the data you want to send, MUST implement Parcelable. If you want to send via Intent a custom object you have to implement Parcelable.
  • Make a “Creator”. “Creator” is used to create new objects.
  • Override describeContents() method.
  • Write to Parcel(into a method).  The Parcel is the destination where the objects will be written. To write to Parcel you have to use a Parcel object, let’s call it “dest”:

  • Read from Parcel(into a constructor). To read from Parcel you have to make a constructor with a Parcel parameter. In this constructor you have to read the data with the help of a Parcel object again, we will call it “source” this time because from here we will get the data:

  • Create an empty constructor
NOTE 1: You have to read the data in the same order you wrote it, otherwise it will not work!
NOTE 2: When you use an arrayList you have to initialize the arrayList into the empty constructor and call this() method from the constructor where you make the reading from parcel. 


 
Step 2: Create the BROADCAST RECEIVER
  • You must create a class which extends BroadcastReceiver into the class where you want to receive the broadcast
  • You will have a class that sends the broadcast and a class that receives the broadcast.
  • The class that sends the broadcast will use an Intent
  • The class that receives the broadcast will use an IntentFilter ( because it has to receive only the message it wants to receive, it will filter the messages)
  • The broadcast must be registered and unregsitered
  • Usualy the broadcast is registered in the onResume()
  • Usualy the broadcast is unregistered in the onPause()
  • The broadcast must be sent only after the registration of the broadcast
  • All the work for data we want to send, must be done in the class which sends the broadcast
Now let’s make a practical example to understand the above theory better.
1. Create a new project and call your main activity class “MyActivity”
2. Go to res – layout – main.xml and create the ExpandableList:

3. Go to res – layout  and create a new xml file and call it “list_item_parent”

4. Go to res – layout and create another xml file and call it “list_item_child”

5. Create an ApplicationContextProvider class following the step 4 first point from this tutorial.

6. Create a new java class and call it “Child”

6. Create a new java class and call it “Parent”

7. Create a new java class and call it “MyCustomAdapter”. This class is the adapter that we will use for ExpandableList.

8. Create a new class and call it “BroadcastSender” . This class is the sender class, so here we will create the arrays of parents and children and then we will send them to MyActivity class with a broadcast via intent.

9. Now go to MyActivity class (this is the receiver class) and put the following code:

Now the result should be like this:

Android Expandable List Example

In this tutorial I will show you how to make a simple Expandable List.

1. Create a new project and call your java class(the one that is generated by Eclipse or other IDE) “MyActivity”.

2. Go to res – layout -main.xml and put the following code:

NOTE: The attribute “android:transcriptMode=”alwaysScroll”” will make your expandable list always to scroll to the end of the list when you want to expand or collapse a group. If you do not want this set it this way: “android:transcriptMode=”disabled“. Now when you expand a group the list will scroll to the group that is expanded.

3. Now create another 2 xml files in res – layout,  one called list_item_parent.xml and the other one called list_item_child.xml.

  • The xml code for list_item_parent.xml file is:

NOTE: If you put a button on the parent the group will not expand anymore, so in order to make the group expand when you have a button you must set its focus to FALSE. Also, if that doesn’t work, it should work with:

 

  • The xml code for list_item_child.xml file is:

4. Now create a new java class and call it “Parent”. In this class we will store the data about the parent: the  name and the children (an array list of children). After you create the class put the following code:

5. At this step you have to create the adapter for the expandable list. To do this, create a new java class called “MyCustomAdapter” and put the following code:

6. Now go to the MyActivity and put the following code:

Now the result should look like this:

If you want to see the source code, please visit our GitHub repo here. There you can see a working project that might help you get started with ExpandableListView.