Shubham Tunwal
Shubham Tunwal

Reputation: 237

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

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>**
        }
    });

}

Upvotes: 4

Views: 9252

Answers (2)

Gaurav Naik
Gaurav Naik

Reputation: 11

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>

Upvotes: 1

Goku
Goku

Reputation: 9692

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();
        }
    });

}

Upvotes: 8

Related Questions