- Advanced Android - Home
- Advanced Android - Material Design
- Advanced Android - Sliding Menu
- Advanced Android - Tabs
- Advanced Android - Sliders
- Advanced Android - Floating Labels
- Advanced Android - ViewPager
- Material Login & Registration
- Advanced Android - Snackbar
- Expandable List View
- Search Functionality
- Count Down Functionality
- Draw Spritesheet
- Advanced Android - Linkify Class
- Advanced Android - ViewFlipper
- Advanced Android - loader
- Advanced Android - Slider Menu
- SwipeRefreshLayout
- Shake to Refresh
- Advanced Android - Color Palette
- Crumbling Layout
- Advanced Android - Page Flip
- Ken Burns effect
- Advanced Android - Parallax Header
- Borderless Dialog
- Advanced Android - Holder View
- Scrolling TextView by Ticker
- Floating Action Menu
- Spinner Data from Database
- Advanced Android - Flexbox Layout
- Advanced Android - YouTube API
- Video Streaming
- Cardview & Recycleview
Advanced Android - SwipeRefreshLayout
This Android Material Design UI pattern is very commonly seen in many applications like Gmail, Facebook, Twitter and implemented using Android SwipeRefreshLayout.
Example
This example demostrate about how to integrate Android SwipeRefreshLayout.
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:tools = "http://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:paddingBottom = "@dimen/activity_vertical_margin"
android:paddingLeft = "@dimen/activity_horizontal_margin"
android:paddingRight = "@dimen/activity_horizontal_margin"
android:paddingTop = "@dimen/activity_vertical_margin">
<android.support.v4.widget.SwipeRefreshLayout
android:id = "@+id/swipeToRefresh"
android:layout_width = "match_parent"
android:layout_height = "wrap_content">
<ListView
android:id = "@+id/listView"
android:layout_width = "match_parent"
android:layout_height = "match_parent" >
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
Step 3 − Add the following code to src/MainActivity.java
package myapplication.example.com.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class MainActivity extends Activity {
ArrayList arrayList = new ArrayList();
SwipeRefreshLayout mSwipeRefreshLayout;
ListView mListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeToRefresh);
mListView = (ListView) findViewById(R.id.listView);
mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent);
arrayList.add("First Element");
arrayList.add("Second Element");
arrayList.add("Third Element");
arrayList.add("Fourth Element");
arrayList.add("Fifth Element");
ArrayAdapter adapter = new ArrayAdapter(
this, android.R.layout.simple_list_item_1, arrayList);
mListView.setAdapter(adapter);
mSwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
shuffle();
mSwipeRefreshLayout.setRefreshing(false);
}
});
}
public void shuffle(){
Collections.shuffle(arrayList, new Random(System.currentTimeMillis()));
ArrayAdapter adapter = new ArrayAdapter(
this, android.R.layout.simple_list_item_1, arrayList);
mListView.setAdapter(adapter);
}
}
Step 4 − 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.android.support:support-v4:21.0.+'
}
Step 5 − No need to change manifest.xml file
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
icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −