Search code examples
androiduiactionsheet

Using options in ActionSheet to change Button text


I am trying to develop an UI actionsheet in android. Using dependency:

compile 'com.baoyz.actionsheet:library:1.1.6'

I have developed an actionsheet in android which look like this: ActionSheet in Android

I am trying to change the button text whenever any option is selected from the actionSheet. The index in the javafile below contains the string position just like array but i am unable to get the string value from that index.

Here is my javafile:

    public class billBookInfo extends AppCompatActivity implements ActionSheet.ActionSheetListener{

        private Button change;
        private String setValue;
        private Button vtype;
        private Button zone;

        Button next;
        Button previous;

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

            vtype = (Button) findViewById(R.id.info_billbook_bt_vtype);
            zone = (Button)findViewById(R.id.info_billbook_bt_zone);
        }
     }

    public void onClick(View v){
        switch (v.getId()) {
            case R.id.info_billbook_bt_vtype:
                setTheme(R.style.ActionSheetStyleiOS7);
                change = vtype;
                showActionSheet1();
                break;

            case R.id.info_billbook_bt_zone:
                setTheme(R.style.ActionSheetStyleiOS7);
                showActionSheet2();
                change = zone;
                break;
        }
    }

    private void showActionSheet1() {
        ActionSheet.createBuilder(this,getSupportFragmentManager())
                   .setCancelButtonTitle("Cancel")
                   .setOtherButtonTitles("Motorcycle","Scooter","Car","Van")
                    .setCancelableOnTouchOutside(true).setListener(this).show();
    }

    private void showActionSheet2() {
        ActionSheet.createBuilder(this,getSupportFragmentManager())
                .setCancelButtonTitle("Cancel")
                .setOtherButtonTitles("Me", "Ko", "Sa", "Ja")
                .setCancelableOnTouchOutside(true).setListener(this).show();
    }

    @Override
    public void onDismiss(ActionSheet actionSheet, boolean isCancel) {

    }

    @Override
    public void onOtherButtonClick(ActionSheet actionSheet, int index) {
        int i = 0;
        if ( i == index){

            setValue = "".concat(getString(index));
            change.setText(setValue);
        }
    }
}

Solution

  • Why don't you use a variable to store those values.

        String vArr[] = new String[]{"Motorcycle","Scooter","Car","Van"};
        String dArr[] = new String[]{"Me", "Ko", "Sa", "Ja"};
        ActionSheet vSheet, dSheet;
        private void showActionSheet1() {
            vSheet = ActionSheet.createBuilder(this,getSupportFragmentManager())
                       .setCancelButtonTitle("Cancel")
                       .setOtherButtonTitles(vArr[0],vArr[1],vArr[2],vArr[3])
                        .setCancelableOnTouchOutside(true).setListener(this).show();
        }
        private void showActionSheet2() {
            dSheet = ActionSheet.createBuilder(this,getSupportFragmentManager())
                    .setCancelButtonTitle("Cancel")
                    .setOtherButtonTitles(dArr[0],dArr[1],dArr[2],dArr[3])
                    .setCancelableOnTouchOutside(true).setListener(this).show();
        }
    
        @Override
        public void onDismiss(ActionSheet actionSheet, boolean isCancel) {
    
        }
    
        @Override
        public void onOtherButtonClick(ActionSheet actionSheet, int index) {
            if ( actionSheet == vSheet){
                change.setText(vArr[index]);
            }
            else{
                change.setText(dArr[index]);
            }
    
        }