Search code examples
androidandroid-layoutandroid-recyclerviewnotifydatasetchanged

android : change layout screen in runtime


I have two layout xml files. In one Layout I have RecyclerView and another layout just few textViews and button. Now my question is I want to change my layout when RecyclerView is empty. RecyclerView contains list of products which user can remove by tapping on delete icon present in each row of RecyclerView. When user reaches to size zero of list then I want to change layout to other which contains text like your cart is empty please add some products.

My first layout look like this.

enter image description here

after deleting all items from list it should look like this. My all xml file are ready.

enter image description here

Here is my some code of my adapter.

onCreateViewHolder

public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = layoutInflater.inflate(R.layout.checkout_single_design, parent, false);
    return new ViewHolder(view);
}

delete Icon Click listener

    holder.ivDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            openDialog(design);
        }
    });

And here is openDialog() method. Here Design is my model class.

public void openDialog(final Design design) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("Confirm");
    builder.setMessage("Remove item from cart?");
    builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            designList.remove(design);
            notifyDataSetChanged();
            saveSharedPref(context, "cart", new Gson().toJson(designList));
            Toast.makeText(context, "deleted", Toast.LENGTH_SHORT).show();
            dialog.dismiss();
        }
    });

    builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    AlertDialog alert = builder.create();
    alert.show();
}

Design.java

public class Design implements Serializable{

    public int id;
    public String qualityId;
    public String qualityName;
    public String designId;
    public String designName;
    public String image;
    public String name; //use as shade name
    public String discPercent;
    public String amount;
    public int pcs;
    public String qty;
}

In activity I am just setting adapter to recyclerView.

co_recycler_view.setLayoutManager(new LinearLayoutManager(CheckOutActivity.this));
co_recycler_view.setHasFixedSize(true);
adapter = new CheckOutAdapter(CheckOutActivity.this, designList);
co_recycler_view.setAdapter(adapter);

I hope my question is clear to all. I don't know how to achieve this. please suggest.


Solution

  • There are many ways you can achieve that. 1) you can use a ViewSwitcher. It uses two layouts and you can switch them back and forth.

    Here is the tutorial on it.

    2) you can override setContentView in the activity and implement your logic in it and call it from anywhere you want. Like you can set a boolean flag in it and set the layout accordingly! you don't need to restart the activity to do that.