Dismiss a bottom sheet dialogue in android from its own Custom Activity

╄→尐↘猪︶ㄣ 提交于 2021-02-08 03:00:10

问题


I have created a custom bottom sheet android dialogue with the help of this answer by Chintan Khetiya:How to create a Custom Dialog box in android?.

I want to dismiss the dialogue from the button defined in the BottomDialogue's own Activity.Not from Calling activity.

Here is my code in the Calling activity in which i have created my custom BottomSheet_liab instance by click of a button:

openBottomDialogeButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                   **//Creating the BottomDialogue Instance**`Bottomsheet_liab dialog;
    dialog=new Bottomsheet_liab(getActivity());
dialog.getWindow().setBackgroundDrawable(newColorDrawable(Color.TRANSPARENT));`

        }
    });

Here is my code in the dialogue activity:

public class Bottomsheet_liab extends BottomSheetDialog{
@Override
 protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.cashflow_bottomsheet);
         Button btn=(Button)findViewByID(R.id.btnx);
         btn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                   **//I want to dismiss this BottomSheetDialogue from here.How can I do this>**
        }
    });

}

回答1:


Try this You need to call dismiss(); method like below code

btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
      dismiss();
    }
});

sample code

public class Bottomsheet_liab extends BottomSheetDialog{
@Override
 protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.cashflow_bottomsheet);
         Button btn=(Button)findViewByID(R.id.btnx);
         btn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                   dismiss();
        }
    });

}



回答2:


Try Following Code

private Button startbtn, okaybtn, cancelbtn;
BottomSheetDialog mBottomSheetDialog;
View sheetView;

    mBottomSheetDialog = new BottomSheetDialog(HomeActivity.this);
    sheetView = getLayoutInflater().inflate(R.layout.botomdialouge,null);
    mBottomSheetDialog.setContentView(sheetView);


    okaybtn = sheetView.findViewById(R.id.okaybtn);
    cancelbtn = sheetView.findViewById(R.id.cancelbtn);

   okaybtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

    cancelbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mBottomSheetDialog.dismiss();
        }
    });

Layout code as below

    <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="250dp"
         android:id="@+id/bottomsheet"
         android:clipToPadding="true"
         android:background="@color/colorwhite"
 app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

        <ImageView
            android:layout_gravity="center"
            android:src="@drawable/logowhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

        <TextView
            android:layout_gravity="center"
            android:textSize="@dimen/textsizenormal"
            android:textColor="@color/colortheme"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/exit"
            android:padding="16dp"/>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <Button
                android:id="@+id/cancelbtn"
                android:text="Cancel"
                android:textColor="@color/colorwhite"
                android:layout_margin="10dp"
                android:textSize="@dimen/textsizenormal"
                android:background="@drawable/buttonclick"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
            <Button
                android:id="@+id/okaybtn"
                android:text="Okay"
                android:textColor="@color/colorwhite"
                android:layout_margin="10dp"
                android:textSize="@dimen/textsizenormal"
                android:background="@drawable/buttonclick"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>
    </LinearLayout>


来源:https://stackoverflow.com/questions/47607029/dismiss-a-bottom-sheet-dialogue-in-android-from-its-own-custom-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!