Bottom Sheet è un componente di material design che si apre dal basso verso l’alto ,utilizzato per fornire descrizioni aggiuntive. In questo esempio andremo a creare un foglio inferiore persistente per visualizzare il contenuto nella parte bassa dello schermo ,rendendo visibile solo una parte che impostiamo nel nostro layout con app: behavior_peekHeight=”100dp”.
activity_main.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<androidx.coodinatorlayout.widget.CoodinatorLayout 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”
tools:context=”.MainActivity”>
<RelativeLayout
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:id=”@+id/bottom_sheet”
android:background=”#CCBBFF”
app:layout_behavior=”@String/bottom_sheet_behavior”
app:behavior_peekHeight=”100dp”>
<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”BottomSheet”
android:textColor=”#B30DAD”
android:textSize=”30sp”/>
</RelativeLayout>
</androidx.coodinatorlayout.widget.CoodinatorLayout>
MainActivity.java
package com.example.bottomSheet;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
public class MainActivity extends AppCompatActivity {
private BottomSheetBehavior<View>bottomSheetBehavior;
private View bottomSheet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomSheet=findViewById(R.id.bottom_sheet);
bottomSheetBehavior=BottomSheetBehavior.from(bottomSheet);
}
}