Android recyclerview with cardview android example- @E-Commerce
Hi, everyone in this Article I am sharing how to create an e-commerce App. And how to show there product in App with a Good UI Design. Android recycler view with card view android example-#E-Commerce
I am Using Android RecyclerView with CardView for Show List of product in your App. Let’s start to create your App easy way.
In this example, I am Using Recycler View List to show data in a List with Adapter. and Using CardView For Creating A Good UI for these.
Android recycler view with card view android example-#E-Commerce
Step 1:- Create a Product List Activity In your Project
In your project create an activity and name with Product List. and used these code.
Step 2:- Open your activity_product_List.XML
in this XML file, I am adding layout for Recyclerview.
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="codeplayon.com.Package_List">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/refreshMainCat"
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/Product_Recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"/>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
Step 3: Open your Product java file and adding these code.
In your you java file you can use an API for product data. I am using #Volley Libary for getting data in JSONArrey Form
public class Package_List extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener{
public static final String KEY_joint="joint";
private RecyclerView firstRecyclerView;
private ProductAdapter rAdapter;
public static String EMAIL;
private SwipeRefreshLayout swipeRefreshLayout;
String joint="",Appoi_id=""; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_package__list);
joint=getIntent().getStringExtra("Joint");
Appoi_id=getIntent().getStringExtra("appoi_id");
SessionManagement sessionManagement =new SessionManagement();
Boolean checkLogin = sessionManagement.getLogin(this);
EMAIL = sessionManagement.getSavedEmail(this);
if (!checkLogin){
Intent intent = new Intent(this,LoginActivity.class);
startActivity(intent);
}
if (AppStatus.getInstance(this).isOnline()) {
MyProductLIst(); } else {
ContextThemeWrapper ctw = new ContextThemeWrapper(Package_List.this, R.style.Theme_AlertDialog);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctw);
alertDialogBuilder.setTitle("No internet connection");
alertDialogBuilder.setMessage("Check your internet connection or try again");
alertDialogBuilder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { }
});
alertDialogBuilder.show();
}
swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.refreshMainCat);
swipeRefreshLayout.setOnRefreshListener(this);
} private void MyProductLIst() {
// progress Dialog
final ProgressDialog loading = new ProgressDialog(Package_List.this);
loading.setMessage("Please Wait...");
loading.show();
loading.setCanceledOnTouchOutside(false);
// json response code
StringRequest stringRequest = new StringRequest(Request.Method.POST, ConfiURL.Packges_List,
new Response.Listener<String>() {
@Override
public void onResponse(String response) { try {
Log.d("JSON", response);
loading.dismiss();
swipeRefreshLayout.setRefreshing(false);
JSONObject jsonObject = new JSONObject(response);
String error_status = jsonObject.getString("status");
if (error_status.equals("0")) {
String error_msg = jsonObject.getString("message");
// Toast.makeText(LogIn.this, error_msg, Toast.LENGTH_SHORT).show();
ContextThemeWrapper ctw = new ContextThemeWrapper(Package_List.this, R.style.Theme_AlertDialog);
final android.app.AlertDialog.Builder alertDialogBuilder = new android.app.AlertDialog.Builder(ctw);
alertDialogBuilder.setTitle("Message");
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setMessage(error_msg);
alertDialogBuilder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { }
});
alertDialogBuilder.show();
} else { JSONArray jArray = jsonObject.getJSONArray("allData");
if (jArray.length() == 0) {
/*of array length is 0 then show alert dialog if
* array is not 0 then go else*/
ContextThemeWrapper ctw = new ContextThemeWrapper(Package_List.this, R.style.Theme_AlertDialog);
final android.app.AlertDialog.Builder alertDialogBuilder = new android.app.AlertDialog.Builder(ctw);
alertDialogBuilder.setTitle("Message");
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setMessage("No any history data please used Ro care india service");
alertDialogBuilder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { }
});
alertDialogBuilder.show();
} else {
onPostExecute(jArray); } } } catch (Exception e) {
Log.d("Tag", e.getMessage()); }
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
loading.dismiss();
swipeRefreshLayout.setRefreshing(false);
ContextThemeWrapper ctw = new ContextThemeWrapper(Package_List.this, R.style.Theme_AlertDialog);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctw);
alertDialogBuilder.setTitle("No connection");
alertDialogBuilder.setMessage(" Network Timeout error please try again ");
alertDialogBuilder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { }
});
alertDialogBuilder.show(); }
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<String, String>();
map.put(KEY_joint, joint);
return map;
}
}; RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
} protected void onPostExecute(JSONArray result) {
//this method will be running on UI thread
List<DataObject> data = new ArrayList<>();
data.equals(null);
try {
// Extract data from json and store into ArrayList as class objects
for (int i = 0; i < result.length(); i++) {
JSONObject json_data = result.getJSONObject(i);
DataObject report = new DataObject();
report.mText1 = json_data.getString("id");
report.mText2 = json_data.getString("name");
report.mText3 = json_data.getString("package_amount");
report.mText4 = json_data.getString("validity");
report.mText5 = json_data.getString("days");
report.mText6 = json_data.getString("joints");
report.mText7 =Appoi_id;
data.add(report);
}
// Setup and Handover data to recyclerview
firstRecyclerView = (RecyclerView) findViewById(R.id.Product_Recycler);
rAdapter = new ProductAdapter(Package_List.this, data);
firstRecyclerView.setAdapter(rAdapter);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(),2);
firstRecyclerView.setLayoutManager(gridLayoutManager);
// recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
// @Override
// public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
// if(dy > 0){
// fab.hide();
// } else{
// fab.show();
// }
//
// super.onScrolled(recyclerView, dx, dy);
// }
// }); } catch (JSONException e) {
Toast.makeText(Package_List.this, e.toString(), Toast.LENGTH_LONG).show();
}
} @Override
public void onRefresh() {
MyProductLIst();
}}
Step 4: Create A DataObject java Class.
in this java class, you can create a getter and Setter method for getting and setting your value.
public class DataObject {
public String mText1;
public String mText2;
public String mText3;
public String mText4;
public String mText5;
public String mText6;
public String mText7;
public String mText8;
public String mText9;
public String Image; public String getImage() {
return Image;
} public void setImage(String image) {
Image = image;
}
public String getmText1() {
return mText1;
} public void setmText1(String mText1) {
this.mText1 = mText1;
} public String getmText2() {
return mText2;
} public void setmText2(String mText2) {
this.mText2 = mText2;
} public String getmText3() {
return mText3;
} public void setmText3(String mText3) {
this.mText3 = mText3;
} public String getmText4() {
return mText4;
} public void setmText4(String mText4) {
this.mText4 = mText4;
} public String getmText5() {
return mText5;
} public void setmText5(String mText5) {
this.mText5 = mText5;
}
public String getmText6() {
return mText6;
} public void setmText6(String mText6) {
this.mText6 = mText6;
} public String getmText7() {
return mText7;
} public void setmText7(String mText7) {
this.mText7 = mText7;
}
public String getmText8() {
return mText8;
} public void setmText8(String mText8) {
this.mText8 = mText8;
}
public String getmText9() {
return mText9;
} public void setmText9(String mText9) {
this.mText9 = mText9;
}