How can I set the order of the positive and negative buttons in AlertDialog?

后端 未结 8 1712
挽巷
挽巷 2021-02-05 06:05

Why I want to do this is another discussion entirely, but I need to figure out the best way to make all my alert dialogs have the positive button on the right side. Note that i

8条回答
  •  时光说笑
    2021-02-05 06:40

    I've created a solution to force order of buttons for any Android version on any device.

    The idea is that we can get list of buttons of dialog in order they appear on screen and set text and actions in order we want. It guarantees that we would have same order on any device.

    Please check my GitHub repository

    Here is example of creating and showing dialog:

     new FixedOrderDialogFragment.Builder()
        .setLeftButtonText(R.string.left)
        .setCenterButtonText(R.string.center)
        .setRightButtonText(R.string.right)
        .setDefaultButton(FixedOrderDialogFragment.BUTTON_RIGHT)
        .setMessage(R.string.message)
        .setTitle(R.string.app_name)
        .create()
     .show(getSupportFragmentManager(), "DEMO");
    

    Please note that owner Activity should implement FixedOrderDialogFragment.FixedOrderDialogListener to be able restore dialog state on Activity re-creation.

提交回复
热议问题