简体   繁体   中英

Open Bottom Sheet automatically without using a Button

I'm doing this project where I want to start an application ( Main activity ) with a bottom sheet as instruction to know how to use some functions, while the main activity is open at the background.

I've known how to transition between a bottom sheet to another but my main problem that the first bottom sheet needs a button it self to be activated, so my question is can it be done automatically when the application starts without the need of a button then dismissed after clicking on the button inside the bottom sheet?

This is my Java Code:

public class MainActivity extends AppCompatActivity {

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


    Button buttonShow = findViewById(R.id.button_start);
    buttonShow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


             final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(
                    MainActivity.this, R.style.BottomSheetDesign
            );
             View bottomSheetView = LayoutInflater.from(getApplicationContext())
                    .inflate(
                            R.layout.layout_bottom_sheet,
                            (LinearLayout)findViewById(R.id.BottomSheetContainer)
                    );
            bottomSheetView.findViewById(R.id.ButtonNext).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    final BottomSheetDialog bottomSheetDialog1 = new BottomSheetDialog(
                            MainActivity.this, R.style.BottomSheetDesign
                    );

                    View bottomSheetView1 = LayoutInflater.from(getApplicationContext())
                            .inflate(
                                    R.layout.layout_bottom_sheet1,
                                    (LinearLayout)findViewById(R.id.BottomSheetContainer1)

                            );

                    bottomSheetView1.findViewById(R.id.ButtonDone).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            bottomSheetDialog1.dismiss();

                        }

                    });

                    bottomSheetDialog.dismiss();
                    bottomSheetDialog1.setContentView(bottomSheetView1);
                    bottomSheetDialog1.show();

                }
            });

            bottomSheetDialog.setContentView(bottomSheetView);
            bottomSheetDialog.show();

        }

    });

call bottomsheet directly in oncreate method

Try this code:

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

private void openBottomSheet() {

    final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(
            MainActivity.this, R.style.BottomSheetDesign
    );
    View bottomSheetView = LayoutInflater.from(getApplicationContext())
            .inflate(
                    R.layout.layout_bottom_sheet,
                    (LinearLayout) findViewById(R.id.BottomSheetContainer)
            );
    bottomSheetView.findViewById(R.id.ButtonNext).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final BottomSheetDialog bottomSheetDialog1 = new BottomSheetDialog(
                    MainActivity.this, R.style.BottomSheetDesign
            );

            View bottomSheetView1 = LayoutInflater.from(getApplicationContext())
                    .inflate(
                            R.layout.layout_bottom_sheet1,
                            (LinearLayout) findViewById(R.id.BottomSheetContainer1)

                    );

            bottomSheetView1.findViewById(R.id.ButtonDone).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    bottomSheetDialog1.dismiss();

                }

            });

            bottomSheetDialog.dismiss();
            bottomSheetDialog1.setContentView(bottomSheetView1);
            bottomSheetDialog1.show();

        }
    });

    bottomSheetDialog.setContentView(bottomSheetView);
    bottomSheetDialog.show();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM