Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MainActivity.class
- -------------------------------------
- package com.labs.ankitt.multichoice;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.support.v7.app.AlertDialog;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.LinearLayoutManager;
- import android.support.v7.widget.RecyclerView;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.Button;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- private Button buttonClick;
- private List<String> foodAllergy;
- private CustomAdapter adapter;
- private RecyclerView.LayoutManager layoutManager;
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- buttonClick = (Button) findViewById(R.id.buttonFoodAllergy);
- buttonClick.setOnClickListener(this);
- //arraylist
- foodAllergy = new ArrayList<>();
- String[] food = getResources().getStringArray(R.array.food_allergy);
- Collections.addAll(foodAllergy, food);
- Log.i("TAG", "onCreate: " + food.length);
- }
- @Override
- public void onClick(View v) {
- if (v == buttonClick) {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- LayoutInflater inflater = getLayoutInflater();
- View dialogView = (View) inflater.inflate(R.layout.custom_alert_dialog, null);
- builder.setView(dialogView);
- AlertDialog dialog = builder.create();
- dialog.show();
- RecyclerView rv = (RecyclerView) dialogView.findViewById(R.id.recycler_data);
- rv.setLayoutManager(new LinearLayoutManager(this));
- adapter= new CustomAdapter(this, foodAllergy);
- rv.setAdapter(adapter);
- }
- }
- }
- Adapter
- ---------------------------------
- package com.labs.ankitt.multichoice;
- import android.content.Context;
- import android.support.annotation.NonNull;
- import android.support.v7.widget.RecyclerView;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.CheckBox;
- import java.util.List;
- public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.DataViewHolder> {
- private Context context;
- private List<String> foodAllergy;
- public CustomAdapter(Context context, List<String> foodAllergy) {
- this.context = context;
- this.foodAllergy = foodAllergy;
- }
- @NonNull
- @Override
- public CustomAdapter.DataViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
- View inflater = LayoutInflater.from(context).inflate(R.layout.item_list_data, parent, false);
- return new DataViewHolder(inflater);
- }
- @Override
- public void onBindViewHolder(@NonNull CustomAdapter.DataViewHolder holder, int position) {
- holder.checkBoxData.setText(foodAllergy.get(position).toString());
- }
- @Override
- public int getItemCount() {
- return foodAllergy.size();
- }
- public class DataViewHolder extends RecyclerView.ViewHolder {
- private CheckBox checkBoxData;
- public DataViewHolder(View itemView) {
- super(itemView);
- checkBoxData = (CheckBox) itemView.findViewById(R.id.checkbox_data);
- }
- }
- }
- ---------------------------------------
- activity_main
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout 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"
- tools:context=".MainActivity">
- <Button
- android:id="@+id/buttonFoodAllergy"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_marginEnd="16dp"
- android:layout_marginStart="16dp"
- android:layout_marginTop="33dp"
- android:text="Select Food Allergy"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- </android.support.constraint.ConstraintLayout>
- ---------------------------------------------------
- custom_dialog
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout 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.support.v7.widget.RecyclerView
- android:id="@+id/recycler_data"
- android:layout_width="0dp"
- android:layout_height="0dp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- </android.support.constraint.ConstraintLayout>
- -------------------------------------------------------
- item_data
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout 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">
- <CheckBox
- android:id="@+id/checkbox_data"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_marginEnd="16dp"
- android:layout_marginStart="16dp"
- android:layout_marginTop="16dp"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- </android.support.constraint.ConstraintLayout>
- -------------------
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string-array name="food_allergy">
- <item>Ankit</item>
- <item>Ankit</item>
- <item>Ankit</item>
- <item>Ankit</item>
- <item>Ankit</item>
- <item>Ankit</item>
- <item>Ankit</item>
- <item>Ankit</item>
- <item>Ankit</item>
- <item>Ankit</item>
- <item>Ankit</item>
- <item>Ankit</item>
- <item>Ankit</item>
- </string-array>
- </resources>
Add Comment
Please, Sign In to add comment