How to create Chip object in AlertDialog?

412 views Asked by At

I am having main activity which loads fragments. In that fragment, I am having alert dialog. I want to load chip dynamically in this alert dialog.

Alert Dialog code is as below :

private void showAddDialog() {
    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(getContext());
    builder.setTitle("Create Service");

    View itemView = LayoutInflater.from(getContext()).inflate(R.layout.layout_update_service, null);
  
    chip_group_filter = (ChipGroup) itemView.findViewById(R.id.chip_group_addon);
    
   //Load All Chip Filters
    displayFilterList(chip_group_filter);       

    builder.setNegativeButton("CANCEL", (dialogInterface, which) -> {
        dialogInterface.dismiss();
    });

    builder.setPositiveButton("CREATE", (dialogInterface, which) -> {
         dialogInterface.dismiss();

    });

    builder.setView(itemView);
    android.app.AlertDialog updateDialog = builder.create();
    updateDialog.show();
}

To load dynamically method logic as follow, it crash my app at Chip Object creation:

private void displayFilterList(ChipGroup chip_group_filter) {
    if (Common.categorySelected.getFilters().size() > 0) {
        chip_group_filter.clearCheck();
        chip_group_filter.removeAllViews();

        //Add on All Views
        for (FilterModel filterModel : Common.categorySelected.getFilters()) {
            Chip chip = (Chip) getLayoutInflater().inflate(R.layout.layout_filter_item, null); //APP CRASH HERE
            chip.setText(new StringBuilder(filterModel.getName()));

            chip_group_filter.addView(chip);

        }
    }
}

Stack Trace :

2020-10-11 00:05:15.394 13562-13562/com.example.hstudioserver E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.hstudioserver, PID: 13562
    android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class com.google.android.material.chip.Chip
    Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class com.google.android.material.chip.Chip
    Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at android.view.LayoutInflater.createView(LayoutInflater.java:647)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at com.heli.hbstudioserver.ui.servicelist.ServiceListFragment.displayFilterList(ServiceListFragment.java:391)
        at com.heli.hbstudioserver.ui.servicelist.ServiceListFragment.showAddDialog(ServiceListFragment.java:312)
        at com.heli.hbstudioserver.ui.servicelist.ServiceListFragment.onOptionsItemSelected(ServiceListFragment.java:94)
        at androidx.fragment.app.Fragment.performOptionsItemSelected(Fragment.java:2830)
        at androidx.fragment.app.FragmentManager.dispatchOptionsItemSelected(FragmentManager.java:2717)
        at androidx.fragment.app.Fragment.performOptionsItemSelected(Fragment.java:2834)
        at androidx.fragment.app.FragmentManager.dispatchOptionsItemSelected(FragmentManager.java:2717)
        at androidx.fragment.app.FragmentController.dispatchOptionsItemSelected(FragmentController.java:412)
        at androidx.fragment.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:389)
        at androidx.appcompat.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:219)
        at androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:109)
        at androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:109)
        at androidx.appcompat.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:64)
        at androidx.appcompat.widget.Toolbar$1.onMenuItemClick(Toolbar.java:207)
        at androidx.appcompat.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:781)
        at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:840)
        at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
        at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:991)
        at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:981)
        at androidx.appcompat.widget.ActionMenuView.invokeItem(ActionMenuView.java:625)
        at androidx.appcompat.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:151)
        at android.view.View.performClick(View.java:6597)
        at android.view.View.performClickInternal(View.java:6574)
        at android.view.View.access$3100(View.java:778)
        at android.view.View$PerformClick.run(View.java:25885)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
        at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:248)
        at com.google.android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.java:222)
        at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:150)
2020-10-11 00:05:15.394 13562-13562/com.example.hstudioserver E/AndroidRuntime:     at com.google.android.material.internal.ThemeEnforcement.obtainStyledAttributes(ThemeEnforcement.java:81)
        at com.google.android.material.chip.ChipDrawable.loadFromAttributes(ChipDrawable.java:334)
        at com.google.android.material.chip.ChipDrawable.createFromAttributes(ChipDrawable.java:276)
        at com.google.android.material.chip.Chip.(Chip.java:193)
        at com.google.android.material.chip.Chip.(Chip.java:186)
            ... 40 more
    
    
    --------- beginning of system
2020-10-11 00:05:16.147 2314-2416/? E/ModuleIdSetter: exception when setting module id
    java.lang.IllegalStateException: Unable to get current module info in ModuleManager created with non-module Context
        at com.google.android.chimera.config.ModuleManager.getCurrentModule(:com.google.android.gms@[email protected] (100700-333172415):2)
        at aflp.a(:com.google.android.gms@[email protected] (100700-333172415):4)
        at afls.b(:com.google.android.gms@[email protected] (100700-333172415):9)
        at afia.a(Unknown Source:0)
        at rve.a(:com.google.android.gms@[email protected] (100700-333172415):0)
        at rrl.c(:com.google.android.gms@[email protected] (100700-333172415):1)
        at rrj.b(:com.google.android.gms@[email protected] (100700-333172415):1)
        at rua.b(:com.google.android.gms@[email protected] (100700-333172415):6)
        at rua.c(:com.google.android.gms@[email protected] (100700-333172415):5)
        at rua.b(:com.google.android.gms@[email protected] (100700-333172415):10)
        at rua.a(:com.google.android.gms@[email protected] (100700-333172415):17)
        at rua.e(:com.google.android.gms@[email protected] (100700-333172415):7)
        at sjj.a(:com.google.android.gms@[email protected] (100700-333172415):2)
        at sik.a(:com.google.android.gms@[email protected] (100700-333172415):10)
        at sib.a(:com.google.android.gms@[email protected] (100700-333172415):0)
        at sie.handleMessage(:com.google.android.gms@[email protected] (100700-333172415):27)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at afas.a(:com.google.android.gms@[email protected] (100700-333172415):2)
        at afas.dispatchMessage(:com.google.android.gms@[email protected] (100700-333172415):11)
        at android.os.Looper.loop(Looper.java:193)
        at android.os.HandlerThread.run(HandlerThread.java:65)
1

There are 1 answers

0
Gabriele Mariotti On

You have to use Theme.MaterialComponents theme as app theme. For example:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
  ...
</style>