Updated 2 December 2020
BottomSheetDialogFragment
Bottom Sheet Fragment can be implemented by extending BottomSheetDialogFragment
Implementation
Add Dependency
1 |
implementation 'com.google.android.material:material:1.0.0' |
Create Bottom Sheet layout
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".BottomSheetFragment"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/bottom_sheet_fragment" /> </FrameLayout> |
BottomSheetFragment class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package com.example.bottomsheetfragment; import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.google.android.material.bottomsheet.BottomSheetDialogFragment; public class BottomSheetFragent extends BottomSheetDialogFragment { public BottomSheetFragment() { // Required empty public constructor } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_bottom_sheet , container , false); } } |
MainActivity layout
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0" encoding="utf-8"?> <androidx.appcompat.widget.LinearLayoutCompat 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:orientation="vertical" android:layout_height="match_parent" tools:context=".MainActivity"> <androidx.appcompat.widget.AppCompatButton android:layout_width="match_parent" android:text="@string/app_name" android:id="@+id/btm_frag" android:layout_height="wrap_content"/> </androidx.appcompat.widget.LinearLayoutCompat> |
MainActivity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.example.bottomsheetfragment; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.AppCompatButton; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AppCompatButton button=findViewById(R.id.btm_frag); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { BottomSheetFrag frag= new BottomSheetFrag(); frag.show(getSupportFragmentManager(),"Ashwani"); } }); } } |
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.