Home

Awesome

<h1 align="center">ExpandableLayout</h1></br> <p align="center"> šŸ¦š An expandable layout that shows a two-level layout with an indicator. </p> <br> <p align="center"> <a href="https://opensource.org/licenses/Apache-2.0"><img alt="License" src="https://img.shields.io/badge/License-Apache%202.0-blue.svg"/></a> <a href="https://android-arsenal.com/api?level=16"><img alt="API" src="https://img.shields.io/badge/API-16%2B-brightgreen.svg?style=flat"/></a> <a href="https://travis-ci.org/skydoves/ExpandableLayout"><img alt="Travis" src="https://travis-ci.org/skydoves/ExpandableLayout.svg?branch=master"/></a> <a href="https://androidweekly.net/issues/issue-382"><img alt="Android Weekly" src="https://img.shields.io/badge/Android%20Weekly-%23382-orange"/></a> <a href="https://skydoves.github.io/libraries/expandablelayout/javadoc/expandablelayout/com.skydoves.expandablelayout/index.html"><img alt="Javadoc" src="https://img.shields.io/badge/Javadoc-ExpandableLayout-yellow"/></a> </p> <p align="center"> <img src="https://user-images.githubusercontent.com/24237865/67871177-4b05e880-fb73-11e9-8de8-965a9e67aa1e.gif" width="32%"/> <img src="https://user-images.githubusercontent.com/24237865/65830792-bd25ac00-e2ed-11e9-8ce5-890db409ea05.gif" width="32%"/> </p>

Including in your project

Maven Central Jitpack

Gradle

Add below codes to your root build.gradle file (not your module build.gradle file).

allprojects {
    repositories {
        mavenCentral()
    }
}

And add a dependency code to your module's build.gradle file.

dependencies {
    implementation "com.github.skydoves:expandablelayout:1.0.7"
}

Usage

Add following XML namespace inside your XML layout file.

xmlns:app="http://schemas.android.com/apk/res-auto"

ExpandableLayout

Here is a basic example of implementing ExpandableLayout.

<com.skydoves.expandablelayout.ExpandableLayout
  android:id="@+id/expandable"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="30dp"
  app:expandable_duration="300"
  app:expandable_isExpanded="false" // expand the second layout initially or not.
  app:expandable_parentLayout="@layout/layout_parent" // sets the parent layout. 
  app:expandable_secondLayout="@layout/layout_second" // sets the second layout.
  app:expandable_showSpinner="true" // shows the spinner or not.
  app:expandable_spinner="@drawable/ic_arrow_down" // sets the spinner's drawable.
  app:expandable_spinner_animate="true" // animates the spinner when expanding or collapse.
  app:expandable_spinner_margin="14dp" // sets the margin to the spinner.
  app:expandable_spinner_gravity="start" // sets the gravity to the spinner.
  app:expandable_spinner_size="32dp" // sets the spinner size.
/>

Create using builder class

We can create an instance of ExpandableLayout using the builder class.

val myExpandableLayout = expandableLayout(context) {
  setParentLayoutResource(R.layout.layout_parent)
  setSecondLayoutResource(R.layout.layout_second)
  setShowSpinner(true)
  setSpinnerAnimate(true)
  setSpinnerMargin(12f)
  setSpinnerRotation(90)
  setDuration(200)
  setOnExpandListener { toast("is expanded : $it") }
}

Expand and Collapse

We can expand and collapse using the below methods.

expandablelayout.expand() // expand the second layout with indicator animation.
expandablelayout.collapse() // collapse the second layout with indicator animation.

ParentLayout and SecondLayout

We can get the parentLayout and secondLayout of the ExpandableLayout.<br> And we can access child views of them.

expandablelayout.parentLayout.setOnClickListener {
  toast("the parent layout is clicked!")
}
expandablelayout.secondLayout.setOnClickListener {
  toast("the second layout is clicked!")
}

// getting child view using findViewById.
expandablelayout.secondLayout.findViewById<Button>(R.id.button0).setOnClickListener { 
    toast("button0 clicked") 
}
// getting child view using android extension.
expandablelayout.secondLayout.button0.setOnClickListener { toast("button0 clicked") }

OnExpandListener

We can listen to the ExpandableLayout is expanded or collapsed.

expandablelayout.onExpandListener = object : OnExpandListener {
  override fun onExpand(isExpanded: Boolean) {
    toast("Expanded : $it")
  }
}

// or we can listen using a lambda expression.
expandable.setOnExpandListener {
  if (it) {
    toast("expanded")
  } else {
    toast("collapse")
  }
}

ExpandableAnimation

We can customize the expanding and collapsing animation.<br>

ExpandableAnimation.NORMAL
ExpandableAnimation.ACCELERATE
ExpandableAnimation.BOUNCE
NORMALACCELERATEBOUNCE
<img src="https://user-images.githubusercontent.com/24237865/67871179-4b9e7f00-fb73-11e9-8ffe-563d64b876b4.gif" align="center" width="100%"/><img src="https://user-images.githubusercontent.com/24237865/67871176-4a6d5200-fb73-11e9-8d25-26c616ca7b9d.gif" align="center" width="100%"/><img src="https://user-images.githubusercontent.com/24237865/67871178-4b9e7f00-fb73-11e9-9f93-fef72b2d627d.gif" align="center" width="100%"/>

ExpandableLayout Attributes

AttributesTypeDefaultDescription
isExpandedBooleanfalseExpand the second layout initially or not.
parentLayoutlayoutdefault layoutSets the parent layout.
secondLayoutlayoutdefault layoutSets the second layout.
durationLong250LSets the duration of the spinner animation.
spinnerDrawablearrow_downSets the spinner's drawable.
showSpinnerBooleantrueShows the spinner or not.
spinner_animateBooleantrueAnimates the spinner when expanding or collapse.
spinner_rotationInteger-180Sets the rotation of the spinner animation.
spinner_sizeDimension36dpSets the size of the spinner.
spinner_marginDimension8dpSets the margin of the spinner.
spinner_gravitySpinnerGravityendSets the gravity of the spinner.

Find this library useful? :heart:

Support it by joining stargazers for this repository. :star:<br> And follow me for my next creations! šŸ¤©

License

Copyright 2019 skydoves (Jaewoong Eum)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.