CoordinatorLayout semplifica la creazione dell’interfaccia utente con barre degli strumenti a scomparsa, barre degli snake, animazioni complesse e altro ancora, in grado di rispondere all’ input dell’utente, comunicando con le viste secondarie tramite l’attributo “app:layout_behavior” nel file xml.
app:layout_constraintBottom_toBottomOf=”parent”
app:layout_constraintLeft_toLeftOf=”parent”
app:layout_constraintRight_toRightOf=”parent”
app:layout_constraintTop_toTopOf=”parent” />
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Nel codice java andiamo ad impostare il metodo setSupportActionBar() per sostituire ActionBar con Toolbar.
In themes.xml
<style name=”Theme.MyApplication” parent=”Theme.MaterialComponents.DayNight.NoActionBar”>
package com.example.coordinatorLayout;
import androidx.appcompat.widget.Toolbar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toobar=findViewById(R.id.app_bar);
setSupportActionBar(toobar);
}
}
Qui un altro esempio utilizzando un bottomSheet:
<?xml version=”1.0″ encoding=”utf-8″?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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.coordinatorlayout.widget.CoordinatorLayout>
package com.example.coordinatorLayout;
import android.view.View;
import androidx.appcompat.widget.Toolbar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
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);
}
}