-1

I'm building an Android app and when the user clicks on one of the items of my RecyclerView, an AlertDialog pops up that allows them to edit that item of the list.

However I would like to display some information about the item they're editing on the AlertDialog but can't find a way to fill the content in my custom layout I added to the Dialog.

onClickListener that creates the AlertDialog

    holder.ivLinearLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(mContext instanceof MainActivity) {
                MainActivity mainActivity = (MainActivity) mContext;
                AlertDialog.Builder builder = new AlertDialog.Builder(mainActivity);

                builder.setTitle(mContext.getString(R.string.alert_food_detail_title));
                builder.setView(mainActivity.getLayoutInflater().inflate(R.layout.alert_detail_food, null))
                        .setPositiveButton(mContext.getString(R.string.alert_food_detail_ok), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        })
                        .setNeutralButton(mContext.getString(R.string.alert_food_detail_cancel), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        })
                        .setNegativeButton(mContext.getString(R.string.alert_food_detail_exclude), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        });

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

        }
    });

XML for the custom layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/textview_alert_food_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="4"
        android:textColor="@color/gray"
        android:textSize="16sp" />

    <EditText
        android:id="@+id/edittext_alert_food_quantity"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="numberDecimal"
        android:textColor="@color/gray"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/textview_alert_food_kcal"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="@color/gray"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/textview_alert_food_carb"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="@color/gray"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/textview_alert_food_prot"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="@color/gray"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/textview_alert_food_fat"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="@color/gray"
        android:textSize="16sp" />
</LinearLayout>

If more code is needed let me know.

This is how my AlertDialog looks like now, the custom view is being applied correctly but there are 5 TextViews i would like to fill with custom data and one EditText, any idea on how to do it?

My AlertDialog looks like this right now

leofontes
  • 898
  • 4
  • 16
  • 40

1 Answers1

0

You should create your Custom Dialog class for it. You created your own layout for Dialog, use it for your own dialog class.

    public class CustomDialog extends Dialog implements View.OnClickListener{
    private TextView foodName,foodQuantity,foodKcal,foodCarb,foodProt,foodFat;
    public CustomDialog(Context context) {
        super(context);
        setContentView(R.layout.dialog_layout);
        foodName = (TextView)findViewById(R.id.textview_alert_food_name);
        foodQuantity = (TextView)findViewById(R.id.textview_alert_food_name);
        foodKcal = (TextView)findViewById(R.id.textview_alert_food_kcal);
        foodCarb = (TextView)findViewById(R.id.textview_alert_food_carb);
        foodProt = (TextView)findViewById(R.id.textview_alert_food_prot);
        foodFat = (TextView)findViewById(R.id.textview_alert_food_fat);
        foodName.setOnClickListener(this);
        foodQuantity.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if(view == foodName){

        }else if(view == foodQuantity){
            // access them like this
        }
    }
}

This class would solve your solution. If you want you can send View.OnClickListener as a parameter and declare your body inside the activity. Or use it like this way.

Abdullah Tellioglu
  • 1,434
  • 1
  • 10
  • 26