Links & Dependenciesactivity_main.xmlbottom_sheet_layout.xmlExampleBottomSheetDialog.javaMainActivity.java
Design Support Library:
developer.android.com/topic/libraries/support-library/packages.html#design
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" android:padding="16dp" tools:context="com.codinginflow.modalbottomsheetexample.MainActivity"> <Button android:id="@+id/button_open_bottom_sheet" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Open Modal Bottom Sheet" /> <TextView android:id="@+id/text_view_button_clicked" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button X clicked" android:textSize="25sp" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" android:padding="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is a BottomSheet" android:textSize="25sp" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button 1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button 2" /> </LinearLayout>
package com.codinginflow.modalbottomsheetexample; import android.content.Context; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.design.widget.BottomSheetDialogFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; public class ExampleBottomSheetDialog extends BottomSheetDialogFragment { private BottomSheetListener mListener; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v = inflater.inflate(R.layout.bottom_sheet_layout, container, false); Button button1 = v.findViewById(R.id.button1); Button button2 = v.findViewById(R.id.button2); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mListener.onButtonClicked("Button 1 clicked"); dismiss(); } }); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mListener.onButtonClicked("Button 2 clicked"); dismiss(); } }); return v; } public interface BottomSheetListener { void onButtonClicked(String text); } @Override public void onAttach(Context context) { super.onAttach(context); try { mListener = (BottomSheetListener) context; } catch (ClassCastException e) { throw new ClassCastException(context.toString() + " must implement BottomSheetListener"); } } }
package com.codinginflow.modalbottomsheetexample; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements ExampleBottomSheetDialog.BottomSheetListener{ private TextView mTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = findViewById(R.id.text_view_button_clicked); Button buttonOpenBottomSheet = findViewById(R.id.button_open_bottom_sheet); buttonOpenBottomSheet.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ExampleBottomSheetDialog bottomSheet = new ExampleBottomSheetDialog(); bottomSheet.show(getSupportFragmentManager(), "exampleBottomSheet"); } }); } @Override public void onButtonClicked(String text) { mTextView.setText(text); } }