Skip to content

Example application

maltaisn edited this page Dec 17, 2021 · 7 revisions

This page explains how to setup your application to use the icon dialog.

The icon pack is stored in an Application class (declared in android:name in the manifest) in order to keep it when the activity is destroyed, for example on a configuration change. It is loaded when the app starts, and will only be reloaded on app restart, for example after the system has killed the app in background. There are many other options for storing it, for example in a Dagger singleton, in a view model, or it can even be kept in the activity and reloaded each time the activity is recreated (i.e. in onCreate). You can also load it asynchronously, using RxJava or kotlin coroutines for example.

The example application consist only of a button that opens the dialog when clicked. A toast is shown when icons are selected. Communication between the Activity or Fragment and the dialog is done by implementing the IconDialog.Callback interface. The icon pack is also provided via this callback.

Note that your application must use Google's Material Components for themes and styles to work correctly. Also note that icon packs drawable are never loaded after being pack is created. For icons to be displayed in dialog, it's necessary to call iconPack.loadDrawables.

Dependencies

This application uses the following dependencies:

implementation "com.maltaisn:icondialog:3.3.0"
implementation "com.maltaisn:iconpack-default:1.1.0"

Please use the latest versions as indicated in the README!

Kotlin

App.kt
class App : Application() {

    var iconPack: IconPack? = null

    override fun onCreate() {
        super.onCreate()

        // Load the icon pack on application start.
        loadIconPack()
    }

    private fun loadIconPack() {
        // Create an icon pack loader with application context.
        val loader = IconPackLoader(this)

        // Create an icon pack and load all drawables.
        val iconPack = createDefaultIconPack(loader)
        iconPack.loadDrawables(loader.drawableLoader)

        this.iconPack = iconPack
    }
}
MainActivity.kt
class MainActivity : AppCompatActivity(), IconDialog.Callback {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // If dialog is already added to fragment manager, get it. If not, create a new instance.
        val iconDialog = supportFragmentManager.findFragmentByTag(ICON_DIALOG_TAG) as IconDialog?
            ?: IconDialog.newInstance(IconDialogSettings())

        val btn: Button = findViewById(R.id.btn_open_dialog)
        btn.setOnClickListener {
            // Open icon dialog
            iconDialog.show(supportFragmentManager, ICON_DIALOG_TAG)
        }
    }

    override val iconDialogIconPack: IconPack?
        get() = (application as App).iconPack

    override fun onIconDialogIconsSelected(dialog: IconDialog, icons: List<Icon>) {
        // Show a toast with the list of selected icon IDs.
        Toast.makeText(this, "Icons selected: ${icons.map { it.id }}", Toast.LENGTH_SHORT).show()
    }

    companion object {
        private const val ICON_DIALOG_TAG = "icon-dialog"
    }
}

Java

App.java
public class App extends Application {

    @Nullable
    private IconPack iconPack;

    @Override
    public void onCreate() {
        super.onCreate();

        // Load the icon pack on application start.
        loadIconPack();
    }

    @Nullable
    public IconPack getIconPack() {
        return iconPack != null ? iconPack : loadIconPack();
    }

    private IconPack loadIconPack() {
        // Create an icon pack loader with application context.
        IconPackLoader loader = new IconPackLoader(this);

        // Create an icon pack and load all drawables.
        iconPack = IconPackDefault.createDefaultIconPack(loader);
        iconPack.loadDrawables(loader.getDrawableLoader());

        return iconPack;
    }
}
MainActivity.java
public class MainActivity extends AppCompatActivity implements IconDialog.Callback {

    private static final String ICON_DIALOG_TAG = "icon-dialog";

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

        // If dialog is already added to fragment manager, get it. If not, create a new instance.
        IconDialog dialog = (IconDialog) getSupportFragmentManager().findFragmentByTag(ICON_DIALOG_TAG);
        IconDialog iconDialog = dialog != null ? dialog
                : IconDialog.newInstance(new IconDialogSettings.Builder().build());

        Button btn = findViewById(R.id.btn_open_dialog);
        btn.setOnClickListener(v -> {
            // Open icon dialog
            iconDialog.show(getSupportFragmentManager(), ICON_DIALOG_TAG);
        });
    }

    @Nullable
    @Override
    public IconPack getIconDialogIconPack() {
        return ((App) getApplication()).getIconPack();
    }

    @Override
    public void onIconDialogIconsSelected(@NonNull IconDialog dialog, @NonNull List<Icon> icons) {
        // Show a toast with the list of selected icon IDs.
        StringBuilder sb = new StringBuilder();
        for (Icon icon : icons) {
            sb.append(icon.getId());
            sb.append(", ");
        }
        sb.delete(sb.length() - 2, sb.length());
        Toast.makeText(this, "Icons selected: " + sb, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onIconDialogCancelled() {}
}
Clone this wiki locally