Android Create Expansion Panel Basic

Android Create Expansion Panel Basic


1. Project Structure


2. Gradle

plugins {
  id 'com.android.application'
}

android {
  compileSdkVersion 30
  buildToolsVersion "30.0.3"

  defaultConfig {
    applicationId "com.expansionpanel.basic"
    minSdkVersion 19
    targetSdkVersion 30
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  }

  buildTypes {
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

dependencies {
  implementation 'com.balysv:material-ripple:1.0.2'
  implementation 'androidx.appcompat:appcompat:1.3.0'
  implementation 'com.google.android.material:material:1.4.0'
  implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
  testImplementation 'junit:junit:4.+'
  androidTestImplementation 'androidx.test.ext:junit:1.1.3'
  androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}


3. Main Activity

package com.expansionpanel.basic;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.widget.NestedScrollView;

import com.expansionpanel.basic.utils.Tools;
import com.expansionpanel.basic.utils.ViewAnimation;
import com.google.android.material.snackbar.Snackbar;

public class MainActivity extends AppCompatActivity {

  private View parent_view;

  private NestedScrollView nested_scroll_view;
  private ImageButton bt_toggle_text, bt_toggle_input;
  private Button bt_hide_text, bt_save_input, bt_hide_input;
  private View lyt_expand_text, lyt_expand_input;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_expansion_panel_basic);
    parent_view = findViewById(android.R.id.content);

    initToolbar();
    initComponent();
  }

  private void initToolbar() {
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setNavigationIcon(R.drawable.ic_menu);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("Basic");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    Tools.setSystemBarColor(this);
  }

  private void initComponent() {

    // text section
    bt_toggle_text = (ImageButton) findViewById(R.id.bt_toggle_text);
    bt_hide_text = (Button) findViewById(R.id.bt_hide_text);
    lyt_expand_text = (View) findViewById(R.id.lyt_expand_text);
    lyt_expand_text.setVisibility(View.GONE);

    bt_toggle_text.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        toggleSectionText(bt_toggle_text);
      }
    });

    bt_hide_text.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        toggleSectionText(bt_toggle_text);
      }
    });

    // input section
    bt_toggle_input = (ImageButton) findViewById(R.id.bt_toggle_input);
    bt_hide_input = (Button) findViewById(R.id.bt_hide_input);
    bt_save_input = (Button) findViewById(R.id.bt_save_input);
    lyt_expand_input = (View) findViewById(R.id.lyt_expand_input);
    lyt_expand_input.setVisibility(View.GONE);

    bt_toggle_input.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        toggleSectionInput(bt_toggle_input);
      }
    });

    bt_hide_input.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        toggleSectionInput(bt_toggle_input);
      }
    });

    bt_save_input.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        Snackbar.make(parent_view, "Data saved", Snackbar.LENGTH_SHORT).show();
        toggleSectionInput(bt_toggle_input);
      }
    });

    // nested scrollview
    nested_scroll_view = (NestedScrollView) findViewById(R.id.nested_scroll_view);
  }

  private void toggleSectionText(View view) {
    boolean show = toggleArrow(view);
    if (show) {
      ViewAnimation.expand(lyt_expand_text, new ViewAnimation.AnimListener() {
        @Override
        public void onFinish() {
          Tools.nestedScrollTo(nested_scroll_view, lyt_expand_text);
        }
      });
    } else {
      ViewAnimation.collapse(lyt_expand_text);
    }
  }

  private void toggleSectionInput(View view) {
    boolean show = toggleArrow(view);
    if (show) {
      ViewAnimation.expand(lyt_expand_input, new ViewAnimation.AnimListener() {
        @Override
        public void onFinish() {
          Tools.nestedScrollTo(nested_scroll_view, lyt_expand_input);
        }
      });
    } else {
      ViewAnimation.collapse(lyt_expand_input);
    }
  }

  public boolean toggleArrow(View view) {
    if (view.getRotation() == 0) {
      view.animate().setDuration(200).rotation(180);
      return true;
    } else {
      view.animate().setDuration(200).rotation(0);
      return false;
    }
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_setting, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
      finish();
    } else {
      Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
    }
    return super.onOptionsItemSelected(item);
  }
}


4. Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@color/grey_10"
  android:orientation="vertical">

  <com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <include layout="@layout/toolbar" />
  </com.google.android.material.appbar.AppBarLayout>

  <androidx.core.widget.NestedScrollView
    android:id="@+id/nested_scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none"
    android:scrollingCache="true">

    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:paddingTop="@dimen/spacing_medium">

      <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/spacing_medium"
        android:layout_marginLeft="@dimen/spacing_middle"
        android:layout_marginRight="@dimen/spacing_middle"
        android:layout_marginTop="@dimen/spacing_medium"
        android:visibility="visible"
        app:cardCornerRadius="2dp"
        app:cardElevation="2dp">

        <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical">

          <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:gravity="center_vertical"
            android:minHeight="?android:attr/actionBarSize"
            android:orientation="horizontal">

            <View
              android:layout_width="@dimen/spacing_large"
              android:layout_height="wrap_content" />

            <TextView
              android:id="@+id/textView3"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:text="Text"
              android:textAppearance="@style/TextAppearance.AppCompat.Medium"
              android:textColor="@color/grey_80" />

            <ImageButton
              android:id="@+id/bt_toggle_text"
              android:layout_width="?android:attr/actionBarSize"
              android:layout_height="?android:attr/actionBarSize"
              android:background="?attr/selectableItemBackgroundBorderless"
              android:tint="@color/grey_80"
              app:srcCompat="@drawable/ic_expand_arrow" />

          </LinearLayout>

          <LinearLayout
            android:id="@+id/lyt_expand_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="@dimen/spacing_large"
              android:text="@string/lorem_ipsum" />

            <View
              android:layout_width="match_parent"
              android:layout_height="1dp"
              android:background="@color/grey_10" />

            <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:gravity="end"
              android:orientation="horizontal">

              <Button
                android:id="@+id/bt_hide_text"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="@string/HIDE" />

            </LinearLayout>

          </LinearLayout>

        </LinearLayout>

      </androidx.cardview.widget.CardView>

      <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/spacing_medium"
        android:layout_marginLeft="@dimen/spacing_middle"
        android:layout_marginRight="@dimen/spacing_middle"
        android:layout_marginTop="@dimen/spacing_medium"
        android:visibility="visible"
        app:cardCornerRadius="2dp"
        app:cardElevation="2dp">

        <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical">

          <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:gravity="center_vertical"
            android:minHeight="?android:attr/actionBarSize"
            android:orientation="horizontal">

            <View
              android:layout_width="@dimen/spacing_large"
              android:layout_height="wrap_content" />

            <TextView
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:text="Input"
              android:textAppearance="@style/TextAppearance.AppCompat.Medium"
              android:textColor="@color/grey_80" />

            <ImageButton
              android:id="@+id/bt_toggle_input"
              android:layout_width="?android:attr/actionBarSize"
              android:layout_height="?android:attr/actionBarSize"
              android:background="?attr/selectableItemBackgroundBorderless"
              android:tint="@color/grey_80"
              app:srcCompat="@drawable/ic_expand_arrow" />

          </LinearLayout>

          <LinearLayout
            android:id="@+id/lyt_expand_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <EditText
              android:id="@+id/appCompatEditText"
              android:layout_width="match_parent"
              android:layout_height="50dp"
              android:layout_marginLeft="@dimen/spacing_large"
              android:layout_marginRight="@dimen/spacing_large"
              android:layout_marginTop="@dimen/spacing_large"
              android:background="@color/grey_5"
              android:gravity="center_vertical"
              android:hint="Name"
              android:padding="@dimen/spacing_middle"
              android:textColor="@color/grey_60" />

            <RadioGroup
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_margin="@dimen/spacing_large"
              android:layout_marginTop="@dimen/spacing_medium">

              <androidx.appcompat.widget.AppCompatRadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Male" />

              <androidx.appcompat.widget.AppCompatRadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Female" />

            </RadioGroup>

            <View
              android:layout_width="match_parent"
              android:layout_height="1dp"
              android:background="@color/grey_10" />

            <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:gravity="end"
              android:orientation="horizontal">

              <Button
                android:id="@+id/bt_hide_input"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="@string/HIDE" />

              <Button
                android:id="@+id/bt_save_input"
                style="@style/Button.Accent.Borderless"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="@string/SAVE" />

            </LinearLayout>

          </LinearLayout>

        </LinearLayout>

      </androidx.cardview.widget.CardView>

    </LinearLayout>

  </androidx.core.widget.NestedScrollView>

</LinearLayout>


5. Value

<?xml version="1.0" encoding="utf-8"?>
<resources>

  <color name="grey_3">#f7f7f7</color>
  <color name="grey_5">#f2f2f2</color>
  <color name="grey_10">#e6e6e6</color>
  <color name="grey_20">#cccccc</color>
  <color name="grey_40">#999999</color>
  <color name="grey_60">#666666</color>
  <color name="grey_80">#37474F</color>
  <color name="grey_90">#263238</color>
  <color name="grey_95">#1a1a1a</color>
  <color name="grey_100_">#0d0d0d</color>

</resources>

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:ns1="urn:oasis:names:tc:xliff:document:1.2">

  <style name="TextAppearance.AppCompat.Medium" parent="Base.TextAppearance.AppCompat.Medium"/>
  <style name="Widget.AppCompat.Button.Borderless" parent="Base.Widget.AppCompat.Button.Borderless"/>

</resources>

<resources>

  <string name="SAVE">SAVE</string>
  <string name="HIDE">HIDE</string>
  <string name="lorem_ipsum">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam efficitur ipsum in placerat molestie. Fusce quis mauris a enim sollicitudin
    \n\nLorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam efficitur ipsum in placerat molestie. Fusce quis mauris a enim sollicitudin
  </string>

</resources>