jacosta
jacosta

Reputation: 424

How to Expand and Hide Scrollviews with Button click in Android

I'm trying to create a page with 4 buttons on top of each other.

When I click one button, the button should stay highlighted, a scrollview should appear below it with text and push the buttons below that button down (even off screen if necessary). Upon reclicking the button, the scrollview will hide again.

How do I get the scroll view to hide and reappear on a button click?

This is what i want it to look like: http://img407.imageshack.us/img407/3448/47163794.png

Upvotes: 1

Views: 1118

Answers (1)

prolink007
prolink007

Reputation: 34614

Here is what i have done recently with ExpandableListView. It is fairly easy.

It has one of the biggest constructors i have ever seen in android so far.


Create a layout with an ExpandableListView.

<LinearLayout>

    <ExpandableListView>
        <!-- Set the properties, if you need to see them, let me know. -->
    </ExpandableListView>

</LinearLayout>

Now use the layout in some view.

public void onCreate() {

    ExpandableListView example = findViewById(/* The id of the expandable list view */);


    example.setAdapter(
            new SimpleExpandableListAdapter(
                        /* the context */,
                        /* the map of parents/groups */,
                        /* the layout to map the parents/groups to */,
                        /* the key to search for in the parents/groups map */,
                        /* the id of the textview inside the layout to map the parent/group to */,
                        /* the map of children */,
                        /* the layout to map the children to */,
                        /* the key to search for in the children map */,
                        /* the id of the textview inside the layout to map the children to */)
            )
}

There are other adapters available, but this is the most basic one.

Basically just take a look at the developer website. This example was also somewhat helpful, but the developer's website goes a long way.

If you have any questions let me know.


FOLLOW UP

he put createGroupList(). Looks like this was a function he commented out. What is this doing exactly?

He basically is creating the List of HashMaps in that function and returning the created object. Yes, you should do something like that, it helps keep your code a little cleaner.

Also, these lists show up really tiny, anyway to make them bigger?

Modify the TextView you have displaying your children and groups. Add padding to their View in their xml layout. Just adjust the dp. And you can increase the textSize or you can add a style like i show below.

<TextView 
        android:id="@+id/groupTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@android:style/TextAppearance.Medium"
        android:paddingLeft="50dp"
        android:paddingTop="25dp"
        android:paddingBottom="25dp"/>

I want the parent group to be lists, but dont want the child group to be sublists, I want them to be a text box I can add text to (which I grab from the net). How do i make the children textboxes rather than lists.

You will probably need to create your own Adapter for that situation. You will need to extend the BaseExpandableListAdapter. I could be wrong, there may be an easier way... but i am not aware of one.


ADDITIONAL FOLLOW UP

Not sure what you mean. The text getting entered in the child list comes in at this line of code: child.put( "Sub Item", "test"). I can put the text I want where it says "test" but "test" comes up under every parent list.

Your createChildList needs to return a List<ArrayList<HashMap<String, String>>>.

Your createChildList should look something like this:

private List<ArrayList<HashMap<String, String>>> createChildList(int maxValue) {

                ArrayList<ArrayList<HashMap<String, String>>> groupList = 
                                new ArrayList<ArrayList<HashMap<String, String>>>();

                for (int i = 0; i <= maxValue; i++) {
                        ArrayList<HashMap<String, String>> childList = 
                                        new ArrayList<HashMap<String, String>>();

                        for (int j = 0; j <= maxValue; j++) {
                                HashMap<String, String> map = new HashMap<String, String>();

                                map.put("Sub Item", "test: " + String.valueOf(j));
                                childList.add(map);
                        }

                        groupList.add(childList);
                }

                return groupList;
        }

Let me know if that works.

Upvotes: 1

Related Questions