Search code examples
javaandroidandroid-layoutandroid-custom-viewandroid-dialogfragment

Custom Dialog with Dynamic Layout is not displaying properly


I'm trying to create a custom dialog that display a dynamic text field based on a search result. This custom dialog is called from an activity. When the dialog is created, it didn't display as expected. Somehow the dialog doesn't look like it has been created.

The dialog created now :

Screenshot of created dialog

I was expecting it to be something like this :

Screenshot of expected dialog

Below is my code calling the dialog :

public class DeleteVehicle extends AppCompatActivity {

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

        Button deleteVehicleButton;

        deleteVehicleButton = (Button) findViewById(R.id.deleteVehicleBySearchButton);

        deleteVehicleButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Pattern pattern;
                Matcher matcher;
                int searchIndex = 0;
                boolean result = false;
                ArrayList<String> arrayList;

                if (!vehicleOwnerEditView.getText().toString().isEmpty()) {
                    pattern = Pattern.compile(vehicleOwnerEditView.getText().toString());
                    arrayList = db.fetchDatas(getId,"fullname");
                } else {
                    pattern = Pattern.compile(vehiclePlateNumberEditView.getText().toString());
                    arrayList = db.fetchDatas(getId,"plate_number");
                }
                if (arrayList.isEmpty()) {
                    Log.e("DeleteVehicle","arrayList is 0");
                } else {
                    while (searchIndex != arrayList.size()) {
                        matcher = pattern.matcher(arrayList.get(searchIndex));
                        result = matcher.matches();
                        if (result == true){
                            Log.e("DeleteVehicle","result true, searchIndex = " + arrayList.get(searchIndex));
                            searchIndex++;
                            //Toast.makeText(DeleteVehicle.this, arrayList.get(searchIndex), Toast.LENGTH_SHORT).show();
                        } else {
                            Log.e("DeleteVehicle","result false, searchIndex = " + arrayList.get(searchIndex));
                            arrayList.remove(searchIndex);
                            Log.e("DeleteVehicle","size after remove = " + arrayList.size());
                        }
                        Log.e("DeleteVehicle","searchIndex = " + searchIndex);
                    }

Related to calling the dialog ---> DeleteVehicleDialog deleteVehicleDialog = DeleteVehicleDialog.getInstanceFor(arrayList);
Related to calling the dialog ---> deleteVehicleDialog.show(getSupportFragmentManager(),"deleteVehicle");
                }
            }
        });
    }
}

And this is how I create the dialog :

public class DeleteVehicleDialog extends DialogFragment {
    private ArrayList<String> list;

    public static DeleteVehicleDialog getInstanceFor(ArrayList<String> list) {
        DeleteVehicleDialog deleteVehicleDialog = new DeleteVehicleDialog();
        deleteVehicleDialog.list=list;
        return deleteVehicleDialog;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        LinearLayout linearLayout = new LinearLayout(getContext());
        for(int i=0; i<5; i++){
            TextView textView= new TextView(getContext());
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            textView.setGravity(Gravity.CENTER_VERTICAL);
            textView.setText("Testing Textview: " + i);
            linearLayout.addView(textView);
        }

        final Dialog dialog = new Dialog(getContext());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(linearLayout);
        dialog.setTitle("deleteVehicle");
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        dialog.show();

        return dialog;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

}

I didn't create any XML for this layout as its going to be a dynamic display. There is no ID or widget to refer to.So I thought I should do everything just from coding. Is it wrong to think this way? Could this be the problem?

I use here, here & here as guide to create the dynamic Linearlayout before applying it into my custom dialog. What could be the problem with my custom dialog?


Solution

  • Remove

    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    

    If you don't want to have transparent dialog.