Open In App

How to Use Android Sliding Activity Library in Android App?

Last Updated : 30 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Sliding activities allow you to easily set header content, menus, and data onto slidable screens. Easily create activities that can slide vertically on the screen and fit well into the material design age. We can set header images to our sliding activity. We can also customize the color that will affect the header and the status bar. We can also disable the header and show only the content that is readable. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language. 

Use Android Sliding Activity Library in Android App Sample GIF

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Add dependency

Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.   

compile ‘com.klinkerapps:sliding-activity:1.5.2’

Step 3: Creating XML for slider activity. This will be displayed as content in the slider activity 

XML




<TextView 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:text="@string/placeholder"/>


Step 4: Creating XML for the MainActivity. This contains a Button. After clicking on it Slider activity will open 

XML




<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  
    <View android:layout_height="100dp"
        android:layout_width="match_parent"
        android:layout_centerInParent="true"
        android:id="@+id/expansion_view"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"/>
  
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
  
        <LinearLayout xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingBottom="@dimen/activity_vertical_margin"
            tools:context="com.klinker.android.sliding.sample.SampleActivity">
  
            <Button
                android:id="@+id/show_image"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/show_image_activity" />
  
        </LinearLayout>
  
    </ScrollView>
  
</RelativeLayout>


Step 5: Instead of using the onCreate() method we are going to use the init() method.

A) Create an Activity named ImageActivity. Configuring image using setImage(R.drawable.profile_picture);

B) expandFromPoints(Arg1,Arg2,Arg3,Arg4) method is used for creating animations when activity opening.

Java




import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
  
import com.klinker.android.sliding.SlidingActivity;
  
public class ImageActivity extends SlidingActivity {
    @Override
    public void init(Bundle savedInstanceState) {
        setTitle(R.string.image_activity);
        setContent(R.layout.activity_content);
  
        // no need to set a color here, palette
          // will generate colors for us to be set
        setImage(R.drawable.profile_picture);
  
         // if we wanted to set some manually instead,
           // do this after setting the image
         setPrimaryColors(
                 getResources().getColor(R.color.image_activity_primary),
                 getResources().getColor(R.color.image_activity_primary_dark)
         );
  
         // if we want the image to animate in, then set it after
           // the activity has been created
         // NOTE: this will not change the activity's colors using palette,
           // so make sure you call
         // setPrimaryColors() first
         new Handler().postDelayed(new Runnable() {
             @Override
             public void run() {
                 setImage(R.drawable.profile_picture);
             }
         }, 500);
  
        Intent intent = getIntent();
        if (intent.getBooleanExtra(MainActivity.ARG_USE_EXPANSION, false)) {
            expandFromPoints(
                    intent.getIntExtra(MainActivity.ARG_EXPANSION_LEFT_OFFSET, 0),
                    intent.getIntExtra(MainActivity.ARG_EXPANSION_TOP_OFFSET, 0),
                    intent.getIntExtra(MainActivity.ARG_EXPANSION_VIEW_WIDTH, 0),
                    intent.getIntExtra(MainActivity.ARG_EXPANSION_VIEW_HEIGHT, 0)
            );
        }
    }
}


Step 6: Create Button object. Register on click listener. Inside onClick() method start slider activity.(ImageActivity.class)

Java




import androidx.appcompat.app.AppCompatActivity;
  
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
  
public class MainActivity extends AppCompatActivity {
  
    public static final String ARG_USE_EXPANSION = "arg_use_expansion";
    public static final String ARG_EXPANSION_LEFT_OFFSET = "arg_left_offset";
    public static final String ARG_EXPANSION_TOP_OFFSET = "arg_top_offset";
    public static final String ARG_EXPANSION_VIEW_WIDTH = "arg_view_width";
    public static final String ARG_EXPANSION_VIEW_HEIGHT = "arg_view_height";
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
  
        setContentView(R.layout.activity_main);
  
        // registering click event
        findViewById(R.id.show_image).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(), ImageActivity.class));
            }
        });
    }
}


Output:

Project Link: Click here



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads