Skip to main content

Recyclerview Sample

Android RecyclerView is more advanced version of ListView with improved performance and other benefits. Using RecyclerView and CardView together, both lists and grids can be created very easily. Here is the complete information about RecyclerView and other examples.

In this tutorial we are going to learn how to render a simple RecyclerView with a custom layout. We’ll also learn writing a adapter class, adding list divider and row click listener. 

Before you start, make sure that you updated your Android Studio to latest version. Currently my Android Studio is updated to Android Studio 3.0.


1. Creating New Project 

1. In Android Studio, go to File ⇒ New Project and fill all the details required to create a new project. When it prompts to select a default activity, select Blank Activity and proceed.

2. Open build.gradle and add recycler view dependency. com.android.support:recyclerview-v7:26.1.0 and rebuild the project.
apply plugin: 'com.android.application'
android {
    compileSdkVersion 26    buildToolsVersion "26.0.2"    defaultConfig {
        applicationId "com.example.xxxxx.projectname"        minSdkVersion 15        targetSdkVersion 26        versionCode 1        versionName "1.0"        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"    }
    buildTypes {
        release {
            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'    })
    compile 'com.android.support:appcompat-v7:26.0.2'    compile 'com.android.support.constraint:constraint-layout:1.0.2'    compile 'com.android.support:design:26.0.2'    compile 'com.android.support:cardview-v7:26.0.2'    compile 'com.squareup.retrofit2:retrofit:2.1.0'    compile 'com.squareup.retrofit2:converter-gson:2.1.0'    compile 'com.github.bumptech.glide:glide:3.7.0'
    testCompile 'junit:junit:4.12'}
//gradle-wrapper properties
distributionUrl=\https\://services.gradle.org/distributions/gradle-4.1-all.zip

2. Writing the Adapter Class
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context context;
    RecyclerView recycleview;
    List<BookModel> bookList;

    public RecyclerAdapter(MainActivity mContext, List<BookModel> mbookList, RecyclerView mrecyclerView) {
        context = mContext;
        recycleview = mrecyclerView;
        bookList = mbookList;
    }

    @Override    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder vh;
        View itemLayoutView = null;
        itemLayoutView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.book_item, parent, false);
        vh = new ViewHolder(itemLayoutView);

        return vh;
    }

    @Override    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        try{
            if (holder instanceof ViewHolder) {
                final BookModel item = bookList.get(position);

                ((ViewHolder) holder).txtTitle.setText(item.getTitle());
                ((ViewHolder) holder).txtBy.setText("By : " + item.getBy());
                ((ViewHolder) holder).txtLocation.setText("Location : " +item.getLocation()+","+item.getCountry());

                ((ViewHolder) holder).cardView.setOnClickListener(new View.OnClickListener() {
                    @Override                    public void onClick(View view) {

                        final String strRef = item.getUrl();

                        StringTokenizer tokens = new StringTokenizer(strRef, "?");
                        final String strfirst = tokens.nextToken();
                        String strsecond = tokens.nextToken();

                        AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
                        builder1.setTitle(item.getTitle());
                        builder1.setMessage(context.getResources().getString(R.string.for_more));
                        builder1.setCancelable(true);
                        builder1.setNegativeButton("stop here!", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });

                        builder1.setPositiveButton("continue", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                Uri uri = Uri.parse(Constant.base_url + strfirst);
                                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                                context.startActivity(intent);
                                dialog.cancel();
                            }
                        });

                        AlertDialog alert11 = builder1.create();
                        alert11.show();
                    }
                });
            }

        }catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override    public int getItemCount() {
        return bookList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView txtTitle, txtBy, txtLocation;
        private CardView cardView;

        public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);

            txtTitle = (TextView) itemLayoutView.findViewById(R.id.id_title);
            txtBy = (TextView) itemLayoutView.findViewById(R.id.id_author);
            txtLocation = (TextView) itemLayoutView.findViewById(R.id.id_location);
            cardView = (CardView) itemLayoutView.findViewById(R.id.card_view);
        }
    }
}
3. Model class
public class BookModel {

    String slno,amtpledge,blurb,by,country,currency,end_time,location,percentage,num_backer,state,title,type,url;

    public BookModel(String slno,String amtpledge,String blurb,String by,String country,String currency,String end_time,
                     String location, String percentage,String num_backer,String state,String title,String type, String url){
        this.slno = slno;
        this.amtpledge = amtpledge;
        this.blurb = blurb;
        this.by = by;
        this.country = country;
        this.currency = currency;
        this.end_time = end_time;
        this.location = location;
        this.percentage = percentage;
        this.num_backer = num_backer;
        this.state = state;
        this.title = title;
        this.type = type;
        this.url = url;
    }

    public String getSlno() {
        return slno;
    }

    public void setSlno(String slno) {
        this.slno = slno;
    }

    public String getAmtpledge() {
        return amtpledge;
    }

    public void setAmtpledge(String amtpledge) {
        this.amtpledge = amtpledge;
    }

    public String getBlurb() {
        return blurb;
    }

    public void setBlurb(String blurb) {
        this.blurb = blurb;
    }

    public String getBy() {
        return by;
    }

    public void setBy(String by) {
        this.by = by;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getCurrency() {
        return currency;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }

    public String getEnd_time() {
        return end_time;
    }

    public void setEnd_time(String end_time) {
        this.end_time = end_time;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getPercentage() {
        return percentage;
    }

    public void setPercentage(String percentage) {
        this.percentage = percentage;
    }

    public String getNum_backer() {
        return num_backer;
    }

    public void setNum_backer(String num_backer) {
        this.num_backer = num_backer;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

}
Create XML

activity_main.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"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:orientation="vertical">
    <android.support.design.widget.AppBarLayout        android:id="@+id/idToolBar"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:theme="@style/AppTheme.AppBarOverlay">
        <android.support.v7.widget.Toolbar            android:id="@+id/toolbar"            android:layout_width="match_parent"            android:layout_height="?attr/actionBarSize"            android:background="@android:color/black"            app:titleTextColor="@color/colorWhite"            app:popupTheme="@style/AppTheme.PopupOverlay" />
    </android.support.design.widget.AppBarLayout>
    <android.support.v7.widget.RecyclerView        android:id="@+id/recyclerView"        android:layout_below="@+id/idToolBar"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="5dp"        android:scrollbars="none">
    </android.support.v7.widget.RecyclerView>
</RelativeLayout>
book_item.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:card_view="http://schemas.android.com/apk/res-auto"    android:id="@+id/card_view"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_margin="@dimen/card_margin"    android:elevation="3dp"    card_view:cardCornerRadius="@dimen/card_album_radius">
    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_margin="5dp">
        <TextView            android:id="@+id/id_title"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:textColor="@color/colorAccent"            android:textStyle="bold"            android:fontFamily="casual"            android:text="TITLE"            android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" />
        <TextView            android:id="@+id/id_author"            android:layout_below="@+id/id_title"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:textColor="@color/description"            android:textStyle="bold"            android:text="By :"            android:textAppearance="@style/Base.TextAppearance.AppCompat.Small" />
        <TextView            android:id="@+id/id_location"            android:layout_below="@+id/id_author"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:textColor="@color/description"            android:textStyle="bold"            android:text="Location"            android:textAppearance="@style/Base.TextAppearance.AppCompat.Small" />
    </RelativeLayout>
</android.support.v7.widget.CardView>
MainActivity.java

public class MainActivity extends AppCompatActivity {

    Toolbar toolbar;
    RecyclerView recyclerView;
    LinearLayoutManager layout_manager;

    private Retrofit retrofit;
    private int mStatusCode;
    private APICallInterface service;
    final OkHttpClient client = new OkHttpClient().newBuilder()
            .connectTimeout(1, TimeUnit.MINUTES)
            .readTimeout(1, TimeUnit.MINUTES)
            .build();
    private String baseUrl;
    boolean progressenabled = true;

    List<BookModel> bookList;
    RecyclerAdapter adapter;
    private BroadcastReceiver networkStateReceiver;
    MainActivity mainActivity;

    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle(getString(R.string.books_list));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        layout_manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layout_manager);

        mainActivity = this;

        baseUrl = Constant.base_main;

        bookList = new ArrayList<BookModel>();

        /** Creating REST API Calls **/        retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(client)
                .build();
        service = retrofit.create(APICallInterface.class);

        /*check_internet_connection*/        networkStateReceiver = new BroadcastReceiver() {
            @Override            public void onReceive(Context context, Intent intent) {
                ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo info = manager.getActiveNetworkInfo();
                if (info != null) {
                    callBookapi();
                } else {
                    Constant.alertbox(MainActivity.this,
                            getString(R.string.str_networkmessage),
                            getString(R.string.str_networktitlemessage));
                }
            }
        };

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                onBackPressed();
            }
        });

    }

    private void callBookapi() {

        if (progressenabled) {
            Constant.showDialog(MainActivity.this, "Loading Books Detail");
        }

        String strUrl = baseUrl + Constant.main_method;

        Call<ResponseBody> caller = service.callAPI(strUrl);
        caller.enqueue(new Callback<ResponseBody>() {
            @Override            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

                    mStatusCode = response.code();

                    if (mStatusCode == Constant.SUCEESSRESPONSECODE) {

                        try{
                            JSONArray result_array = new JSONArray(response.body().string());
//                            Log.e("OBJECTXXX",result_array.toString());                            List<BookModel> items = new Gson().fromJson(result_array.toString(), new TypeToken<List<BookModel>>() {
                            }.getType());

                            bookList.addAll(items);

                        }catch (JSONException e){
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        adapter = new RecyclerAdapter(MainActivity.this, bookList, recyclerView);
                        recyclerView.setAdapter(adapter);

                        if (bookList.size() > 0) {
                            adapter.notifyDataSetChanged();
                        }
                        Constant.dismissDialog();
                    }else if (mStatusCode == Constant.FAILURERESPONSECODE) {
                        Constant.dismissDialog();
                    } else {
                        if (mStatusCode == Constant.INTERNALERRORRESPONSECODE) {
                            Constant.dismissDialog();
                        }
                    }
            }

            @Override            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Constant.dismissDialog();
                if (t instanceof SocketTimeoutException) {
//                    Log.v("SocketTimeOut", "SocketTimeOut");                    Toast.makeText(MainActivity.this, "TimeOut"                            , Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override    public void onBackPressed() {
     Constant.alertDialog(MainActivity.this,"Do you really exit?",mainActivity);

    }

    @Override    protected void onResume() {
        super.onResume();
        registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION));
    }

    @Override    protected void onPause() {
        unregisterReceiver(networkStateReceiver);
        super.onPause();
    }

    private void alertDialog(String message) {
        AlertDialog.Builder builder;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            builder = new AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_Alert);
        } else {
            builder = new AlertDialog.Builder(this);
        }

        builder.setTitle(getString(R.string.app_name))
                .setMessage(message)
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                })
                .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .setIcon(android.R.drawable.ic_dialog_alert)
                .show();
    }
}
APICallInterface

public interface APICallInterface {
    @GET @Headers("Connection:close")
    Call<ResponseBody> callAPI(@Url String url);
    @GET @Headers("Connection:close")
    Call<ResponseBody> callAPI(@Url String url, @QueryMap Map<String, String> parameters);
    @POST @Headers("Connection:close")
    Call<ResponseBody> postToAPI(@Url String url, @Body RequestBody parameter);
}
Constant.java
public class Constant {

    public static ProgressDialog pDialog;

    public static String base_url = "http://www.ssssss.com/";
    public static String base_main = "http://sssss.method.com/";
    public static String main_method = "method_name";

    public static final int SUCEESSRESPONSECODE = 200;
    public static final int FAILURERESPONSECODE = 204;
    public static final int INTERNALERRORRESPONSECODE = 500;


    public static void alertbox(Context _context, String message, String title) {

        AlertDialog.Builder builder1 = new AlertDialog.Builder(_context);
        builder1.setTitle(title);
        builder1.setMessage(message);
        builder1.setCancelable(true);
        builder1.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });

        AlertDialog alert11 = builder1.create();
        alert11.show();
    }


    public static void showDialog(Context mContext, String strMessage) {
        try {
            if (pDialog != null)
                if (pDialog.isShowing())
                    pDialog.dismiss();
            pDialog = ProgressDialog.show(mContext, "", strMessage, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void dismissDialog() {
        try {
            if (pDialog.isShowing())
                pDialog.dismiss();
        } catch (Exception e) {
            // TODO Auto-generated catch block            e.printStackTrace();
        }
    }

    public static void alertDialog(Context context, String message, final MainActivity activity) {
        android.app.AlertDialog.Builder builder;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            builder = new android.app.AlertDialog.Builder(context, android.R.style.Theme_Material_Dialog_Alert);
        } else {
            builder = new android.app.AlertDialog.Builder(context);
        }

        builder.setTitle(context.getString(R.string.app_name))
                .setMessage(message)
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        activity.finish();
                    }
                })
                .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .setIcon(android.R.drawable.ic_dialog_alert)
                .show();
    }

}
Handler

private static int Splash_Time_Out = 3000;

new Handler().postDelayed(new Runnable() {
    @Override    public void run() {
        //do action
    }
}, Splash_Time_Out);
Styles
<resources>

    <!-- Base application theme. -->    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Light" >
        <item name="colorControlNormal">@color/colorWhite</item>
    </style>

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light">
        <item name="colorControlNormal">@color/colorWhite</item> </style>
</resources>
public void insertItem(String itemId, String itemName, String itemDesc, String itemPrice, String itemThumb) {
        String selectQuery = "SELECT  * FROM " + TABLE_NAME + " WHERE id = '" + itemId + "'";
        db = dbHelper.getWritableDatabase();

        Cursor cursor = db.rawQuery(selectQuery, null);
        if (cursor.getCount() > 0) {
            ContentValues newValues = new ContentValues();
            // Assign values for each row.
            newValues.put("id", itemId);
            newValues.put("name", itemName);
            newValues.put("description", itemDesc);
            newValues.put("price", itemPrice);
            newValues.put("thumbnail", itemThumb);

            int rowsUpdated = db.update(TABLE_NAME, newValues, "id = '" + itemId + "'", null);
            Log.v("resultState", "resultState==" + rowsUpdated);

        } else {
            ContentValues newValues = new ContentValues();
            // Assign values for each row.
            newValues.put("id", itemId);
            newValues.put("name", itemName);
            newValues.put("description", itemDesc);
            newValues.put("price", itemPrice);
            newValues.put("thumbnail", itemThumb);

            long result = db.insert(TABLE_NAME, null, newValues);
            Log.v("resultState", "resultState==" + result);
        }
        cursor.close();
    }
//this tutorial is not yet completed, will update shortly. Thank You










Comments

Popular posts from this blog

Get Phone Number from Contact List - Android Tutorial

When you create an application to send sms or an application to make calls, getting a destination number from the contacts list is a common task. In this Android tip, I am going to show the code to fetch a number from the contacts list. Now let me tell you how to achieve the goal. First, you need to create an Intent object for the PICK_ACTION action. To open the contacts list, the table that contains the contacts information must be specified as a parameter of the constructor of the Intent class. You can refer to the table using ContactsContract.Contacts.CONTENT_URI. Then call the startActivityForResult () method passing the Intent object and request code to open the contacts list. After a contact is selected from the contacts list, to get the result, you need to override the onActivityResult(int reqCode, int resultCode, Intent data) method of the activity. You can call the getData() method of the data parameter to get the table or uri that contains the selected contact. From the t

Spinner with Search on DropDown - Android Tutorial

If you have more values on Dropdown of Spinner its hard to select the last item by making a long scroll. To overcome this issue Android introduced a component called  AutoCompleteTextView Yes it is!!! Then why Spinner with Search? There may be some requirement even though gave much knowledge about it. There is a simple and good library that helps us to achieve this -  SearchableSpinner Gradle dependencies {     ...     implementation 'com.toptoche.searchablespinner:searchablespinnerlibrary:1.3.1' } Usage Now replace your Normal Android Spinner on XML with the following < com.toptoche.searchablespinnerlibrary.SearchableSpinner     android:id="@+id/id_city"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:background="@android:color/transparent"     android:padding="5dp" /> ___________________________________________________________

Set Focus on Spinner when select Item on Vertical Scroll - Android Tutorial

We may face an issue on Spinner lies on long vertical scroll, (i.e.) when selected and item from dropdown the focus moves to top of scroll. To avoid this please follow this piece of code spinner.setFocusableInTouchMode( true ); spinner.setOnFocusChangeListener( new View.OnFocusChangeListener() {     @Override     public void onFocusChange(View v, boolean hasFocus) {         if (hasFocus) {             if (spinner.getWindowToken() != null ) {                 spinner.performClick();             }         }     } });   _______________________________________________________________________________ Happy Coding...