Advanced Android - Crumbling Layout



This is an official widget to make swipe view in Android is ViewPager

Example

This example demostrate about how to integrate Crumbling Layout.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<RelativeLayout 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 = "#fddc9b"
   android:padding = "@dimen/activity_horizontal_margin">
   <TextView
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content"
      android:layout_centerHorizontal = "true"
      android:gravity = "center"
      android:text = "Tutorialspoint"
      android:textStyle = "bold" />
   <android.support.v4.view.ViewPager
      android:id = "@+id/pager"
      android:layout_width = "match_parent"
      android:layout_height = "match_parent" />
   <com.cleveroad.splittransformation.SquareViewPagerIndicator
      android:id = "@+id/indicator"
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content"
      android:layout_alignParentBottom = "true"
      android:layout_marginTop = "@dimen/activity_horizontal_margin"
      app:trans_debugItemsCount = "4" />
</RelativeLayout>

Step 3 − Add the following code to src/MainActivity.java

package myapplication.example.com.myapplication;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.cleveroad.splittransformation.SquareViewPagerIndicator;
import com.cleveroad.splittransformation.TransformationAdapterWrapper;

public class MainActivity extends AppCompatActivity {
   private ViewPager viewPager;
   private SquareViewPagerIndicator indicator;
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      viewPager = (ViewPager) findViewById(R.id.pager);
      indicator = (SquareViewPagerIndicator) findViewById(R.id.indicator);
      
      //Initializing an adapter and wrap it to TransformationAdapterWrapper
      SimplePagerAdapter adapter = new SimplePagerAdapter(getSupportFragmentManager());
      TransformationAdapterWrapper wrapper = TransformationAdapterWrapper
         .wrap(this, adapter) //wrap existing page adapter
         .rows(10) //number of rows to split image.
         .columns(7) // number of columns to split image
         .marginTop(getResources().getDimensionPixelSize(
         R.dimen.activity_horizontal_margin))
         .build(); //initializing
      
      viewPager.setAdapter(wrapper);
      viewPager.setPageTransformer(false, wrapper); //never forget this important line!
      indicator.initializeWith(viewPager); //attaching indicator with ViewPager
   } 
   private static class SimplePagerAdapter extends FragmentStatePagerAdapter {
      public SimplePagerAdapter(FragmentManager fm) {
         super(fm);
      } 
      
      @Override
      public Fragment getItem(int position) {
         return ViewPagerFragment.getInstances(position);
      } 
      
      @Override
      public int getCount() {
         return 7;
      } 
   }
}

Step 4 − Add the following code to src/ViewPagerFragment.java

Here abc indicates the logo of tutorialspoint.com
package myapplication.example.com.myapplication;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class ViewPagerFragment extends Fragment {
   private ImageView imageView;
   private TextView textView;
   private static String[] phoneTypes = {
      "tutorialspoint", 
      "tutorialspoint", 
      "tutorialspoint", 
      "tutorialspoint", 
      "tutorialspoint", 
      "tutorialspoint", 
      "tutorialspoint"
   }; 
   private static int[] drawables = {
      R.drawable.abc, 
      R.drawable.abc, 
      R.drawable.abc,
      R.drawable.abc, 
      R.drawable.abc, 
      R.drawable.abc, 
      R.drawable.abc
   }; 
   public static ViewPagerFragment getInstances(int position) {
      ViewPagerFragment fragment = new ViewPagerFragment();
      Bundle args = new Bundle();
      args.putInt("position", position);
      fragment.setArguments(args);
      return fragment;
   } 
   @Nullable
   
   @Override
   public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 
      @Nullable Bundle savedInstanceState) {
      
      return inflater.inflate(R.layout.fragment_layout, container, false);
   } 
   
   @Override
   public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
      super.onViewCreated(view, savedInstanceState);
      int position = getArguments().getInt("position");
      imageView = (ImageView) view.findViewById(R.id.image);
      textView = (TextView) view.findViewById(R.id.text_view);
      
      imageView.setImageResource(drawables[position]);
      textView.setText(phoneTypes[position]);
   }
}

Step 5 − Add the following code to res/layout/fragment_layout.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent">
   <ImageView
      android:id = "@+id/image"
      android:layout_width = "250dp"
      android:layout_height = "250dp"
      android:layout_centerInParent = "true" />
   <TextView
      android:id = "@+id/text_view"
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content"
      android:layout_below = "@id/image"
      android:layout_marginTop = "@dimen/activity_horizontal_margin"
      android:gravity = "center"
      android:text = "dsfsdfdsf"
      android:textColor = "@android:color/holo_green_dark"
      android:textStyle = "bold" />
</RelativeLayout>

Step 6 − Add the following code to build.gradle

dependencies {
   compile fileTree(dir: 'libs', include: ['*.jar'])
   testCompile 'junit:junit:4.12'
   compile 'com.android.support:appcompat-v7:24.2.1'
   compile 'com.cleveroad:splittransformation:0.9.0'
}

Step 7 − No need to change manifest.xml

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

Crumbling
Advertisements