Android BottomSheetDialog layout have button, onclick navigat to activity

Hemanth picture Hemanth · Jan 20, 2017 · Viewed 21.8k times · Source

I have BottomSheetDialog layout xml file which been called by my activity. And BottomSheetDialog has 2 buttons. On clicking on button inside BottomSheetDialog it should take me to another Activity.

Related BottomSheetDialog Layout Image

main_layout.xml

    <?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:background="#fff"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.ezeelearn.colorstonz.ezeelearn.PhysicsTestSelectorActivity"
    tools:showIn="@layout/app_bar_physics_lesson">


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:id="@+id/lesson_list">



        </TableLayout>


    </ScrollView>

    <include layout="@layout/bottom_sheet_layout" />


</LinearLayout>

bottom_sheet_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/RelativeLayoutSheet"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/bottom_sheet_behavior"
    android:background="#ffffff"
    app:behavior_hideable="true"
    android:padding="20dp"
    >

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="2">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:paddingRight="50dp"
                android:paddingLeft="50dp"
                android:paddingTop="20dp"
                android:paddingBottom="20dp"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <Button
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_weight="1"
                    android:background="#fff"
                    android:drawableTop="@drawable/google_cardboard"
                    android:drawablePadding="6dp"
                    android:gravity="left|center"
                    android:height="60dp"
                    android:padding="6dp"
                    android:text="Video"
                    android:id="@+id/bottom_sheet_video_btn"
                    android:textAlignment="center"
                    android:fontFamily="sans-serif-light"
                    android:foreground="?android:attr/selectableItemBackground"
                    android:textColor="#666" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:paddingRight="50dp"
                android:paddingLeft="50dp"
                android:paddingTop="20dp"
                android:paddingBottom="20dp"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <Button
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_weight="1"
                    android:background="#fff"
                    android:drawableTop="@drawable/bulletin_board"
                    android:drawablePadding="6dp"
                    android:gravity="left|center"
                    android:height="60dp"
                    android:padding="6dp"
                    android:text="Lesson"
                    android:id="@+id/bottom_sheet_lesson_btn"
                    android:textAlignment="center"
                    android:fontFamily="sans-serif-light"
                    android:foreground="?android:attr/selectableItemBackground"
                    android:textColor="#666" />

            </LinearLayout>

        </TableRow>

    </TableLayout>



</LinearLayout>

MainActivity.java

public class MainActivity extends Activity{

    BottomSheetDialog bottomSheetDialog;
    BottomSheetBehavior bottomSheetBehavior ;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);

        List<String> lessonNames = new ArrayList<String>();
        lessonNames.add("Photosynthesis in Higher Plants");
        lessonNames.add("The Living World");
        lessonNames.add("Biological Classification");
        lessonNames.add("Plant Kingdom");
        lessonNames.add("Animal Kingdom");
        lessonNames.add("Morphology of Flowering Plants");
        lessonNames.add("Anatomy of Flowering Plants");
        lessonNames.add("Structural Organisation in Animals");
        lessonNames.add("Cell-The Unit of Life");
        lessonNames.add("Biomolecules");
        lessonNames.add("Transport in Plants");
        lessonNames.add("Mineral Nutrition");
        lessonNames.add("Flowering Plants");

        TableLayout lessonList = (TableLayout) findViewById(R.id.lesson_list);
        List<TableLayout> lessonTableList = new ArrayList<TableLayout>();

        for(int i = 0; i < lessonNames.size(); i++) {

            TableRow tableRow = new TableRow(this);
            TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
            tableRow.setLayoutParams(tableLayoutParams);
            tableRow.setPadding(30, 25, 30, 25);
            tableRow.setBackgroundDrawable(getResources().getDrawable(R.drawable.textlines));


            TextView textView = new TextView(this);
            textView.setText(String.format("%02d", (i+1))+".  "+lessonNames.get(i));
            /*textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_dots_vertical, 0);*/
            textView.setTextSize(17);

            tableRow.addView(textView);


            /*TextView textView = new TextView(this);
            textView.setText(String.format("%02d", (i+1))+".  "+lessonNames.get(i));
            textView.setPadding(20, 25, 20, 25);
            textView.setTextSize(17);
            textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_dots_vertical, 0);
            textView.setLayoutParams(new TableLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));

            textView.setBackgroundDrawable(getResources().getDrawable(R.drawable.textlines));

            lessonList.addView(textView);*/
            lessonList.addView(tableRow);
            lessonTableList.add(lessonList);
        }

        for(final TableLayout tableLayout: lessonTableList) {
            tableLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    bottomSheetDialog = new BottomSheetDialog(PhysicsLessonActivity.this);

                    View view = getLayoutInflater().inflate(R.layout.bottom_sheet_layout, null);
                    bottomSheetDialog.setContentView(view);
                    bottomSheetDialog.show();


                    bottomSheetDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                        @Override
                        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                            Log.d("BottomSheetVideoBtn", "called");
                            return false;
                        }
                    });


                }
            });
        }

        Button bottomSheetVideoBtn = (Button) findViewById(R.id.bottom_sheet_video_btn);
        Button bottomSheetLessonBtn = (Button) findViewById(R.id.bottom_sheet_lesson_btn);

        bottomSheetVideoBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), PhysicsActivity.class);
                Log.d("BottomSheetVideoBtn", "called");
                startActivity(intent);
            }
        });

        bottomSheetLessonBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getBaseContext(), "Lesson Button Clicked", Toast.LENGTH_LONG).show();
            }
        });

        ImageButton backToPHome = (ImageButton) findViewById(R.id.back_to_home);
        backToPHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), PhysicsActivity.class);
                startActivity(intent);
            }
        });

    }

}

Answer

Anton Kazakov picture Anton Kazakov · Jan 20, 2017

Use this

  public class CustomBottomSheetDialogFragment extends BottomSheetDialogFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.content_dialog_bottom_sheet, container, false);
        Button btn1 = (Button)v.findViewById(R.id.btn1);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(),YourActivity.class));
            }
        });
        return v;
    }
 }

Then call this bottomSheet in your activity by

new CustomBottomSheetDialogFragment().show(getSupportFragmentManager(), "Dialog");