How to put a button in toolbar in my layout with a list?

Viewed 50

I have a layout with an item. In MenuActivity I have a list, every time I add an item to the list, this layout is duplicated and I add more drinks. I would like to be able to add a toolbar to this layout without duplicating itself for each new element in the list. If I add a toolbar, this happens:

enter image description here

And I want it to look like this:

enter image description here

My code:

MenuActivity:

public class MenuActivity extends AppCompatActivity {

List<MenuModel> modelList;
RecyclerView recyclerView;
MenuAdapter mAdapter;

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

    // creating an arraylist

    modelList = new ArrayList<>();
    modelList.add(new MenuModel("Green Tea", getString(R.string.greentea), R.drawable.greentea ));
    modelList.add(new MenuModel("Latte", getString(R.string.latte), R.drawable.late));
    modelList.add(new MenuModel("Orange Smoothie", getString(R.string.orangesmoothie), R.drawable.orange));
    modelList.add(new MenuModel("Orange Vanilla", getString(R.string.orangevanilla), R.drawable.orangevanilla));
    modelList.add(new MenuModel("Cappucino", getString(R.string.cappcuni), R.drawable.cappcunio));
    modelList.add(new MenuModel("Thai Tea", getString(R.string.thaitea), R.drawable.thaitea));
    modelList.add(new MenuModel("Tea", getString(R.string.tea), R.drawable.tea));
    modelList.add(new MenuModel("Bubble Tea", getString(R.string.bubbletea), R.drawable.milk));
    modelList.add(new MenuModel("Matcha", getString(R.string.match), R.drawable.match));

    // recyclerview
    recyclerView = findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(null));
    // adapter
    mAdapter = new MenuAdapter(this, modelList);
    recyclerView.setAdapter(mAdapter);

}

MenuModel:

public class MenuModel {
String mFoodName;
String mFoodDetail;
int mFoodPhoto;

public MenuModel(String mFoodName, String mFoodDetail, int mFoodPhoto) {
    this.mFoodName = mFoodName;
    this.mFoodDetail = mFoodDetail;
    this.mFoodPhoto = mFoodPhoto;
}


public String getmFoodName() {
    return mFoodName;
}

public void setmFoodName(String mFoodName) {
    this.mFoodName = mFoodName;
}

public String getmFoodDetail() {
    return mFoodDetail;
}

public void setmFoodDetail(String mFoodDetail) {
    this.mFoodDetail = mFoodDetail;
}

public int getmFoodPhoto() {
    return mFoodPhoto;
}

public void setmFoodPhoto(int mFoodPhoto) {
    this.mFoodPhoto = mFoodPhoto;
}

MenuAdapter:

public class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.ViewHolder> {

List<MenuModel> modelList;
Context context;

public MenuAdapter(Context context, List<MenuModel> modelList) {
    this.context = context;
    this.modelList = modelList;
}

@Override
public ViewHolder onCreateViewHolder( ViewGroup parent, int i) {

    View view = LayoutInflater.from(context).inflate(R.layout.menu_list_item, parent, false);
    return new ViewHolder(view);

}

@Override
public void onBindViewHolder( ViewHolder holder, int position) {

    // here we will find the position and start setting the output on our views

    String nameofFood = modelList.get(position).getmFoodName();
    String detailofFood = modelList.get(position).getmFoodDetail();
    int images = modelList.get(position).getmFoodPhoto();

    holder.mDrinkName.setText(nameofFood);
    holder.mDrinkDescription.setText(detailofFood);
    holder.imageView.setImageResource(images);

}

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

// in order to make our views responsive we can implement onclicklistener on our recyclerview

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    // here we will find the views on which we will inflate our data

    TextView mDrinkName, mDrinkDescription;
    ImageView imageView;


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

        mDrinkName = itemView.findViewById(R.id.coffeeName);
        mDrinkDescription = itemView.findViewById(R.id.description);
        imageView = itemView.findViewById(R.id.coffeeImage);
        itemView.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {

        // lets get the position of the view in list and then work on it

        int position = getAdapterPosition();

        if (position == 0) {
            Intent intent = new Intent(context, InfoActivity.class);
            context.startActivity(intent);
        }

        if (position == 1) {
            Intent intent2 = new Intent(context, LatteActivity.class);
            context.startActivity(intent2);
        }
    }
}

This is how my xml is:

enter image description here

<?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:orientation="vertical"
    android:layout_height="wrap_content"
android:background="@color/color4"
    tools:context=".MenuScreen.MenuActivity">

<androidx.appcompat.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:layout_width="wrap_content"
        android:layout_marginLeft="320dp"
        android:text="MyButton"
        android:layout_height="match_parent"></Button>

</androidx.appcompat.widget.Toolbar>
    <androidx.cardview.widget.CardView
        app:cardBackgroundColor="@color/color1"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_marginLeft="11dp"
        android:layout_marginRight="11dp"
        android:layout_marginTop="7dp"
        android:layout_marginBottom="7dp"
        app:cardElevation="10dp"
        app:cardCornerRadius="10dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">


            <ImageView
                android:layout_width="88dp"
                android:layout_height="88dp"
                android:id="@+id/coffeeImage"
                android:layout_marginTop="25dp"
                android:src="@drawable/greentea"/>

            <TextView
                android:textColor="#ffffff"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                tools:text=""
                android:textSize="12sp"
                android:layout_marginRight="12dp"
                android:layout_below="@+id/coffeeName"
                android:layout_marginLeft="12dp"
                android:id="@+id/description"
                android:layout_toRightOf="@+id/coffeeImage"/>
            <TextView
                android:textColor="#ffffff"

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="12dp"
                android:layout_marginTop="25dp"
                android:id="@+id/coffeeName"
                android:textStyle="italic"
                android:textSize="26sp"
                android:layout_toRightOf="@+id/coffeeImage"
                tools:text="Green Tea"/>

            <TextView
                android:textColor="#ffffff"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/description"
                android:layout_toRightOf="@+id/coffeeImage"
                android:id="@+id/clickheretoorder"
                android:layout_marginLeft="12dp"
                android:layout_marginTop="4dp"
                tools:text="Click  to Order"/>




        </RelativeLayout>

    </androidx.cardview.widget.CardView>


</LinearLayout>

1 Answers

In this XML, you are making a mistake by keeping toolbar inside a row item. You need to pull out the tool bar stuff out of the xml (the single for list).Because it will be duplicated whatever item(view) you have in your xml. If you don't a view to be duplicated get it out and place that out side the recyclerview in your main XML. For reference, I am attaching sketch for your help.

enter image description here

Related