Escolar Documentos
Profissional Documentos
Cultura Documentos
Definação:
Snackbars fornecem feedback leve sobre uma operação. Eles mostram uma breve mensagem
na parte inferior da tela em dispositivos móveis e no canto inferior esquerdo em dispositivos
maiores. Snackbars aparecem acima de todos os outros elementos na tela e apenas um pode ser
exibido por vez.
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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=".MainActivity"
tools:ignore="HardcodedText">
<Button
android:id="@+id/showSnackbarButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:backgroundTint="@color/colorPrimary"
android:text="SHOW SNACKBAR"
android:textColor="@android:color/white" />
</RelativeLayout>
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:ignore="HardcodedText">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:elevation="4dp"
app:cardBackgroundColor="@color/colorPrimaryDark"
app:cardCornerRadius="4dp"
2
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentStart="true"
android:src="@drawable/image_logo" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_toEndOf="@id/imageView"
android:text="GeeksforGeeks"
android:textAlignment="center"
android:textColor="@android:color/white"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView1"
android:layout_marginStart="4dp"
android:layout_toEndOf="@id/imageView"
android:text="Computer Science Portal"
android:textColor="@android:color/white"
android:textSize="14sp" />
<Button
android:id="@+id/gotoWebsiteButton"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="wrap_content"
3
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="GOTO WEBSITE"
android:textColor="@android:color/white"
android:textSize="14sp" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
O que produz a seguinte visão:
Java
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.material.snackbar.Snackbar;
Button bShowSnackbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bShowSnackbar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
4
// create an instance of the snackbar
final Snackbar snackbar = Snackbar.make(v, "",
Snackbar.LENGTH_LONG);
snackbar.getView().setBackgroundColor(Color.TRANSPARENT);
snackbar.show();
}
});
}
}
5
Passo a passo de como criar um Snackbar:
https://www.geeksforgeeks.org/custom-snackbars-in-android/